-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_diff.go
487 lines (439 loc) · 14.2 KB
/
parse_diff.go
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package diff2html
import (
"regexp"
"strconv"
"strings"
)
const (
oldFileNameHeader = "--- "
newFileNameHeader = "+++ "
hunkHeaderPrefix = "@@"
inserts = "d2h-ins"
deletes = "d2h-del"
insertChanges = "d2h-ins d2h-change"
deleteChanges = "d2h-del d2h-change"
context = "d2h-cntx"
info = "d2h-info"
)
var (
oldMode = regexp.MustCompile(`^old mode (\d{6})`)
newMode = regexp.MustCompile(`^new mode (\d{6})`)
deletedFileMode = regexp.MustCompile(`^deleted file mode (\d{6})`)
newFileMode = regexp.MustCompile(`^new file mode (\d{6})`)
copyFrom = regexp.MustCompile(`^copy from "?(.+)"?`)
copyTo = regexp.MustCompile(`^copy to "?(.+)"?`)
renameFrom = regexp.MustCompile(`^rename from "?(.+)"?`)
renameTo = regexp.MustCompile(`^rename to "?(.+)"?`)
similarityIndex = regexp.MustCompile(`^similarity index (\d+)%`)
dissimilarityIndex = regexp.MustCompile(`^dissimilarity index (\d+)%`)
index = regexp.MustCompile(`^index ([0-9a-z]+)\.\.([0-9a-z]+)\s*(\d{6})?`)
binaryFiles = regexp.MustCompile(`^Binary files (.*) and (.*) differ`)
binaryDiff = regexp.MustCompile(`^GIT binary patch`)
combinedIndex = regexp.MustCompile(`^index ([0-9a-z]+),([0-9a-z]+)\.\.([0-9a-z]+)`)
combinedMode = regexp.MustCompile(`^mode (\d{6}),(\d{6})\.\.(\d{6})`)
combinedNewFile = regexp.MustCompile(`^new file mode (\d{6})`)
combinedDeletedFile = regexp.MustCompile(`^deleted file mode (\d{6}),(\d{6})`)
gitDiffStart = regexp.MustCompile(`^diff --git "?(.+)"? "?(.+)"?`)
filenameRegexp = regexp.MustCompile(`\s+\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? \+\d{4}.*$`)
crlf = regexp.MustCompile(`\r\n?`)
combined1 = regexp.MustCompile(`^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@.*`)
combined2 = regexp.MustCompile(`^@@@ -(\d+)(?:,\d+)? -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@@.*`)
)
type Config struct {
DstPrefix string
SrcPrefix string
}
func newDiff(conf Config) *Diff {
return &Diff{
conf: conf,
Files: []*File{},
}
}
type Diff struct {
conf Config
Files []*File
currentFile *File
currentBlock *Block
oldLine int
oldLine2 int
newLine int
possibleOldName string
possibleNewName string
}
type File struct {
IsGiftDiff bool `json:"isGiftDiff"`
IsCombined bool `json:"isCombined"`
IsDeleted bool `json:"isDeleted"`
IsNew bool `json:"isNew"`
IsCopy bool `json:"isCopy"`
IsRename bool `json:"isRename"`
IsBinary bool `json:"isBinary"`
OldName string `json:"oldName"`
NewName string `json:"newName"`
Language string `json:"language"`
UnchangedPercentage string `json:"unchangedPercentage"`
ChangedPercentage string `json:"changedPercentage"`
ChecksumBefore string `json:"checksumBefore"`
ChecksumAfter string `json:"checksumAfter"`
Mode string `json:"mode"`
NewFileMode string `json:"newFileMode"`
DeletedFileMode string `json:"deletedFileMode"`
OldMode string `json:"oldMode"`
NewMode string `json:"newMode"`
Blocks []*Block `json:"blocks"`
DeletedLines int `json:"deletedLines"`
AddedLines int `json:"addedLines"`
}
type Block struct {
Lines []*Line `json:"lines"`
OldStartLine int `json:"oldStartLine"`
OldStartLine2 int `json:"oldStartLine2"`
NewStartLine int `json:"newStartLine"`
Header string `json:"header"`
}
type Line struct {
Content string `json:"content"`
Type string `json:"type"`
OldNumber int `json:"oldNumber"`
NewNumber int `json:"newNumber"`
}
func (b *Block) addLine(l *Line) {
b.Lines = append(b.Lines, l)
}
func (d *Diff) startFile() {
d.saveBlock()
d.saveFile()
d.currentFile = &File{
DeletedLines: 0,
AddedLines: 0,
Blocks: []*Block{},
}
}
func (d *Diff) startBlock(line string) {
d.saveBlock()
if values := combined1.FindStringSubmatch(line); len(values) >= 3 {
d.currentFile.IsCombined = false
oldLine, err := strconv.Atoi(values[1])
if err != nil {
panic(err) // todo...
}
d.oldLine = oldLine
newLine, err := strconv.Atoi(values[2])
if err != nil {
panic(err) // todo...
}
d.newLine = newLine
} else if values = combined2.FindStringSubmatch(line); len(values) >= 4 {
d.currentFile.IsCombined = true
oldLine, err := strconv.Atoi(values[1])
if err != nil {
panic(err)
}
d.oldLine = oldLine
oldLine2, err := strconv.Atoi(values[2])
if err != nil {
panic(err)
}
d.oldLine2 = oldLine2
newLine, err := strconv.Atoi(values[3])
if err != nil {
panic(err)
}
d.newLine = newLine
} else {
//if strings.HasPrefix(line, hunkHeaderPrefix) {
//
//}
d.oldLine = 0
d.newLine = 0
d.currentFile.IsCombined = false
}
d.currentBlock = &Block{
Lines: []*Line{},
OldStartLine: d.oldLine,
OldStartLine2: d.oldLine2,
NewStartLine: d.newLine,
Header: line,
}
}
func (d *Diff) createLine(line string) {
currentLine := &Line{}
currentLine.Content = line
newLinePrefixes := []string{"+", " +"}
delLinePrefixes := []string{"-", " -"}
if !d.currentFile.IsCombined {
newLinePrefixes = []string{"+"}
delLinePrefixes = []string{"-"}
}
if startsWith(line, newLinePrefixes) {
d.currentFile.AddedLines++
currentLine.Type = inserts
currentLine.OldNumber = 0
currentLine.NewNumber = d.newLine
d.newLine++
d.currentBlock.addLine(currentLine)
} else if startsWith(line, delLinePrefixes) {
d.currentFile.DeletedLines++
currentLine.Type = deletes
currentLine.OldNumber = d.oldLine
d.oldLine++
currentLine.NewNumber = 0
d.currentBlock.addLine(currentLine)
} else {
currentLine.Type = context
currentLine.OldNumber = d.oldLine
d.oldLine++
currentLine.NewNumber = d.newLine
d.newLine++
d.currentBlock.addLine(currentLine)
}
}
func (d *Diff) saveBlock() {
if d.currentBlock != nil {
d.currentFile.Blocks = append(d.currentFile.Blocks, d.currentBlock)
d.currentBlock = nil
}
}
func (d *Diff) saveFile() {
if d.currentFile != nil {
if d.currentFile.OldName == "" {
d.currentFile.OldName = d.possibleOldName
}
if d.currentFile.NewName == "" {
d.currentFile.NewName = d.possibleNewName
}
if d.currentFile.NewName != "" {
d.Files = append(d.Files, d.currentFile)
d.currentFile = nil
}
}
d.possibleOldName = ""
d.possibleNewName = ""
}
func (d *Diff) Parser(input string) error {
input = strings.Replace(input, "\\ No newline at end of file", "", -1)
input = crlf.ReplaceAllString(input, "\n")
lines := strings.Split(input, "\n")
for idx, line := range lines {
if line == "" || strings.HasPrefix(line, "*") {
continue
}
prevLine := ""
if idx > 0 {
prevLine = lines[idx-1]
}
nxtLine := ""
if idx+1 < len(lines) {
nxtLine = lines[idx+1]
}
afterNxtLine := ""
if idx+2 < len(lines) {
afterNxtLine = lines[idx+2]
}
if strings.HasPrefix(line, "diff") {
d.startFile()
if values := gitDiffStart.FindStringSubmatch(line); len(values) >= 3 {
var err error
d.possibleOldName, err = getFilename("", values[1], d.conf.DstPrefix)
if err != nil {
return err
}
d.possibleNewName, err = getFilename("", values[2], d.conf.SrcPrefix)
if err != nil {
return err
}
}
d.currentFile.IsGiftDiff = true
continue
}
if d.currentFile == nil || (!d.currentFile.IsGiftDiff &&
(strings.HasPrefix(line, oldFileNameHeader) && strings.HasPrefix(nxtLine, newFileNameHeader) && strings.HasPrefix(afterNxtLine, hunkHeaderPrefix))) {
d.startFile()
}
if (strings.HasPrefix(line, oldFileNameHeader) && strings.HasPrefix(nxtLine, newFileNameHeader)) ||
(strings.HasPrefix(line, newFileNameHeader) && strings.HasPrefix(prevLine, oldFileNameHeader)) {
/*
* --- Date Timestamp[FractionalSeconds] TimeZone
* --- 2002-02-21 23:30:39.942229878 -0800
*/
srcFilename, err := getSrcFilename(line, d.conf)
if err != nil {
return err
}
if d.currentFile != nil && d.currentFile.OldName == "" && strings.HasPrefix(line, "--- ") && srcFilename != "" {
d.currentFile.OldName = srcFilename
d.currentFile.Language = getExtension(d.currentFile.OldName, d.currentFile.Language)
continue
}
/*
* +++ Date Timestamp[FractionalSeconds] TimeZone
* +++ 2002-02-21 23:30:39.942229878 -0800
*/
dstFilename, err := getDstFilename(line, d.conf)
if err != nil {
return err
}
if d.currentFile != nil && d.currentFile.NewName == "" && strings.HasPrefix(line, "+++ ") && dstFilename != "" {
d.currentFile.NewName = dstFilename
d.currentFile.Language = getExtension(d.currentFile.NewName, d.currentFile.Language)
continue
}
}
if (d.currentFile != nil && strings.HasPrefix(line, hunkHeaderPrefix)) ||
(d.currentFile != nil && d.currentFile.IsGiftDiff && d.currentFile.OldName != "" && d.currentFile.NewName != "" && d.currentBlock == nil) {
d.startBlock(line)
continue
}
/*
* There are three types of diff lines. These lines are defined by the way they start.
* 1. New line starts with: +
* 2. Old line starts with: -
* 3. Context line starts with: <SPACE>
*/
if d.currentBlock != nil &&
(strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") || strings.HasPrefix(line, " ")) {
d.createLine(line)
continue
}
doesNotExistHunkHeader := existHunkHeader(lines, line, idx)
if values := oldMode.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.OldMode = values[1]
} else if values = newMode.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.NewMode = values[1]
} else if values = deletedFileMode.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.DeletedFileMode = values[1]
d.currentFile.IsDeleted = true
} else if values = newFileMode.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.NewFileMode = values[1]
d.currentFile.IsNew = true
} else if values = copyFrom.FindStringSubmatch(line); len(values) >= 2 {
if doesNotExistHunkHeader {
d.currentFile.OldName = values[1]
}
d.currentFile.IsCopy = true
} else if values = copyTo.FindStringSubmatch(line); len(values) >= 2 {
if doesNotExistHunkHeader {
d.currentFile.NewName = values[1]
}
d.currentFile.IsCopy = true
} else if values = renameFrom.FindStringSubmatch(line); len(values) >= 2 {
if doesNotExistHunkHeader {
d.currentFile.OldName = values[1]
}
d.currentFile.IsRename = true
} else if values = renameTo.FindStringSubmatch(line); len(values) >= 2 {
if doesNotExistHunkHeader {
d.currentFile.NewName = values[1]
}
d.currentFile.IsRename = true
} else if values = binaryFiles.FindStringSubmatch(line); len(values) >= 3 {
d.currentFile.IsBinary = true
oldName, err := getFilename("", values[1], d.conf.SrcPrefix)
if err != nil {
return err
}
d.currentFile.OldName = oldName
newName, err := getFilename("", values[2], d.conf.DstPrefix)
if err != nil {
return err
}
d.currentFile.NewName = newName
d.startBlock("Binary file")
} else if values = binaryDiff.FindStringSubmatch(line); len(values) >= 1 {
d.currentFile.IsBinary = true
d.startBlock(line)
} else if values = similarityIndex.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.UnchangedPercentage = values[1]
} else if values = dissimilarityIndex.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.ChangedPercentage = values[1]
} else if values = index.FindStringSubmatch(line); len(values) >= 3 {
d.currentFile.ChecksumBefore = values[1]
d.currentFile.ChecksumAfter = values[2]
if len(values) >= 4 {
d.currentFile.Mode = values[3]
}
} else if values = combinedIndex.FindStringSubmatch(line); len(values) >= 4 {
d.currentFile.ChecksumBefore = strings.Join([]string{values[2], values[3]}, ",")
d.currentFile.ChecksumAfter = values[1]
} else if values = combinedMode.FindStringSubmatch(line); len(values) >= 4 {
d.currentFile.OldMode = strings.Join([]string{values[2], values[3]}, ",")
d.currentFile.NewMode = values[1]
} else if values = combinedNewFile.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.NewFileMode = values[1]
d.currentFile.IsNew = true
} else if values = combinedDeletedFile.FindStringSubmatch(line); len(values) >= 2 {
d.currentFile.DeletedFileMode = values[1]
d.currentFile.IsDeleted = true
}
}
d.saveBlock()
d.saveFile()
return nil
}
func getExtension(filename, language string) string {
names := strings.Split(filename, ".")
if len(names) > 1 {
return names[len(names)-1]
}
return language
}
func getSrcFilename(line string, conf Config) (string, error) {
return getFilename("---", line, conf.SrcPrefix)
}
func getDstFilename(line string, conf Config) (string, error) {
return getFilename("\\+\\+\\+", line, conf.DstPrefix)
}
func getFilename(linePrefix, line, extraPrefix string) (string, error) {
prefixes := []string{"a/", "b/", "i/", "w/", "c/", "o/"}
if extraPrefix != "" {
prefixes = append(prefixes, extraPrefix)
}
var reg *regexp.Regexp
var err error
if linePrefix != "" {
reg, err = regexp.Compile("^" + linePrefix + `?(.+?)"?$`)
} else {
reg, err = regexp.Compile(`^"?(.+?)"?$`)
}
if err != nil {
return "", err
}
var filename string
values := reg.FindStringSubmatch(line)
if len(values) >= 2 {
filename = values[1]
matchingPrefixes := []string{}
for _, p := range prefixes {
if strings.HasPrefix(filename, p) {
matchingPrefixes = append(matchingPrefixes, p)
}
}
if len(matchingPrefixes) >= 1 {
filename = filename[len(matchingPrefixes[0]):]
}
filename = filenameRegexp.ReplaceAllString(filename, "")
}
return filename, nil
}
func startsWith(str string, prefixes []string) bool {
for _, p := range prefixes {
return strings.HasPrefix(str, p)
}
return false
}
func existHunkHeader(lines []string, line string, lineIdx int) bool {
idx := lineIdx
l := len(lines)
for idx < l-3 {
if strings.HasPrefix(line, "diff") {
return false
}
if strings.HasPrefix(lines[idx], oldFileNameHeader) &&
strings.HasPrefix(lines[idx+1], newFileNameHeader) &&
strings.HasPrefix(lines[idx+2], hunkHeaderPrefix) {
return true
}
idx++
}
return false
}