248 lines
21 KiB
JavaScript
248 lines
21 KiB
JavaScript
|
/*istanbul ignore start*/
|
||
|
"use strict";
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.structuredPatch = structuredPatch;
|
||
|
exports.createTwoFilesPatch = createTwoFilesPatch;
|
||
|
exports.createPatch = createPatch;
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
var
|
||
|
/*istanbul ignore start*/
|
||
|
_line = require("../diff/line")
|
||
|
/*istanbul ignore end*/
|
||
|
;
|
||
|
|
||
|
/*istanbul ignore start*/ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
|
||
|
|
||
|
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
|
||
|
|
||
|
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
|
||
|
|
||
|
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) {
|
||
|
if (!options) {
|
||
|
options = {};
|
||
|
}
|
||
|
|
||
|
if (typeof options.context === 'undefined') {
|
||
|
options.context = 4;
|
||
|
}
|
||
|
|
||
|
var diff =
|
||
|
/*istanbul ignore start*/
|
||
|
(0,
|
||
|
/*istanbul ignore end*/
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
_line
|
||
|
/*istanbul ignore end*/
|
||
|
.
|
||
|
/*istanbul ignore start*/
|
||
|
diffLines)
|
||
|
/*istanbul ignore end*/
|
||
|
(oldStr, newStr, options);
|
||
|
diff.push({
|
||
|
value: '',
|
||
|
lines: []
|
||
|
}); // Append an empty value to make cleanup easier
|
||
|
|
||
|
function contextLines(lines) {
|
||
|
return lines.map(function (entry) {
|
||
|
return ' ' + entry;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
var hunks = [];
|
||
|
var oldRangeStart = 0,
|
||
|
newRangeStart = 0,
|
||
|
curRange = [],
|
||
|
oldLine = 1,
|
||
|
newLine = 1;
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
var _loop = function _loop(
|
||
|
/*istanbul ignore end*/
|
||
|
i) {
|
||
|
var current = diff[i],
|
||
|
lines = current.lines || current.value.replace(/\n$/, '').split('\n');
|
||
|
current.lines = lines;
|
||
|
|
||
|
if (current.added || current.removed) {
|
||
|
/*istanbul ignore start*/
|
||
|
var _curRange;
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
// If we have previous context, start with that
|
||
|
if (!oldRangeStart) {
|
||
|
var prev = diff[i - 1];
|
||
|
oldRangeStart = oldLine;
|
||
|
newRangeStart = newLine;
|
||
|
|
||
|
if (prev) {
|
||
|
curRange = options.context > 0 ? contextLines(prev.lines.slice(-options.context)) : [];
|
||
|
oldRangeStart -= curRange.length;
|
||
|
newRangeStart -= curRange.length;
|
||
|
}
|
||
|
} // Output our changes
|
||
|
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
(_curRange =
|
||
|
/*istanbul ignore end*/
|
||
|
curRange).push.
|
||
|
/*istanbul ignore start*/
|
||
|
apply
|
||
|
/*istanbul ignore end*/
|
||
|
(
|
||
|
/*istanbul ignore start*/
|
||
|
_curRange
|
||
|
/*istanbul ignore end*/
|
||
|
,
|
||
|
/*istanbul ignore start*/
|
||
|
_toConsumableArray(
|
||
|
/*istanbul ignore end*/
|
||
|
lines.map(function (entry) {
|
||
|
return (current.added ? '+' : '-') + entry;
|
||
|
}))); // Track the updated file position
|
||
|
|
||
|
|
||
|
if (current.added) {
|
||
|
newLine += lines.length;
|
||
|
} else {
|
||
|
oldLine += lines.length;
|
||
|
}
|
||
|
} else {
|
||
|
// Identical context lines. Track line changes
|
||
|
if (oldRangeStart) {
|
||
|
// Close out any changes that have been output (or join overlapping)
|
||
|
if (lines.length <= options.context * 2 && i < diff.length - 2) {
|
||
|
/*istanbul ignore start*/
|
||
|
var _curRange2;
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
// Overlapping
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
(_curRange2 =
|
||
|
/*istanbul ignore end*/
|
||
|
curRange).push.
|
||
|
/*istanbul ignore start*/
|
||
|
apply
|
||
|
/*istanbul ignore end*/
|
||
|
(
|
||
|
/*istanbul ignore start*/
|
||
|
_curRange2
|
||
|
/*istanbul ignore end*/
|
||
|
,
|
||
|
/*istanbul ignore start*/
|
||
|
_toConsumableArray(
|
||
|
/*istanbul ignore end*/
|
||
|
contextLines(lines)));
|
||
|
} else {
|
||
|
/*istanbul ignore start*/
|
||
|
var _curRange3;
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
// end the range and output
|
||
|
var contextSize = Math.min(lines.length, options.context);
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
(_curRange3 =
|
||
|
/*istanbul ignore end*/
|
||
|
curRange).push.
|
||
|
/*istanbul ignore start*/
|
||
|
apply
|
||
|
/*istanbul ignore end*/
|
||
|
(
|
||
|
/*istanbul ignore start*/
|
||
|
_curRange3
|
||
|
/*istanbul ignore end*/
|
||
|
,
|
||
|
/*istanbul ignore start*/
|
||
|
_toConsumableArray(
|
||
|
/*istanbul ignore end*/
|
||
|
contextLines(lines.slice(0, contextSize))));
|
||
|
|
||
|
var hunk = {
|
||
|
oldStart: oldRangeStart,
|
||
|
oldLines: oldLine - oldRangeStart + contextSize,
|
||
|
newStart: newRangeStart,
|
||
|
newLines: newLine - newRangeStart + contextSize,
|
||
|
lines: curRange
|
||
|
};
|
||
|
|
||
|
if (i >= diff.length - 2 && lines.length <= options.context) {
|
||
|
// EOF is inside this hunk
|
||
|
var oldEOFNewline = /\n$/.test(oldStr);
|
||
|
var newEOFNewline = /\n$/.test(newStr);
|
||
|
var noNlBeforeAdds = lines.length == 0 && curRange.length > hunk.oldLines;
|
||
|
|
||
|
if (!oldEOFNewline && noNlBeforeAdds) {
|
||
|
// special case: old has no eol and no trailing context; no-nl can end up before adds
|
||
|
curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file');
|
||
|
}
|
||
|
|
||
|
if (!oldEOFNewline && !noNlBeforeAdds || !newEOFNewline) {
|
||
|
curRange.push('\\ No newline at end of file');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
hunks.push(hunk);
|
||
|
oldRangeStart = 0;
|
||
|
newRangeStart = 0;
|
||
|
curRange = [];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
oldLine += lines.length;
|
||
|
newLine += lines.length;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
for (var i = 0; i < diff.length; i++) {
|
||
|
/*istanbul ignore start*/
|
||
|
_loop(
|
||
|
/*istanbul ignore end*/
|
||
|
i);
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
oldFileName: oldFileName,
|
||
|
newFileName: newFileName,
|
||
|
oldHeader: oldHeader,
|
||
|
newHeader: newHeader,
|
||
|
hunks: hunks
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) {
|
||
|
var diff = structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options);
|
||
|
var ret = [];
|
||
|
|
||
|
if (oldFileName == newFileName) {
|
||
|
ret.push('Index: ' + oldFileName);
|
||
|
}
|
||
|
|
||
|
ret.push('===================================================================');
|
||
|
ret.push('--- ' + diff.oldFileName + (typeof diff.oldHeader === 'undefined' ? '' : '\t' + diff.oldHeader));
|
||
|
ret.push('+++ ' + diff.newFileName + (typeof diff.newHeader === 'undefined' ? '' : '\t' + diff.newHeader));
|
||
|
|
||
|
for (var i = 0; i < diff.hunks.length; i++) {
|
||
|
var hunk = diff.hunks[i];
|
||
|
ret.push('@@ -' + hunk.oldStart + ',' + hunk.oldLines + ' +' + hunk.newStart + ',' + hunk.newLines + ' @@');
|
||
|
ret.push.apply(ret, hunk.lines);
|
||
|
}
|
||
|
|
||
|
return ret.join('\n') + '\n';
|
||
|
}
|
||
|
|
||
|
function createPatch(fileName, oldStr, newStr, oldHeader, newHeader, options) {
|
||
|
return createTwoFilesPatch(fileName, fileName, oldStr, newStr, oldHeader, newHeader, options);
|
||
|
}
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9wYXRjaC9jcmVhdGUuanMiXSwibmFtZXMiOlsic3RydWN0dXJlZFBhdGNoIiwib2xkRmlsZU5hbWUiLCJuZXdGaWxlTmFtZSIsIm9sZFN0ciIsIm5ld1N0ciIsIm9sZEhlYWRlciIsIm5ld0hlYWRlciIsIm9wdGlvbnMiLCJjb250ZXh0IiwiZGlmZiIsImRpZmZMaW5lcyIsInB1c2giLCJ2YWx1ZSIsImxpbmVzIiwiY29udGV4dExpbmVzIiwibWFwIiwiZW50cnkiLCJodW5rcyIsIm9sZFJhbmdlU3RhcnQiLCJuZXdSYW5nZVN0YXJ0IiwiY3VyUmFuZ2UiLCJvbGRMaW5lIiwibmV3TGluZSIsImkiLCJjdXJyZW50IiwicmVwbGFjZSIsInNwbGl0IiwiYWRkZWQiLCJyZW1vdmVkIiwicHJldiIsInNsaWNlIiwibGVuZ3RoIiwiY29udGV4dFNpemUiLCJNYXRoIiwibWluIiwiaHVuayIsIm9sZFN0YXJ0Iiwib2xkTGluZXMiLCJuZXdTdGFydCIsIm5ld0xpbmVzIiwib2xkRU9GTmV3bGluZSIsInRlc3QiLCJuZXdFT0ZOZXdsaW5lIiwibm9ObEJlZm9yZUFkZHMiLCJzcGxpY2UiLCJjcmVhdGVUd29GaWxlc1BhdGNoIiwicmV0IiwiYXBwbHkiLCJqb2luIiwiY3JlYXRlUGF0Y2giLCJmaWxlTmFtZSJdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7QUFB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