149 lines
18 KiB
JavaScript
149 lines
18 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;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9wYXRjaC9jcmVhdGUuanMiXSwibmFtZXMiOlsic3RydWN0dXJlZFBhdGNoIiwiY3JlYXRlVHdvRmlsZXNQYXRjaCIsImNyZWF0ZVBhdGNoIiwib2xkRmlsZU5hbWUiLCJuZXdGaWxlTmFtZSIsIm9sZFN0ciIsIm5ld1N0ciIsIm9sZEhlYWRlciIsIm5ld0hlYWRlciIsIm9wdGlvbnMiLCJjb250ZXh0IiwiZGlmZiIsInB1c2giLCJ2YWx1ZSIsImxpbmVzIiwiY29udGV4dExpbmVzIiwibWFwIiwiZW50cnkiLCJodW5rcyIsIm9sZFJhbmdlU3RhcnQiLCJuZXdSYW5nZVN0YXJ0IiwiY3VyUmFuZ2UiLCJvbGRMaW5lIiwibmV3TGluZSIsImkiLCJjdXJyZW50IiwicmVwbGFjZSIsInNwbGl0IiwiYWRkZWQiLCJyZW1vdmVkIiwicHJldiIsInNsaWNlIiwibGVuZ3RoIiwiY29udGV4dFNpemUiLCJNYXRoIiwibWluIiwiaHVuayIsIm9sZFN0YXJ0Iiwib2xkTGluZXMiLCJuZXdTdGFydCIsIm5ld0xpbmVzIiwib2xkRU9GTmV3bGluZSIsInRlc3QiLCJuZXdFT0ZOZXds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