Skip to content

Commit 6c2844e

Browse files
committed
Ignore header content when applying patch
Fixes #49
1 parent 1c07d36 commit 6c2844e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Diff for: diff.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,16 @@
458458
applyPatch: function(oldStr, uniDiff) {
459459
var diffstr = uniDiff.split('\n');
460460
var diff = [];
461-
var remEOFNL = false,
461+
var i = 0,
462+
remEOFNL = false,
462463
addEOFNL = false;
463464

464-
for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) {
465+
// Skip to the first change chunk
466+
while (i < diffstr.length && !/^@@/.test(diffstr[i])) {
467+
i++;
468+
}
469+
470+
for (; i < diffstr.length; i++) {
465471
if (diffstr[i][0] === '@') {
466472
var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/);
467473
diff.unshift({

Diff for: test/applyPatch.js

+20
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,24 @@ describe('#applyPatch', function() {
269269
+ '+++ testFileName\tNew Header\n';
270270
diff.applyPatch(oldFile, diffFile).should.equal(oldFile);
271271
});
272+
273+
it('should apply patches that lack an index header', function() {
274+
diff.applyPatch(
275+
'line2\n'+
276+
'line3\n'+
277+
'line5\n',
278+
279+
'--- test\theader1\n'
280+
+ '+++ test\theader2\n'
281+
+ '@@ -1,3 +1,4 @@\n'
282+
+ ' line2\n'
283+
+ ' line3\n'
284+
+ '+line4\n'
285+
+ ' line5\n')
286+
.should.equal(
287+
'line2\n'
288+
+ 'line3\n'
289+
+ 'line4\n'
290+
+ 'line5\n');
291+
});
272292
});

0 commit comments

Comments
 (0)