-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatemap.go
151 lines (134 loc) · 3.02 KB
/
statemap.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package xignore
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/spf13/afero"
)
type stateMap map[string]bool
func collectFiles(fs afero.Fs) (files []string, err error) {
files = []string{}
afero.Walk(fs, "", func(path string, info os.FileInfo, werr error) error {
if werr != nil {
err = werr
return nil
}
files = append(files, path)
return nil
})
return
}
func (state stateMap) merge(source stateMap) {
for k, val := range source {
state[k] = val
}
}
func (state stateMap) mergeFiles(files []string, value bool) {
for _, f := range files {
state[f] = value
}
}
func (state stateMap) applyPatterns(vfs afero.Fs, files []string, patterns []*Pattern) error {
filesMap := stateMap{}
dirPatterns := []*Pattern{}
for _, pattern := range patterns {
if pattern.IsEmpty() {
continue
}
currFiles := pattern.Matches(files)
if pattern.IsExclusion() {
for _, f := range currFiles {
filesMap[f] = false
}
} else {
for _, f := range currFiles {
filesMap[f] = true
}
}
// generate dir based patterns
for _, f := range currFiles {
ok, err := afero.IsDir(vfs, f)
if err != nil {
return err
}
if ok {
strPattern := f + "/**"
if pattern.IsExclusion() {
strPattern = "!" + strPattern
}
dirPattern := NewPattern(strPattern)
dirPatterns = append(dirPatterns, dirPattern)
err := dirPattern.Prepare()
if err != nil {
return err
}
}
}
}
// handle dirs batch matches
dirFileMap := stateMap{}
for _, pattern := range dirPatterns {
if pattern.IsEmpty() {
continue
}
currFiles := pattern.Matches(files)
if pattern.IsExclusion() {
for _, f := range currFiles {
dirFileMap[f] = false
}
} else {
for _, f := range currFiles {
dirFileMap[f] = true
}
}
}
state.merge(dirFileMap)
state.merge(filesMap)
return nil
}
func (state stateMap) applyIgnorefile(vfs afero.Fs, ignorefile string, nested bool) error {
// Apply nested ignorefile
ignorefiles := []string{}
if nested {
for file := range state {
// all subdir ignorefiles
if strings.HasSuffix(file, ignorefile) {
ignorefiles = append(ignorefiles, file)
}
}
// Sort by dir deep level
sort.Slice(ignorefiles, func(i, j int) bool {
ilen := len(strings.Split(ignorefiles[i], string(os.PathSeparator)))
jlen := len(strings.Split(ignorefiles[j], string(os.PathSeparator)))
return ilen < jlen
})
} else {
ignorefiles = []string{ignorefile}
}
for _, ifile := range ignorefiles {
currBasedir := filepath.Dir(ifile)
currFs := vfs
if currBasedir != "." {
currFs = afero.NewBasePathFs(vfs, currBasedir)
}
patterns, err := loadPatterns(currFs, ignorefile)
if err != nil {
return err
}
currMap := stateMap{}
currFiles, err := collectFiles(currFs)
if err != nil {
return err
}
err = currMap.applyPatterns(currFs, currFiles, patterns)
if err != nil {
return err
}
for nfile, matched := range currMap {
parentFile := filepath.Join(currBasedir, nfile)
state[parentFile] = matched
}
}
return nil
}