-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
136 lines (126 loc) · 3.94 KB
/
git.go
File metadata and controls
136 lines (126 loc) · 3.94 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
package repomap
import (
"bytes"
"context"
"os/exec"
"path/filepath"
"strings"
)
// gitHeadSHA returns the full SHA of HEAD. Returns an error (not empty string)
// so callers can distinguish "git call failed" from "clean repo with no commits".
func gitHeadSHA(ctx context.Context, root string) (string, error) {
cmd := exec.CommandContext(ctx, "git", "-C", root, "rev-parse", "HEAD")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", err
}
return strings.TrimSpace(out.String()), nil
}
// gitChangedFiles returns added, modified, and deleted paths between sinceSHA
// and HEAD, plus untracked files. Paths are relative to root, matching the
// FileInfo.Path convention from scanner.go.
//
// Untracked files (git ls-files --others --exclude-standard) are treated as
// "added" — they respect .gitignore and represent files new since the cache
// was written. This catches the common edit-without-commit workflow.
//
// Renames are reported as delete(old) + add(new) via --diff-filter semantics.
// `git diff --name-status -M` would give R entries; we use the simpler
// status-letter form and let callers re-parse the new path.
func gitChangedFiles(ctx context.Context, root, sinceSHA string) (added, modified, deleted []string, err error) {
// Committed changes: A (added), M (modified), D (deleted), R (renamed -> treat as D old + A new).
cmd := exec.CommandContext(ctx, "git", "-C", root, "diff", "--name-status", "-z", sinceSHA, "HEAD")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, nil, nil, err
}
parseDiffNameStatus(out.String(), &added, &modified, &deleted)
// Worktree changes not yet committed: unstaged + staged vs HEAD.
cmd = exec.CommandContext(ctx, "git", "-C", root, "diff", "--name-status", "-z", "HEAD")
out.Reset()
cmd.Stdout = &out
if err := cmd.Run(); err == nil { // best-effort
parseDiffNameStatus(out.String(), &added, &modified, &deleted)
}
// Untracked (not ignored) — counted as added.
cmd = exec.CommandContext(ctx, "git", "-C", root, "ls-files", "--others", "--exclude-standard", "-z")
out.Reset()
cmd.Stdout = &out
if err := cmd.Run(); err == nil {
for _, p := range splitNUL(out.String()) {
if p != "" {
added = append(added, p)
}
}
}
added = dedupePaths(added)
modified = dedupePaths(modified)
deleted = dedupePaths(deleted)
return added, modified, deleted, nil
}
// parseDiffNameStatus parses the NUL-delimited output of
// `git diff --name-status -z`. Each record is STATUS\0PATH (optionally with a
// second PATH for renames R/C). Status letters: A, M, D, T (type change,
// treated as modify), R<score>, C<score>.
func parseDiffNameStatus(raw string, added, modified, deleted *[]string) {
tokens := splitNUL(raw)
for i := 0; i < len(tokens); i++ {
status := tokens[i]
if status == "" {
continue
}
switch status[0] {
case 'A':
if i+1 < len(tokens) {
*added = append(*added, tokens[i+1])
i++
}
case 'M', 'T':
if i+1 < len(tokens) {
*modified = append(*modified, tokens[i+1])
i++
}
case 'D':
if i+1 < len(tokens) {
*deleted = append(*deleted, tokens[i+1])
i++
}
case 'R', 'C':
// Format: R<score>\0<old>\0<new>. Treat as delete(old) + add(new).
if i+2 < len(tokens) {
*deleted = append(*deleted, tokens[i+1])
*added = append(*added, tokens[i+2])
i += 2
}
}
}
}
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)
}