@@ -13,7 +13,6 @@ import (
13
13
"html"
14
14
"html/template"
15
15
"io"
16
- "io/ioutil"
17
16
"net/url"
18
17
"os"
19
18
"os/exec"
@@ -413,13 +412,14 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
413
412
}
414
413
415
414
leftLine , rightLine int
416
- lineCount int
417
415
curFileLinesCount int
418
416
curFileLFSPrefix bool
419
417
)
420
418
421
419
input := bufio .NewReader (reader )
422
420
isEOF := false
421
+ diff .NumFiles = 0
422
+
423
423
for ! isEOF {
424
424
var linebuf bytes.Buffer
425
425
for {
@@ -437,7 +437,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
437
437
}
438
438
if linebuf .Len () < maxLineCharacters {
439
439
linebuf .WriteByte (b )
440
- } else if linebuf .Len () == maxLineCharacters {
440
+ } else if linebuf .Len () == maxLineCharacters && curFile != nil {
441
441
curFile .IsIncomplete = true
442
442
}
443
443
}
@@ -449,11 +449,11 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
449
449
450
450
trimLine := strings .Trim (line , "+- " )
451
451
452
- if trimLine == models .LFSMetaFileIdentifier {
452
+ if trimLine == models .LFSMetaFileIdentifier && curFile != nil {
453
453
curFileLFSPrefix = true
454
454
}
455
455
456
- if curFileLFSPrefix && strings .HasPrefix (trimLine , models .LFSMetaFileOidPrefix ) {
456
+ if curFileLFSPrefix && strings .HasPrefix (trimLine , models .LFSMetaFileOidPrefix ) && curFile != nil {
457
457
oid := strings .TrimPrefix (trimLine , models .LFSMetaFileOidPrefix )
458
458
459
459
if len (oid ) == 64 {
@@ -469,20 +469,25 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
469
469
}
470
470
471
471
curFileLinesCount ++
472
- lineCount ++
473
472
474
473
// Diff data too large, we only show the first about maxLines lines
475
- if curFileLinesCount >= maxLines {
474
+ if curFileLinesCount >= maxLines && curFile != nil {
476
475
curFile .IsIncomplete = true
477
476
}
478
477
switch {
479
478
case line [0 ] == ' ' :
479
+ if curFile == nil {
480
+ continue
481
+ }
480
482
diffLine := & DiffLine {Type : DiffLinePlain , Content : line , LeftIdx : leftLine , RightIdx : rightLine }
481
483
leftLine ++
482
484
rightLine ++
483
485
curSection .Lines = append (curSection .Lines , diffLine )
484
486
continue
485
487
case line [0 ] == '@' :
488
+ if curFile == nil {
489
+ continue
490
+ }
486
491
curSection = & DiffSection {}
487
492
curFile .Sections = append (curFile .Sections , curSection )
488
493
lineSectionInfo := getDiffLineSectionInfo (curFile .Name , line , leftLine - 1 , rightLine - 1 )
@@ -497,36 +502,35 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
497
502
rightLine = lineSectionInfo .RightIdx
498
503
continue
499
504
case line [0 ] == '+' :
500
- curFile .Addition ++
501
505
diff .TotalAddition ++
506
+ if curFile == nil {
507
+ continue
508
+ }
509
+ curFile .Addition ++
502
510
diffLine := & DiffLine {Type : DiffLineAdd , Content : line , RightIdx : rightLine }
503
511
rightLine ++
504
512
curSection .Lines = append (curSection .Lines , diffLine )
505
513
continue
506
514
case line [0 ] == '-' :
507
- curFile .Deletion ++
508
515
diff .TotalDeletion ++
516
+ if curFile == nil {
517
+ continue
518
+ }
519
+ curFile .Deletion ++
509
520
diffLine := & DiffLine {Type : DiffLineDel , Content : line , LeftIdx : leftLine }
510
521
if leftLine > 0 {
511
522
leftLine ++
512
523
}
513
524
curSection .Lines = append (curSection .Lines , diffLine )
514
525
case strings .HasPrefix (line , "Binary" ):
515
- curFile .IsBin = true
516
- continue
526
+ if curFile != nil {
527
+ curFile .IsBin = true
528
+ continue
529
+ }
517
530
}
518
531
519
532
// Get new file.
520
533
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
-
530
534
var middle int
531
535
532
536
// 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
562
566
563
567
}
564
568
569
+ if diff .NumFiles > maxFiles {
570
+ diff .NumFiles ++
571
+ curFile = nil
572
+ continue
573
+ }
574
+
565
575
curFile = & DiffFile {
566
576
Name : b ,
567
577
OldName : a ,
@@ -571,6 +581,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
571
581
IsRenamed : a != b ,
572
582
}
573
583
diff .Files = append (diff .Files , curFile )
584
+ diff .NumFiles ++
574
585
curFileLinesCount = 0
575
586
leftLine = 1
576
587
rightLine = 1
@@ -634,7 +645,11 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
634
645
}
635
646
}
636
647
}
637
- diff .NumFiles = len (diff .Files )
648
+
649
+ if diff .NumFiles > maxFiles {
650
+ diff .IsIncomplete = true
651
+ }
652
+
638
653
return diff , nil
639
654
}
640
655
0 commit comments