-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathignore.go
57 lines (44 loc) · 1.29 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
package shared
import (
"fmt"
"os"
gitignore "github.com/sabhiram/go-gitignore"
)
type ignoreFn = func(fileName string) bool
var (
DEFAULT_IGNORE_FILE = ".gitignore"
)
func loadIgnoreFile(fileName string) ignoreFn {
if fileName == "" {
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: ignore file disabled\n")
}
return func(fileName string) bool { return false }
}
_, statErr := os.Stat(fileName)
if statErr != nil {
if fileName == DEFAULT_IGNORE_FILE { //LATER: preferrable to use pflags.Changed, but would need a global var for rootCmd...
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: skipping default ignore file %s\n", fileName)
}
return func(fileName string) bool { return false }
}
fmt.Fprintf(os.Stderr, "ERROR: unable to find ignore file '%s': %v\n", fileName, statErr)
os.Exit(5)
}
ignorer, err := gitignore.CompileIgnoreFile(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to compile ignore file %s: %v", fileName, err)
os.Exit(6)
}
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: loaded ignore file %s\n", fileName)
}
return func(target string) bool {
matches, how := ignorer.MatchesPathHow(target)
if Debug && matches {
fmt.Fprintf(os.Stderr, "DEBUG: skipping %s (%s: line %d)\n", target, fileName, how.LineNo)
}
return matches
}
}