-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignore.go
80 lines (68 loc) · 1.52 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
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
)
// ignoreFile reads exclude and include patterns from a file. If the path does
// not exist, no patterns are returned.
func ignoreFile(path string) (exclude, include []string, err error) {
r, err := os.Open(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, nil, fmt.Errorf("unable to open ignore file")
} else if errors.Is(err, os.ErrNotExist) {
// no ignore file
return nil, nil, nil
}
s := bufio.NewScanner(r)
for s.Scan() {
l := strings.TrimSpace(s.Text())
if len(l) == 0 || l[0] == '#' {
continue
}
if l[0] == '!' {
include = append(include, l)
} else {
exclude = append(exclude, l)
}
}
if s.Err() != nil {
return nil, nil, fmt.Errorf("read ignore file: %w", err)
}
return exclude, include, nil
}
// ignore checks whether the string p should be included. If p matches a pattern
// from the exclude list, match will return false unless it also matches a
// pattern from the include list.
func ignore(exclude, include []string, p string) bool {
matchesExclude := false
for _, e := range exclude {
m, err := filepath.Match(e, p)
if err != nil {
slog.Warn("failed to match", "error", err)
continue
}
if m {
matchesExclude = true
break
}
}
if !matchesExclude {
return false
}
for _, i := range include {
m, err := filepath.Match(i, p)
if err != nil {
slog.Warn("failed to match", "error", err)
continue
}
if m {
return false
}
}
return true
}