@@ -39,48 +39,23 @@ const (
3939
4040// DiffLine represents a line in diff.
4141type 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
6646}
6747
6848// DiffSection represents a section in diff.
6949type DiffSection struct {
70- lines []* DiffLine
50+ Lines []* DiffLine // lines in the section
7151
7252 numAdditions int
7353 numDeletions int
7454}
7555
76- // Lines returns lines in the section.
77- func (s * DiffSection ) Lines () []* DiffLine {
78- return s .lines
79- }
80-
8156// NumLines returns the number of lines in the section.
8257func (s * DiffSection ) NumLines () int {
83- return len (s .lines )
58+ return len (s .Lines )
8459}
8560
8661// 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 {
9368 )
9469
9570loop:
96- for _ , diffLine := range s .lines {
97- switch diffLine .typ {
71+ for _ , diffLine := range s .Lines {
72+ switch diffLine .Type {
9873 case DiffLineAdd :
9974 addCount ++
10075 case DiffLineDelete :
@@ -103,18 +78,18 @@ loop:
10378 if matchedDiffLine != nil {
10479 break loop
10580 }
106- difference = diffLine .rightLine - diffLine .leftLine
81+ difference = diffLine .RightLine - diffLine .LeftLine
10782 addCount = 0
10883 delCount = 0
10984 }
11085
11186 switch typ {
11287 case DiffLineDelete :
113- if diffLine .rightLine == 0 && diffLine .leftLine == line - difference {
88+ if diffLine .RightLine == 0 && diffLine .LeftLine == line - difference {
11489 matchedDiffLine = diffLine
11590 }
11691 case DiffLineAdd :
117- if diffLine .leftLine == 0 && diffLine .rightLine == line + difference {
92+ if diffLine .LeftLine == 0 && diffLine .RightLine == line + difference {
11893 matchedDiffLine = diffLine
11994 }
12095 }
@@ -128,10 +103,15 @@ loop:
128103
129104// DiffFile represents a file in diff.
130105type 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
135115
136116 numAdditions int
137117 numDeletions int
@@ -143,29 +123,9 @@ type DiffFile struct {
143123 isIncomplete bool
144124}
145125
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-
166126// NumSections returns the number of sections in the file.
167127func (f * DiffFile ) NumSections () int {
168- return len (f .sections )
128+ return len (f .Sections )
169129}
170130
171131// NumAdditions returns the number of additions in the file.
@@ -180,17 +140,17 @@ func (f *DiffFile) NumDeletions() int {
180140
181141// IsCreated returns true if the file is newly created.
182142func (f * DiffFile ) IsCreated () bool {
183- return f .typ == DiffFileAdd
143+ return f .Type == DiffFileAdd
184144}
185145
186146// IsDeleted returns true if the file has been deleted.
187147func (f * DiffFile ) IsDeleted () bool {
188- return f .typ == DiffFileDelete
148+ return f .Type == DiffFileDelete
189149}
190150
191151// IsRenamed returns true if the file has been renamed.
192152func (f * DiffFile ) IsRenamed () bool {
193- return f .typ == DiffFileRename
153+ return f .Type == DiffFileRename
194154}
195155
196156// OldName returns previous name before renaming.
@@ -215,22 +175,17 @@ func (f *DiffFile) IsIncomplete() bool {
215175
216176// Diff represents a Git diff.
217177type Diff struct {
218- files []* DiffFile
178+ Files []* DiffFile // The files in the diff
219179
220180 totalAdditions int
221181 totalDeletions int
222182
223183 isIncomplete bool
224184}
225185
226- // Files returns the files in the diff.
227- func (d * Diff ) Files () []* DiffFile {
228- return d .files
229- }
230-
231186// NumFiles returns the number of files in the diff.
232187func (d * Diff ) NumFiles () int {
233- return len (d .files )
188+ return len (d .Files )
234189}
235190
236191// TotalAdditions returns the total additions in the diff.
@@ -248,8 +203,8 @@ func (d *Diff) IsIncomplete() bool {
248203 return d .isIncomplete
249204}
250205
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 {
253208 Diff * Diff
254209 Err error
255210}
@@ -312,8 +267,8 @@ func (p *diffParser) parseFileHeader() (*DiffFile, error) {
312267 }
313268
314269 file := & DiffFile {
315- name : a ,
316- typ : DiffFileChange ,
270+ Name : a ,
271+ Type : DiffFileChange ,
317272 }
318273
319274 // Check file diff type and submodule
@@ -333,10 +288,10 @@ checkType:
333288
334289 switch {
335290 case strings .HasPrefix (line , "new file" ):
336- file .typ = DiffFileAdd
291+ file .Type = DiffFileAdd
337292 file .isSubmodule = strings .HasSuffix (line , " 160000" )
338293 case strings .HasPrefix (line , "deleted" ):
339- file .typ = DiffFileDelete
294+ file .Type = DiffFileDelete
340295 file .isSubmodule = strings .HasSuffix (line , " 160000" )
341296 case strings .HasPrefix (line , "index" ): // e.g. index ee791be..9997571 100644
342297 fields := strings .Fields (line [6 :])
@@ -346,15 +301,15 @@ checkType:
346301 }
347302
348303 if file .IsDeleted () {
349- file .index = shas [0 ]
304+ file .Index = shas [0 ]
350305 } else {
351- file .index = shas [1 ]
306+ file .Index = shas [1 ]
352307 }
353308 break checkType
354309 case strings .HasPrefix (line , "similarity index 100%" ):
355- file .typ = DiffFileRename
310+ file .Type = DiffFileRename
356311 file .oldName = a
357- file .name = b
312+ file .Name = b
358313 break checkType
359314 case strings .HasPrefix (line , "old mode" ):
360315 break checkType
@@ -369,10 +324,10 @@ func (p *diffParser) parseSection() (_ *DiffSection, isIncomplete bool, _ error)
369324 p .buffer = nil
370325
371326 section := & DiffSection {
372- lines : []* DiffLine {
327+ Lines : []* DiffLine {
373328 {
374- typ : DiffLineSection ,
375- content : line ,
329+ Type : DiffLineSection ,
330+ Content : line ,
376331 },
377332 },
378333 }
@@ -416,27 +371,27 @@ func (p *diffParser) parseSection() (_ *DiffSection, isIncomplete bool, _ error)
416371
417372 switch line [0 ] {
418373 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 ,
424379 })
425380 leftLine ++
426381 rightLine ++
427382 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 ,
432387 })
433388 section .numAdditions ++
434389 rightLine ++
435390 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 ,
440395 })
441396 section .numDeletions ++
442397 if leftLine > 0 {
@@ -469,7 +424,7 @@ func (p *diffParser) parse() (*Diff, error) {
469424 // Found new file
470425 if bytes .HasPrefix (p .buffer , diffHead ) {
471426 // 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 {
473428 diff .isIncomplete = true
474429 _ , _ = io .Copy (ioutil .Discard , p )
475430 break
@@ -479,7 +434,7 @@ func (p *diffParser) parse() (*Diff, error) {
479434 if err != nil {
480435 return nil , err
481436 }
482- diff .files = append (diff .files , file )
437+ diff .Files = append (diff .Files , file )
483438
484439 currentFileLines = 0
485440 continue
@@ -513,7 +468,7 @@ func (p *diffParser) parse() (*Diff, error) {
513468 if err != nil {
514469 return nil , err
515470 }
516- file .sections = append (file .sections , section )
471+ file .Sections = append (file .Sections , section )
517472 file .numAdditions += section .numAdditions
518473 file .numDeletions += section .numDeletions
519474 diff .totalAdditions += section .numAdditions
@@ -531,15 +486,15 @@ func (p *diffParser) parse() (*Diff, error) {
531486// StreamParseDiff parses the diff read from the given io.Reader. It does parse-on-read to minimize
532487// the time spent on huge diffs. It accepts a channel to notify and send error (if any) to the caller
533488// 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 ) {
535490 p := & diffParser {
536491 Reader : bufio .NewReader (r ),
537492 maxFiles : maxFiles ,
538493 maxFileLines : maxFileLines ,
539494 maxLineChars : maxLineChars ,
540495 }
541496 diff , err := p .parse ()
542- done <- SteamParsePatchResult {
497+ done <- SteamParseDiffResult {
543498 Diff : diff ,
544499 Err : err ,
545500 }
0 commit comments