-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
415 lines (393 loc) · 12.2 KB
/
Copy pathgit.go
File metadata and controls
415 lines (393 loc) · 12.2 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package repomap
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
// cacheStatusSnapshot is one coherent Git view used to decide whether an
// on-disk cache describes the current worktree. Its digest deliberately
// ignores index state: staging identical bytes must not invalidate a cache.
type cacheStatusSnapshot struct {
headSHA string
changes []cacheWorktreeChange
digest string
}
type cacheWorktreeChange struct {
path string
oldPath string
untracked bool
renamed bool
}
// gitStatusSnapshot obtains HEAD and worktree changes in one Git invocation.
// A missing HEAD, conflict, malformed porcelain, or unreadable relevant file
// returns an error so callers fall back to a full build.
func (m *Map) gitStatusSnapshot(ctx context.Context) (cacheStatusSnapshot, error) {
cmd := exec.CommandContext(ctx, "git", "-C", m.root, "status", "--porcelain=v2", "--branch", "-z", "--untracked-files=all", "--renames")
out, err := cmd.Output()
if err != nil {
return cacheStatusSnapshot{}, err
}
snapshot, err := parseCacheStatusSnapshot(string(out))
if err != nil {
return cacheStatusSnapshot{}, err
}
snapshot.changes, err = normalizeCacheStatusPaths(m.root, snapshot.changes)
if err != nil {
return cacheStatusSnapshot{}, err
}
digest, err := m.cacheWorktreeDigest(snapshot.changes)
if err != nil {
return cacheStatusSnapshot{}, err
}
snapshot.digest = digest
return snapshot, nil
}
// parseCacheStatusSnapshot accepts only the porcelain-v2 record types needed
// by the cache. Unlike the commit-analysis parser, it rejects uncertain state
// because cache reuse must be conservative.
func parseCacheStatusSnapshot(raw string) (cacheStatusSnapshot, error) {
var snapshot cacheStatusSnapshot
records := splitNUL(raw)
for i := 0; i < len(records); i++ {
record := records[i]
if record == "" {
return cacheStatusSnapshot{}, fmt.Errorf("empty status record")
}
switch record[0] {
case '#':
if strings.HasPrefix(record, "# branch.oid ") {
oid := strings.TrimPrefix(record, "# branch.oid ")
if !isGitObjectID(oid) {
return cacheStatusSnapshot{}, fmt.Errorf("invalid branch oid %q", oid)
}
snapshot.headSHA = oid
}
case '1':
fields := strings.SplitN(record, " ", 9)
if len(fields) != 9 || fields[0] != "1" || !validPorcelainXY(fields[1]) || !validGitPath(fields[8]) {
return cacheStatusSnapshot{}, fmt.Errorf("malformed ordinary status record %q", record)
}
snapshot.changes = append(snapshot.changes, cacheWorktreeChange{path: fields[8]})
case '2':
fields := strings.SplitN(record, " ", 10)
if len(fields) != 10 || fields[0] != "2" || !validPorcelainXY(fields[1]) || !validGitPath(fields[9]) || i+1 >= len(records) || !validGitPath(records[i+1]) {
return cacheStatusSnapshot{}, fmt.Errorf("malformed rename status record %q", record)
}
snapshot.changes = append(snapshot.changes, cacheWorktreeChange{
path: fields[9], oldPath: records[i+1], renamed: true,
})
i++
case '?':
if !strings.HasPrefix(record, "? ") || !validGitPath(record[2:]) {
return cacheStatusSnapshot{}, fmt.Errorf("malformed untracked status record %q", record)
}
snapshot.changes = append(snapshot.changes, cacheWorktreeChange{path: record[2:], untracked: true})
case 'u':
return cacheStatusSnapshot{}, fmt.Errorf("unmerged status record %q", record)
case '!':
if !strings.HasPrefix(record, "! ") || !validGitPath(record[2:]) {
return cacheStatusSnapshot{}, fmt.Errorf("malformed ignored status record %q", record)
}
default:
return cacheStatusSnapshot{}, fmt.Errorf("unknown status record %q", record)
}
}
if snapshot.headSHA == "" {
return cacheStatusSnapshot{}, fmt.Errorf("status missing branch oid")
}
return snapshot, nil
}
func isGitObjectID(value string) bool {
if len(value) < 7 || value == "(initial)" {
return false
}
for _, r := range value {
if (r < '0' || r > '9') && (r < 'a' || r > 'f') && (r < 'A' || r > 'F') {
return false
}
}
return true
}
func validPorcelainXY(value string) bool {
return len(value) == 2 && value[0] != ' ' && value[1] != ' '
}
func validGitPath(path string) bool {
if path == "" || filepath.IsAbs(path) {
return false
}
clean := filepath.Clean(path)
return clean != "." && clean != ".." && !strings.HasPrefix(clean, ".."+string(filepath.Separator))
}
// normalizeCacheStatusPaths converts porcelain-v2's repository-root-relative
// paths into Map-root-relative paths. Changes outside a nested Map root do not
// affect its cache; cross-boundary renames retain whichever side is inside.
func normalizeCacheStatusPaths(mapRoot string, changes []cacheWorktreeChange) ([]cacheWorktreeChange, error) {
gitRoot, err := containingGitWorktreeRoot(mapRoot)
if err != nil {
return nil, err
}
out := make([]cacheWorktreeChange, 0, len(changes))
for _, change := range changes {
path, pathInside, pathErr := gitPathRelativeToMap(gitRoot, mapRoot, change.path)
if pathErr != nil {
return nil, pathErr
}
oldPath, oldInside, oldErr := gitPathRelativeToMap(gitRoot, mapRoot, change.oldPath)
if oldErr != nil {
return nil, oldErr
}
if !pathInside && !oldInside {
continue
}
change.path = path
change.oldPath = oldPath
out = append(out, change)
}
return out, nil
}
func containingGitWorktreeRoot(root string) (string, error) {
dir, err := filepath.Abs(root)
if err != nil {
return "", fmt.Errorf("resolve map root: %w", err)
}
for {
if _, statErr := os.Lstat(filepath.Join(dir, ".git")); statErr == nil {
return dir, nil
}
parent := filepath.Dir(dir)
if parent == dir {
return "", fmt.Errorf("map root %q is not inside a git worktree", root)
}
dir = parent
}
}
func gitPathRelativeToMap(gitRoot, mapRoot, path string) (string, bool, error) {
if path == "" {
return "", false, nil
}
absMapRoot, err := filepath.Abs(mapRoot)
if err != nil {
return "", false, fmt.Errorf("resolve map root: %w", err)
}
rel, err := filepath.Rel(absMapRoot, filepath.Join(gitRoot, filepath.FromSlash(path)))
if err != nil {
return "", false, fmt.Errorf("normalize git path %q: %w", path, err)
}
if rel == "." || rel == ".." || filepath.IsAbs(rel) || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", false, nil
}
return filepath.ToSlash(rel), true, nil
}
func (m *Map) cacheWorktreeDigest(changes []cacheWorktreeChange) (string, error) {
paths := make(map[string]struct{}, len(changes)*2)
for _, change := range changes {
if m.cacheRelevantPath(change.path) {
paths[change.path] = struct{}{}
}
if change.oldPath != "" && m.cacheRelevantPath(change.oldPath) {
paths[change.oldPath] = struct{}{}
}
}
sorted := make([]string, 0, len(paths))
for path := range paths {
sorted = append(sorted, path)
}
sort.Strings(sorted)
h := sha256.New()
_, _ = h.Write([]byte("repomap-cache-worktree-v1\x00"))
for _, path := range sorted {
contents, err := os.ReadFile(filepath.Join(m.root, path))
if err != nil && !os.IsNotExist(err) {
return "", fmt.Errorf("read changed path %q: %w", path, err)
}
if os.IsNotExist(err) {
_, _ = fmt.Fprintf(h, "%d:%s:-1:", len(path), path)
continue
}
_, _ = fmt.Fprintf(h, "%d:%s:%d:", len(path), path, len(contents))
_, _ = h.Write(contents)
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func cacheCleanWorktreeDigest() string {
h := sha256.New()
_, _ = h.Write([]byte("repomap-cache-worktree-v1\x00"))
return hex.EncodeToString(h.Sum(nil))
}
func (m *Map) cacheRelevantPath(path string) bool {
if path == configFileName {
return true
}
if inSkipDir(path) || isBuildArtifact(path) {
return false
}
base := filepath.Base(path)
if base == "go.mod" || base == "go.sum" || base == "go.work" {
return true
}
if m.blocklist != nil && (m.blocklist.ShouldExcludePath(path) || !m.blocklist.ShouldIncludePath(path)) {
return false
}
if LanguageFor(filepath.Ext(path)) != "" {
return true
}
return false
}
// snapshotChangedFiles translates current worktree state into the categories
// consumed by prepareIncremental. A rename is deliberately old-delete plus
// new-add so cached symbols at the old path cannot survive.
func (m *Map) snapshotChangedFiles(snapshot cacheStatusSnapshot) (added, modified, deleted []string, err error) {
for _, change := range snapshot.changes {
if change.renamed {
if m.cacheRelevantPath(change.oldPath) {
deleted = append(deleted, change.oldPath)
}
if m.cacheRelevantPath(change.path) {
added = append(added, change.path)
}
continue
}
if !m.cacheRelevantPath(change.path) {
continue
}
if change.untracked {
added = append(added, change.path)
continue
}
if _, statErr := os.Lstat(filepath.Join(m.root, change.path)); statErr != nil {
if os.IsNotExist(statErr) {
deleted = append(deleted, change.path)
continue
}
return nil, nil, nil, fmt.Errorf("stat changed path %q: %w", change.path, statErr)
}
modified = append(modified, change.path)
}
return dedupePaths(added), dedupePaths(modified), dedupePaths(deleted), nil
}
// gitCommittedChangedFiles issues the one committed-diff command permitted by
// the HEAD-moved incremental path. Worktree changes are supplied separately by
// the already-captured status snapshot.
func gitCommittedChangedFiles(ctx context.Context, root, sinceSHA string) (added, modified, deleted []string, err error) {
cmd := exec.CommandContext(ctx, "git", "-C", root, "diff", "--name-status", "-z", "-M", sinceSHA, "HEAD")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, nil, nil, err
}
added, modified, deleted, err = parseDiffNameStatusStrict(out.String())
if err != nil {
return nil, nil, nil, err
}
gitRoot, err := containingGitWorktreeRoot(root)
if err != nil {
return nil, nil, nil, err
}
normalize := func(paths []string) ([]string, error) {
normalized := make([]string, 0, len(paths))
for _, path := range paths {
rel, inside, pathErr := gitPathRelativeToMap(gitRoot, root, path)
if pathErr != nil {
return nil, pathErr
}
if inside {
normalized = append(normalized, rel)
}
}
return normalized, nil
}
if added, err = normalize(added); err != nil {
return nil, nil, nil, err
}
if modified, err = normalize(modified); err != nil {
return nil, nil, nil, err
}
if deleted, err = normalize(deleted); err != nil {
return nil, nil, nil, err
}
return added, modified, deleted, nil
}
func parseDiffNameStatusStrict(raw string) (added, modified, deleted []string, err error) {
tokens := splitNUL(raw)
for i := 0; i < len(tokens); i++ {
status := tokens[i]
if status == "" {
return nil, nil, nil, fmt.Errorf("empty diff status")
}
needPath := func() (string, error) {
if i+1 >= len(tokens) || !validGitPath(tokens[i+1]) {
return "", fmt.Errorf("malformed diff record %q", status)
}
i++
return tokens[i], nil
}
switch status[0] {
case 'A':
path, pathErr := needPath()
if pathErr != nil {
return nil, nil, nil, pathErr
}
added = append(added, path)
case 'M', 'T':
path, pathErr := needPath()
if pathErr != nil {
return nil, nil, nil, pathErr
}
modified = append(modified, path)
case 'D':
path, pathErr := needPath()
if pathErr != nil {
return nil, nil, nil, pathErr
}
deleted = append(deleted, path)
case 'R', 'C':
oldPath, pathErr := needPath()
if pathErr != nil {
return nil, nil, nil, pathErr
}
newPath, pathErr := needPath()
if pathErr != nil {
return nil, nil, nil, pathErr
}
deleted = append(deleted, oldPath)
added = append(added, newPath)
default:
return nil, nil, nil, fmt.Errorf("unknown diff status %q", status)
}
}
return dedupePaths(added), dedupePaths(modified), dedupePaths(deleted), nil
}
func splitNUL(s string) []string {
s = strings.TrimRight(s, "\x00")
if s == "" {
return nil
}
return strings.Split(s, "\x00")
}
func dedupePaths(in []string) []string {
if len(in) < 2 {
return in
}
seen := make(map[string]struct{}, len(in))
out := in[:0]
for _, p := range in {
if _, ok := seen[p]; ok {
continue
}
seen[p] = struct{}{}
out = append(out, p)
}
return out
}
// joinAbs converts a repo-relative path to an absolute path rooted at root.
// Thin wrapper so incremental.go stays focused on orchestration.
func joinAbs(root, rel string) string {
return filepath.Join(root, rel)
}