forked from ipfs/go-ipfs-files
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #10 from TRON-US/ipfs-0.5.1
Upgrade to upstream ipfs 0.5.1
- Loading branch information
Showing
8 changed files
with
240 additions
and
75 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ os: | |
language: go | ||
|
||
go: | ||
- 1.11.x | ||
- 1.14.x | ||
|
||
env: | ||
global: | ||
|
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,9 @@ | ||
module github.com/TRON-US/go-btfs-files | ||
|
||
require golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae | ||
require ( | ||
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 | ||
github.com/stretchr/testify v1.5.1 // indirect | ||
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,15 @@ | ||
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae h1:xiXzMMEQdQcric9hXtr1QU98MHunKK7OTtsoU6bYWs4= | ||
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
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= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= | ||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | ||
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= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
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
Oops, something went wrong.