268 lines
23 KiB
JavaScript
268 lines
23 KiB
JavaScript
|
/*istanbul ignore start*/
|
||
|
"use strict";
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.structuredPatch = structuredPatch;
|
||
|
exports.formatPatch = formatPatch;
|
||
|
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) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
||
|
|
||
|
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
||
|
|
||
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
||
|
|
||
|
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
||
|
|
||
|
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
||
|
|
||
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; 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*/
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
(_curRange =
|
||
|
/*istanbul ignore end*/
|
||
|
curRange).push.apply(
|
||
|
/*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*/
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
(_curRange2 =
|
||
|
/*istanbul ignore end*/
|
||
|
curRange).push.apply(
|
||
|
/*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*/
|
||
|
|
||
|
/*istanbul ignore end*/
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
(_curRange3 =
|
||
|
/*istanbul ignore end*/
|
||
|
curRange).push.apply(
|
||
|
/*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 && oldStr.length > 0) {
|
||
|
// special case: old has no eol and no trailing context; no-nl can end up before adds
|
||
|
// however, if the old file is empty, do not output the no-nl line
|
||
|
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 formatPatch(diff) {
|
||
|
var ret = [];
|
||
|
|
||
|
if (diff.oldFileName == diff.newFileName) {
|
||
|
ret.push('Index: ' + diff.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]; // Unified Diff Format quirk: If the chunk size is 0,
|
||
|
// the first number is one lower than one would expect.
|
||
|
// https://www.artima.com/weblogs/viewpost.jsp?thread=164293
|
||
|
|
||
|
if (hunk.oldLines === 0) {
|
||
|
hunk.oldStart -= 1;
|
||
|
}
|
||
|
|
||
|
if (hunk.newLines === 0) {
|
||
|
hunk.newStart -= 1;
|
||
|
}
|
||
|
|
||
|
ret.push('@@ -' + hunk.oldStart + ',' + hunk.oldLines + ' +' + hunk.newStart + ',' + hunk.newLines + ' @@');
|
||
|
ret.push.apply(ret, hunk.lines);
|
||
|
}
|
||
|
|
||
|
return ret.join('\n') + '\n';
|
||
|
}
|
||
|
|
||
|
function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) {
|
||
|
return formatPatch(structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options));
|
||
|
}
|
||
|
|
||
|
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9wYXRjaC9jcmVhdGUuanMiXSwibmFtZXMiOlsic3RydWN0dXJlZFBhdGNoIiwib2xkRmlsZU5hbWUiLCJuZXdGaWxlTmFtZSIsIm9sZFN0ciIsIm5ld1N0ciIsIm9sZEhlYWRlciIsIm5ld0hlYWRlciIsIm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