Skip to content

Commit

Permalink
* Remove ErrorFiles and ErrorDirs.
Browse files Browse the repository at this point in the history
  • Loading branch information
zealic committed Jan 29, 2019
1 parent de52b0e commit 374dead
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 58 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A golang package for pattern matching of file paths. Like gitignore, dockerignor
```golang
result := xignore.DirMatches("/workspace/my_project", &MatchesOptions{
Ignorefile: ".gitignore",
Nested: true, // Handle nested ignorefile
})

// ignorefile rules matched files
Expand All @@ -27,8 +28,6 @@ fmt.Printf("%#v\n", result.UnmatchedFiles)
fmt.Printf("%#v\n", result.MatchedDirs)
// ignorefile rules unmatched dirs
fmt.Printf("%#v\n", result.UnmatchedDirs)
// error files when return error
fmt.Printf("%#v\n", result.ErrorFiles)
```


Expand Down
21 changes: 7 additions & 14 deletions matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (m *Matcher) Matches(basedir string, options *MatchesOptions) (*MatchesResu

// Root filemap
rootMap := stateMap{}
files, errorFiles := collectFiles(vfs)
files, err := collectFiles(vfs)
if err != nil {
return nil, err
}
// Init all files state
rootMap.mergeFiles(files, false)

Expand All @@ -45,13 +48,10 @@ func (m *Matcher) Matches(basedir string, options *MatchesOptions) (*MatchesResu
}

// Apply ignorefile patterns
ierrFiles, err := rootMap.applyIgnorefile(vfs, ignorefile, options.Nested)
err = rootMap.applyIgnorefile(vfs, ignorefile, options.Nested)
if err != nil {
return nil, err
}
for _, efile := range ierrFiles {
errorFiles = append(errorFiles, efile)
}

// Apply after patterns
afterPatterns, err := makePatterns(options.AfterPatterns)
Expand All @@ -63,23 +63,20 @@ func (m *Matcher) Matches(basedir string, options *MatchesOptions) (*MatchesResu
return nil, err
}

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

func makeResult(vfs afero.Fs, basedir string,
fileMap stateMap, errorFiles []string) (*MatchesResult, error) {
func makeResult(vfs afero.Fs, basedir string, fileMap stateMap) (*MatchesResult, error) {
matchedFiles := []string{}
unmatchedFiles := []string{}
matchedDirs := []string{}
unmatchedDirs := []string{}
errorDirs := []string{}
for f, matched := range fileMap {
if f == "" {
continue
}
isDir, err := afero.IsDir(vfs, f)
if err != nil {
errorDirs = append(errorDirs, f)
return nil, err
}
if isDir {
Expand All @@ -99,17 +96,13 @@ func makeResult(vfs afero.Fs, basedir string,

sort.Strings(matchedFiles)
sort.Strings(unmatchedFiles)
sort.Strings(errorFiles)
sort.Strings(matchedDirs)
sort.Strings(unmatchedDirs)
sort.Strings(errorDirs)
return &MatchesResult{
BaseDir: basedir,
MatchedFiles: matchedFiles,
UnmatchedFiles: unmatchedFiles,
ErrorFiles: errorFiles,
MatchedDirs: matchedDirs,
UnmatchedDirs: unmatchedDirs,
ErrorDirs: errorDirs,
}, nil
}
16 changes: 0 additions & 16 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ func TestMatches_Simple(t *testing.T) {

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

func TestMatches_Simple_WithBeforePatterns(t *testing.T) {
Expand All @@ -31,10 +29,8 @@ func TestMatches_Simple_WithBeforePatterns(t *testing.T) {

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) {
Expand All @@ -47,10 +43,8 @@ func TestMatches_Simple_WithAfterPatterns(t *testing.T) {

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) {
Expand All @@ -62,10 +56,8 @@ func TestMatches_Folder(t *testing.T) {

require.Equal(t, []string{"foo/bar/1.txt"}, result.MatchedFiles)
require.Equal(t, []string{".xignore", "foo/bar/tool/lex.txt", "foo/tar/2.txt"}, result.UnmatchedFiles)
require.Empty(t, result.ErrorFiles)
require.Equal(t, []string{"foo/bar"}, result.MatchedDirs)
require.Equal(t, []string{"foo", "foo/bar/tool", "foo/tar"}, result.UnmatchedDirs)
require.Empty(t, result.ErrorDirs)
}

func TestMatches_Root(t *testing.T) {
Expand All @@ -77,10 +69,8 @@ func TestMatches_Root(t *testing.T) {

require.Equal(t, []string{"1.txt"}, result.MatchedFiles)
require.Equal(t, []string{".xignore", "sub/1.txt", "sub/2.txt"}, result.UnmatchedFiles)
require.Empty(t, result.ErrorFiles)
require.Empty(t, result.MatchedDirs)
require.Equal(t, []string{"sub"}, result.UnmatchedDirs)
require.Empty(t, result.ErrorDirs)
}

func TestMatches_Exclusion(t *testing.T) {
Expand All @@ -92,10 +82,8 @@ func TestMatches_Exclusion(t *testing.T) {

require.Equal(t, []string{"e1.txt", "e3.txt", "en/e3.txt"}, result.MatchedFiles)
require.Equal(t, []string{"!", ".xignore", "e2.txt", "en/e1.txt", "en/e2.txt"}, result.UnmatchedFiles)
require.Empty(t, result.ErrorFiles)
require.Empty(t, result.MatchedDirs)
require.Equal(t, []string{"en"}, result.UnmatchedDirs)
require.Empty(t, result.ErrorDirs)
}

func TestMatches_DisabledNested(t *testing.T) {
Expand All @@ -114,10 +102,8 @@ func TestMatches_DisabledNested(t *testing.T) {
"inner/.xignore", "inner/2.lst",
"inner/inner2/.xignore", "inner/inner2/jess.ini", "inner/inner2/moss.ini",
}, result.UnmatchedFiles)
require.Empty(t, result.ErrorFiles)
require.Empty(t, result.MatchedDirs)
require.Equal(t, []string{"inner", "inner/inner2"}, result.UnmatchedDirs)
require.Empty(t, result.ErrorDirs)
}

func TestMatches_Nested(t *testing.T) {
Expand All @@ -136,8 +122,6 @@ func TestMatches_Nested(t *testing.T) {
"inner/.xignore",
"inner/inner2/.xignore", "inner/inner2/jess.ini",
}, result.UnmatchedFiles)
require.Empty(t, result.ErrorFiles)
require.Empty(t, result.MatchedDirs)
require.Equal(t, result.UnmatchedDirs, []string{"inner", "inner/inner2"})
require.Empty(t, result.ErrorDirs)
}
34 changes: 12 additions & 22 deletions statemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import (

type stateMap map[string]bool

func collectFiles(fs afero.Fs) (files []string, errFiles []string) {
func collectFiles(fs afero.Fs) (files []string, err error) {
files = []string{}
errFiles = []string{}

afero.Walk(fs, "", func(path string, info os.FileInfo, werr error) error {
if werr != nil {
errFiles = append(errFiles, path)
} else {
files = append(files, path)
err = werr
return nil
}
files = append(files, path)
return nil
})
return
Expand Down Expand Up @@ -100,7 +99,7 @@ func (state stateMap) applyPatterns(vfs afero.Fs, files []string, patterns []*Pa
return nil
}

func (state stateMap) applyIgnorefile(vfs afero.Fs, ignorefile string, nested bool) ([]string, error) {
func (state stateMap) applyIgnorefile(vfs afero.Fs, ignorefile string, nested bool) error {
// Apply nested ignorefile
ignorefiles := []string{}

Expand All @@ -121,7 +120,6 @@ func (state stateMap) applyIgnorefile(vfs afero.Fs, ignorefile string, nested bo
ignorefiles = []string{ignorefile}
}

errorFiles := []string{}
for _, ifile := range ignorefiles {
currBasedir := filepath.Dir(ifile)
currFs := vfs
Expand All @@ -130,17 +128,17 @@ func (state stateMap) applyIgnorefile(vfs afero.Fs, ignorefile string, nested bo
}
patterns, err := loadPatterns(currFs, ignorefile)
if err != nil {
return nil, err
return err
}

currMap := stateMap{}
currFiles, errorFiles := collectFiles(currFs)
currMap.applyPatterns(currFs, currFiles, patterns)
for _, efile := range errorFiles {
errorFiles = append(errorFiles, filepath.Join(currBasedir, efile))
currFiles, err := collectFiles(currFs)
if err != nil {
return err
}
err = currMap.applyPatterns(currFs, currFiles, patterns)
if err != nil {
return errorFiles, err
return err
}

for nfile, matched := range currMap {
Expand All @@ -149,13 +147,5 @@ func (state stateMap) applyIgnorefile(vfs afero.Fs, ignorefile string, nested bo
}
}

// Remove error files
for _, efile := range errorFiles {
errorFiles = append(errorFiles, efile)
if ok := state[efile]; ok {
delete(state, efile)
}
}

return errorFiles, nil
return nil
}
4 changes: 0 additions & 4 deletions xignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ type MatchesResult struct {
MatchedDirs []string
// ignorefile rules unmatched dirs
UnmatchedDirs []string
// error files when return error
ErrorFiles []string
// error files when return error
ErrorDirs []string
}

// DirMatches returns match result from basedir.
Expand Down

0 comments on commit 374dead

Please sign in to comment.