Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plumbing: gitignore, fix incorrect parsing. Fixes #500 #781

Merged
merged 1 commit into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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