@@ -39,48 +39,23 @@ const (
39
39
40
40
// DiffLine represents a line in diff.
41
41
type DiffLine struct {
42
- typ DiffLineType
43
- content string
44
- leftLine int
45
- rightLine int
46
- }
47
-
48
- // Type returns the type of the line.
49
- func (l * DiffLine ) Type () DiffLineType {
50
- return l .typ
51
- }
52
-
53
- // Content returns the content of the line.
54
- func (l * DiffLine ) Content () string {
55
- return l .content
56
- }
57
-
58
- // Left returns the left line number.
59
- func (l * DiffLine ) Left () int {
60
- return l .leftLine
61
- }
62
-
63
- // Right returns the right line number.
64
- func (l * DiffLine ) Right () int {
65
- return l .rightLine
42
+ Type DiffLineType // The type of the line
43
+ Content string // The content of the line
44
+ LeftLine int // The left line number
45
+ RightLine int // The right line number
66
46
}
67
47
68
48
// DiffSection represents a section in diff.
69
49
type DiffSection struct {
70
- lines []* DiffLine
50
+ Lines []* DiffLine // lines in the section
71
51
72
52
numAdditions int
73
53
numDeletions int
74
54
}
75
55
76
- // Lines returns lines in the section.
77
- func (s * DiffSection ) Lines () []* DiffLine {
78
- return s .lines
79
- }
80
-
81
56
// NumLines returns the number of lines in the section.
82
57
func (s * DiffSection ) NumLines () int {
83
- return len (s .lines )
58
+ return len (s .Lines )
84
59
}
85
60
86
61
// Line returns a specific line by given type and line number in a section.
@@ -93,8 +68,8 @@ func (s *DiffSection) Line(typ DiffLineType, line int) *DiffLine {
93
68
)
94
69
95
70
loop:
96
- for _ , diffLine := range s .lines {
97
- switch diffLine .typ {
71
+ for _ , diffLine := range s .Lines {
72
+ switch diffLine .Type {
98
73
case DiffLineAdd :
99
74
addCount ++
100
75
case DiffLineDelete :
@@ -103,18 +78,18 @@ loop:
103
78
if matchedDiffLine != nil {
104
79
break loop
105
80
}
106
- difference = diffLine .rightLine - diffLine .leftLine
81
+ difference = diffLine .RightLine - diffLine .LeftLine
107
82
addCount = 0
108
83
delCount = 0
109
84
}
110
85
111
86
switch typ {
112
87
case DiffLineDelete :
113
- if diffLine .rightLine == 0 && diffLine .leftLine == line - difference {
88
+ if diffLine .RightLine == 0 && diffLine .LeftLine == line - difference {
114
89
matchedDiffLine = diffLine
115
90
}
116
91
case DiffLineAdd :
117
- if diffLine .leftLine == 0 && diffLine .rightLine == line + difference {
92
+ if diffLine .LeftLine == 0 && diffLine .RightLine == line + difference {
118
93
matchedDiffLine = diffLine
119
94
}
120
95
}
@@ -128,10 +103,15 @@ loop:
128
103
129
104
// DiffFile represents a file in diff.
130
105
type DiffFile struct {
131
- name string
132
- typ DiffFileType
133
- index string // Changed/New: new SHA; Deleted: old SHA
134
- sections []* DiffSection
106
+ // The name of the file.
107
+ Name string
108
+ // The type of the file.
109
+ Type DiffFileType
110
+ // The index (SHA1 hash) of the file. For a changed/new file, it is the new SHA,
111
+ // and for a deleted file it is the old SHA.
112
+ Index string
113
+ // The sections in the file.
114
+ Sections []* DiffSection
135
115
136
116
numAdditions int
137
117
numDeletions int
@@ -143,29 +123,9 @@ type DiffFile struct {
143
123
isIncomplete bool
144
124
}
145
125
146
- // Name returns the name of the file.
147
- func (f * DiffFile ) Name () string {
148
- return f .name
149
- }
150
-
151
- // Type returns the type of the file.
152
- func (f * DiffFile ) Type () DiffFileType {
153
- return f .typ
154
- }
155
-
156
- // Index returns the index (SHA1 hash) of the file.
157
- func (f * DiffFile ) Index () string {
158
- return f .index
159
- }
160
-
161
- // Sections returns the sections in the file.
162
- func (f * DiffFile ) Sections () []* DiffSection {
163
- return f .sections
164
- }
165
-
166
126
// NumSections returns the number of sections in the file.
167
127
func (f * DiffFile ) NumSections () int {
168
- return len (f .sections )
128
+ return len (f .Sections )
169
129
}
170
130
171
131
// NumAdditions returns the number of additions in the file.
@@ -180,17 +140,17 @@ func (f *DiffFile) NumDeletions() int {
180
140
181
141
// IsCreated returns true if the file is newly created.
182
142
func (f * DiffFile ) IsCreated () bool {
183
- return f .typ == DiffFileAdd
143
+ return f .Type == DiffFileAdd
184
144
}
185
145
186
146
// IsDeleted returns true if the file has been deleted.
187
147
func (f * DiffFile ) IsDeleted () bool {
188
- return f .typ == DiffFileDelete
148
+ return f .Type == DiffFileDelete
189
149
}
190
150
191
151
// IsRenamed returns true if the file has been renamed.
192
152
func (f * DiffFile ) IsRenamed () bool {
193
- return f .typ == DiffFileRename
153
+ return f .Type == DiffFileRename
194
154
}
195
155
196
156
// OldName returns previous name before renaming.
@@ -215,22 +175,17 @@ func (f *DiffFile) IsIncomplete() bool {
215
175
216
176
// Diff represents a Git diff.
217
177
type Diff struct {
218
- files []* DiffFile
178
+ Files []* DiffFile // The files in the diff
219
179
220
180
totalAdditions int
221
181
totalDeletions int
222
182
223
183
isIncomplete bool
224
184
}
225
185
226
- // Files returns the files in the diff.
227
- func (d * Diff ) Files () []* DiffFile {
228
- return d .files
229
- }
230
-
231
186
// NumFiles returns the number of files in the diff.
232
187
func (d * Diff ) NumFiles () int {
233
- return len (d .files )
188
+ return len (d .Files )
234
189
}
235
190
236
191
// TotalAdditions returns the total additions in the diff.
@@ -248,8 +203,8 @@ func (d *Diff) IsIncomplete() bool {
248
203
return d .isIncomplete
249
204
}
250
205
251
- // SteamParsePatchResult contains results of stream parsing a patch .
252
- type SteamParsePatchResult struct {
206
+ // SteamParseDiffResult contains results of streaming parsing a diff .
207
+ type SteamParseDiffResult struct {
253
208
Diff * Diff
254
209
Err error
255
210
}
@@ -312,8 +267,8 @@ func (p *diffParser) parseFileHeader() (*DiffFile, error) {
312
267
}
313
268
314
269
file := & DiffFile {
315
- name : a ,
316
- typ : DiffFileChange ,
270
+ Name : a ,
271
+ Type : DiffFileChange ,
317
272
}
318
273
319
274
// Check file diff type and submodule
@@ -333,10 +288,10 @@ checkType:
333
288
334
289
switch {
335
290
case strings .HasPrefix (line , "new file" ):
336
- file .typ = DiffFileAdd
291
+ file .Type = DiffFileAdd
337
292
file .isSubmodule = strings .HasSuffix (line , " 160000" )
338
293
case strings .HasPrefix (line , "deleted" ):
339
- file .typ = DiffFileDelete
294
+ file .Type = DiffFileDelete
340
295
file .isSubmodule = strings .HasSuffix (line , " 160000" )
341
296
case strings .HasPrefix (line , "index" ): // e.g. index ee791be..9997571 100644
342
297
fields := strings .Fields (line [6 :])
@@ -346,15 +301,15 @@ checkType:
346
301
}
347
302
348
303
if file .IsDeleted () {
349
- file .index = shas [0 ]
304
+ file .Index = shas [0 ]
350
305
} else {
351
- file .index = shas [1 ]
306
+ file .Index = shas [1 ]
352
307
}
353
308
break checkType
354
309
case strings .HasPrefix (line , "similarity index 100%" ):
355
- file .typ = DiffFileRename
310
+ file .Type = DiffFileRename
356
311
file .oldName = a
357
- file .name = b
312
+ file .Name = b
358
313
break checkType
359
314
case strings .HasPrefix (line , "old mode" ):
360
315
break checkType
@@ -369,10 +324,10 @@ func (p *diffParser) parseSection() (_ *DiffSection, isIncomplete bool, _ error)
369
324
p .buffer = nil
370
325
371
326
section := & DiffSection {
372
- lines : []* DiffLine {
327
+ Lines : []* DiffLine {
373
328
{
374
- typ : DiffLineSection ,
375
- content : line ,
329
+ Type : DiffLineSection ,
330
+ Content : line ,
376
331
},
377
332
},
378
333
}
@@ -416,27 +371,27 @@ func (p *diffParser) parseSection() (_ *DiffSection, isIncomplete bool, _ error)
416
371
417
372
switch line [0 ] {
418
373
case ' ' :
419
- section .lines = append (section .lines , & DiffLine {
420
- typ : DiffLinePlain ,
421
- content : line ,
422
- leftLine : leftLine ,
423
- rightLine : rightLine ,
374
+ section .Lines = append (section .Lines , & DiffLine {
375
+ Type : DiffLinePlain ,
376
+ Content : line ,
377
+ LeftLine : leftLine ,
378
+ RightLine : rightLine ,
424
379
})
425
380
leftLine ++
426
381
rightLine ++
427
382
case '+' :
428
- section .lines = append (section .lines , & DiffLine {
429
- typ : DiffLineAdd ,
430
- content : line ,
431
- rightLine : rightLine ,
383
+ section .Lines = append (section .Lines , & DiffLine {
384
+ Type : DiffLineAdd ,
385
+ Content : line ,
386
+ RightLine : rightLine ,
432
387
})
433
388
section .numAdditions ++
434
389
rightLine ++
435
390
case '-' :
436
- section .lines = append (section .lines , & DiffLine {
437
- typ : DiffLineDelete ,
438
- content : line ,
439
- leftLine : leftLine ,
391
+ section .Lines = append (section .Lines , & DiffLine {
392
+ Type : DiffLineDelete ,
393
+ Content : line ,
394
+ LeftLine : leftLine ,
440
395
})
441
396
section .numDeletions ++
442
397
if leftLine > 0 {
@@ -469,7 +424,7 @@ func (p *diffParser) parse() (*Diff, error) {
469
424
// Found new file
470
425
if bytes .HasPrefix (p .buffer , diffHead ) {
471
426
// Check if reached maximum number of files
472
- if p .maxFiles > 0 && len (diff .files ) >= p .maxFiles {
427
+ if p .maxFiles > 0 && len (diff .Files ) >= p .maxFiles {
473
428
diff .isIncomplete = true
474
429
_ , _ = io .Copy (ioutil .Discard , p )
475
430
break
@@ -479,7 +434,7 @@ func (p *diffParser) parse() (*Diff, error) {
479
434
if err != nil {
480
435
return nil , err
481
436
}
482
- diff .files = append (diff .files , file )
437
+ diff .Files = append (diff .Files , file )
483
438
484
439
currentFileLines = 0
485
440
continue
@@ -513,7 +468,7 @@ func (p *diffParser) parse() (*Diff, error) {
513
468
if err != nil {
514
469
return nil , err
515
470
}
516
- file .sections = append (file .sections , section )
471
+ file .Sections = append (file .Sections , section )
517
472
file .numAdditions += section .numAdditions
518
473
file .numDeletions += section .numDeletions
519
474
diff .totalAdditions += section .numAdditions
@@ -531,15 +486,15 @@ func (p *diffParser) parse() (*Diff, error) {
531
486
// StreamParseDiff parses the diff read from the given io.Reader. It does parse-on-read to minimize
532
487
// the time spent on huge diffs. It accepts a channel to notify and send error (if any) to the caller
533
488
// when the process is done. Therefore, this method should be called in a goroutine asynchronously.
534
- func StreamParseDiff (r io.Reader , done chan <- SteamParsePatchResult , maxFiles , maxFileLines , maxLineChars int ) {
489
+ func StreamParseDiff (r io.Reader , done chan <- SteamParseDiffResult , maxFiles , maxFileLines , maxLineChars int ) {
535
490
p := & diffParser {
536
491
Reader : bufio .NewReader (r ),
537
492
maxFiles : maxFiles ,
538
493
maxFileLines : maxFileLines ,
539
494
maxLineChars : maxLineChars ,
540
495
}
541
496
diff , err := p .parse ()
542
- done <- SteamParsePatchResult {
497
+ done <- SteamParseDiffResult {
543
498
Diff : diff ,
544
499
Err : err ,
545
500
}
0 commit comments