Merge pull request #478 from rtfpessoa/support-unix-diff-binaries

support unix diff binaires
This commit is contained in:
Rodrigo Fernandes 2023-01-20 23:06:53 +00:00 committed by GitHub
commit 09cbe87595
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 10 deletions

View file

@ -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' +
'-<a>test</a>\n' +
'+<a>new test</a>\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": "-<a>test</a>",
"newNumber": undefined,
"oldNumber": 1,
"type": "delete",
},
{
"content": "+<a>new test</a>",
"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",
},
]
`);
});
});
});

View file

@ -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 &&

View file

@ -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"