feature: new diffMaxChanges in parser config

If `diffMaxChanges` is set in parser config, and its value is exceeded for a given file diff, then parsing for this file is stopped, and `isTooBig` is set to `true` in the `DiffFile`
This commit is contained in:
Pierric Cistac 2021-02-25 13:32:03 -05:00
parent f046d183df
commit ea9c1fee48
No known key found for this signature in database
GPG key ID: 9D98B3AF93C7362D
3 changed files with 158 additions and 0 deletions

View file

@ -1975,5 +1975,149 @@ describe('DiffParser', () => {
]
`);
});
it('should stop parsing file and mark it as `isTooBig` if `diffMaxChanges` is set and excedeed', () => {
const diff =
'diff --git a/src/core/init.js b/src/core/init.js\n' +
'index e49196a..50f310c 100644\n' +
'--- a/src/core/init.js\n' +
'+++ b/src/core/init.js\n' +
'@@ -101,7 +101,7 @@ var rootjQuery,\n' +
' // HANDLE: $(function)\n' +
' // Shortcut for document ready\n' +
' } else if ( jQuery.isFunction( selector ) ) {\n' +
'- return typeof rootjQuery.ready !== "undefined" ?\n' +
'+ return rootjQuery.ready !== undefined ?\n' +
' rootjQuery.ready( selector ) :\n' +
' // Execute immediately if ready is not present\n' +
' selector( jQuery );\n' +
'diff --git a/src/event.js b/src/event.js\n' +
'index 7336f4d..6183f70 100644\n' +
'--- a/src/event.js\n' +
'+++ b/src/event.js\n' +
'@@ -1,6 +1,5 @@\n' +
' define([\n' +
' "./core",\n' +
'- "./var/strundefined",\n' +
' "./var/rnotwhite",\n' +
' "./var/hasOwn",\n' +
' "./var/slice",\n';
const result = parse(diff, { diffMaxChanges: 1 });
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"addedLines": 1,
"blocks": Array [
Object {
"header": "@@ -101,7 +101,7 @@ var rootjQuery,",
"lines": Array [
Object {
"content": " // HANDLE: $(function)",
"newNumber": 101,
"oldNumber": 101,
"type": "context",
},
Object {
"content": " // Shortcut for document ready",
"newNumber": 102,
"oldNumber": 102,
"type": "context",
},
Object {
"content": " } else if ( jQuery.isFunction( selector ) ) {",
"newNumber": 103,
"oldNumber": 103,
"type": "context",
},
Object {
"content": "- return typeof rootjQuery.ready !== \\"undefined\\" ?",
"newNumber": undefined,
"oldNumber": 104,
"type": "delete",
},
Object {
"content": "+ return rootjQuery.ready !== undefined ?",
"newNumber": 104,
"oldNumber": undefined,
"type": "insert",
},
],
"newStartLine": 101,
"oldStartLine": 101,
"oldStartLine2": null,
},
],
"checksumAfter": "50f310c",
"checksumBefore": "e49196a",
"deletedLines": 1,
"isCombined": false,
"isGitDiff": true,
"isTooBig": true,
"language": "js",
"mode": "100644",
"newName": "src/core/init.js",
"oldName": "src/core/init.js",
},
Object {
"addedLines": 0,
"blocks": Array [
Object {
"header": "@@ -1,6 +1,5 @@",
"lines": Array [
Object {
"content": " define([",
"newNumber": 1,
"oldNumber": 1,
"type": "context",
},
Object {
"content": " \\"./core\\",",
"newNumber": 2,
"oldNumber": 2,
"type": "context",
},
Object {
"content": "- \\"./var/strundefined\\",",
"newNumber": undefined,
"oldNumber": 3,
"type": "delete",
},
Object {
"content": " \\"./var/rnotwhite\\",",
"newNumber": 3,
"oldNumber": 4,
"type": "context",
},
Object {
"content": " \\"./var/hasOwn\\",",
"newNumber": 4,
"oldNumber": 5,
"type": "context",
},
Object {
"content": " \\"./var/slice\\",",
"newNumber": 5,
"oldNumber": 6,
"type": "context",
},
],
"newStartLine": 1,
"oldStartLine": 1,
"oldStartLine2": null,
},
],
"checksumAfter": "6183f70",
"checksumBefore": "7336f4d",
"deletedLines": 1,
"isCombined": false,
"isGitDiff": true,
"language": "js",
"mode": "100644",
"newName": "src/event.js",
"oldName": "src/event.js",
},
]
`);
});
});
});

View file

@ -4,6 +4,7 @@ import { escapeForRegExp } from './utils';
export interface DiffParserConfig {
srcPrefix?: string;
dstPrefix?: string;
diffMaxChanges?: number;
}
function getExtension(filename: string, language: string): string {
@ -225,6 +226,13 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
currentLine.newNumber = newLine++;
}
currentBlock.lines.push(currentLine);
if (
typeof config.diffMaxChanges === 'number' &&
currentFile.addedLines + currentFile.deletedLines > config.diffMaxChanges
) {
currentFile.isTooBig = true;
currentFile.blocks = [];
}
}
/*
@ -299,6 +307,11 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
startFile();
}
// Ignore remaining diff for current file if marked as too big
if (currentFile?.isTooBig) {
return;
}
/*
* We need to make sure that we have the three lines of the header.
* This avoids cases like the ones described in:

View file

@ -62,6 +62,7 @@ export interface DiffFile extends DiffFileName {
isCopy?: boolean;
isRename?: boolean;
isBinary?: boolean;
isTooBig?: boolean;
unchangedPercentage?: number;
changedPercentage?: number;
checksumBefore?: string | string[];