-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathapply.js
291 lines (262 loc) · 9.2 KB
/
apply.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {hasOnlyWinLineEndings, hasOnlyUnixLineEndings} from '../util/string';
import {isWin, isUnix, unixToWin, winToUnix} from './line-endings';
import {parsePatch} from './parse';
import distanceIterator from '../util/distance-iterator';
export function applyPatch(source, uniDiff, options = {}) {
if (typeof uniDiff === 'string') {
uniDiff = parsePatch(uniDiff);
}
if (Array.isArray(uniDiff)) {
if (uniDiff.length > 1) {
throw new Error('applyPatch only works with a single input.');
}
uniDiff = uniDiff[0];
}
if (options.autoConvertLineEndings || options.autoConvertLineEndings == null) {
if (hasOnlyWinLineEndings(source) && isUnix(uniDiff)) {
uniDiff = unixToWin(uniDiff);
} else if (hasOnlyUnixLineEndings(source) && isWin(uniDiff)) {
uniDiff = winToUnix(uniDiff);
}
}
// Apply the diff to the input
let lines = source.split('\n'),
hunks = uniDiff.hunks,
compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => line === patchContent),
fuzzFactor = options.fuzzFactor || 0,
minLine = 0;
if (fuzzFactor < 0 || !Number.isInteger(fuzzFactor)) {
throw new Error('fuzzFactor must be a non-negative integer');
}
// Special case for empty patch.
if (!hunks.length) {
return source;
}
// Before anything else, handle EOFNL insertion/removal. If the patch tells us to make a change
// to the EOFNL that is redundant/impossible - i.e. to remove a newline that's not there, or add a
// newline that already exists - then we either return false and fail to apply the patch (if
// fuzzFactor is 0) or simply ignore the problem and do nothing (if fuzzFactor is >0).
// If we do need to remove/add a newline at EOF, this will always be in the final hunk:
let prevLine = '',
removeEOFNL = false,
addEOFNL = false;
for (let i = 0; i < hunks[hunks.length - 1].lines.length; i++) {
const line = hunks[hunks.length - 1].lines[i];
if (line[0] == '\\') {
if (prevLine[0] == '+') {
removeEOFNL = true;
} else if (prevLine[0] == '-') {
addEOFNL = true;
}
}
prevLine = line;
}
if (removeEOFNL) {
if (addEOFNL) {
// This means the final line gets changed but doesn't have a trailing newline in either the
// original or patched version. In that case, we do nothing if fuzzFactor > 0, and if
// fuzzFactor is 0, we simply validate that the source file has no trailing newline.
if (!fuzzFactor && lines[lines.length - 1] == '') {
return false;
}
} else if (lines[lines.length - 1] == '') {
lines.pop();
} else if (!fuzzFactor) {
return false;
}
} else if (addEOFNL) {
if (lines[lines.length - 1] != '') {
lines.push('');
} else if (!fuzzFactor) {
return false;
}
}
/**
* Checks if the hunk can be made to fit at the provided location with at most `maxErrors`
* insertions, substitutions, or deletions, while ensuring also that:
* - lines deleted in the hunk match exactly, and
* - wherever an insertion operation or block of insertion operations appears in the hunk, the
* immediately preceding and following lines of context match exactly
*
* `toPos` should be set such that lines[toPos] is meant to match hunkLines[0].
*
* If the hunk can be applied, returns an object with properties `oldLineLastI` and
* `replacementLines`. Otherwise, returns null.
*/
function applyHunk(
hunkLines,
toPos,
maxErrors,
hunkLinesI = 0,
lastContextLineMatched = true,
patchedLines = [],
patchedLinesLength = 0,
) {
let nConsecutiveOldContextLines = 0;
let nextContextLineMustMatch = false;
for (; hunkLinesI < hunkLines.length; hunkLinesI++) {
let hunkLine = hunkLines[hunkLinesI],
operation = (hunkLine.length > 0 ? hunkLine[0] : ' '),
content = (hunkLine.length > 0 ? hunkLine.substr(1) : hunkLine);
if (operation === '-') {
if (compareLine(toPos + 1, lines[toPos], operation, content)) {
toPos++;
nConsecutiveOldContextLines = 0;
} else {
if (!maxErrors || lines[toPos] == null) {
return null;
}
patchedLines[patchedLinesLength] = lines[toPos];
return applyHunk(
hunkLines,
toPos + 1,
maxErrors - 1,
hunkLinesI,
false,
patchedLines,
patchedLinesLength + 1,
);
}
}
if (operation === '+') {
if (!lastContextLineMatched) {
return null;
}
patchedLines[patchedLinesLength] = content;
patchedLinesLength++;
nConsecutiveOldContextLines = 0;
nextContextLineMustMatch = true;
}
if (operation === ' ') {
nConsecutiveOldContextLines++;
patchedLines[patchedLinesLength] = lines[toPos];
if (compareLine(toPos + 1, lines[toPos], operation, content)) {
patchedLinesLength++;
lastContextLineMatched = true;
nextContextLineMustMatch = false;
toPos++;
} else {
if (nextContextLineMustMatch || !maxErrors) {
return null;
}
// Consider 3 possibilities in sequence:
// 1. lines contains a *substitution* not included in the patch context, or
// 2. lines contains an *insertion* not included in the patch context, or
// 3. lines contains a *deletion* not included in the patch context
// The first two options are of course only possible if the line from lines is non-null -
// i.e. only option 3 is possible if we've overrun the end of the old file.
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
)
);
}
}
}
// Before returning, trim any unmodified context lines off the end of patchedLines and reduce
// toPos (and thus oldLineLastI) accordingly. This allows later hunks to be applied to a region
// that starts in this hunk's trailing context.
patchedLinesLength -= nConsecutiveOldContextLines;
toPos -= nConsecutiveOldContextLines;
patchedLines.length = patchedLinesLength;
return {
patchedLines,
oldLineLastI: toPos - 1
};
}
const resultLines = [];
// Search best fit offsets for each hunk based on the previous ones
let prevHunkOffset = 0;
for (let i = 0; i < hunks.length; i++) {
const hunk = hunks[i];
let hunkResult;
let maxLine = lines.length - hunk.oldLines + fuzzFactor;
let toPos;
for (let maxErrors = 0; maxErrors <= fuzzFactor; maxErrors++) {
toPos = hunk.oldStart + prevHunkOffset - 1;
let iterator = distanceIterator(toPos, minLine, maxLine);
for (; toPos !== undefined; toPos = iterator()) {
hunkResult = applyHunk(hunk.lines, toPos, maxErrors);
if (hunkResult) {
break;
}
}
if (hunkResult) {
break;
}
}
if (!hunkResult) {
return false;
}
// Copy everything from the end of where we applied the last hunk to the start of this hunk
for (let i = minLine; i < toPos; i++) {
resultLines.push(lines[i]);
}
// Add the lines produced by applying the hunk:
for (let i = 0; i < hunkResult.patchedLines.length; i++) {
const line = hunkResult.patchedLines[i];
resultLines.push(line);
}
// Set lower text limit to end of the current hunk, so next ones don't try
// to fit over already patched text
minLine = hunkResult.oldLineLastI + 1;
// Note the offset between where the patch said the hunk should've applied and where we
// applied it, so we can adjust future hunks accordingly:
prevHunkOffset = toPos + 1 - hunk.oldStart;
}
// Copy over the rest of the lines from the old text
for (let i = minLine; i < lines.length; i++) {
resultLines.push(lines[i]);
}
return resultLines.join('\n');
}
// Wrapper that supports multiple file patches via callbacks.
export function applyPatches(uniDiff, options) {
if (typeof uniDiff === 'string') {
uniDiff = parsePatch(uniDiff);
}
let currentIndex = 0;
function processIndex() {
let index = uniDiff[currentIndex++];
if (!index) {
return options.complete();
}
options.loadFile(index, function(err, data) {
if (err) {
return options.complete(err);
}
let updatedContent = applyPatch(data, index, options);
options.patched(index, updatedContent, function(err) {
if (err) {
return options.complete(err);
}
processIndex();
});
});
}
processIndex();
}