codekingpro/portable-devtools
115k
1import { hasOnlyWinLineEndings, hasOnlyUnixLineEndings } from '../util/string.js';
2import { isWin, isUnix, unixToWin, winToUnix } from './line-endings.js';
3import { parsePatch } from './parse.js';
4import distanceIterator from '../util/distance-iterator.js';
5/**
6 * attempts to apply a unified diff patch.
7 *
8 * Hunks are applied first to last.
9 * `applyPatch` first tries to apply the first hunk at the line number specified in the hunk header, and with all context lines matching exactly.
10 * If that fails, it tries scanning backwards and forwards, one line at a time, to find a place to apply the hunk where the context lines match exactly.
11 * If that still fails, and `fuzzFactor` is greater than zero, it increments the maximum number of mismatches (missing, extra, or changed context lines) that there can be between the hunk context and a region where we are trying to apply the patch such that the hunk will still be considered to match.
12 * Regardless of `fuzzFactor`, lines to be deleted in the hunk *must* be present for a hunk to match, and the context lines *immediately* before and after an insertion must match exactly.
13 *
14 * Once a hunk is successfully fitted, the process begins again with the next hunk.
15 * Regardless of `fuzzFactor`, later hunks must be applied later in the file than earlier hunks.
16 *
17 * If a hunk cannot be successfully fitted *anywhere* with fewer than `fuzzFactor` mismatches, `applyPatch` fails and returns `false`.
18 *
19 * If a hunk is successfully fitted but not at the line number specified by the hunk header, all subsequent hunks have their target line number adjusted accordingly.
20 * (e.g. if the first hunk is applied 10 lines below where the hunk header said it should fit, `applyPatch` will *start* looking for somewhere to apply the second hunk 10 lines below where its hunk header says it goes.)
21 *
22 * If the patch was applied successfully, returns a string containing the patched text.
23 * If the patch could not be applied (because some hunks in the patch couldn't be fitted to the text in `source`), `applyPatch` returns false.
24 *
25 * @param patch a string diff or the output from the `parsePatch` or `structuredPatch` methods.
26 */
27export function applyPatch(source, patch, options = {}) {
28 let patches;
29 if (typeof patch === 'string') {
30 patches = parsePatch(patch);
31 }
32 else if (Array.isArray(patch)) {
33 patches = patch;
34 }
35 else {
36 patches = [patch];
37 }
38 if (patches.length > 1) {
39 throw new Error('applyPatch only works with a single input.');
40 }
41 return applyStructuredPatch(source, patches[0], options);
42}
43function applyStructuredPatch(source, patch, options = {}) {
44 if (options.autoConvertLineEndings || options.autoConvertLineEndings == null) {
45 if (hasOnlyWinLineEndings(source) && isUnix(patch)) {
46 patch = unixToWin(patch);
47 }
48 else if (hasOnlyUnixLineEndings(source) && isWin(patch)) {
49 patch = winToUnix(patch);
50 }
51 }
52 // Apply the diff to the input
53 const lines = source.split('\n'), hunks = patch.hunks, compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => line === patchContent), fuzzFactor = options.fuzzFactor || 0;
54 let minLine = 0;
55 if (fuzzFactor < 0 || !Number.isInteger(fuzzFactor)) {
56 throw new Error('fuzzFactor must be a non-negative integer');
57 }
58 // Special case for empty patch.
59 if (!hunks.length) {
60 return source;
61 }
62 // Before anything else, handle EOFNL insertion/removal. If the patch tells us to make a change
63 // to the EOFNL that is redundant/impossible - i.e. to remove a newline that's not there, or add a
64 // newline that already exists - then we either return false and fail to apply the patch (if
65 // fuzzFactor is 0) or simply ignore the problem and do nothing (if fuzzFactor is >0).
66 // If we do need to remove/add a newline at EOF, this will always be in the final hunk:
67 let prevLine = '', removeEOFNL = false, addEOFNL = false;
68 for (let i = 0; i < hunks[hunks.length - 1].lines.length; i++) {
69 const line = hunks[hunks.length - 1].lines[i];
70 if (line[0] == '\\') {
71 if (prevLine[0] == '+') {
72 removeEOFNL = true;
73 }
74 else if (prevLine[0] == '-') {
75 addEOFNL = true;
76 }
77 }
78 prevLine = line;
79 }
80 if (removeEOFNL) {
81 if (addEOFNL) {
82 // This means the final line gets changed but doesn't have a trailing newline in either the
83 // original or patched version. In that case, we do nothing if fuzzFactor > 0, and if
84 // fuzzFactor is 0, we simply validate that the source file has no trailing newline.
85 if (!fuzzFactor && lines[lines.length - 1] == '') {
86 return false;
87 }
88 }
89 else if (lines[lines.length - 1] == '') {
90 lines.pop();
91 }
92 else if (!fuzzFactor) {
93 return false;
94 }
95 }
96 else if (addEOFNL) {
97 if (lines[lines.length - 1] != '') {
98 lines.push('');
99 }
100 else if (!fuzzFactor) {
101 return false;
102 }
103 }
104 /**
105 * Checks if the hunk can be made to fit at the provided location with at most `maxErrors`
106 * insertions, substitutions, or deletions, while ensuring also that:
107 * - lines deleted in the hunk match exactly, and
108 * - wherever an insertion operation or block of insertion operations appears in the hunk, the
109 * immediately preceding and following lines of context match exactly
110 *
111 * `toPos` should be set such that lines[toPos] is meant to match hunkLines[0].
112 *
113 * If the hunk can be applied, returns an object with properties `oldLineLastI` and
114 * `replacementLines`. Otherwise, returns null.
115 */
116 function applyHunk(hunkLines, toPos, maxErrors, hunkLinesI = 0, lastContextLineMatched = true, patchedLines = [], patchedLinesLength = 0) {
117 let nConsecutiveOldContextLines = 0;
118 let nextContextLineMustMatch = false;
119 for (; hunkLinesI < hunkLines.length; hunkLinesI++) {
120 const hunkLine = hunkLines[hunkLinesI], operation = (hunkLine.length > 0 ? hunkLine[0] : ' '), content = (hunkLine.length > 0 ? hunkLine.substr(1) : hunkLine);
121 if (operation === '-') {
122 if (compareLine(toPos + 1, lines[toPos], operation, content)) {
123 toPos++;
124 nConsecutiveOldContextLines = 0;
125 }
126 else {
127 if (!maxErrors || lines[toPos] == null) {
128 return null;
129 }
130 patchedLines[patchedLinesLength] = lines[toPos];
131 return applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI, false, patchedLines, patchedLinesLength + 1);
132 }
133 }
134 if (operation === '+') {
135 if (!lastContextLineMatched) {
136 return null;
137 }
138 patchedLines[patchedLinesLength] = content;
139 patchedLinesLength++;
140 nConsecutiveOldContextLines = 0;
141 nextContextLineMustMatch = true;
142 }
143 if (operation === ' ') {
144 nConsecutiveOldContextLines++;
145 patchedLines[patchedLinesLength] = lines[toPos];
146 if (compareLine(toPos + 1, lines[toPos], operation, content)) {
147 patchedLinesLength++;
148 lastContextLineMatched = true;
149 nextContextLineMustMatch = false;
150 toPos++;
151 }
152 else {
153 if (nextContextLineMustMatch || !maxErrors) {
154 return null;
155 }
156 // Consider 3 possibilities in sequence:
157 // 1. lines contains a *substitution* not included in the patch context, or
158 // 2. lines contains an *insertion* not included in the patch context, or
159 // 3. lines contains a *deletion* not included in the patch context
160 // The first two options are of course only possible if the line from lines is non-null -
161 // i.e. only option 3 is possible if we've overrun the end of the old file.
162 return (lines[toPos] && (applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI + 1, false, patchedLines, patchedLinesLength + 1) || applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI, false, patchedLines, patchedLinesLength + 1)) || applyHunk(hunkLines, toPos, maxErrors - 1, hunkLinesI + 1, false, patchedLines, patchedLinesLength));
163 }
164 }
165 }
166 // Before returning, trim any unmodified context lines off the end of patchedLines and reduce
167 // toPos (and thus oldLineLastI) accordingly. This allows later hunks to be applied to a region
168 // that starts in this hunk's trailing context.
169 patchedLinesLength -= nConsecutiveOldContextLines;
170 toPos -= nConsecutiveOldContextLines;
171 patchedLines.length = patchedLinesLength;
172 return {
173 patchedLines,
174 oldLineLastI: toPos - 1
175 };
176 }
177 const resultLines = [];
178 // Search best fit offsets for each hunk based on the previous ones
179 let prevHunkOffset = 0;
180 for (let i = 0; i < hunks.length; i++) {
181 const hunk = hunks[i];
182 let hunkResult;
183 const maxLine = lines.length - hunk.oldLines + fuzzFactor;
184 let toPos;
185 for (let maxErrors = 0; maxErrors <= fuzzFactor; maxErrors++) {
186 toPos = hunk.oldStart + prevHunkOffset - 1;
187 const iterator = distanceIterator(toPos, minLine, maxLine);
188 for (; toPos !== undefined; toPos = iterator()) {
189 hunkResult = applyHunk(hunk.lines, toPos, maxErrors);
190 if (hunkResult) {
191 break;
192 }
193 }
194 if (hunkResult) {
195 break;
196 }
197 }
198 if (!hunkResult) {
199 return false;
200 }
201 // Copy everything from the end of where we applied the last hunk to the start of this hunk
202 for (let i = minLine; i < toPos; i++) {
203 resultLines.push(lines[i]);
204 }
205 // Add the lines produced by applying the hunk:
206 for (let i = 0; i < hunkResult.patchedLines.length; i++) {
207 const line = hunkResult.patchedLines[i];
208 resultLines.push(line);
209 }
210 // Set lower text limit to end of the current hunk, so next ones don't try
211 // to fit over already patched text
212 minLine = hunkResult.oldLineLastI + 1;
213 // Note the offset between where the patch said the hunk should've applied and where we
214 // applied it, so we can adjust future hunks accordingly:
215 prevHunkOffset = toPos + 1 - hunk.oldStart;
216 }
217 // Copy over the rest of the lines from the old text
218 for (let i = minLine; i < lines.length; i++) {
219 resultLines.push(lines[i]);
220 }
221 return resultLines.join('\n');
222}
223/**
224 * applies one or more patches.
225 *
226 * `patch` may be either an array of structured patch objects, or a string representing a patch in unified diff format (which may patch one or more files).
227 *
228 * This method will iterate over the contents of the patch and apply to data provided through callbacks. The general flow for each patch index is:
229 *
230 * - `options.loadFile(index, callback)` is called. The caller should then load the contents of the file and then pass that to the `callback(err, data)` callback. Passing an `err` will terminate further patch execution.
231 * - `options.patched(index, content, callback)` is called once the patch has been applied. `content` will be the return value from `applyPatch`. When it's ready, the caller should call `callback(err)` callback. Passing an `err` will terminate further patch execution.
232 *
233 * Once all patches have been applied or an error occurs, the `options.complete(err)` callback is made.
234 */
235export function applyPatches(uniDiff, options) {
236 const spDiff = typeof uniDiff === 'string' ? parsePatch(uniDiff) : uniDiff;
237 let currentIndex = 0;
238 function processIndex() {
239 const index = spDiff[currentIndex++];
240 if (!index) {
241 return options.complete();
242 }
243 options.loadFile(index, function (err, data) {
244 if (err) {
245 return options.complete(err);
246 }
247 const updatedContent = applyPatch(data, index, options);
248 options.patched(index, updatedContent, function (err) {
249 if (err) {
250 return options.complete(err);
251 }
252 processIndex();
253 });
254 });
255 }
256 processIndex();
257}
258 