Skip to content

Commit

Permalink
+ BeforePatterns and AfterPatterns support.
Browse files Browse the repository at this point in the history
  • Loading branch information
zealic committed Jan 29, 2019
1 parent 29b5fc1 commit de52b0e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
22 changes: 21 additions & 1 deletion matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ func (m *Matcher) Matches(basedir string, options *MatchesOptions) (*MatchesResu
// Init all files state
rootMap.mergeFiles(files, false)

// Apply ignorefile pattern
// Apply before patterns
beforePatterns, err := makePatterns(options.BeforePatterns)
if err != nil {
return nil, err
}
err = rootMap.applyPatterns(vfs, files, beforePatterns)
if err != nil {
return nil, err
}

// Apply ignorefile patterns
ierrFiles, err := rootMap.applyIgnorefile(vfs, ignorefile, options.Nested)
if err != nil {
return nil, err
Expand All @@ -43,6 +53,16 @@ func (m *Matcher) Matches(basedir string, options *MatchesOptions) (*MatchesResu
errorFiles = append(errorFiles, efile)
}

// Apply after patterns
afterPatterns, err := makePatterns(options.AfterPatterns)
if err != nil {
return nil, err
}
err = rootMap.applyPatterns(vfs, files, afterPatterns)
if err != nil {
return nil, err
}

return makeResult(vfs, basedir, rootMap, errorFiles)
}

Expand Down
32 changes: 32 additions & 0 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ func TestMatches_Simple(t *testing.T) {
require.Empty(t, result.ErrorDirs)
}

func TestMatches_Simple_WithBeforePatterns(t *testing.T) {
matcher := NewSystemMatcher()
result, err := matcher.Matches("testdata/simple", &MatchesOptions{
Ignorefile: ".xignore",
BeforePatterns: []string{"rain.txt"},
})
require.NoError(t, err)

require.Equal(t, result.MatchedFiles, []string{".xignore", "empty.log", "rain.txt"})
require.Equal(t, result.UnmatchedFiles, []string{})
require.Empty(t, result.ErrorFiles)
require.Empty(t, result.MatchedDirs)
require.Empty(t, result.UnmatchedDirs)
require.Empty(t, result.ErrorDirs)
}

func TestMatches_Simple_WithAfterPatterns(t *testing.T) {
matcher := NewSystemMatcher()
result, err := matcher.Matches("testdata/simple", &MatchesOptions{
Ignorefile: ".xignore",
AfterPatterns: []string{"!.xignore", "!empty.log", "rain.txt"},
})
require.NoError(t, err)

require.Equal(t, result.MatchedFiles, []string{"rain.txt"})
require.Equal(t, result.UnmatchedFiles, []string{".xignore", "empty.log"})
require.Empty(t, result.ErrorFiles)
require.Empty(t, result.MatchedDirs)
require.Empty(t, result.UnmatchedDirs)
require.Empty(t, result.ErrorDirs)
}

func TestMatches_Folder(t *testing.T) {
matcher := NewSystemMatcher()
result, err := matcher.Matches("testdata/folder", &MatchesOptions{
Expand Down
16 changes: 16 additions & 0 deletions pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,19 @@ func loadPatterns(vfs afero.Fs, ignorefile string) ([]*Pattern, error) {

return patterns, nil
}

func makePatterns(strPatterns []string) ([]*Pattern, error) {
if strPatterns == nil || len(strPatterns) == 0 {
return []*Pattern{}, nil
}

patterns := make([]*Pattern, len(strPatterns))
for i, sp := range strPatterns {
pattern := NewPattern(sp)
if err := pattern.Prepare(); err != nil {
return nil, err
}
patterns[i] = pattern
}
return patterns, nil
}
4 changes: 4 additions & 0 deletions xignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type MatchesOptions struct {
Ignorefile string
// Allow nested ignorefile
Nested bool
// apply patterns before all ignorefile
BeforePatterns []string
// apply patterns after all ignorefile
AfterPatterns []string
}

// MatchesResult matches result
Expand Down

0 comments on commit de52b0e

Please sign in to comment.