Skip to content

Commit 09cbe87

Browse files
authored
Merge pull request #478 from rtfpessoa/support-unix-diff-binaries
support unix diff binaires
2 parents 24ccfef + a716739 commit 09cbe87

File tree

3 files changed

+112
-10
lines changed

3 files changed

+112
-10
lines changed

src/__tests__/diff-parser-tests.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,5 +2427,96 @@ describe('DiffParser', () => {
24272427
]
24282428
`);
24292429
});
2430+
2431+
it('should parse unix diff with binary file', () => {
2432+
const diff =
2433+
'diff -ur a/htest.html b/htest.html\n' +
2434+
'--- a/htest.html 2023-01-10 09:43:04.284427636 +0800\n' +
2435+
'+++ b/htest.html 2023-01-10 09:43:10.308388990 +0800\n' +
2436+
'@@ -1 +1 @@\n' +
2437+
'-<a>test</a>\n' +
2438+
'+<a>new test</a>\n' +
2439+
'Binary files a/image.gif and b/image.gif differ\n' +
2440+
'diff -ur a/test.json b/test.json\n' +
2441+
'--- a/test.json 2023-01-10 09:43:07.832404870 +0800\n' +
2442+
'+++ b/test.json 2023-01-10 09:43:12.708373605 +0800\n' +
2443+
'@@ -1 +1 @@\n' +
2444+
'-{"list": [1, 2]}\n' +
2445+
'+{"list": [1, 2, 3]}';
2446+
2447+
const result = parse(diff);
2448+
expect(result).toMatchInlineSnapshot(`
2449+
[
2450+
{
2451+
"addedLines": 1,
2452+
"blocks": [
2453+
{
2454+
"header": "@@ -1 +1 @@",
2455+
"lines": [
2456+
{
2457+
"content": "-<a>test</a>",
2458+
"newNumber": undefined,
2459+
"oldNumber": 1,
2460+
"type": "delete",
2461+
},
2462+
{
2463+
"content": "+<a>new test</a>",
2464+
"newNumber": 1,
2465+
"oldNumber": undefined,
2466+
"type": "insert",
2467+
},
2468+
],
2469+
"newStartLine": 1,
2470+
"oldStartLine": 1,
2471+
"oldStartLine2": null,
2472+
},
2473+
],
2474+
"deletedLines": 1,
2475+
"isCombined": false,
2476+
"language": "html",
2477+
"newName": "htest.html",
2478+
"oldName": "htest.html",
2479+
},
2480+
{
2481+
"addedLines": 0,
2482+
"blocks": [],
2483+
"deletedLines": 0,
2484+
"isBinary": true,
2485+
"newName": "image.gif",
2486+
"oldName": "image.gif",
2487+
},
2488+
{
2489+
"addedLines": 1,
2490+
"blocks": [
2491+
{
2492+
"header": "@@ -1 +1 @@",
2493+
"lines": [
2494+
{
2495+
"content": "-{"list": [1, 2]}",
2496+
"newNumber": undefined,
2497+
"oldNumber": 1,
2498+
"type": "delete",
2499+
},
2500+
{
2501+
"content": "+{"list": [1, 2, 3]}",
2502+
"newNumber": 1,
2503+
"oldNumber": undefined,
2504+
"type": "insert",
2505+
},
2506+
],
2507+
"newStartLine": 1,
2508+
"oldStartLine": 1,
2509+
"oldStartLine2": null,
2510+
},
2511+
],
2512+
"deletedLines": 1,
2513+
"isCombined": false,
2514+
"language": "json",
2515+
"newName": "test.json",
2516+
"oldName": "test.json",
2517+
},
2518+
]
2519+
`);
2520+
});
24302521
});
24312522
});

src/diff-parser.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
272272
const nxtLine = diffLines[lineIndex + 1];
273273
const afterNxtLine = diffLines[lineIndex + 2];
274274

275-
if (line.startsWith('diff')) {
275+
if (line.startsWith('diff --git') || line.startsWith('diff --combined')) {
276276
startFile();
277277

278278
// diff --git a/blocked_delta_results.png b/blocked_delta_results.png
@@ -290,6 +290,22 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
290290
return;
291291
}
292292

293+
if (line.startsWith('Binary files') && !currentFile?.isGitDiff) {
294+
startFile();
295+
const unixDiffBinaryStart = /^Binary files "?([a-ciow]\/.+)"? and "?([a-ciow]\/.+)"? differ/;
296+
if ((values = unixDiffBinaryStart.exec(line))) {
297+
possibleOldName = getFilename(values[1], undefined, config.dstPrefix);
298+
possibleNewName = getFilename(values[2], undefined, config.srcPrefix);
299+
}
300+
301+
if (currentFile === null) {
302+
throw new Error('Where is my file !!!');
303+
}
304+
305+
currentFile.isBinary = true;
306+
return;
307+
}
308+
293309
if (
294310
!currentFile || // If we do not have a file yet, we should crete one
295311
(!currentFile.isGitDiff &&

yarn.lock

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,15 +1811,10 @@ caniuse-api@^3.0.0:
18111811
lodash.memoize "^4.1.2"
18121812
lodash.uniq "^4.5.0"
18131813

1814-
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001366:
1815-
version "1.0.30001369"
1816-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001369.tgz#58ca6974acf839a72a02003258a005cbb0cb340d"
1817-
integrity sha512-OY1SBHaodJc4wflDIKnlkdqWzJZd1Ls/2zbVJHBSv3AT7vgOJ58yAhd2CN4d57l2kPJrgMb7P9+N1Mhy4tNSQA==
1818-
1819-
caniuse-lite@^1.0.30001400, caniuse-lite@^1.0.30001407:
1820-
version "1.0.30001420"
1821-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001420.tgz#f62f35f051e0b6d25532cf376776d41e45b47ef6"
1822-
integrity sha512-OnyeJ9ascFA9roEj72ok2Ikp7PHJTKubtEJIQ/VK3fdsS50q4KWy+Z5X0A1/GswEItKX0ctAp8n4SYDE7wTu6A==
1814+
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001366, caniuse-lite@^1.0.30001400, caniuse-lite@^1.0.30001407:
1815+
version "1.0.30001446"
1816+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001446.tgz"
1817+
integrity sha512-fEoga4PrImGcwUUGEol/PoFCSBnSkA9drgdkxXkJLsUBOnJ8rs3zDv6ApqYXGQFOyMPsjh79naWhF4DAxbF8rw==
18231818

18241819
caw@^2.0.0, caw@^2.0.1:
18251820
version "2.0.1"

0 commit comments

Comments
 (0)