-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
268 lines (246 loc) · 6.63 KB
/
diff.go
File metadata and controls
268 lines (246 loc) · 6.63 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
package main
import (
"cmp"
"context"
"fmt"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
)
var hunkHeaderRE = regexp.MustCompile(`^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@`)
type changeKind string
// changeKind mirrors git diff status letters. T is a type change.
const (
changeAdded changeKind = "A"
changeDeleted changeKind = "D"
changeModified changeKind = "M"
changeRenamed changeKind = "R"
changeType changeKind = "T"
)
type testFileChange struct {
Kind changeKind
OldPath string
NewPath string
}
func (change testFileChange) displayPath() string {
return cmp.Or(change.NewPath, change.OldPath)
}
func (change testFileChange) pathspecs() []string {
oldPath := cmp.Or(change.OldPath, change.NewPath)
newPath := cmp.Or(change.NewPath, change.OldPath)
if oldPath == "" {
return []string{newPath}
}
if newPath == "" || newPath == oldPath {
return []string{oldPath}
}
return []string{oldPath, newPath}
}
// lineRange uses End < Start to represent an empty span from a zero-count diff
// hunk. hasLines reports whether the span contains any real source lines.
type lineRange struct {
Start int
End int
}
type diffHunk struct {
Old lineRange
New lineRange
}
func newSideOnlyHunks(hunks []diffHunk) []diffHunk {
trimmed := make([]diffHunk, 0, len(hunks))
for _, hunk := range hunks {
hunk.Old = lineRange{}
trimmed = append(trimmed, hunk)
}
return trimmed
}
func listChangedTestFiles(ctx context.Context, cfg config, git gitRunner) ([]testFileChange, error) {
result, err := git(
ctx,
cfg.RepoRoot,
"diff",
"--name-status",
"-z",
"--find-renames",
"--diff-filter=ADMRT",
diffRangeSpec(cfg),
)
if err != nil {
return nil, err
}
if result.Stdout == "" {
return nil, nil
}
fields := strings.Split(result.Stdout, "\x00")
changes := make([]testFileChange, 0)
for index := 0; index < len(fields); {
status := fields[index]
index++
if status == "" {
continue
}
kind, err := parseChangeKind(status)
if err != nil {
return nil, err
}
switch kind {
case changeRenamed:
if index+1 >= len(fields) {
return nil, fmt.Errorf("rename status %q is missing paths", status)
}
oldPath := cleanGitPath(fields[index])
newPath := cleanGitPath(fields[index+1])
index += 2
change := testFileChange{Kind: kind, OldPath: oldPath, NewPath: newPath}
if !isRunnableTestFilePath(change.OldPath) && !isRunnableTestFilePath(change.NewPath) {
continue
}
changes = append(changes, change)
default:
if index >= len(fields) {
return nil, fmt.Errorf("status %q is missing a path", status)
}
path := cleanGitPath(fields[index])
index++
change := testFileChange{Kind: kind, OldPath: path, NewPath: path}
switch kind {
case changeAdded:
change.OldPath = ""
case changeDeleted:
change.NewPath = ""
}
if !isRunnableTestFilePath(change.displayPath()) {
continue
}
changes = append(changes, change)
}
}
slices.SortFunc(changes, func(left, right testFileChange) int {
return cmp.Compare(left.displayPath(), right.displayPath())
})
return changes, nil
}
func parseChangeKind(status string) (changeKind, error) {
switch {
case strings.HasPrefix(status, string(changeAdded)):
return changeAdded, nil
case strings.HasPrefix(status, string(changeDeleted)):
return changeDeleted, nil
case strings.HasPrefix(status, string(changeModified)):
return changeModified, nil
case strings.HasPrefix(status, string(changeRenamed)):
return changeRenamed, nil
case strings.HasPrefix(status, string(changeType)):
return changeType, nil
default:
return "", fmt.Errorf("unsupported diff status %q", status)
}
}
func cleanGitPath(path string) string {
return filepath.ToSlash(filepath.Clean(path))
}
func isRunnableTestFilePath(path string) bool {
if !strings.HasSuffix(path, "_test.go") {
return false
}
cleanPath := cleanGitPath(path)
baseName := filepath.Base(cleanPath)
if strings.HasPrefix(baseName, ".") || strings.HasPrefix(baseName, "_") {
return false
}
for segment := range strings.SplitSeq(filepath.ToSlash(filepath.Dir(cleanPath)), "/") {
if segment == "." || segment == "" {
continue
}
if segment == "testdata" || segment == "vendor" || strings.HasPrefix(segment, ".") || strings.HasPrefix(segment, "_") {
return false
}
}
return true
}
func listDiffHunks(ctx context.Context, cfg config, git gitRunner, change testFileChange) ([]diffHunk, error) {
args := []string{"diff", "--unified=0", "--no-color", "--find-renames", diffRangeSpec(cfg), "--"}
args = append(args, change.pathspecs()...)
result, err := git(ctx, cfg.RepoRoot, args...)
if err != nil {
return nil, err
}
return parseDiffHunks(result.Stdout)
}
func parseDiffHunks(diff string) ([]diffHunk, error) {
hunks := make([]diffHunk, 0)
for line := range strings.Lines(diff) {
line = strings.TrimSuffix(line, "\n")
matches := hunkHeaderRE.FindStringSubmatch(line)
if matches == nil {
continue
}
oldRange, err := parseRange(matches[1], matches[2])
if err != nil {
return nil, err
}
newRange, err := parseRange(matches[3], matches[4])
if err != nil {
return nil, err
}
hunks = append(hunks, diffHunk{Old: oldRange, New: newRange})
}
return hunks, nil
}
func parseRange(startText, countText string) (lineRange, error) {
start, err := parseNonNegativeInt(startText)
if err != nil {
return lineRange{}, err
}
count := 1
if countText != "" {
count, err = parseNonNegativeInt(countText)
if err != nil {
return lineRange{}, err
}
}
if count == 0 {
if start == 0 {
start = 1
}
return lineRange{Start: start, End: start - 1}, nil
}
return lineRange{Start: start, End: start + count - 1}, nil
}
func parseNonNegativeInt(value string) (int, error) {
parsed, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("parse integer %q: %w", value, err)
}
if parsed < 0 {
return 0, fmt.Errorf("negative value %q", value)
}
return parsed, nil
}
func fileExistsAtRevision(ctx context.Context, cfg config, git gitRunner, revision, filePath string) (bool, error) {
result, err := git(ctx, cfg.RepoRoot, "ls-tree", "-z", "--name-only", revision, "--", filePath)
if err != nil {
return false, fmt.Errorf("check whether %s exists at %s: %w", filePath, revision, err)
}
cleanPath := cleanGitPath(filePath)
for part := range strings.SplitSeq(result.Stdout, "\x00") {
if part == "" {
continue
}
if cleanGitPath(part) == cleanPath {
return true, nil
}
}
return false, nil
}
func (r lineRange) hasLines() bool {
return r.Start > 0 && r.End >= r.Start
}
func (r lineRange) overlaps(other lineRange) bool {
if !r.hasLines() || !other.hasLines() {
return false
}
return r.Start <= other.End && other.Start <= r.End
}