debian-mirror-gitlab/app/assets/javascripts/ide/lib/diff/diff.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

37 lines
953 B
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import { diffLines } from 'diff';
2020-06-23 00:09:42 +05:30
import { defaultDiffOptions } from '../editor_options';
2018-05-09 12:01:36 +05:30
export const computeDiff = (originalContent, newContent) => {
2020-06-23 00:09:42 +05:30
// prevent EOL changes from highlighting the entire file
const changes = diffLines(
originalContent.replace(/\r\n/g, '\n'),
newContent.replace(/\r\n/g, '\n'),
defaultDiffOptions,
);
2018-05-09 12:01:36 +05:30
let lineNumber = 1;
return changes.reduce((acc, change) => {
2021-03-08 18:12:59 +05:30
const findOnLine = acc.find((c) => c.lineNumber === lineNumber);
2018-05-09 12:01:36 +05:30
if (findOnLine) {
Object.assign(findOnLine, change, {
modified: true,
2018-12-13 13:39:08 +05:30
endLineNumber: lineNumber + change.count - 1,
2018-05-09 12:01:36 +05:30
});
} else if ('added' in change || 'removed' in change) {
2020-05-24 23:13:21 +05:30
acc.push({
...change,
lineNumber,
modified: undefined,
endLineNumber: lineNumber + change.count - 1,
});
2018-05-09 12:01:36 +05:30
}
if (!change.removed) {
lineNumber += change.count;
}
return acc;
}, []);
};