-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_mutation.go
More file actions
182 lines (167 loc) · 3.75 KB
/
Copy pathnode_mutation.go
File metadata and controls
182 lines (167 loc) · 3.75 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package contexting
import (
"os"
"path/filepath"
"strings"
)
func removeNodeByRelPath(root *Node, relPath string) bool {
if root == nil {
return false
}
parts := splitRelPath(relPath)
if len(parts) == 0 {
return false
}
parent := root
for i := 0; i < len(parts)-1; i++ {
next, ok := parent.Children[parts[i]]
if !ok {
return false
}
parent = next
}
name := parts[len(parts)-1]
if _, ok := parent.Children[name]; !ok {
return false
}
delete(parent.Children, name)
return true
}
func upsertNodeByRelPath(root *Node, absRoot string, relPath string, isDir bool, mtime int64, cache SynonymResponse, maxSynonyms int) bool {
if root == nil {
return false
}
parts := splitRelPath(relPath)
if len(parts) == 0 {
return false
}
changed := false
parent := root
currPath := absRoot
for i := 0; i < len(parts)-1; i++ {
part := parts[i]
currPath = filepath.Join(currPath, part)
next, ok := parent.Children[part]
if !ok {
next = &Node{
FullPath: currPath,
Type: "directory",
Children: make(map[string]*Node),
}
parent.Children[part] = next
next.Synonyms = buildNodeSynonyms(part, cache, maxSynonyms)
changed = true
}
if next.Type != "directory" {
next.Type = "directory"
changed = true
}
if next.Children == nil {
next.Children = make(map[string]*Node)
changed = true
}
if mtime > 0 && next.ModTime != mtime {
next.ModTime = mtime
}
parent = next
}
name := parts[len(parts)-1]
fullPath := filepath.Join(absRoot, filepath.FromSlash(strings.Join(parts, "/")))
nodeType := "file"
if isDir {
nodeType = "directory"
}
nodeSynonyms := buildNodeSynonyms(name, cache, maxSynonyms)
node, ok := parent.Children[name]
if !ok {
var symList []string
if !isDir {
symList, _ = extractSymbols(fullPath)
}
parent.Children[name] = &Node{
FullPath: fullPath,
Type: nodeType,
Synonyms: nodeSynonyms,
Symbols: symList,
ModTime: mtime,
Children: make(map[string]*Node),
}
return true
}
if node.FullPath != fullPath {
node.FullPath = fullPath
changed = true
}
if node.Type != nodeType {
node.Type = nodeType
changed = true
if nodeType == "directory" {
node.Symbols = nil
}
}
if node.Children == nil {
node.Children = make(map[string]*Node)
changed = true
}
if node.ModTime != mtime {
node.ModTime = mtime
changed = true
}
if !stringSlicesEqual(node.Synonyms, nodeSynonyms) {
node.Synonyms = nodeSynonyms
changed = true
}
if node.Type == "file" {
symList, _ := extractSymbols(fullPath)
if !stringSlicesEqual(node.Symbols, symList) {
node.Symbols = symList
changed = true
}
}
return changed
}
func buildNodeSynonyms(name string, cache SynonymResponse, maxSynonyms int) []string {
combined := make([]string, 0, maxSynonyms+4)
if cache != nil {
if syns, ok := cache[name]; ok {
combined = append(combined, syns...)
}
}
combined = append(combined, lexicalSynonyms(name)...)
return sanitizeSynonyms(combined, maxSynonyms)
}
func splitRelPath(relPath string) []string {
normalized := strings.TrimSpace(filepath.ToSlash(relPath))
normalized = strings.Trim(normalized, "/")
if normalized == "" || normalized == "." {
return nil
}
parts := strings.Split(normalized, "/")
out := make([]string, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" || part == "." {
continue
}
out = append(out, part)
}
return out
}
func isExistingDirectory(path string) (isDir bool, mtime int64, err error) {
info, err := os.Stat(path)
if err != nil {
return false, 0, err
}
return info.IsDir(), info.ModTime().UnixNano(), nil
}
func stringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}