@@ -13,7 +13,6 @@ import (
1313 "html"
1414 "html/template"
1515 "io"
16- "io/ioutil"
1716 "net/url"
1817 "os"
1918 "os/exec"
@@ -413,13 +412,14 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
413412 }
414413
415414 leftLine , rightLine int
416- lineCount int
417415 curFileLinesCount int
418416 curFileLFSPrefix bool
419417 )
420418
421419 input := bufio .NewReader (reader )
422420 isEOF := false
421+ diff .NumFiles = 0
422+
423423 for ! isEOF {
424424 var linebuf bytes.Buffer
425425 for {
@@ -437,7 +437,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
437437 }
438438 if linebuf .Len () < maxLineCharacters {
439439 linebuf .WriteByte (b )
440- } else if linebuf .Len () == maxLineCharacters {
440+ } else if linebuf .Len () == maxLineCharacters && curFile != nil {
441441 curFile .IsIncomplete = true
442442 }
443443 }
@@ -449,11 +449,11 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
449449
450450 trimLine := strings .Trim (line , "+- " )
451451
452- if trimLine == models .LFSMetaFileIdentifier {
452+ if trimLine == models .LFSMetaFileIdentifier && curFile != nil {
453453 curFileLFSPrefix = true
454454 }
455455
456- if curFileLFSPrefix && strings .HasPrefix (trimLine , models .LFSMetaFileOidPrefix ) {
456+ if curFileLFSPrefix && strings .HasPrefix (trimLine , models .LFSMetaFileOidPrefix ) && curFile != nil {
457457 oid := strings .TrimPrefix (trimLine , models .LFSMetaFileOidPrefix )
458458
459459 if len (oid ) == 64 {
@@ -469,20 +469,25 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
469469 }
470470
471471 curFileLinesCount ++
472- lineCount ++
473472
474473 // Diff data too large, we only show the first about maxLines lines
475- if curFileLinesCount >= maxLines {
474+ if curFileLinesCount >= maxLines && curFile != nil {
476475 curFile .IsIncomplete = true
477476 }
478477 switch {
479478 case line [0 ] == ' ' :
479+ if curFile == nil {
480+ continue
481+ }
480482 diffLine := & DiffLine {Type : DiffLinePlain , Content : line , LeftIdx : leftLine , RightIdx : rightLine }
481483 leftLine ++
482484 rightLine ++
483485 curSection .Lines = append (curSection .Lines , diffLine )
484486 continue
485487 case line [0 ] == '@' :
488+ if curFile == nil {
489+ continue
490+ }
486491 curSection = & DiffSection {}
487492 curFile .Sections = append (curFile .Sections , curSection )
488493 lineSectionInfo := getDiffLineSectionInfo (curFile .Name , line , leftLine - 1 , rightLine - 1 )
@@ -497,36 +502,35 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
497502 rightLine = lineSectionInfo .RightIdx
498503 continue
499504 case line [0 ] == '+' :
500- curFile .Addition ++
501505 diff .TotalAddition ++
506+ if curFile == nil {
507+ continue
508+ }
509+ curFile .Addition ++
502510 diffLine := & DiffLine {Type : DiffLineAdd , Content : line , RightIdx : rightLine }
503511 rightLine ++
504512 curSection .Lines = append (curSection .Lines , diffLine )
505513 continue
506514 case line [0 ] == '-' :
507- curFile .Deletion ++
508515 diff .TotalDeletion ++
516+ if curFile == nil {
517+ continue
518+ }
519+ curFile .Deletion ++
509520 diffLine := & DiffLine {Type : DiffLineDel , Content : line , LeftIdx : leftLine }
510521 if leftLine > 0 {
511522 leftLine ++
512523 }
513524 curSection .Lines = append (curSection .Lines , diffLine )
514525 case strings .HasPrefix (line , "Binary" ):
515- curFile .IsBin = true
516- continue
526+ if curFile != nil {
527+ curFile .IsBin = true
528+ continue
529+ }
517530 }
518531
519532 // Get new file.
520533 if strings .HasPrefix (line , cmdDiffHead ) {
521- if len (diff .Files ) >= maxFiles {
522- diff .IsIncomplete = true
523- _ , err := io .Copy (ioutil .Discard , reader )
524- if err != nil {
525- return nil , fmt .Errorf ("Copy: %v" , err )
526- }
527- break
528- }
529-
530534 var middle int
531535
532536 // Note: In case file name is surrounded by double quotes (it happens only in git-shell).
@@ -562,6 +566,12 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
562566
563567 }
564568
569+ if diff .NumFiles > maxFiles {
570+ diff .NumFiles ++
571+ curFile = nil
572+ continue
573+ }
574+
565575 curFile = & DiffFile {
566576 Name : b ,
567577 OldName : a ,
@@ -571,6 +581,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
571581 IsRenamed : a != b ,
572582 }
573583 diff .Files = append (diff .Files , curFile )
584+ diff .NumFiles ++
574585 curFileLinesCount = 0
575586 leftLine = 1
576587 rightLine = 1
@@ -634,7 +645,11 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
634645 }
635646 }
636647 }
637- diff .NumFiles = len (diff .Files )
648+
649+ if diff .NumFiles > maxFiles {
650+ diff .IsIncomplete = true
651+ }
652+
638653 return diff , nil
639654}
640655
0 commit comments