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

sourceignore: make a copy of domain for pattern #512

Merged
merged 1 commit into from
Mar 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions sourceignore/sourceignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion sourceignore/sourceignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the copied domain, the DeepEqual below fails when this domain is nil, even though all the components of the pattern are exactly the same.

gitignore.ParsePattern("subdir.txt", []string{"a", "b"}),
gitignore.ParsePattern("last.txt", []string{"z"}),
},
Expand Down