Skip to content
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ m.AddFromFile("/path/to/repo/src/.gitignore", "src")
m.AddPatterns([]byte("*.log\nbuild/\n"), "")
```

When pattern bytes came from another filesystem, use `AddPatternsWithSource`
to preserve the source label returned by `MatchDetail` and `Errors`:

```go
m.AddPatternsWithSource(data, "src", "src/.gitignore")
```

To limit the bytes read from each ignore file, pass `MaxIgnoreFileSize` to any constructor or walk function:

```go
Expand Down
6 changes: 6 additions & 0 deletions gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ func (m *Matcher) AddPatterns(data []byte, dir string) {
m.addPatterns(data, dir, "")
}

// AddPatternsWithSource parses gitignore pattern lines from data, scopes them
// to the given relative directory, and retains source as match provenance.
func (m *Matcher) AddPatternsWithSource(data []byte, dir, source string) {
m.addPatterns(data, dir, source)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

// AddFromFile reads a .gitignore file at the given absolute path and scopes
// its patterns to the given relative directory. It uses the matcher's file-size
// limit, if set, and records oversized files in Errors without applying any rules.
Expand Down
15 changes: 15 additions & 0 deletions gitignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,21 @@ func TestAddPatterns(t *testing.T) {
}
}

func TestAddPatternsWithSource(t *testing.T) {
m := gitignore.New("")
m.AddPatternsWithSource([]byte("*.env\n[[:bogus:]]\n"), "services/api", "services/api/.gitignore")

result := m.MatchDetail("services/api/local.env")
if !result.Ignored || result.Pattern != "*.env" || result.Line != 1 ||
result.Source != "services/api/.gitignore" {
t.Fatalf("MatchDetail = %#v", result)
}
errors := m.Errors()
if len(errors) != 1 || errors[0].Source != "services/api/.gitignore" || errors[0].Line != 2 {
t.Fatalf("Errors = %#v", errors)
}
}

func TestNewEmptyRootSkipsFilesystem(t *testing.T) {
// Create a temporary directory with a .gitignore that excludes *.exe
dir := t.TempDir()
Expand Down
Loading