@@ -2478,6 +2478,11 @@ var Hogan = {};
2478
2478
. replace ( / \r \n ? / g, '\n' )
2479
2479
. split ( '\n' ) ;
2480
2480
2481
+ /* Diff Header */
2482
+ var oldFileNameHeader = '--- ' ;
2483
+ var newFileNameHeader = '+++ ' ;
2484
+ var hunkHeaderPrefix = '@@' ;
2485
+
2481
2486
/* Diff */
2482
2487
var oldMode = / ^ o l d m o d e ( \d { 6 } ) / ;
2483
2488
var newMode = / ^ n e w m o d e ( \d { 6 } ) / ;
@@ -2500,22 +2505,31 @@ var Hogan = {};
2500
2505
var combinedNewFile = / ^ n e w f i l e m o d e ( \d { 6 } ) / ;
2501
2506
var combinedDeletedFile = / ^ d e l e t e d f i l e m o d e ( \d { 6 } ) , ( \d { 6 } ) / ;
2502
2507
2503
- diffLines . forEach ( function ( line ) {
2508
+ diffLines . forEach ( function ( line , lineIndex ) {
2504
2509
// Unmerged paths, and possibly other non-diffable files
2505
2510
// https://github.com/scottgonzalez/pretty-diff/issues/11
2506
2511
// Also, remove some useless lines
2507
2512
if ( ! line || utils . startsWith ( line , '*' ) ) {
2508
2513
return ;
2509
2514
}
2510
2515
2511
- if (
2512
- utils . startsWith ( line , 'diff' ) || // Git diffs always start with diff
2513
- ! currentFile || // If we do not have a file yet, we should crete one
2516
+ var prevLine = diffLines [ lineIndex - 1 ] ;
2517
+ var nxtLine = diffLines [ lineIndex + 1 ] ;
2518
+ var afterNxtLine = diffLines [ lineIndex + 2 ] ;
2519
+
2520
+ if ( utils . startsWith ( line , 'diff' ) ) {
2521
+ startFile ( ) ;
2522
+ currentFile . isGitDiff = true ;
2523
+ return ;
2524
+ }
2525
+
2526
+ if ( ! currentFile || // If we do not have a file yet, we should crete one
2514
2527
(
2515
- currentFile && // If we already have some file in progress and
2528
+ ! currentFile . isGitDiff && currentFile && // If we already have some file in progress and
2516
2529
(
2517
- currentFile . oldName && utils . startsWith ( line , '--- ' ) || // Either we reached a old file identification line
2518
- currentFile . newName && utils . startsWith ( line , '+++ ' ) // Or we reached a new file identification line
2530
+ utils . startsWith ( line , oldFileNameHeader ) && // If we get to an old file path header line
2531
+ // And is followed by the new file path header line and the hunk header line
2532
+ utils . startsWith ( nxtLine , newFileNameHeader ) && utils . startsWith ( afterNxtLine , hunkHeaderPrefix )
2519
2533
)
2520
2534
)
2521
2535
) {
@@ -2525,28 +2539,43 @@ var Hogan = {};
2525
2539
var values ;
2526
2540
2527
2541
/*
2528
- * --- Date Timestamp[FractionalSeconds] TimeZone
2529
- * --- 2002-02-21 23:30:39.942229878 -0800
2542
+ * We need to make sure that we have the three lines of the header.
2543
+ * This avoids cases like the ones described in:
2544
+ * - https://github.com/rtfpessoa/diff2html/issues/87
2530
2545
*/
2531
- if ( currentFile && ! currentFile . oldName &&
2532
- utils . startsWith ( line , '--- ' ) && ( values = getSrcFilename ( line , config ) ) ) {
2533
- currentFile . oldName = values ;
2534
- currentFile . language = getExtension ( currentFile . oldName , currentFile . language ) ;
2535
- return ;
2536
- }
2546
+ if (
2547
+ ( utils . startsWith ( line , oldFileNameHeader ) &&
2548
+ utils . startsWith ( nxtLine , newFileNameHeader ) && utils . startsWith ( afterNxtLine , hunkHeaderPrefix ) ) ||
2549
+
2550
+ ( utils . startsWith ( line , newFileNameHeader ) &&
2551
+ utils . startsWith ( prevLine , oldFileNameHeader ) && utils . startsWith ( nxtLine , hunkHeaderPrefix ) )
2552
+ ) {
2553
+
2554
+ /*
2555
+ * --- Date Timestamp[FractionalSeconds] TimeZone
2556
+ * --- 2002-02-21 23:30:39.942229878 -0800
2557
+ */
2558
+ if ( currentFile && ! currentFile . oldName &&
2559
+ utils . startsWith ( line , '--- ' ) && ( values = getSrcFilename ( line , config ) ) ) {
2560
+ currentFile . oldName = values ;
2561
+ currentFile . language = getExtension ( currentFile . oldName , currentFile . language ) ;
2562
+ return ;
2563
+ }
2564
+
2565
+ /*
2566
+ * +++ Date Timestamp[FractionalSeconds] TimeZone
2567
+ * +++ 2002-02-21 23:30:39.942229878 -0800
2568
+ */
2569
+ if ( currentFile && ! currentFile . newName &&
2570
+ utils . startsWith ( line , '+++ ' ) && ( values = getDstFilename ( line , config ) ) ) {
2571
+ currentFile . newName = values ;
2572
+ currentFile . language = getExtension ( currentFile . newName , currentFile . language ) ;
2573
+ return ;
2574
+ }
2537
2575
2538
- /*
2539
- * +++ Date Timestamp[FractionalSeconds] TimeZone
2540
- * +++ 2002-02-21 23:30:39.942229878 -0800
2541
- */
2542
- if ( currentFile && ! currentFile . newName &&
2543
- utils . startsWith ( line , '+++ ' ) && ( values = getDstFilename ( line , config ) ) ) {
2544
- currentFile . newName = values ;
2545
- currentFile . language = getExtension ( currentFile . newName , currentFile . language ) ;
2546
- return ;
2547
2576
}
2548
2577
2549
- if ( currentFile && utils . startsWith ( line , '@' ) ) {
2578
+ if ( currentFile && utils . startsWith ( line , hunkHeaderPrefix ) ) {
2550
2579
startBlock ( line ) ;
2551
2580
return ;
2552
2581
}
@@ -2562,13 +2591,6 @@ var Hogan = {};
2562
2591
return ;
2563
2592
}
2564
2593
2565
- if (
2566
- ( currentFile && currentFile . blocks . length ) ||
2567
- ( currentBlock && currentBlock . lines . length )
2568
- ) {
2569
- startFile ( ) ;
2570
- }
2571
-
2572
2594
/*
2573
2595
* Git diffs provide more information regarding files modes, renames, copies,
2574
2596
* commits between changes and similarity indexes
@@ -3854,7 +3876,7 @@ module.exports = global.browserTemplates;
3854
3876
return result ;
3855
3877
}
3856
3878
3857
- return str . indexOf ( start ) === 0 ;
3879
+ return str && str . indexOf ( start ) === 0 ;
3858
3880
} ;
3859
3881
3860
3882
Utils . prototype . valueOrEmpty = function ( value ) {
0 commit comments