From 1cf9c3b5eb6d7aa34368bb4ec3f360ea95803188 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 15 Mar 2023 12:17:26 +0000 Subject: [PATCH] sourceignore: make a copy of domain for pattern LoadIgnorePatterns() modified the domain string slice after creating ignore pattern, due to which the domain of certain patterns created below the first directory level with multiple directories at the same level resulted in the patterns to have the domain of the last directory in the same directory level. This is fixed by making a copy of the domain before creating a pattern. Signed-off-by: Sunny --- sourceignore/sourceignore.go | 9 +++++++-- sourceignore/sourceignore_test.go | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sourceignore/sourceignore.go b/sourceignore/sourceignore.go index 80b8ed62..f8d6ec8a 100644 --- a/sourceignore/sourceignore.go +++ b/sourceignore/sourceignore.go @@ -103,7 +103,12 @@ func ReadIgnoreFile(path string, domain []string) ([]gitignore.Pattern, error) { // LoadIgnorePatterns recursively loads the IgnoreFile patterns found // in the directory. func LoadIgnorePatterns(dir string, domain []string) ([]gitignore.Pattern, error) { - ps, err := ReadIgnoreFile(filepath.Join(dir, IgnoreFile), domain) + // Make a copy of the domain so that the underlying string array of domain + // in the gitignore patterns are unique without any side effects. + dom := make([]string, len(domain)) + copy(dom, domain) + + ps, err := ReadIgnoreFile(filepath.Join(dir, IgnoreFile), dom) if err != nil { return nil, err } @@ -114,7 +119,7 @@ func LoadIgnorePatterns(dir string, domain []string) ([]gitignore.Pattern, error for _, fi := range fis { if fi.IsDir() && fi.Name() != ".git" { var subps []gitignore.Pattern - if subps, err = LoadIgnorePatterns(filepath.Join(dir, fi.Name()), append(domain, fi.Name())); err != nil { + if subps, err = LoadIgnorePatterns(filepath.Join(dir, fi.Name()), append(dom, fi.Name())); err != nil { return nil, err } if len(subps) > 0 { diff --git a/sourceignore/sourceignore_test.go b/sourceignore/sourceignore_test.go index e298bdc3..6e0b36fe 100644 --- a/sourceignore/sourceignore_test.go +++ b/sourceignore/sourceignore_test.go @@ -203,6 +203,8 @@ func TestLoadExcludePatterns(t *testing.T) { "d/.gitignore": "ignored", "z/.sourceignore": "last.txt", "a/b/.sourceignore": "subdir.txt", + "e/last.txt": "foo", + "a/c/subdir.txt": "bar", } for n, c := range files { if err := os.MkdirAll(filepath.Join(tmpDir, filepath.Dir(n)), 0o750); err != nil { @@ -222,7 +224,7 @@ func TestLoadExcludePatterns(t *testing.T) { name: "traverse loads", dir: tmpDir, want: []gitignore.Pattern{ - gitignore.ParsePattern("root.txt", nil), + gitignore.ParsePattern("root.txt", []string{}), gitignore.ParsePattern("subdir.txt", []string{"a", "b"}), gitignore.ParsePattern("last.txt", []string{"z"}), },