-
Notifications
You must be signed in to change notification settings - Fork 142
/
ignore.go
68 lines (59 loc) · 1.49 KB
/
ignore.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
package the_platinum_searcher
import (
"os/exec"
"path/filepath"
"strings"
"github.com/monochromegane/go-gitignore"
"github.com/monochromegane/go-home"
)
type ignoreMatchers []gitignore.IgnoreMatcher
func (im ignoreMatchers) Match(path string, isDir bool) bool {
for _, ig := range im {
if ig == nil {
return false
}
if ig.Match(path, isDir) {
return true
}
}
return false
}
func newIgnoreMatchers(path string, ignores []string) ignoreMatchers {
var matchers ignoreMatchers
for _, i := range ignores {
if matcher, err := gitignore.NewGitIgnore(filepath.Join(path, i), path); err == nil {
matchers = append(matchers, matcher)
}
}
return matchers
}
func globalGitIgnore(base string) gitignore.IgnoreMatcher {
if homeDir := home.Dir(); homeDir != "" {
globalIgnore := globalGitIgnoreName()
if globalIgnore != "" {
if matcher, err := gitignore.NewGitIgnore(filepath.Join(homeDir, globalIgnore), base); err == nil {
return matcher
}
}
}
return nil
}
func globalGitIgnoreName() string {
gitCmd, err := exec.LookPath("git")
if err != nil {
return ""
}
file, err := exec.Command(gitCmd, "config", "--get", "core.excludesfile").Output()
if err != nil {
return ""
}
return strings.TrimSpace(filepath.Base(string(file)))
}
func homePtIgnore(base string) gitignore.IgnoreMatcher {
if homeDir := home.Dir(); homeDir != "" {
if matcher, err := gitignore.NewGitIgnore(filepath.Join(homeDir, ".ptignore"), base); err == nil {
return matcher
}
}
return nil
}