178 lines
16 KiB
JavaScript
178 lines
16 KiB
JavaScript
|
/*istanbul ignore start*/'use strict';
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
exports. /*istanbul ignore end*/applyPatch = applyPatch;
|
||
|
/*istanbul ignore start*/exports. /*istanbul ignore end*/applyPatches = applyPatches;
|
||
|
|
||
|
var /*istanbul ignore start*/_parse = require('./parse') /*istanbul ignore end*/;
|
||
|
|
||
|
var /*istanbul ignore start*/_distanceIterator = require('../util/distance-iterator') /*istanbul ignore end*/;
|
||
|
|
||
|
/*istanbul ignore start*/
|
||
|
var _distanceIterator2 = _interopRequireDefault(_distanceIterator);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
||
|
|
||
|
/*istanbul ignore end*/function applyPatch(source, uniDiff) {
|
||
|
/*istanbul ignore start*/var /*istanbul ignore end*/options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
||
|
|
||
|
if (typeof uniDiff === 'string') {
|
||
|
uniDiff = /*istanbul ignore start*/(0, _parse.parsePatch) /*istanbul ignore end*/(uniDiff);
|
||
|
}
|
||
|
|
||
|
if (Array.isArray(uniDiff)) {
|
||
|
if (uniDiff.length > 1) {
|
||
|
throw new Error('applyPatch only works with a single input.');
|
||
|
}
|
||
|
|
||
|
uniDiff = uniDiff[0];
|
||
|
}
|
||
|
|
||
|
// Apply the diff to the input
|
||
|
var lines = source.split(/\r\n|[\n\v\f\r\x85]/),
|
||
|
delimiters = source.match(/\r\n|[\n\v\f\r\x85]/g) || [],
|
||
|
hunks = uniDiff.hunks,
|
||
|
compareLine = options.compareLine || function (lineNumber, line, operation, patchContent) /*istanbul ignore start*/{
|
||
|
return (/*istanbul ignore end*/line === patchContent
|
||
|
);
|
||
|
},
|
||
|
errorCount = 0,
|
||
|
fuzzFactor = options.fuzzFactor || 0,
|
||
|
minLine = 0,
|
||
|
offset = 0,
|
||
|
removeEOFNL = /*istanbul ignore start*/void 0 /*istanbul ignore end*/,
|
||
|
addEOFNL = /*istanbul ignore start*/void 0 /*istanbul ignore end*/;
|
||
|
|
||
|
/**
|
||
|
* Checks if the hunk exactly fits on the provided location
|
||
|
*/
|
||
|
function hunkFits(hunk, toPos) {
|
||
|
for (var j = 0; j < hunk.lines.length; j++) {
|
||
|
var line = hunk.lines[j],
|
||
|
operation = line[0],
|
||
|
content = line.substr(1);
|
||
|
|
||
|
if (operation === ' ' || operation === '-') {
|
||
|
// Context sanity check
|
||
|
if (!compareLine(toPos + 1, lines[toPos], operation, content)) {
|
||
|
errorCount++;
|
||
|
|
||
|
if (errorCount > fuzzFactor) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
toPos++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// Search best fit offsets for each hunk based on the previous ones
|
||
|
for (var i = 0; i < hunks.length; i++) {
|
||
|
var hunk = hunks[i],
|
||
|
maxLine = lines.length - hunk.oldLines,
|
||
|
localOffset = 0,
|
||
|
toPos = offset + hunk.oldStart - 1;
|
||
|
|
||
|
var iterator = /*istanbul ignore start*/(0, _distanceIterator2['default']) /*istanbul ignore end*/(toPos, minLine, maxLine);
|
||
|
|
||
|
for (; localOffset !== undefined; localOffset = iterator()) {
|
||
|
if (hunkFits(hunk, toPos + localOffset)) {
|
||
|
hunk.offset = offset += localOffset;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (localOffset === undefined) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Set lower text limit to end of the current hunk, so next ones don't try
|
||
|
// to fit over already patched text
|
||
|
minLine = hunk.offset + hunk.oldStart + hunk.oldLines;
|
||
|
}
|
||
|
|
||
|
// Apply patch hunks
|
||
|
for (var _i = 0; _i < hunks.length; _i++) {
|
||
|
var _hunk = hunks[_i],
|
||
|
_toPos = _hunk.offset + _hunk.newStart - 1;
|
||
|
if (_hunk.newLines == 0) {
|
||
|
_toPos++;
|
||
|
}
|
||
|
|
||
|
for (var j = 0; j < _hunk.lines.length; j++) {
|
||
|
var line = _hunk.lines[j],
|
||
|
operation = line[0],
|
||
|
content = line.substr(1),
|
||
|
delimiter = _hunk.linedelimiters[j];
|
||
|
|
||
|
if (operation === ' ') {
|
||
|
_toPos++;
|
||
|
} else if (operation === '-') {
|
||
|
lines.splice(_toPos, 1);
|
||
|
delimiters.splice(_toPos, 1);
|
||
|
/* istanbul ignore else */
|
||
|
} else if (operation === '+') {
|
||
|
lines.splice(_toPos, 0, content);
|
||
|
delimiters.splice(_toPos, 0, delimiter);
|
||
|
_toPos++;
|
||
|
} else if (operation === '\\') {
|
||
|
var previousOperation = _hunk.lines[j - 1] ? _hunk.lines[j - 1][0] : null;
|
||
|
if (previousOperation === '+') {
|
||
|
removeEOFNL = true;
|
||
|
} else if (previousOperation === '-') {
|
||
|
addEOFNL = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Handle EOFNL insertion/removal
|
||
|
if (removeEOFNL) {
|
||
|
while (!lines[lines.length - 1]) {
|
||
|
lines.pop();
|
||
|
delimiters.pop();
|
||
|
}
|
||
|
} else if (addEOFNL) {
|
||
|
lines.push('');
|
||
|
delimiters.push('\n');
|
||
|
}
|
||
|
for (var _k = 0; _k < lines.length - 1; _k++) {
|
||
|
lines[_k] = lines[_k] + delimiters[_k];
|
||
|
}
|
||
|
return lines.join('');
|
||
|
}
|
||
|
|
||
|
// Wrapper that supports multiple file patches via callbacks.
|
||
|
function applyPatches(uniDiff, options) {
|
||
|
if (typeof uniDiff === 'string') {
|
||
|
uniDiff = /*istanbul ignore start*/(0, _parse.parsePatch) /*istanbul ignore end*/(uniDiff);
|
||
|
}
|
||
|
|
||
|
var currentIndex = 0;
|
||
|
function processIndex() {
|
||
|
var index = uniDiff[currentIndex++];
|
||
|
if (!index) {
|
||
|
return options.complete();
|
||
|
}
|
||
|
|
||
|
options.loadFile(index, function (err, data) {
|
||
|
if (err) {
|
||
|
return options.complete(err);
|
||
|
}
|
||
|
|
||
|
var updatedContent = applyPatch(data, index, options);
|
||
|
options.patched(index, updatedContent, function (err) {
|
||
|
if (err) {
|
||
|
return options.complete(err);
|
||
|
}
|
||
|
|
||
|
processIndex();
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
processIndex();
|
||
|
}
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9wYXRjaC9hcHBseS5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Z0NBR2dCLFUsR0FBQSxVO3lEQStIQSxZLEdBQUEsWTs7QUFsSWhCLEkseUJBQUEsMkIsd0JBQUE7O0FBQ0EsSSx5QkFBQSx3RCx3QkFBQTs7Ozs7Ozt1QkFFTyxTQUFTLFVBQVQsQ0FBb0IsTUFBcEIsRUFBNEIsT0FBNUIsRUFBbUQ7MkJBQUEsSSx1QkFBZCxPQUFjLHlEQUFKLEVBQUk7O0FBQ3hELE1BQUksT0FBTyxPQUFQLEtBQW1CLFFBQXZCLEVBQWlDO0FBQy9CLGMseUJBQVUsc0Isd0JBQUEsQ0FBVyxPQUFYLENBQVY7QUFDRDs7QUFFRCxNQUFJLE1BQU0sT0FBTixDQUFjLE9BQWQsQ0FBSixFQUE0QjtBQUMxQixRQUFJLFFBQVEsTUFBUixHQUFpQixDQUFyQixFQUF3QjtBQUN0QixZQUFNLElBQUksS0FBSixDQUFVLDRDQUFWLENBQU47QUFDRDs7QUFFRCxjQUFVLFFBQVEsQ0FBUixDQUFWO0FBQ0Q7OztBQUdELE1BQUksUUFBUSxPQUFPLEtBQVAsQ0FBYSxxQkFBYixDQUFaO0FBQUEsTUFDSSxhQUFhLE9BQU8sS0FBUCxDQUFhLHNCQUFiLEtBQXdDLEVBRHpEO0FBQUEsTUFFSSxRQUFRLFFBQVEsS0FGcEI7QUFBQSxNQUlJLGNBQWMsUUFBUSxXQUFSLElBQXdCLFVBQUMsVUFBRCxF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