More parse rules, handling for special chars which are escaped
diff --git a/.gitignore b/.gitignore
index 2cb309f..7392e7b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,3 +26,4 @@
*.exe
*.test
*.prof
+
diff --git a/ignore.go b/ignore.go
index 1495a89..ac4394e 100644
--- a/ignore.go
+++ b/ignore.go
@@ -79,9 +79,14 @@
// listed above at the start of this file
func getPatternFromLine(line string) *regexp.Regexp {
// Strip comments [rule 2]
- // TODO: Handle [rule 2], when # is escaped with a \
- r := regexp.MustCompile("^(.*?)#.*$")
- line = r.ReplaceAllString(line, "$1")
+ if regexp.MustCompile(`^#`).MatchString(line) { return nil }
+
+ // TODO: Handle [rule 4] which negates the match for patterns leading with "!"
+
+ // Handle [rule 2, 4], when # or ! is escaped with a \
+ if regexp.MustCompile(`^(\#|\!)`).MatchString(line) {
+ line = line[1:]
+ }
// Trim string [rule 3]
// TODO: Hanlde [rule 3], when the " " is escaped with a \
diff --git a/ignore_test.go b/ignore_test.go
index 49ff92a..d8c2f77 100644
--- a/ignore_test.go
+++ b/ignore_test.go
@@ -111,7 +111,7 @@
# Another comment
- # Comment
+ # Invalid Comment
abc/def
`)
@@ -121,12 +121,12 @@
assert.Nil(test, error, "error should be nil")
assert.NotNil(test, object, "object should not be nil")
- assert.Equal(test, 1, len(object.patterns), "should have single regex pattern")
+ assert.Equal(test, 2, len(object.patterns), "should have two regex pattern")
assert.Equal(test, false, object.IgnoresPath("abc/abc"), "/abc/abc should not be ignored")
assert.Equal(test, true, object.IgnoresPath("abc/def"), "/abc/def should be ignored")
}
-// Validate "CompileIgnoreFile()" for correct handling leading / chars
+// Validate "CompileIgnoreFile()" for correct handling of leading / chars
func TestCompileIgnoreLines_HandleLeadingSlash(test *testing.T) {
writeFileToTestDir("test.gitignore", `
/a/b/c
@@ -150,3 +150,22 @@
// FILE based path checking tests
//
+// Validate "CompileIgnoreFile()" for correct handling of files starting with # or !
+func TestCompileIgnoreLines_HandleLeadingSpecialChars(test *testing.T) {
+ writeFileToTestDir("test.gitignore", `
+# Comment
+\#file.txt
+\!file.txt
+file.txt
+`)
+ defer cleanupTestDir()
+
+ object, _ := CompileIgnoreFile("./test_fixtures/test.gitignore")
+ assert.NotNil(test, object, "object should not be nil")
+
+ assert.Equal(test, true, object.IgnoresPath("#file.txt"), "#file.txt should be ignored")
+ assert.Equal(test, true, object.IgnoresPath("!file.txt"), "!file.txt should be ignored")
+ assert.Equal(test, true, object.IgnoresPath("file.txt"), "file.txt should be ignored")
+ assert.Equal(test, false, object.IgnoresPath("file2.txt"), "file2.txt should not be ignored")
+ assert.Equal(test, false, object.IgnoresPath("a/file.txt"), "a/file.txt should not be ignored")
+}