Skip to content

Commit c73b432

Browse files
committed
Ignore unexpected content outside of strict mode
Fixes #76
1 parent 429eb33 commit c73b432

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/patch/parse.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ export function parsePatch(uniDiff, options = {}) {
77
let index = {};
88
list.push(index);
99

10+
// Ignore any leading junk
11+
while (i < diffstr.length) {
12+
if ((/^Index:/.test(diffstr[i])) || (/^@@/.test(diffstr[i]))) {
13+
break;
14+
}
15+
i++;
16+
}
17+
1018
let header = (/^Index: (.*)/.exec(diffstr[i]));
1119
if (header) {
1220
index.index = header[1];
@@ -31,10 +39,11 @@ export function parsePatch(uniDiff, options = {}) {
3139
break;
3240
} else if (/^@@/.test(diffstr[i])) {
3341
index.hunks.push(parseHunk());
34-
} else if (!diffstr[i]) {
35-
i++;
36-
} else {
42+
} else if (diffstr[i] && options.strict) {
43+
// Ignore unexpected content unless in strict mode
3744
throw new Error('Unknown line ' + (i + 1) + ' ' + JSON.stringify(diffstr[i]));
45+
} else {
46+
i++;
3847
}
3948
}
4049
}

test/patch/parse.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,17 @@ Index: test2
238238
parsePatch(`@@ -1,4 +1 @@`, {strict: true});
239239
}).to['throw']('Removed line count did not match for hunk at line 1');
240240
});
241-
it('should throw on invalid input', function() {
241+
242+
it('should not throw on invalid input', function() {
243+
expect(parsePatch('blit\nblat\nIndex: foo\nfoo'))
244+
.to.eql([{
245+
hunks: [],
246+
index: 'foo'
247+
}]);
248+
});
249+
it('should throw on invalid input in strict mode', function() {
242250
expect(function() {
243-
parsePatch('Index: foo\nfoo');
251+
parsePatch('Index: foo\nfoo', {strict: true});
244252
}).to['throw'](/Unknown line 2 "foo"/);
245253
});
246254
});

0 commit comments

Comments
 (0)