Skip to content

Commit

Permalink
Adding handling of leading slash + test case
Browse files Browse the repository at this point in the history
Also adding a debug version of the match fn for easier debug
  • Loading branch information
sabhiram committed Jan 9, 2015
1 parent f6c17d0 commit 869a72e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
30 changes: 28 additions & 2 deletions ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,16 @@ func getPatternFromLine(line string) (*regexp.Regexp, bool) {
}

// Handle [Rule 2, 4], when # or ! is escaped with a \
// Handle [Rule 8], strip leading /
// Handle [Rule 4] once we tag negatePattern, strip the leading ! char
if regexp.MustCompile(`^(\#|\!|/)`).MatchString(line) {
if regexp.MustCompile(`^(\#|\!)`).MatchString(line) {
line = line[1:]
}

// Handle [Rule 8], strip leading / and enforce path checking if its present
if regexp.MustCompile(`^/`).MatchString(line) {
line = "^" + line[1:]
}

// If we encounter a foo/*.blah in a folder, prepend the ^ char
if regexp.MustCompile(`([^\/+])/.*\*\.`).MatchString(line) {
line = "^" + line
Expand Down Expand Up @@ -158,16 +162,38 @@ func CompileIgnoreFile(fpath string) (*GitIgnore, error) {
func (g GitIgnore) IncludesPath(f string) bool {
includesPath := true
for idx, pattern := range g.patterns {
if pattern.MatchString(f) {
if !g.negate[idx] {
includesPath = false
} else if !includesPath {
includesPath = true
}
}
}
return includesPath
}

/* DEBUG VERSION OF ABOVE FUNCTION *\
func (g GitIgnore) IncludesPath(f string) bool {
includesPath := true
fmt.Println("Matching path for: " + f)
for idx, pattern := range g.patterns {
fmt.Printf(" with pattern: " + pattern.String())
if pattern.MatchString(f) {
if !g.negate[idx] {
fmt.Println( " MATCHED +")
includesPath = false
} else if !includesPath {
fmt.Println( " MATCHED -")
includesPath = true
}
} else {
fmt.Println( " NOT MATCHED")
}
}
return includesPath
}
\* END OF DEBUG VERSION */

// IgnoresPath is an interface function for the IgnoreParser interface.
// It returns true if the given GitIgnore structure would reject the path
Expand Down
15 changes: 15 additions & 0 deletions ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,18 @@ bar
assert.Equal(test, true, object.IgnoresPath("bar"), "bar should be ignored")
assert.Equal(test, true, object.IgnoresPath("baz/bar"), "baz/bar should be ignored")
}

// Validate the correct handling of leading slash
func TestCompileIgnoreLines_HandleLeadingSlashPath(test *testing.T) {
writeFileToTestDir("test.gitignore", `
/*.c
`)
defer cleanupTestDir()

object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
assert.Nil(test, error, "error should be nil")
assert.NotNil(test, object, "object should not be nil")

assert.Equal(test, true, object.IgnoresPath("hello.c"), "hello.c should be ignored")
assert.Equal(test, false, object.IgnoresPath("foo/hello.c"), "foo/hello.c should not be ignored")
}

0 comments on commit 869a72e

Please sign in to comment.