Skip to content

Commit

Permalink
respecting the ignore negation for double asterisk
Browse files Browse the repository at this point in the history
  • Loading branch information
zabawaba99 committed Oct 24, 2015
1 parent ba03bd7 commit 5491861
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
6 changes: 5 additions & 1 deletion gitglob.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func globMatch(pattern, value string) bool {
// Two consecutive asterisks ("**") in patterns matched
// against full pathname may have special meaning:
if strings.Contains(pattern, dblAsterisks) {
return evalDblAsterisk(pattern, value)
result := evalDblAsterisk(pattern, value)
if negate {
result = !result
}
return result
}

// If the pattern does not contain a slash /, Git treats it as a shell glob
Expand Down
53 changes: 22 additions & 31 deletions gitglob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,66 +34,57 @@ func TestGlobMatch(t *testing.T) {
defer os.RemoveAll(os.TempDir() + "/snag")

// blank line
p := ""
assert.False(t, globMatch(p, v), "Expected %q to NOT match %q", p, v)
assert.False(t, globMatch("", v))

// a comment
p = "#a comment"
assert.False(t, globMatch(p, v), "Expected %q to NOT match %q", p, v)
assert.False(t, globMatch("#a comment", v))

// regular match no slash
p = "gitglob.go"
assert.True(t, globMatch(p, "gitglob.go"), "Expected %q to match %q", p, v)
assert.True(t, globMatch("gitglob.go", "gitglob.go"))

// negation no slash
p = "!gitglob.go"
assert.False(t, globMatch(p, "gitglob.go"), "Expected %q to NOT match %q", p, v)
assert.False(t, globMatch("!gitglob.go", "gitglob.go"))

// match with slash
p = tempLoc() + "/foo.txt"

tmpFiles := []string{"foo.txt"}
createFiles(t, tmpFiles)
assert.True(t, globMatch(p, v+"/foo.txt"), "Expected %q to match %q", p, v)
assert.True(t, globMatch(tempLoc()+"/foo.txt", v+"/foo.txt"))
deleteFiles(t, tmpFiles)

// negate match with slash
p = "!" + tempLoc() + "/foo.txt"

tmpFiles = []string{"foo.txt"}
createFiles(t, tmpFiles)
assert.False(t, globMatch(p, v+"/foo.txt"), "Expected %q to NOT match %q", p, v)
assert.False(t, globMatch("!"+tempLoc()+"/foo.txt", v+"/foo.txt"))
deleteFiles(t, tmpFiles)

// directory
p = tempLoc()
assert.True(t, globMatch(p, v), "Expected %q to match %q", p, v)
assert.True(t, globMatch(tempLoc(), v))

// directory with trailing slash
p = tempLoc() + "/"
assert.True(t, globMatch(p, v), "Expected %q to match %q", p, v)
assert.True(t, globMatch(tempLoc()+"/", v))

// star matching
p = tempLoc() + "/*.txt"
tmpFiles = []string{"foo.txt"}
createFiles(t, tmpFiles)
assert.True(t, globMatch(p, v+"/foo.txt"), "Expected %q to match %q", p, v)
assert.False(t, globMatch(p, v+"/somedir/foo.txt"), "Expected %q to NOT match %q", p, v)
assert.True(t, globMatch(tempLoc()+"/*.txt", v+"/foo.txt"))
assert.False(t, globMatch(tempLoc()+"/*.txt", v+"/somedir/foo.txt"))
deleteFiles(t, tmpFiles)

// double star prefix
p = "**/foo.txt"
assert.True(t, globMatch(p, v+"/hello/foo.txt"), "Expected %q to match %q", p, v)
assert.True(t, globMatch(p, v+"/some/dirs/foo.txt"), "Expected %q to match %q", p, v)
assert.True(t, globMatch("**/foo.txt", v+"/hello/foo.txt"))
assert.True(t, globMatch("**/foo.txt", v+"/some/dirs/foo.txt"))

// double star suffix
p = tempLoc() + "/hello/**"
assert.True(t, globMatch(p, v+"/hello/foo.txt"), "Expected %q to match %q", p, v)
assert.False(t, globMatch(p, v+"/some/dirs/foo.txt"), "Expected %q to NOT match %q", p, v)
assert.True(t, globMatch(tempLoc()+"/hello/**", v+"/hello/foo.txt"))
assert.False(t, globMatch(tempLoc()+"/hello/**", v+"/some/dirs/foo.txt"))

// double star in path
p = tempLoc() + "/hello/**/world.txt"
assert.True(t, globMatch(p, v+"/hello/world.txt"), "Expected %q to match %q", p, v)
assert.True(t, globMatch(p, v+"/hello/stuff/world.txt"), "Expected %q to match %q", p, v)
assert.False(t, globMatch(p, v+"/some/dirs/foo.txt"), "Expected %q to NOT match %q", p, v)
assert.True(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/hello/world.txt"))
assert.True(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/hello/stuff/world.txt"))
assert.False(t, globMatch(tempLoc()+"/hello/**/world.txt", v+"/some/dirs/foo.txt"))

// negate doubl start patterns
assert.False(t, globMatch("!**/foo.txt", v+"/hello/foo.txt"))
assert.False(t, globMatch("!"+tempLoc()+"/hello/**", v+"/hello/foo.txt"))
assert.False(t, globMatch("!"+tempLoc()+"/hello/**/world.txt", v+"/hello/world.txt"))
}

0 comments on commit 5491861

Please # to comment.