Skip to content

Commit

Permalink
plumbing: gitignore, fix incorrect parsing. Fixes #500
Browse files Browse the repository at this point in the history
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
  • Loading branch information
AriehSchneier committed May 29, 2023
1 parent 44feb6c commit 80d5f72
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
44 changes: 38 additions & 6 deletions plumbing/format/gitignore/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ func (s *MatcherSuite) SetUpTest(c *C) {
err = fs.MkdirAll("vendor/gopkg.in", os.ModePerm)
c.Assert(err, IsNil)

err = fs.MkdirAll("multiple/sub/ignores/first", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("multiple/sub/ignores/second", os.ModePerm)
c.Assert(err, IsNil)
f, err = fs.Create("multiple/sub/ignores/first/.gitignore")
c.Assert(err, IsNil)
_, err = f.Write([]byte("ignore_dir\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
f, err = fs.Create("multiple/sub/ignores/second/.gitignore")
c.Assert(err, IsNil)
_, err = f.Write([]byte("ignore_dir\n"))
c.Assert(err, IsNil)
err = f.Close()
c.Assert(err, IsNil)
err = fs.MkdirAll("multiple/sub/ignores/first/ignore_dir", os.ModePerm)
c.Assert(err, IsNil)
err = fs.MkdirAll("multiple/sub/ignores/second/ignore_dir", os.ModePerm)
c.Assert(err, IsNil)

s.GFS = fs

// setup root that contains user home
Expand Down Expand Up @@ -211,15 +232,26 @@ func (s *MatcherSuite) SetUpTest(c *C) {
}

func (s *MatcherSuite) TestDir_ReadPatterns(c *C) {
checkPatterns := func(ps []Pattern) {
c.Assert(ps, HasLen, 6)
m := NewMatcher(ps)

c.Assert(m.Match([]string{"exclude.crlf"}, true), Equals, true)
c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true)
c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true)
c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false)
c.Assert(m.Match([]string{"multiple", "sub", "ignores", "first", "ignore_dir"}, true), Equals, true)
c.Assert(m.Match([]string{"multiple", "sub", "ignores", "second", "ignore_dir"}, true), Equals, true)
}

ps, err := ReadPatterns(s.GFS, nil)
c.Assert(err, IsNil)
c.Assert(ps, HasLen, 4)
checkPatterns(ps)

m := NewMatcher(ps)
c.Assert(m.Match([]string{"exclude.crlf"}, true), Equals, true)
c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true)
c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true)
c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false)
// passing an empty slice with capacity to check we don't hit a bug where the extra capacity is reused incorrectly
ps, err = ReadPatterns(s.GFS, make([]string, 0, 6))
c.Assert(err, IsNil)
checkPatterns(ps)
}

func (s *MatcherSuite) TestDir_ReadRelativeGlobalGitIgnore(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions plumbing/format/gitignore/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type pattern struct {

// ParsePattern parses a gitignore pattern string into the Pattern structure.
func ParsePattern(p string, domain []string) Pattern {
// storing domain, copy it to ensure it isn't changed externally
domain = append([]string(nil), domain...)
res := pattern{domain: domain}

if strings.HasPrefix(p, inclusionPrefix) {
Expand Down

0 comments on commit 80d5f72

Please sign in to comment.