Debian Codegraph @

glob/watchCriteria.go

raw symbols SCIP
1 package glob
2
3 import (
4 "fmt"
5 "path/filepath"
6 "regexp"
7 "strings"
8
9 "github.com/mgutz/str"
10 )
11
12 // WatchCriterion is the criteria needed to test if a file
13 // matches a pattern.
14 type WatchCriterion struct {
15 // Root is the root directory to start watching.
16 Root string
17 // Includes are the regexp for including files
18 IncludesRegexp []*regexp.Regexp
19 // Excludes are the regexp for excluding files
20 ExcludesRegexp []*regexp.Regexp
21 Includes []string
22 Excludes []string
23 }
24
25 func newWatchCriterion(r string) *WatchCriterion {
26 return &WatchCriterion{
27 Root: r,
28 IncludesRegexp: []*regexp.Regexp{},
29 ExcludesRegexp: []*regexp.Regexp{},
30 Includes: []string{},
31 Excludes: []string{},
32 }
33 }
34
35 // WatchCriteria is the set of criterion to watch one or more glob patterns.
36 type WatchCriteria struct {
37 Items []*WatchCriterion
38 }
39
40 func newWatchCriteria() *WatchCriteria {
41 return &WatchCriteria{
42 Items: []*WatchCriterion{},
43 }
44 }
45
46 func (cr *WatchCriteria) findParent(root string) *WatchCriterion {
47 for _, item := range cr.Items {
48 if item.Root == root || strings.Contains(item.Root, root) {
49 return item
50 }
51 }
52 return nil
53 }
54
55 func (cr *WatchCriteria) add(glob string) error {
56 var err error
57
58 if glob == "" || glob == "!" {
59 return nil
60 }
61
62 isExclude := strings.HasPrefix(glob, "!")
63 if isExclude {
64 glob = glob[1:]
65 }
66
67 // determine if the root of pattern already exists
68 root := PatternRoot(glob)
69 root, err = filepath.Abs(root)
70 if err != nil {
71 return err
72 }
73 root = filepath.ToSlash(root)
74 cri := cr.findParent(root)
75 if cri == nil {
76 cri = newWatchCriterion(root)
77 cr.Items = append(cr.Items, cri)
78 }
79
80 glob, err = filepath.Abs(glob)
81 if err != nil {
82 return err
83 }
84
85 // add glob to {in,ex}cludes
86 if isExclude {
87 if str.SliceIndexOf(cri.Excludes, glob) < 0 {
88 re := Globexp(glob)
89 cri.ExcludesRegexp = append(cri.ExcludesRegexp, re)
90 cri.Excludes = append(cri.Excludes, glob)
91 }
92 } else {
93 if str.SliceIndexOf(cri.Includes, glob) < 0 {
94 re := Globexp(glob)
95 cri.IncludesRegexp = append(cri.IncludesRegexp, re)
96 cri.Includes = append(cri.Includes, glob)
97 }
98 }
99
100 return nil
101 }
102
103 // Roots returns the root paths of all criteria.
104 func (cr *WatchCriteria) Roots() []string {
105 if cr.Items == nil || len(cr.Items) == 0 {
106 return nil
107 }
108
109 roots := make([]string, len(cr.Items))
110 for i, it := range cr.Items {
111 roots[i] = it.Root
112 }
113 return roots
114 }
115
116 // Matches determines if pth is matched by internal criteria.
117 func (cr *WatchCriteria) Matches(pth string) bool {
118 match := false
119 pth = filepath.ToSlash(pth)
120 for _, it := range cr.Items {
121 // if sub path
122 if strings.HasPrefix(pth, it.Root) {
123 // check if matches an include pattern
124 for _, re := range it.IncludesRegexp {
125 if re.MatchString(pth) {
126 match = true
127 break
128 }
129 }
130 // when found, check if it is excluded
131 if match {
132 for _, re := range it.ExcludesRegexp {
133 if re.MatchString(pth) {
134 match = false
135 break
136 }
137 }
138 if match {
139 return true
140 }
141 }
142 }
143 }
144
145 return false
146 }
147
148 // EffectiveCriteria is the minimum set of criteria to watch the
149 // items in patterns
150 func EffectiveCriteria(globs ...string) (*WatchCriteria, error) {
151 if len(globs) == 0 {
152 return nil, nil
153 }
154 result := newWatchCriteria()
155 for _, glob := range globs {
156 err := result.add(glob)
157 if err != nil {
158 fmt.Println(err.Error())
159 return nil, err
160 }
161 }
162
163 return result, nil
164 }