-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdiff.go
More file actions
303 lines (251 loc) · 6.25 KB
/
diff.go
File metadata and controls
303 lines (251 loc) · 6.25 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
package github
import (
"bufio"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/google/go-github/github"
"gopkg.in/src-d/go-errors.v1"
)
var (
// ErrLineOutOfDiff is returned when the file line number is not
// in the patch diff
ErrLineOutOfDiff = errors.NewKind("line number is not in diff")
// ErrLineNotAddition is returned when the file line number is not
// a + change in the patch diff
ErrLineNotAddition = errors.NewKind("line number is not an added change")
// ErrFileNotFound is returned when the file name is not part of the diff
ErrFileNotFound = errors.NewKind("file not found")
// ErrBadPatch is returned when there was a problem parsing the diff
ErrBadPatch = errors.NewKind("diff patch could not be parsed")
)
type diffLines struct {
cc *github.CommitsComparison
parsed map[string]*parsedFile
}
type lineType int
const (
lineAdded lineType = iota
lineDeleted
lineContext
)
type linesChunk struct {
Type lineType
Lines int
}
type hunk struct {
OldStartLine, OldLines int
NewStartLine, NewLines int
Chunks []linesChunk
}
type posRange struct {
AbsStart, AbsEnd int
RelStart, RelEnd int
}
type parsedFile struct {
ranges []*posRange
linesAdded map[int]bool
}
func newDiffLines(cc *github.CommitsComparison) *diffLines {
return &diffLines{
cc: cc,
parsed: make(map[string]*parsedFile, len(cc.Files)),
}
}
// ConvertLine takes a line number on the original file, and returns the
// corresponding line number in the patch diff. It will return ErrLineOutOfDiff
// if the line falls outside of the diff (changed lines plus context).
// With strict set to true, ErrLineNotAddition will be returned for lines
// that are not an addition (+ lines in the diff).
func (d *diffLines) ConvertLine(file string, line int, strict bool) (int, error) {
parsedFile, err := d.parseFile(file)
if err != nil {
return 0, err
}
diffLine, err := d.convertLine(parsedFile.ranges, line)
if err != nil {
return 0, err
}
if strict {
if !parsedFile.linesAdded[diffLine] {
return 0, ErrLineNotAddition.New()
}
}
return diffLine, nil
}
func (d *diffLines) convertLine(ranges []*posRange, line int) (int, error) {
for _, r := range ranges {
if line >= r.AbsStart && line < r.AbsEnd {
return line - r.AbsStart + r.RelStart, nil
}
}
return 0, ErrLineOutOfDiff.New()
}
func (d *diffLines) parseFile(file string) (*parsedFile, error) {
if parsedFile, ok := d.parsed[file]; ok {
return parsedFile, nil
}
hunks, linesAdded, err := d.hunks(file)
if err != nil {
return nil, err
}
ranges := convertRanges(hunks)
d.parsed[file] = &parsedFile{ranges: ranges, linesAdded: linesAdded}
return d.parsed[file], nil
}
func (d *diffLines) filePatch(file string) (string, error) {
var ff *github.CommitFile
for _, f := range d.cc.Files {
if file == *f.Filename {
ff = &f
break
}
}
if ff == nil {
return "", ErrFileNotFound.New()
}
if ff.Patch == nil {
return "", ErrLineOutOfDiff.New()
}
return *ff.Patch, nil
}
func (d *diffLines) hunks(file string) ([]*hunk, map[int]bool, error) {
patch, err := d.filePatch(file)
if err != nil {
return nil, nil, err
}
hunks, linesAdded, err := parseHunks(patch)
if err != nil {
return nil, nil, ErrBadPatch.Wrap(err)
}
return hunks, linesAdded, nil
}
var hunkPattern = regexp.MustCompile(`^(@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@[^@]*)(?:@@.*|$)`)
func parseHunks(s string) ([]*hunk, map[int]bool, error) {
r := strings.NewReader(s)
scanner := bufio.NewScanner(r)
var hs []*hunk
var h *hunk
var err error
var lChunk linesChunk
linesAdded := make(map[int]bool)
for i := 0; scanner.Scan(); i++ {
var lt lineType
line := scanner.Text()
switch true {
case strings.HasPrefix(line, "@@"):
if lChunk.Lines > 0 {
h.Chunks = append(h.Chunks, lChunk)
}
lChunk = linesChunk{}
h, err = parseHunkHeader(line)
if err != nil {
return nil, nil, err
}
hs = append(hs, h)
continue
case strings.HasPrefix(line, "+"):
lt = lineAdded
linesAdded[i] = true
case strings.HasPrefix(line, "-"):
lt = lineDeleted
default:
lt = lineContext
}
if lChunk.Lines != 0 && lChunk.Type != lt {
h.Chunks = append(h.Chunks, lChunk)
lChunk = linesChunk{}
}
lChunk.Type = lt
lChunk.Lines++
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
if lChunk.Lines > 0 {
h.Chunks = append(h.Chunks, lChunk)
}
return hs, linesAdded, nil
}
func parseHunkHeader(line string) (*hunk, error) {
var (
err error
h = &hunk{}
)
matches := hunkPattern.FindStringSubmatch(line)
if len(matches) == 0 {
return nil, fmt.Errorf("bad hunk line format: %s", line)
}
h.OldStartLine, err = strconv.Atoi(matches[2])
if err != nil {
return nil, fmt.Errorf("bad hunk line format: %s", line)
}
if matches[3] == "" {
h.OldLines = 1
} else {
h.OldLines, err = strconv.Atoi(matches[3])
if err != nil {
return nil, fmt.Errorf("bad hunk line format: %s", line)
}
}
h.NewStartLine, err = strconv.Atoi(matches[4])
if err != nil {
return nil, fmt.Errorf("bad hunk line format: %s", line)
}
if matches[5] == "" {
h.NewLines = 1
} else {
h.NewLines, err = strconv.Atoi(matches[5])
if err != nil {
return nil, fmt.Errorf("bad hunk line format: %s", line)
}
}
return h, nil
}
func convertRanges(hunks []*hunk) []*posRange {
if len(hunks) == 0 {
return nil
}
ranges := make([]*posRange, 0)
// relative position of the last range end
lastRelEnd := 0
for _, hunk := range hunks {
absStart := hunk.NewStartLine
// number of lines in diff to skip
// each hunk has a header line which should be skipped
// delete lines should be also skipped
skipLines := 1
// number of lines for the range
lines := 0
newRange := func() {
r := &posRange{
AbsStart: absStart,
AbsEnd: absStart + lines,
RelStart: lastRelEnd + skipLines,
RelEnd: lastRelEnd + lines + skipLines,
}
ranges = append(ranges, r)
absStart = r.AbsEnd
lastRelEnd = r.RelEnd
}
for _, chunk := range hunk.Chunks {
if chunk.Type != lineDeleted {
lines += chunk.Lines
} else {
// create a range for the lines before first deleted line
if lines > 0 {
newRange()
lines = 0
}
skipLines = chunk.Lines
continue
}
}
if lines > 0 {
newRange()
skipLines = 0
}
}
return ranges
}