This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from fission-suite/feat/add-ignore-rules
Feat/add ignore rules
- Loading branch information
Showing
6 changed files
with
194 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package files | ||
|
||
import ( | ||
"os" | ||
|
||
ignore "github.com/crackcomm/go-gitignore" | ||
) | ||
|
||
// Filter represents a set of rules for determining if a file should be included or excluded. | ||
// A rule follows the syntax for patterns used in .gitgnore files for specifying untracked files. | ||
// Examples: | ||
// foo.txt | ||
// *.app | ||
// bar/ | ||
// **/baz | ||
// fizz/** | ||
type Filter struct { | ||
// IncludeHidden - Include hidden files | ||
IncludeHidden bool | ||
// Rules - File filter rules | ||
Rules *ignore.GitIgnore | ||
} | ||
|
||
// NewFilter creates a new file filter from a .gitignore file and/or a list of ignore rules. | ||
// An ignoreFile is a path to a file with .gitignore-style patterns to exclude, one per line | ||
// rules is an array of strings representing .gitignore-style patterns | ||
// For reference on ignore rule syntax, see https://git-scm.com/docs/gitignore | ||
func NewFilter(ignoreFile string, rules []string, includeHidden bool) (*Filter, error) { | ||
var ignoreRules *ignore.GitIgnore | ||
var err error | ||
if ignoreFile == "" { | ||
ignoreRules, err = ignore.CompileIgnoreLines(rules...) | ||
} else { | ||
ignoreRules, err = ignore.CompileIgnoreFileAndLines(ignoreFile, rules...) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Filter{IncludeHidden: includeHidden, Rules: ignoreRules}, nil | ||
} | ||
|
||
// ShouldExclude takes an os.FileInfo object and applies rules to determine if its target should be excluded. | ||
func (filter *Filter) ShouldExclude(fileInfo os.FileInfo) (result bool) { | ||
path := fileInfo.Name() | ||
if !filter.IncludeHidden && isHidden(fileInfo) { | ||
return true | ||
} | ||
return filter.Rules.MatchesPath(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package files | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
type mockFileInfo struct { | ||
os.FileInfo | ||
name string | ||
} | ||
|
||
func (m *mockFileInfo) Name() string { | ||
return m.name | ||
} | ||
|
||
var _ os.FileInfo = &mockFileInfo{} | ||
|
||
func TestFileFilter(t *testing.T) { | ||
includeHidden := true | ||
filter, err := NewFilter("", nil, includeHidden) | ||
if err != nil { | ||
t.Errorf("failed to create filter with empty rules") | ||
} | ||
if filter.IncludeHidden != includeHidden { | ||
t.Errorf("new filter should include hidden files") | ||
} | ||
_, err = NewFilter("ignoreFileThatDoesNotExist", nil, false) | ||
if err == nil { | ||
t.Errorf("creating a filter without an invalid ignore file path should have failed") | ||
} | ||
tmppath, err := ioutil.TempDir("", "filter-test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
ignoreFilePath := filepath.Join(tmppath, "ignoreFile") | ||
ignoreFileContents := []byte("a.txt") | ||
if err := ioutil.WriteFile(ignoreFilePath, ignoreFileContents, 0666); err != nil { | ||
t.Fatal(err) | ||
} | ||
filterWithIgnoreFile, err := NewFilter(ignoreFilePath, nil, false) | ||
if err != nil { | ||
t.Errorf("failed to create filter with ignore file") | ||
} | ||
if !filterWithIgnoreFile.ShouldExclude(&mockFileInfo{name: "a.txt"}) { | ||
t.Errorf("filter should've excluded expected file from ignoreFile: %s", "a.txt") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module github.com/ipfs/go-ipfs-files | ||
|
||
require golang.org/x/sys v0.0.0-20190302025703-b6889370fb10 | ||
require ( | ||
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 | ||
golang.org/x/sys v0.0.0-20190302025703-b6889370fb10 | ||
) | ||
|
||
go 1.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 h1:HVTnpeuvF6Owjd5mniCL8DEXo7uYXdQEmOP4FJbV5tg= | ||
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3/go.mod h1:p1d6YEZWvFzEh4KLyvBcVSnrfNDDvK2zfK/4x2v/4pE= | ||
golang.org/x/sys v0.0.0-20190302025703-b6889370fb10 h1:xQJI9OEiErEQ++DoXOHqEpzsGMrAv2Q2jyCpi7DmfpQ= | ||
golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters