aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/gitignore
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing/format/gitignore')
-rw-r--r--plumbing/format/gitignore/dir.go8
-rw-r--r--plumbing/format/gitignore/dir_test.go119
-rw-r--r--plumbing/format/gitignore/pattern.go2
3 files changed, 121 insertions, 8 deletions
diff --git a/plumbing/format/gitignore/dir.go b/plumbing/format/gitignore/dir.go
index 15bc9c7..d8fb30c 100644
--- a/plumbing/format/gitignore/dir.go
+++ b/plumbing/format/gitignore/dir.go
@@ -3,11 +3,12 @@ package gitignore
import (
"bufio"
"bytes"
- "io/ioutil"
+ "io"
"os"
"strings"
"github.com/go-git/go-billy/v5"
+ "github.com/go-git/go-git/v5/internal/path_util"
"github.com/go-git/go-git/v5/plumbing/format/config"
gioutil "github.com/go-git/go-git/v5/utils/ioutil"
)
@@ -25,6 +26,9 @@ const (
// readIgnoreFile reads a specific git ignore file.
func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps []Pattern, err error) {
+
+ ignoreFile, _ = path_util.ReplaceTildeWithHome(ignoreFile)
+
f, err := fs.Open(fs.Join(append(path, ignoreFile)...))
if err == nil {
defer f.Close()
@@ -86,7 +90,7 @@ func loadPatterns(fs billy.Filesystem, path string) (ps []Pattern, err error) {
defer gioutil.CheckClose(f, &err)
- b, err := ioutil.ReadAll(f)
+ b, err := io.ReadAll(f)
if err != nil {
return
}
diff --git a/plumbing/format/gitignore/dir_test.go b/plumbing/format/gitignore/dir_test.go
index facc36d..465c571 100644
--- a/plumbing/format/gitignore/dir_test.go
+++ b/plumbing/format/gitignore/dir_test.go
@@ -2,7 +2,9 @@ package gitignore
import (
"os"
+ "os/user"
"strconv"
+ "strings"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
@@ -12,6 +14,8 @@ import (
type MatcherSuite struct {
GFS billy.Filesystem // git repository root
RFS billy.Filesystem // root that contains user home
+ RFSR billy.Filesystem // root that contains user home, but with relative ~/.gitignore_global
+ RFSU billy.Filesystem // root that contains user home, but with relative ~user/.gitignore_global
MCFS billy.Filesystem // root that contains user home, but missing ~/.gitconfig
MEFS billy.Filesystem // root that contains user home, but missing excludesfile entry
MIFS billy.Filesystem // root that contains user home, but missing .gitignore
@@ -63,6 +67,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
@@ -95,6 +120,64 @@ func (s *MatcherSuite) SetUpTest(c *C) {
s.RFS = fs
+ // root that contains user home, but with relative ~/.gitignore_global
+ fs = memfs.New()
+ err = fs.MkdirAll(home, os.ModePerm)
+ c.Assert(err, IsNil)
+
+ f, err = fs.Create(fs.Join(home, gitconfigFile))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("[core]\n"))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte(" excludesfile = ~/.gitignore_global" + "\n"))
+ c.Assert(err, IsNil)
+ err = f.Close()
+ c.Assert(err, IsNil)
+
+ f, err = fs.Create(fs.Join(home, ".gitignore_global"))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("# IntelliJ\n"))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte(".idea/\n"))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("*.iml\n"))
+ c.Assert(err, IsNil)
+ err = f.Close()
+ c.Assert(err, IsNil)
+
+ s.RFSR = fs
+
+ // root that contains user home, but with relative ~user/.gitignore_global
+ fs = memfs.New()
+ err = fs.MkdirAll(home, os.ModePerm)
+ c.Assert(err, IsNil)
+
+ f, err = fs.Create(fs.Join(home, gitconfigFile))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("[core]\n"))
+ c.Assert(err, IsNil)
+ currentUser, err := user.Current()
+ c.Assert(err, IsNil)
+ // remove domain for windows
+ username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:]
+ _, err = f.Write([]byte(" excludesfile = ~" + username + "/.gitignore_global" + "\n"))
+ c.Assert(err, IsNil)
+ err = f.Close()
+ c.Assert(err, IsNil)
+
+ f, err = fs.Create(fs.Join(home, ".gitignore_global"))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("# IntelliJ\n"))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte(".idea/\n"))
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("*.iml\n"))
+ c.Assert(err, IsNil)
+ err = f.Close()
+ c.Assert(err, IsNil)
+
+ s.RFSU = fs
+
// root that contains user home, but missing ~/.gitconfig
fs = memfs.New()
err = fs.MkdirAll(home, os.ModePerm)
@@ -183,15 +266,39 @@ 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) {
+ for _, fs := range []billy.Filesystem{s.RFSR, s.RFSU} {
+ ps, err := LoadGlobalPatterns(fs)
+ c.Assert(err, IsNil)
+ c.Assert(ps, HasLen, 2)
+
+ m := NewMatcher(ps)
+ c.Assert(m.Match([]string{".idea/"}, true), Equals, false)
+ c.Assert(m.Match([]string{"*.iml"}, true), Equals, true)
+ c.Assert(m.Match([]string{"IntelliJ"}, true), Equals, false)
+ }
}
func (s *MatcherSuite) TestDir_LoadGlobalPatterns(c *C) {
diff --git a/plumbing/format/gitignore/pattern.go b/plumbing/format/gitignore/pattern.go
index 098cb50..450b3cd 100644
--- a/plumbing/format/gitignore/pattern.go
+++ b/plumbing/format/gitignore/pattern.go
@@ -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) {