forked from kpdecker/jsdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.js
162 lines (137 loc) · 4.33 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
import {parsePatch} from './parse';
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];
}
// Apply the diff to the input
let lines = source.split('\n'),
hunks = uniDiff.hunks,
compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => 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 (let j = 0; j < hunk.lines.length; j++) {
let 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 (let i = 0; i < hunks.length; i++) {
let hunk = hunks[i],
outOfLimits = 0,
localOffset = 0,
toPos = offset + hunk.oldStart - 1;
for (;;) {
// Check if trying to fit beyond text length, and if not, check it fits
// after offset location (or desired location on first iteration)
if (lines.length < toPos + localOffset + hunk.oldLines) {
outOfLimits++;
} else if (hunkFits(hunk, toPos + localOffset)) {
hunk.offset = offset += localOffset;
break;
}
// If we tried to fit hunk before text beginning and beyond text lenght,
// then hunk can't be fit on the text so we raise an error
if (outOfLimits === 2) {
return false;
}
// Reset checks of trying to fit outside text limits and increase offset
// of the current hunk relative to its desired location
outOfLimits = 0;
localOffset++;
// Check if trying to fit before text beginning, and if not, check it fits
// before offset location
if (toPos - localOffset < minLine) {
outOfLimits++;
} else if (hunkFits(hunk, toPos - localOffset)) {
hunk.offset = offset -= localOffset;
break;
}
}
// 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 (let i = 0; i < hunks.length; i++) {
let hunk = hunks[i],
toPos = hunk.offset + hunk.newStart - 1;
for (let j = 0; j < hunk.lines.length; j++) {
let line = hunk.lines[j],
operation = line[0],
content = line.substr(1);
if (operation === ' ') {
toPos++;
} else if (operation === '-') {
lines.splice(toPos, 1);
/* istanbul ignore else */
} else if (operation === '+') {
lines.splice(toPos, 0, content);
toPos++;
} else if (operation === '\\') {
let 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();
}
} else if (addEOFNL) {
lines.push('');
}
return lines.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);
setTimeout(processIndex, 0);
});
}
processIndex();
}