-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgitignore.go
79 lines (66 loc) · 1.64 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package wildcat
import (
"path/filepath"
"strings"
ignore "github.com/sabhiram/go-gitignore"
)
// NewNoIgnore creates an instance of Ignore to ignore nothing.
func NewNoIgnore() Ignore {
return &noIgnore{parent: nil}
}
// Ignore is an interface for checking the given path is the ignoring target or not.
type Ignore interface {
IsIgnore(path string) bool
Filter(targets []string) []string
}
type noIgnore struct {
parent Ignore
}
func (ni *noIgnore) Filter(slice []string) []string {
return slice
}
func (ni *noIgnore) IsIgnore(path string) bool {
if ni.parent != nil {
return ni.parent.IsIgnore(path)
}
return false
}
type gitIgnore struct {
ignore *ignore.GitIgnore
parent Ignore
}
func (gi *gitIgnore) Filter(slice []string) []string {
results := []string{}
for _, item := range slice {
if !gi.IsIgnore(item) && !strings.HasSuffix(item, "/.gitignore") {
results = append(results, item)
}
}
return results
}
func (gi *gitIgnore) IsIgnore(path string) bool {
if !gi.ignore.MatchesPath(path) {
if gi.parent != nil {
return gi.parent.IsIgnore(path)
}
return false
}
return true
}
func newIgnoreWithParent(dirPath string, parent Ignore) Ignore {
gitIgnoreFile := filepath.Join(dirPath, ".gitignore")
if ExistFile(gitIgnoreFile) {
return newGitIgnore(gitIgnoreFile, parent)
}
return &noIgnore{parent: parent}
}
func newIgnore(dirPath string) Ignore {
return newIgnoreWithParent(dirPath, nil)
}
func newGitIgnore(gitIgnoreFilePath string, parent Ignore) Ignore {
gi, err := ignore.CompileIgnoreFile(gitIgnoreFilePath)
if err != nil {
return &noIgnore{parent: parent}
}
return &gitIgnore{ignore: gi, parent: parent}
}