forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
251 lines (251 loc) · 10.3 KB
/
Copy pathcreate.js
File metadata and controls
251 lines (251 loc) · 10.3 KB
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
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OMIT_HEADERS = exports.FILE_HEADERS_ONLY = exports.INCLUDE_HEADERS = void 0;
exports.structuredPatch = structuredPatch;
exports.formatPatch = formatPatch;
exports.createTwoFilesPatch = createTwoFilesPatch;
exports.createPatch = createPatch;
var line_js_1 = require("../diff/line.js");
exports.INCLUDE_HEADERS = {
includeIndex: true,
includeUnderline: true,
includeFileHeaders: true
};
exports.FILE_HEADERS_ONLY = {
includeIndex: false,
includeUnderline: false,
includeFileHeaders: true
};
exports.OMIT_HEADERS = {
includeIndex: false,
includeUnderline: false,
includeFileHeaders: false
};
function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) {
var optionsObj;
if (!options) {
optionsObj = {};
}
else if (typeof options === 'function') {
optionsObj = { callback: options };
}
else {
optionsObj = options;
}
if (typeof optionsObj.context === 'undefined') {
optionsObj.context = 4;
}
// We copy this into its own variable to placate TypeScript, which thinks
// optionsObj.context might be undefined in the callbacks below.
var context = optionsObj.context;
// @ts-expect-error (runtime check for something that is correctly a static type error)
if (optionsObj.newlineIsToken) {
throw new Error('newlineIsToken may not be used with patch-generation functions, only with diffing functions');
}
if (!optionsObj.callback) {
return diffLinesResultToPatch((0, line_js_1.diffLines)(oldStr, newStr, optionsObj));
}
else {
var callback_1 = optionsObj.callback;
(0, line_js_1.diffLines)(oldStr, newStr, __assign(__assign({}, optionsObj), { callback: function (diff) {
var patch = diffLinesResultToPatch(diff);
// TypeScript is unhappy without the cast because it does not understand that `patch` may
// be undefined here only if `callback` is StructuredPatchCallbackAbortable:
callback_1(patch);
} }));
}
function diffLinesResultToPatch(diff) {
// STEP 1: Build up the patch with no "\ No newline at end of file" lines and with the arrays
// of lines containing trailing newline characters. We'll tidy up later...
if (!diff) {
return;
}
diff.push({ value: '', lines: [] }); // Append an empty value to make cleanup easier
function contextLines(lines) {
return lines.map(function (entry) { return ' ' + entry; });
}
var hunks = [];
var oldRangeStart = 0, newRangeStart = 0, curRange = [], oldLine = 1, newLine = 1;
for (var i = 0; i < diff.length; i++) {
var current = diff[i], lines = current.lines || splitLines(current.value);
current.lines = lines;
if (current.added || current.removed) {
// If we have previous context, start with that
if (!oldRangeStart) {
var prev = diff[i - 1];
oldRangeStart = oldLine;
newRangeStart = newLine;
if (prev) {
curRange = context > 0 ? contextLines(prev.lines.slice(-context)) : [];
oldRangeStart -= curRange.length;
newRangeStart -= curRange.length;
}
}
// Output our changes
for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
var line = lines_1[_i];
curRange.push((current.added ? '+' : '-') + line);
}
// Track the updated file position
if (current.added) {
newLine += lines.length;
}
else {
oldLine += lines.length;
}
}
else {
// Identical context lines. Track line changes
if (oldRangeStart) {
// Close out any changes that have been output (or join overlapping)
if (lines.length <= context * 2 && i < diff.length - 2) {
// Overlapping
for (var _a = 0, _b = contextLines(lines); _a < _b.length; _a++) {
var line = _b[_a];
curRange.push(line);
}
}
else {
// end the range and output
var contextSize = Math.min(lines.length, context);
for (var _c = 0, _d = contextLines(lines.slice(0, contextSize)); _c < _d.length; _c++) {
var line = _d[_c];
curRange.push(line);
}
var hunk = {
oldStart: oldRangeStart,
oldLines: (oldLine - oldRangeStart + contextSize),
newStart: newRangeStart,
newLines: (newLine - newRangeStart + contextSize),
lines: curRange
};
hunks.push(hunk);
oldRangeStart = 0;
newRangeStart = 0;
curRange = [];
}
}
oldLine += lines.length;
newLine += lines.length;
}
}
// Step 2: eliminate the trailing `\n` from each line of each hunk, and, where needed, add
// "\ No newline at end of file".
for (var _e = 0, hunks_1 = hunks; _e < hunks_1.length; _e++) {
var hunk = hunks_1[_e];
for (var i = 0; i < hunk.lines.length; i++) {
if (hunk.lines[i].endsWith('\n')) {
hunk.lines[i] = hunk.lines[i].slice(0, -1);
}
else {
hunk.lines.splice(i + 1, 0, '\\ No newline at end of file');
i++; // Skip the line we just added, then continue iterating
}
}
}
return {
oldFileName: oldFileName, newFileName: newFileName,
oldHeader: oldHeader, newHeader: newHeader,
hunks: hunks
};
}
}
/**
* creates a unified diff patch.
* @param patch either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`)
*/
function formatPatch(patch, headerOptions) {
if (!headerOptions) {
headerOptions = exports.INCLUDE_HEADERS;
}
if (Array.isArray(patch)) {
if (patch.length > 1 && !headerOptions.includeFileHeaders) {
throw new Error('Cannot omit file headers on a multi-file patch. '
+ '(The result would be unparseable; how would a tool trying to apply '
+ 'the patch know which changes are to which file?)');
}
return patch.map(function (p) { return formatPatch(p, headerOptions); }).join('\n');
}
var ret = [];
if (headerOptions.includeIndex && patch.oldFileName == patch.newFileName) {
ret.push('Index: ' + patch.oldFileName);
}
if (headerOptions.includeUnderline) {
ret.push('===================================================================');
}
if (headerOptions.includeFileHeaders) {
ret.push('--- ' + patch.oldFileName + (typeof patch.oldHeader === 'undefined' ? '' : '\t' + patch.oldHeader));
ret.push('+++ ' + patch.newFileName + (typeof patch.newHeader === 'undefined' ? '' : '\t' + patch.newHeader));
}
for (var i = 0; i < patch.hunks.length; i++) {
var hunk = patch.hunks[i];
// Unified Diff Format quirk: If the chunk size is 0,
// the first number is one lower than one would expect.
// https://www.artima.com/weblogs/viewpost.jsp?thread=164293
if (hunk.oldLines === 0) {
hunk.oldStart -= 1;
}
if (hunk.newLines === 0) {
hunk.newStart -= 1;
}
ret.push('@@ -' + hunk.oldStart + ',' + hunk.oldLines
+ ' +' + hunk.newStart + ',' + hunk.newLines
+ ' @@');
for (var _i = 0, _a = hunk.lines; _i < _a.length; _i++) {
var line = _a[_i];
ret.push(line);
}
}
return ret.join('\n') + '\n';
}
function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) {
if (typeof options === 'function') {
options = { callback: options };
}
if (!(options === null || options === void 0 ? void 0 : options.callback)) {
var patchObj = structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options);
if (!patchObj) {
return;
}
return formatPatch(patchObj, options === null || options === void 0 ? void 0 : options.headerOptions);
}
else {
var callback_2 = options.callback;
structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, __assign(__assign({}, options), { callback: function (patchObj) {
if (!patchObj) {
callback_2(undefined);
}
else {
callback_2(formatPatch(patchObj, options.headerOptions));
}
} }));
}
}
function createPatch(fileName, oldStr, newStr, oldHeader, newHeader, options) {
return createTwoFilesPatch(fileName, fileName, oldStr, newStr, oldHeader, newHeader, options);
}
/**
* Split `text` into an array of lines, including the trailing newline character (where present)
*/
function splitLines(text) {
var hasTrailingNl = text.endsWith('\n');
var result = text.split('\n').map(function (line) { return line + '\n'; });
if (hasTrailingNl) {
result.pop();
}
else {
result.push(result.pop().slice(0, -1));
}
return result;
}