157 lines
17 KiB
JavaScript
157 lines
17 KiB
JavaScript
|
/*istanbul ignore start*/'use strict';
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
exports. /*istanbul ignore end*/structuredPatch = structuredPatch;
|
||
|
/*istanbul ignore start*/exports. /*istanbul ignore end*/createTwoFilesPatch = createTwoFilesPatch;
|
||
|
/*istanbul ignore start*/exports. /*istanbul ignore end*/createPatch = createPatch;
|
||
|
|
||
|
var /*istanbul ignore start*/_line = require('../diff/line') /*istanbul ignore end*/;
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
||
|
|
||
|
/*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, _line.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);
|
||
|
if (lines.length == 0 && !oldEOFNewline) {
|
||
|
// 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');
|
||
|
} else if (!oldEOFNewline || !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;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9wYXRjaC9jcmVhdGUuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O2dDQUVnQixlLEdBQUEsZTt5REFpR0EsbUIsR0FBQSxtQjt5REF3QkEsVyxHQUFBLFc7O0FBM0hoQixJLHlCQUFBLCtCLHdCQUFBOzs7Ozt1QkFFTyxTQUFTLGVBQVQsQ0FBeUIsV0FBekIsRUFBc0MsV0FBdEMsRUFBbUQsTUFBbkQsRUFBMkQsTUFBM0QsRUFBbUUsU0FBbkUsRUFBOEUsU0FBOUUsRUFBeUYsT0FBekYsRUFBa0c7QUFDdkcsTUFBSSxDQUFDLE9BQUwsRUFBYztBQUNaLGNBQVUsRUFBVjtBQUNEO0FBQ0QsTUFBSSxPQUFPLFFBQVEsT0FBZixLQUEyQixXQUEvQixFQUE0QztBQUMxQyxZQUFRLE9BQVIsR0FBa0IsQ0FBbEI7QUFDRDs7QUFFRCxNQUFNLE8seUJBQU8sb0Isd0JBQUEsQ0FBVSxNQUFWLEVBQWtCLE1BQWxCLEVBQTBCLE9BQTFCLENBQWI7QUFDQSxPQUFLLElBQUwsQ0FBV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