File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ export function parsePatch(uniDiff, options = {}) {
2525 i ++ ;
2626 }
2727
28+ // Parse file headers if they are defined. Unified diff requires them, but
29+ // there's no technical issues to have an isolated hunk without file header
2830 parseFileHeader ( index ) ;
2931 parseFileHeader ( index ) ;
3032
@@ -36,7 +38,7 @@ export function parsePatch(uniDiff, options = {}) {
3638
3739 if ( / ^ ( I n d e x : | d i f f | \- \- \- | \+ \+ \+ ) \s / . test ( line ) ) {
3840 break ;
39- } else if ( / ^ @ @ \s / . test ( line ) ) {
41+ } else if ( / ^ @ @ / . test ( line ) ) {
4042 index . hunks . push ( parseHunk ( ) ) ;
4143 } else if ( line && options . strict ) {
4244 // Ignore unexpected content unless in strict mode
@@ -50,7 +52,7 @@ export function parsePatch(uniDiff, options = {}) {
5052 // Parses the --- and +++ headers, if none are found, no lines
5153 // are consumed.
5254 function parseFileHeader ( index ) {
53- let fileHeader = ( / ^ ( \- \- \- | \+ \+ \+ ) \s + ( \S + ) \s ? ( .+ ) / ) . exec ( diffstr [ i ] ) ;
55+ let fileHeader = ( / ^ ( \- \- \- | \+ \+ \+ ) \s + ( \S + ) \s ? ( .+ ? ) \s * $ / ) . exec ( diffstr [ i ] ) ;
5456 if ( fileHeader ) {
5557 let keyPrefix = fileHeader [ 1 ] === '---' ? 'old' : 'new' ;
5658 index [ keyPrefix + 'FileName' ] = fileHeader [ 2 ] ;
Original file line number Diff line number Diff line change @@ -554,5 +554,36 @@ describe('patch/apply', function() {
554554 }
555555 } ) ;
556556 } ) ;
557+ it ( 'should handle patches without Index' , function ( done ) {
558+ const patch =
559+ '===================================================================\n'
560+ + '--- test\theader1\n'
561+ + '+++ test\theader2\n'
562+ + '@@ -1,3 +1,4 @@\n'
563+ + ' line2\n'
564+ + ' line3\n'
565+ + '+line4\n'
566+ + ' line5\n'
567+ + '===================================================================\n'
568+ + '--- test2\theader1\n'
569+ + '+++ test2\theader2\n'
570+ + '@@ -1,3 +1,4 @@\n'
571+ + ' foo2\n'
572+ + ' foo3\n'
573+ + '+foo4\n'
574+ + ' foo5\n' ;
575+
576+ applyPatches ( patch , {
577+ loadFile ( index , callback ) {
578+ callback ( undefined , contents [ index . oldFileName ] ) ;
579+ } ,
580+ patched ( index , content ) {
581+ expect ( content )
582+ . to . equal ( expected [ index . newFileName ] )
583+ . to . not . be . undefined ;
584+ } ,
585+ complete : done
586+ } ) ;
587+ } ) ;
557588 } ) ;
558589} ) ;
You can’t perform that action at this time.
0 commit comments