forked from codeskyblue/fswatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
121 lines (110 loc) · 2.35 KB
/
filter.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"github.com/howeyc/fsnotify"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var (
ignorePattens []*regexp.Regexp
ignoreLoaded = false
)
func loadGitignore(filename string) []*regexp.Regexp {
ignores := make([]*regexp.Regexp, 0, 20)
data, err := ioutil.ReadFile(filename)
if err != nil {
K.Errorf("file '%s' open failed", filename)
return nil
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
if !strings.HasPrefix(line, "#") {
line = strings.Replace(line, ".", "[.]", -1)
line = strings.Replace(line, "*", ".*", -1)
line = "^" + line + "$"
r, err := regexp.Compile(line)
if err != nil {
K.Error(err)
continue
}
K.Debug("init: ", line)
ignores = append(ignores, r)
}
}
return ignores
}
func isLangSource(path string) bool {
basename := filepath.Base(path)
if !strings.HasPrefix(basename, ".") {
ext := filepath.Ext(basename)
for _, okext := range LangExts {
if ext == okext {
return true
}
}
}
return false
}
func isIgnore(s string) bool {
if !ignoreLoaded {
ignorePattens = loadGitignore(".gitignore")
ignoreLoaded = true
}
ok := false
for _, patten := range ignorePattens {
if patten.MatchString(s) {
K.Debugf("patten %s match %s",
strconv.Quote(patten.String()),
strconv.Quote(s))
ok = true
break
}
}
return ok
}
func filter(watch chan *fsnotify.FileEvent) chan *fsnotify.FileEvent {
modifyTime := make(map[string]int64)
filterd := make(chan *fsnotify.FileEvent)
go func() {
for {
ev := <-watch
if isIgnore(filepath.Base(ev.Name)) {
K.Debugf("IGNORE: %s", ev.Name)
continue
}
// only lang src can pass
if !isLangSource(ev.Name) {
K.Debugf("not source file: %s", ev.Name)
continue
}
// delete or rename has no modify time
if ev.IsDelete() || ev.IsRename() {
filterd <- ev
continue
}
fi, err := getFileInfo(ev.Name)
if err != nil {
//K.Warnf("get file mod time failed: %s", err)
continue
}
if fi.IsDir() { // ignore directory changes
K.Debugf("Dir ignore: %s", ev.Name)
continue
}
mt := fi.ModTime().Unix()
if mt == modifyTime[ev.Name] {
K.Debugf("SKIP: %s", ev.Name)
continue
}
filterd <- ev
modifyTime[ev.Name] = mt
}
}()
return filterd
}