@@ -2415,36 +2415,36 @@ process.umask = function() { return 0; };
2415
2415
var hunkHeaderPrefix = '@@' ;
2416
2416
2417
2417
/* Add previous block(if exists) before start a new file */
2418
- var saveBlock = function ( ) {
2418
+ function saveBlock ( ) {
2419
2419
if ( currentBlock ) {
2420
2420
currentFile . blocks . push ( currentBlock ) ;
2421
2421
currentBlock = null ;
2422
2422
}
2423
- } ;
2423
+ }
2424
2424
2425
2425
/*
2426
2426
* Add previous file(if exists) before start a new one
2427
2427
* if it has name (to avoid binary files errors)
2428
2428
*/
2429
- var saveFile = function ( ) {
2429
+ function saveFile ( ) {
2430
2430
if ( currentFile && currentFile . newName ) {
2431
2431
files . push ( currentFile ) ;
2432
2432
currentFile = null ;
2433
2433
}
2434
- } ;
2434
+ }
2435
2435
2436
2436
/* Create file structure */
2437
- var startFile = function ( ) {
2437
+ function startFile ( ) {
2438
2438
saveBlock ( ) ;
2439
2439
saveFile ( ) ;
2440
2440
2441
2441
currentFile = { } ;
2442
2442
currentFile . blocks = [ ] ;
2443
2443
currentFile . deletedLines = 0 ;
2444
2444
currentFile . addedLines = 0 ;
2445
- } ;
2445
+ }
2446
2446
2447
- var startBlock = function ( line ) {
2447
+ function startBlock ( line ) {
2448
2448
saveBlock ( ) ;
2449
2449
2450
2450
var values ;
@@ -2489,9 +2489,9 @@ process.umask = function() { return 0; };
2489
2489
currentBlock . oldStartLine2 = oldLine2 ;
2490
2490
currentBlock . newStartLine = newLine ;
2491
2491
currentBlock . header = line ;
2492
- } ;
2492
+ }
2493
2493
2494
- var createLine = function ( line ) {
2494
+ function createLine ( line ) {
2495
2495
var currentLine = { } ;
2496
2496
currentLine . content = line ;
2497
2497
@@ -2522,7 +2522,34 @@ process.umask = function() { return 0; };
2522
2522
2523
2523
currentBlock . lines . push ( currentLine ) ;
2524
2524
}
2525
- } ;
2525
+ }
2526
+
2527
+ /*
2528
+ * Checks if there is a hunk header coming before a new file starts
2529
+ *
2530
+ * Hunk header is a group of three lines started by ( `--- ` , `+++ ` , `@@` )
2531
+ */
2532
+ function existHunkHeader ( line , lineIdx ) {
2533
+ var idx = lineIdx ;
2534
+
2535
+ while ( idx < diffLines . length - 3 ) {
2536
+ if ( utils . startsWith ( line , 'diff' ) ) {
2537
+ return false ;
2538
+ }
2539
+
2540
+ if (
2541
+ utils . startsWith ( diffLines [ idx ] , oldFileNameHeader ) &&
2542
+ utils . startsWith ( diffLines [ idx + 1 ] , newFileNameHeader ) &&
2543
+ utils . startsWith ( diffLines [ idx + 2 ] , hunkHeaderPrefix )
2544
+ ) {
2545
+ return true ;
2546
+ }
2547
+
2548
+ idx ++ ;
2549
+ }
2550
+
2551
+ return false ;
2552
+ }
2526
2553
2527
2554
var diffLines =
2528
2555
diffInput . replace ( / \\ N o n e w l i n e a t e n d o f f i l e / g, '' )
@@ -2638,6 +2665,8 @@ process.umask = function() { return 0; };
2638
2665
return ;
2639
2666
}
2640
2667
2668
+ var doesNotExistHunkHeader = ! existHunkHeader ( line , lineIndex ) ;
2669
+
2641
2670
/*
2642
2671
* Git diffs provide more information regarding files modes, renames, copies,
2643
2672
* commits between changes and similarity indexes
@@ -2653,16 +2682,24 @@ process.umask = function() { return 0; };
2653
2682
currentFile . newFileMode = values [ 1 ] ;
2654
2683
currentFile . isNew = true ;
2655
2684
} else if ( ( values = copyFrom . exec ( line ) ) ) {
2656
- currentFile . oldName = values [ 1 ] ;
2685
+ if ( doesNotExistHunkHeader ) {
2686
+ currentFile . oldName = values [ 1 ] ;
2687
+ }
2657
2688
currentFile . isCopy = true ;
2658
2689
} else if ( ( values = copyTo . exec ( line ) ) ) {
2659
- currentFile . newName = values [ 1 ] ;
2690
+ if ( doesNotExistHunkHeader ) {
2691
+ currentFile . newName = values [ 1 ] ;
2692
+ }
2660
2693
currentFile . isCopy = true ;
2661
2694
} else if ( ( values = renameFrom . exec ( line ) ) ) {
2662
- currentFile . oldName = values [ 1 ] ;
2695
+ if ( doesNotExistHunkHeader ) {
2696
+ currentFile . oldName = values [ 1 ] ;
2697
+ }
2663
2698
currentFile . isRename = true ;
2664
2699
} else if ( ( values = renameTo . exec ( line ) ) ) {
2665
- currentFile . newName = values [ 1 ] ;
2700
+ if ( doesNotExistHunkHeader ) {
2701
+ currentFile . newName = values [ 1 ] ;
2702
+ }
2666
2703
currentFile . isRename = true ;
2667
2704
} else if ( ( values = similarityIndex . exec ( line ) ) ) {
2668
2705
currentFile . unchangedPercentage = values [ 1 ] ;
0 commit comments