-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgitignore.go
34 lines (29 loc) · 1.06 KB
/
gitignore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package utility
import (
"os"
"strings"
ignore "github.com/sabhiram/go-gitignore"
)
// gitIgnoreFileMatcher contains the information for building a list of files in the given directory.
// It adds the files to include in the fileNames array and uses the ignorer to determine if a given
// file matches and should be added.
type gitIgnoreFileMatcher struct {
ignorer *ignore.GitIgnore
prefix string
}
// NewGitIgnoreFileMatcher returns a FileMatcher that matches the
// expressions rooted at the given prefix. The expressions should be gitignore
// ignore expressions: antyhing that would be matched - and therefore ignored by
// git - is matched.
func NewGitIgnoreFileMatcher(prefix string, exprs ...string) FileMatcher {
ignorer := ignore.CompileIgnoreLines(exprs...)
m := &gitIgnoreFileMatcher{
ignorer: ignorer,
prefix: prefix,
}
return m
}
func (m *gitIgnoreFileMatcher) Match(file string, info os.FileInfo) bool {
file = strings.TrimLeft(strings.TrimPrefix(file, m.prefix), string(os.PathSeparator))
return !info.IsDir() && m.ignorer.MatchesPath(file)
}