Repository navigation
Expand file tree
/
Copy pathfile-patch.js
More file actions
380 lines (315 loc) · 10.8 KB
/
Copy pathfile-patch.js
File metadata and controls
380 lines (315 loc) · 10.8 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import {Emitter} from 'event-kit';
import {nullFile} from './file';
import Patch, {COLLAPSED} from './patch';
import {toGitPathSep} from '../../helpers';
export default class FilePatch {
static createNull() {
return new this(nullFile, nullFile, Patch.createNull());
}
static createHiddenFilePatch(oldFile, newFile, marker, renderStatus, showFn) {
return new this(oldFile, newFile, Patch.createHiddenPatch(marker, renderStatus, showFn));
}
constructor(oldFile, newFile, patch, rawPatches) {
this.oldFile = oldFile;
this.newFile = newFile;
this.patch = patch;
this.rawPatches = rawPatches;
this.emitter = new Emitter();
}
isPresent() {
return this.oldFile.isPresent() || this.newFile.isPresent() || this.patch.isPresent();
}
getRenderStatus() {
return this.patch.getRenderStatus();
}
getOldFile() {
return this.oldFile;
}
getNewFile() {
return this.newFile;
}
getRawContentPatch() {
if (!this.rawPatches) {
throw new Error('FilePatch was not parsed with {perserveOriginal: true}');
}
return this.rawPatches.content;
}
getPatch() {
return this.patch;
}
getMarker() {
return this.getPatch().getMarker();
}
getStartRange() {
return this.getPatch().getStartRange();
}
getOldPath() {
return this.getOldFile().getPath();
}
getNewPath() {
return this.getNewFile().getPath();
}
getOldMode() {
return this.getOldFile().getMode();
}
getNewMode() {
return this.getNewFile().getMode();
}
getOldSymlink() {
return this.getOldFile().getSymlink();
}
getNewSymlink() {
return this.getNewFile().getSymlink();
}
getFirstChangeRange() {
return this.getPatch().getFirstChangeRange();
}
getMaxLineNumberWidth() {
return this.getPatch().getMaxLineNumberWidth();
}
containsRow(row) {
return this.getPatch().containsRow(row);
}
didChangeExecutableMode() {
if (!this.oldFile.isPresent() || !this.newFile.isPresent()) {
return false;
}
return this.oldFile.isExecutable() && !this.newFile.isExecutable() ||
!this.oldFile.isExecutable() && this.newFile.isExecutable();
}
hasSymlink() {
return Boolean(this.getOldFile().getSymlink() || this.getNewFile().getSymlink());
}
hasTypechange() {
if (!this.oldFile.isPresent() || !this.newFile.isPresent()) {
return false;
}
return this.oldFile.isSymlink() && !this.newFile.isSymlink() ||
!this.oldFile.isSymlink() && this.newFile.isSymlink();
}
getPath() {
return this.getOldPath() || this.getNewPath();
}
getStatus() {
return this.getPatch().getStatus();
}
getHunks() {
return this.getPatch().getHunks();
}
updateMarkers(map) {
return this.patch.updateMarkers(map);
}
triggerCollapseIn(patchBuffer, {before, after}) {
if (!this.patch.getRenderStatus().isVisible()) {
return false;
}
const oldPatch = this.patch;
const oldRange = oldPatch.getRange().copy();
const insertionPosition = oldRange.start;
const exclude = new Set([...before, ...after]);
const {patchBuffer: subPatchBuffer, markerMap} = patchBuffer.extractPatchBuffer(oldRange, {exclude});
oldPatch.destroyMarkers();
oldPatch.updateMarkers(markerMap);
// Delete the separating newline after the collapsing patch, if any.
if (!oldRange.isEmpty()) {
patchBuffer.getBuffer().deleteRow(insertionPosition.row);
}
const patchMarker = patchBuffer.markPosition(
Patch.layerName,
insertionPosition,
{invalidate: 'never', exclusive: true},
);
this.patch = Patch.createHiddenPatch(patchMarker, COLLAPSED, () => {
return {patch: oldPatch, patchBuffer: subPatchBuffer};
});
this.didChangeRenderStatus();
return true;
}
triggerExpandIn(patchBuffer, {before, after}) {
if (this.patch.getRenderStatus().isVisible()) {
return false;
}
const {patch: nextPatch, patchBuffer: subPatchBuffer} = this.patch.show();
const atStart = this.patch.getInsertionPoint().isEqual([0, 0]);
const atEnd = this.patch.getInsertionPoint().isEqual(patchBuffer.getBuffer().getEndPosition());
const willHaveContent = !subPatchBuffer.getBuffer().isEmpty();
// The expanding patch's insertion point is just after the unmarked newline that separates adjacent visible
// patches:
// <p0> '\n' * <p1> '\n' <p2>
//
// If it's to become the first (visible) patch, its insertion point is at [0, 0]:
// * <p0> '\n' <p1> '\n' <p2>
//
// If it's to become the final (visible) patch, its insertion point is at the buffer end:
// <p0> '\n' <p1> '\n' <p2> *
//
// Insert a newline *before* the expanding patch if we're inserting at the buffer's end, but the buffer is non-empty
// (so it isn't also the end of the buffer). Insert a newline *after* the expanding patch when inserting anywhere
// but the buffer's end.
if (willHaveContent && atEnd && !atStart) {
const beforeNewline = [];
const afterNewline = after.slice();
for (const marker of before) {
if (marker.getRange().isEmpty()) {
afterNewline.push(marker);
} else {
beforeNewline.push(marker);
}
}
patchBuffer
.createInserterAt(this.patch.getInsertionPoint())
.keepBefore(beforeNewline)
.keepAfter(afterNewline)
.insert('\n')
.apply();
}
patchBuffer
.createInserterAt(this.patch.getInsertionPoint())
.keepBefore(before)
.keepAfter(after)
.insertPatchBuffer(subPatchBuffer, {callback: map => nextPatch.updateMarkers(map)})
.insert(!atEnd ? '\n' : '')
.apply();
this.patch.destroyMarkers();
this.patch = nextPatch;
this.didChangeRenderStatus();
return true;
}
didChangeRenderStatus() {
return this.emitter.emit('change-render-status', this);
}
onDidChangeRenderStatus(callback) {
return this.emitter.on('change-render-status', callback);
}
clone(opts = {}) {
return new this.constructor(
opts.oldFile !== undefined ? opts.oldFile : this.oldFile,
opts.newFile !== undefined ? opts.newFile : this.newFile,
opts.patch !== undefined ? opts.patch : this.patch,
);
}
getStartingMarkers() {
return this.patch.getStartingMarkers();
}
getEndingMarkers() {
return this.patch.getEndingMarkers();
}
buildStagePatchForLines(originalBuffer, nextPatchBuffer, selectedLineSet) {
let newFile = this.getNewFile();
if (this.getStatus() === 'deleted') {
if (
this.patch.getChangedLineCount() === selectedLineSet.size &&
Array.from(selectedLineSet, row => this.patch.containsRow(row)).every(Boolean)
) {
// Whole file deletion staged.
newFile = nullFile;
} else {
// Partial file deletion, which becomes a modification.
newFile = this.getOldFile();
}
}
const patch = this.patch.buildStagePatchForLines(
originalBuffer,
nextPatchBuffer,
selectedLineSet,
);
return this.clone({newFile, patch});
}
buildUnstagePatchForLines(originalBuffer, nextPatchBuffer, selectedLineSet) {
const nonNullFile = this.getNewFile().isPresent() ? this.getNewFile() : this.getOldFile();
let oldFile = this.getNewFile();
let newFile = nonNullFile;
if (this.getStatus() === 'added') {
if (
selectedLineSet.size === this.patch.getChangedLineCount() &&
Array.from(selectedLineSet, row => this.patch.containsRow(row)).every(Boolean)
) {
// Ensure that newFile is null if the patch is an addition because we're deleting the entire file from the
// index. If a symlink was deleted and replaced by a non-symlink file, we don't want the symlink entry to muck
// up the patch.
oldFile = nonNullFile;
newFile = nullFile;
}
} else if (this.getStatus() === 'deleted') {
if (
selectedLineSet.size === this.patch.getChangedLineCount() &&
Array.from(selectedLineSet, row => this.patch.containsRow(row)).every(Boolean)
) {
oldFile = nullFile;
newFile = nonNullFile;
}
}
const patch = this.patch.buildUnstagePatchForLines(
originalBuffer,
nextPatchBuffer,
selectedLineSet,
);
return this.clone({oldFile, newFile, patch});
}
toStringIn(buffer) {
if (!this.isPresent()) {
return '';
}
if (this.hasTypechange()) {
const left = this.clone({
newFile: nullFile,
patch: this.getOldSymlink() ? this.getPatch().clone({status: 'deleted'}) : this.getPatch(),
});
const right = this.clone({
oldFile: nullFile,
patch: this.getNewSymlink() ? this.getPatch().clone({status: 'added'}) : this.getPatch(),
});
return left.toStringIn(buffer) + right.toStringIn(buffer);
} else if (this.getStatus() === 'added' && this.getNewFile().isSymlink()) {
const symlinkPath = this.getNewSymlink();
return this.getHeaderString() + `@@ -0,0 +1 @@\n+${symlinkPath}\n\\ No newline at end of file\n`;
} else if (this.getStatus() === 'deleted' && this.getOldFile().isSymlink()) {
const symlinkPath = this.getOldSymlink();
return this.getHeaderString() + `@@ -1 +0,0 @@\n-${symlinkPath}\n\\ No newline at end of file\n`;
} else {
return this.getHeaderString() + this.getPatch().toStringIn(buffer);
}
}
/*
* Construct a String containing diagnostic information about the internal state of this FilePatch.
*/
/* istanbul ignore next */
inspect(opts = {}) {
const options = {
indent: 0,
...opts,
};
let indentation = '';
for (let i = 0; i < options.indent; i++) {
indentation += ' ';
}
let inspectString = `${indentation}(FilePatch `;
if (this.getOldPath() !== this.getNewPath()) {
inspectString += `oldPath=${this.getOldPath()} newPath=${this.getNewPath()}`;
} else {
inspectString += `path=${this.getPath()}`;
}
inspectString += '\n';
inspectString += this.patch.inspect({indent: options.indent + 2});
inspectString += `${indentation})\n`;
return inspectString;
}
getHeaderString() {
const fromPath = this.getOldPath() || this.getNewPath();
const toPath = this.getNewPath() || this.getOldPath();
let header = `diff --git a/${toGitPathSep(fromPath)} b/${toGitPathSep(toPath)}`;
header += '\n';
if (this.getStatus() === 'added') {
header += `new file mode ${this.getNewMode()}`;
header += '\n';
} else if (this.getStatus() === 'deleted') {
header += `deleted file mode ${this.getOldMode()}`;
header += '\n';
}
header += this.getOldPath() ? `--- a/${toGitPathSep(this.getOldPath())}` : '--- /dev/null';
header += '\n';
header += this.getNewPath() ? `+++ b/${toGitPathSep(this.getNewPath())}` : '+++ /dev/null';
header += '\n';
return header;
}
}