-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4722767
commit 8b6b492
Showing
2 changed files
with
51 additions
and
23 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
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,42 @@ | ||
package ignore | ||
|
||
import ( | ||
"io/ioutil" | ||
"strings" | ||
|
||
gitignore "github.com/sabhiram/go-gitignore" | ||
) | ||
|
||
// DefaultIgnores is the default list of file globs that will be ignored | ||
var DefaultIgnores = []string{ | ||
".git", | ||
} | ||
|
||
type Ignore struct { | ||
compiled *gitignore.GitIgnore | ||
} | ||
|
||
// NewIgnore produces an Ignore object, with compiled lines from .gitignore and DefaultIgnores | ||
// which you can match files against | ||
func NewIgnore(lines []string) (*Ignore, error) { | ||
compiled, err := compileIgnoreLines(lines) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Ignore{compiled: compiled}, nil | ||
} | ||
|
||
// Match returns true if the provided file matches any of the defined ignores | ||
func (i *Ignore) Match(f string) bool { | ||
return i.compiled.MatchesPath(f) | ||
} | ||
|
||
func compileIgnoreLines(lines []string) (*gitignore.GitIgnore, error) { | ||
lines = append(lines, DefaultIgnores...) | ||
|
||
if buffer, err := ioutil.ReadFile(".gitignore"); err == nil { | ||
lines = append(lines, strings.Split(string(buffer), "\n")...) | ||
} | ||
|
||
return gitignore.CompileIgnoreLines(lines...) | ||
} |