Skip to content

Commit

Permalink
Merge pull request sabhiram#1 from falconandy/master
Browse files Browse the repository at this point in the history
fix incorrect Windows-specific behaviour
  • Loading branch information
sabhiram committed Nov 11, 2015
2 parents f9a1328 + a346e04 commit e5158f0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"strings"
"regexp"
"io/ioutil"
"os"
)

// An IgnoreParser is an interface which exposes two methods:
Expand All @@ -75,6 +76,9 @@ type GitIgnore struct {
// This function pretty much attempts to mimic the parsing rules
// listed above at the start of this file
func getPatternFromLine(line string) (*regexp.Regexp, bool) {
// Trim OS-specific carriage returns.
line = strings.TrimRight(line, "\r")

// Strip comments [Rule 2]
if regexp.MustCompile(`^#`).MatchString(line) { return nil, false }

Expand Down Expand Up @@ -156,6 +160,9 @@ func CompileIgnoreFile(fpath string) (*GitIgnore, error) {
// It returns true if the given GitIgnore structure would target a given
// path string "f"
func (g GitIgnore) MatchesPath(f string) bool {
// Replace OS-specific path separator.
f = strings.Replace(f, string(os.PathSeparator), "/", -1)

matchesPath := false
for idx, pattern := range g.patterns {
if pattern.MatchString(f) {
Expand Down
26 changes: 26 additions & 0 deletions ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"runtime"
)

const (
Expand Down Expand Up @@ -257,3 +258,28 @@ func TestCompileIgnoreLines_CheckNestedDotFiles(test *testing.T) {
assert.Equal(test, true, object.MatchesPath("external/barfoo/.gitignore"), "external/barfoo/.gitignore")
assert.Equal(test, true, object.MatchesPath("external/barfoo/.bower.json"), "external/barfoo/.bower.json")
}

func TestCompileIgnoreLines_CarriageReturn(test *testing.T) {
lines := []string{"abc/def\r", "a/b/c\r", "b\r"}
object, error := CompileIgnoreLines(lines...)
assert.Nil(test, error, "error from CompileIgnoreLines should be nil")

assert.Equal(test, true, object.MatchesPath("abc/def/child"), "abc/def/child should match")
assert.Equal(test, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match")

assert.Equal(test, false, object.MatchesPath("abc"), "abc should not match")
assert.Equal(test, false, object.MatchesPath("def"), "def should not match")
assert.Equal(test, false, object.MatchesPath("bd"), "bd should not match")
}

func TestCompileIgnoreLines_WindowsPath(test *testing.T) {
if runtime.GOOS != "windows" {
return
}
lines := []string{"abc/def", "a/b/c", "b"}
object, error := CompileIgnoreLines(lines...)
assert.Nil(test, error, "error from CompileIgnoreLines should be nil")

assert.Equal(test, true, object.MatchesPath("abc\\def\\child"), "abc\\def\\child should match")
assert.Equal(test, true, object.MatchesPath("a\\b\\c\\d"), "a\\b\\c\\d should match")
}

0 comments on commit e5158f0

Please sign in to comment.