From a716739a18509d58dcdb77d4da5976a6dc6f7c1e Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Fri, 20 Jan 2023 23:00:34 +0000 Subject: [PATCH] support unix diff binaires --- src/__tests__/diff-parser-tests.ts | 91 ++++++++++++++++++++++++++++++ src/diff-parser.ts | 18 +++++- yarn.lock | 13 ++--- 3 files changed, 112 insertions(+), 10 deletions(-) diff --git a/src/__tests__/diff-parser-tests.ts b/src/__tests__/diff-parser-tests.ts index 671cf55..e3d0722 100644 --- a/src/__tests__/diff-parser-tests.ts +++ b/src/__tests__/diff-parser-tests.ts @@ -2427,5 +2427,96 @@ describe('DiffParser', () => { ] `); }); + + it('should parse unix diff with binary file', () => { + const diff = + 'diff -ur a/htest.html b/htest.html\n' + + '--- a/htest.html 2023-01-10 09:43:04.284427636 +0800\n' + + '+++ b/htest.html 2023-01-10 09:43:10.308388990 +0800\n' + + '@@ -1 +1 @@\n' + + '-test\n' + + '+new test\n' + + 'Binary files a/image.gif and b/image.gif differ\n' + + 'diff -ur a/test.json b/test.json\n' + + '--- a/test.json 2023-01-10 09:43:07.832404870 +0800\n' + + '+++ b/test.json 2023-01-10 09:43:12.708373605 +0800\n' + + '@@ -1 +1 @@\n' + + '-{"list": [1, 2]}\n' + + '+{"list": [1, 2, 3]}'; + + const result = parse(diff); + expect(result).toMatchInlineSnapshot(` + [ + { + "addedLines": 1, + "blocks": [ + { + "header": "@@ -1 +1 @@", + "lines": [ + { + "content": "-test", + "newNumber": undefined, + "oldNumber": 1, + "type": "delete", + }, + { + "content": "+new test", + "newNumber": 1, + "oldNumber": undefined, + "type": "insert", + }, + ], + "newStartLine": 1, + "oldStartLine": 1, + "oldStartLine2": null, + }, + ], + "deletedLines": 1, + "isCombined": false, + "language": "html", + "newName": "htest.html", + "oldName": "htest.html", + }, + { + "addedLines": 0, + "blocks": [], + "deletedLines": 0, + "isBinary": true, + "newName": "image.gif", + "oldName": "image.gif", + }, + { + "addedLines": 1, + "blocks": [ + { + "header": "@@ -1 +1 @@", + "lines": [ + { + "content": "-{"list": [1, 2]}", + "newNumber": undefined, + "oldNumber": 1, + "type": "delete", + }, + { + "content": "+{"list": [1, 2, 3]}", + "newNumber": 1, + "oldNumber": undefined, + "type": "insert", + }, + ], + "newStartLine": 1, + "oldStartLine": 1, + "oldStartLine2": null, + }, + ], + "deletedLines": 1, + "isCombined": false, + "language": "json", + "newName": "test.json", + "oldName": "test.json", + }, + ] + `); + }); }); }); diff --git a/src/diff-parser.ts b/src/diff-parser.ts index f67140b..bf3c108 100644 --- a/src/diff-parser.ts +++ b/src/diff-parser.ts @@ -272,7 +272,7 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil const nxtLine = diffLines[lineIndex + 1]; const afterNxtLine = diffLines[lineIndex + 2]; - if (line.startsWith('diff')) { + if (line.startsWith('diff --git') || line.startsWith('diff --combined')) { startFile(); // diff --git a/blocked_delta_results.png b/blocked_delta_results.png @@ -290,6 +290,22 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil return; } + if (line.startsWith('Binary files') && !currentFile?.isGitDiff) { + startFile(); + const unixDiffBinaryStart = /^Binary files "?([a-ciow]\/.+)"? and "?([a-ciow]\/.+)"? differ/; + if ((values = unixDiffBinaryStart.exec(line))) { + possibleOldName = getFilename(values[1], undefined, config.dstPrefix); + possibleNewName = getFilename(values[2], undefined, config.srcPrefix); + } + + if (currentFile === null) { + throw new Error('Where is my file !!!'); + } + + currentFile.isBinary = true; + return; + } + if ( !currentFile || // If we do not have a file yet, we should crete one (!currentFile.isGitDiff && diff --git a/yarn.lock b/yarn.lock index 10b6070..e331bc1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1811,15 +1811,10 @@ caniuse-api@^3.0.0: lodash.memoize "^4.1.2" lodash.uniq "^4.5.0" -caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001366: - version "1.0.30001369" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001369.tgz#58ca6974acf839a72a02003258a005cbb0cb340d" - integrity sha512-OY1SBHaodJc4wflDIKnlkdqWzJZd1Ls/2zbVJHBSv3AT7vgOJ58yAhd2CN4d57l2kPJrgMb7P9+N1Mhy4tNSQA== - -caniuse-lite@^1.0.30001400, caniuse-lite@^1.0.30001407: - version "1.0.30001420" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001420.tgz#f62f35f051e0b6d25532cf376776d41e45b47ef6" - integrity sha512-OnyeJ9ascFA9roEj72ok2Ikp7PHJTKubtEJIQ/VK3fdsS50q4KWy+Z5X0A1/GswEItKX0ctAp8n4SYDE7wTu6A== +caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001366, caniuse-lite@^1.0.30001400, caniuse-lite@^1.0.30001407: + version "1.0.30001446" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001446.tgz" + integrity sha512-fEoga4PrImGcwUUGEol/PoFCSBnSkA9drgdkxXkJLsUBOnJ8rs3zDv6ApqYXGQFOyMPsjh79naWhF4DAxbF8rw== caw@^2.0.0, caw@^2.0.1: version "2.0.1"