Skip to content

Commit

Permalink
Merge pull request #365 from huggingface/diff-max-lines
Browse files Browse the repository at this point in the history
feature: bypass parsing/rendering of file diff if specific threshold is crossed
  • Loading branch information
rtfpessoa authored Mar 4, 2021
2 parents e55d145 + a65a3b9 commit 38a9d8f
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ The HTML output accepts a Javascript object with configuration. Possible options
- `outputFormat`: the format of the output data: `'line-by-line'` or `'side-by-side'`, default is `'line-by-line'`
- `drawFileList`: show a file list before the diff: `true` or `false`, default is `true`
- `diffStyle`: show differences level in each line: `word` or `char`, default is `word`
- `diffMaxChanges`: number of changed lines after which a file diff is deemed as too big and not displayed, default is
`undefined`
- `diffTooBigMessage`: function allowing to customize the message in case of file diff too big (if `diffMaxChanges` is
set)
- `matching`: matching level: `'lines'` for matching lines, `'words'` for matching lines and words or `'none'`, default
is `none`
- `matchWordsThreshold`: similarity threshold for word matching, default is `0.25`
Expand Down
226 changes: 226 additions & 0 deletions src/__tests__/diff-parser-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1975,5 +1975,231 @@ describe('DiffParser', () => {
]
`);
});

it('should work when `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": 0,
"blocks": Array [
Object {
"header": "Diff too big to be displayed",
"lines": Array [],
"newStartLine": 0,
"oldStartLine": 0,
"oldStartLine2": null,
},
],
"checksumAfter": "50f310c",
"checksumBefore": "e49196a",
"deletedLines": 0,
"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",
},
]
`);
});

it('should work when `diffMaxChanges` is set and excedeed, and `diffTooBigMessage` is set', () => {
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, diffTooBigMessage: (i: number) => `Custom ${i}` });
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"addedLines": 0,
"blocks": Array [
Object {
"header": "Custom 0",
"lines": Array [],
"newStartLine": 0,
"oldStartLine": 0,
"oldStartLine2": null,
},
],
"checksumAfter": "50f310c",
"checksumBefore": "e49196a",
"deletedLines": 0,
"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",
},
]
`);
});
});
});
59 changes: 59 additions & 0 deletions src/__tests__/line-by-line-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,65 @@ describe('LineByLineRenderer', () => {
</div>"
`);
});

it('should work for too big file diff', () => {
const exampleJson = [
{
blocks: [
{
header: '<a href="http://example.com">Custom link to render</a>',
lines: [],
newStartLine: 0,
oldStartLine: 0,
oldStartLine2: undefined,
},
],
deletedLines: 0,
addedLines: 0,
oldName: 'sample',
language: 'js',
newName: 'sample',
isCombined: false,
isGitDiff: false,
isTooBig: true,
},
];

const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils);
const html = lineByLineRenderer.render(exampleJson);
expect(html).toMatchInlineSnapshot(`
"<div class=\\"d2h-wrapper\\">
<div id=\\"d2h-675094\\" class=\\"d2h-file-wrapper\\" data-lang=\\"js\\">
<div class=\\"d2h-file-header\\">
<span class=\\"d2h-file-name-wrapper\\">
<svg aria-hidden=\\"true\\" class=\\"d2h-icon\\" height=\\"16\\" version=\\"1.1\\" viewBox=\\"0 0 12 16\\" width=\\"12\\">
<path d=\\"M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z\\"></path>
</svg> <span class=\\"d2h-file-name\\">sample</span>
<span class=\\"d2h-tag d2h-changed d2h-changed-tag\\">CHANGED</span></span>
<label class=\\"d2h-file-collapse\\">
<input class=\\"d2h-file-collapse-input\\" type=\\"checkbox\\" name=\\"viewed\\" value=\\"viewed\\">
Viewed
</label>
</div>
<div class=\\"d2h-file-diff\\">
<div class=\\"d2h-code-wrapper\\">
<table class=\\"d2h-diff-table\\">
<tbody class=\\"d2h-diff-tbody\\">
<tr>
<td class=\\"d2h-code-linenumber d2h-info\\"></td>
<td class=\\"d2h-info\\">
<div class=\\"d2h-code-line d2h-info\\"><a href=\\"http://example.com\\">Custom link to render</a></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>"
`);
});
});

describe('_generateFileHtml', () => {
Expand Down
75 changes: 75 additions & 0 deletions src/__tests__/side-by-side-printer-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,81 @@ describe('SideBySideRenderer', () => {
</div>"
`);
});

it('should work for too big file diff', () => {
const exampleJson = [
{
blocks: [
{
header: '<a href="http://example.com">Custom link to render</a>',
lines: [],
newStartLine: 0,
oldStartLine: 0,
oldStartLine2: undefined,
},
],
deletedLines: 0,
addedLines: 0,
oldName: 'sample',
language: 'js',
newName: 'sample',
isCombined: false,
isGitDiff: false,
isTooBig: true,
},
];

const hoganUtils = new HoganJsUtils({});
const sideBySideRenderer = new SideBySideRenderer(hoganUtils);
const html = sideBySideRenderer.render(exampleJson);
expect(html).toMatchInlineSnapshot(`
"<div class=\\"d2h-wrapper\\">
<div id=\\"d2h-675094\\" class=\\"d2h-file-wrapper\\" data-lang=\\"js\\">
<div class=\\"d2h-file-header\\">
<span class=\\"d2h-file-name-wrapper\\">
<svg aria-hidden=\\"true\\" class=\\"d2h-icon\\" height=\\"16\\" version=\\"1.1\\" viewBox=\\"0 0 12 16\\" width=\\"12\\">
<path d=\\"M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z\\"></path>
</svg> <span class=\\"d2h-file-name\\">sample</span>
<span class=\\"d2h-tag d2h-changed d2h-changed-tag\\">CHANGED</span></span>
<label class=\\"d2h-file-collapse\\">
<input class=\\"d2h-file-collapse-input\\" type=\\"checkbox\\" name=\\"viewed\\" value=\\"viewed\\">
Viewed
</label>
</div>
<div class=\\"d2h-files-diff\\">
<div class=\\"d2h-file-side-diff\\">
<div class=\\"d2h-code-wrapper\\">
<table class=\\"d2h-diff-table\\">
<tbody class=\\"d2h-diff-tbody\\">
<tr>
<td class=\\"d2h-code-side-linenumber d2h-info\\"></td>
<td class=\\"d2h-info\\">
<div class=\\"d2h-code-side-line d2h-info\\"><a href=\\"http://example.com\\">Custom link to render</a></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class=\\"d2h-file-side-diff\\">
<div class=\\"d2h-code-wrapper\\">
<table class=\\"d2h-diff-table\\">
<tbody class=\\"d2h-diff-tbody\\">
<tr>
<td class=\\"d2h-code-side-linenumber d2h-info\\"></td>
<td class=\\"d2h-info\\">
<div class=\\"d2h-code-side-line d2h-info\\"></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>"
`);
});
});

describe('processLines', () => {
Expand Down
Loading

0 comments on commit 38a9d8f

Please sign in to comment.