aoc/node_modules/diff/lib/patch/apply.js

244 lines
20 KiB
JavaScript
Raw Normal View History

2022-12-03 22:11:42 +00:00
/*istanbul ignore start*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.applyPatch = applyPatch;
exports.applyPatches = applyPatches;
/*istanbul ignore end*/
var
/*istanbul ignore start*/
_parse = require("./parse")
/*istanbul ignore end*/
;
var
/*istanbul ignore start*/
_distanceIterator = _interopRequireDefault(require("../util/distance-iterator"))
/*istanbul ignore end*/
;
/*istanbul ignore start*/ 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,
/*istanbul ignore end*/
/*istanbul ignore start*/
_parse
/*istanbul ignore end*/
.
/*istanbul ignore start*/
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,
addEOFNL;
/**
* 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.length > 0 ? line[0] : ' ',
content = line.length > 0 ? line.substr(1) : line;
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,
/*istanbul ignore end*/
/*istanbul ignore start*/
_distanceIterator
/*istanbul ignore end*/
.
/*istanbul ignore start*/
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
var diffOffset = 0;
for (var _i = 0; _i < hunks.length; _i++) {
var _hunk = hunks[_i],
_toPos = _hunk.oldStart + _hunk.offset + diffOffset - 1;
diffOffset += _hunk.newLines - _hunk.oldLines;
if (_toPos < 0) {
// Creating a new file
_toPos = 0;
}
for (var j = 0; j < _hunk.lines.length; j++) {
var line = _hunk.lines[j],
operation = line.length > 0 ? line[0] : ' ',
content = line.length > 0 ? line.substr(1) : line,
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,
/*istanbul ignore end*/
/*istanbul ignore start*/
_parse
/*istanbul ignore end*/
.
/*istanbul ignore start*/
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;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9wYXRjaC9hcHBseS5qcyJdLCJuYW1lcyI6WyJhcHBseVBhdGNoIiwic291cmNlIiwidW5pRGlmZiIsIm9wdGlvbnMiLCJwYXJzZVBhdGNoIiwiQXJyYXkiLCJpc0FycmF5IiwibGVuZ3RoIiwiRXJyb3IiLCJsaW5lcyIsInNwbGl0IiwiZGVsaW1pdGVycyIsIm1hdGNoIiwiaHVua3MiLCJjb21wYXJlTGluZSIsImxpbmVOdW1iZXIiLCJsaW5lIiwib3BlcmF0aW9uIiwicGF0Y2hDb250ZW50IiwiZXJyb3JDb3VudCIsImZ1enpGYWN0b3IiLCJtaW5MaW5lIiwib2Zmc2V0IiwicmVtb3ZlRU9GTkwiLCJhZGRFT0ZOTCIsImh1bmtGaXRzIiwiaHVuayIsInRvUG9zIiwiaiIsImNvbnRlbnQiLCJzdWJzdHIiLCJpIiwibWF4TGluZSIsIm9sZExpbmVzIiwibG9jYWxPZmZzZXQiLCJvbGRTdGFydCIsIml0ZXJhdG9yIiwiZGlzdGFuY2VJdGVyYXRvciIsInVuZGVmaW5lZCIsImRpZmZPZmZzZXQiLCJuZXdMaW5lcyIs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