Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

Commit 3ab1df8

Browse files
committed
Less restrictive regexp & 'should handle patches without Index' test
1 parent e9a40c3 commit 3ab1df8

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/patch/parse.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff 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 (/^(Index:|diff|\-\-\-|\+\+\+)\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];

test/patch/apply.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)