Repository navigation
Expand file tree
/
Copy pathbuilder.js
More file actions
254 lines (212 loc) · 7.66 KB
/
Copy pathbuilder.js
File metadata and controls
254 lines (212 loc) · 7.66 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
import {TextBuffer} from 'atom';
import Hunk from './hunk';
import File, {nullFile} from './file';
import Patch from './patch';
import {Unchanged, Addition, Deletion, NoNewline} from './region';
import FilePatch from './file-patch';
import MultiFilePatch from './multi-file-patch';
export function buildFilePatch(diffs) {
const layeredBuffer = initializeBuffer();
let filePatch;
if (diffs.length === 0) {
filePatch = emptyDiffFilePatch();
} else if (diffs.length === 1) {
filePatch = singleDiffFilePatch(diffs[0], layeredBuffer);
} else if (diffs.length === 2) {
filePatch = dualDiffFilePatch(diffs[0], diffs[1], layeredBuffer);
} else {
throw new Error(`Unexpected number of diffs: ${diffs.length}`);
}
return new MultiFilePatch({filePatches: [filePatch], ...layeredBuffer});
}
export function buildMultiFilePatch(diffs) {
const layeredBuffer = initializeBuffer();
const byPath = new Map();
const actions = [];
let index = 0;
for (const diff of diffs) {
const thePath = diff.oldPath || diff.newPath;
if (diff.status === 'added' || diff.status === 'deleted') {
// Potential paired diff. Either a symlink deletion + content addition or a symlink addition +
// content deletion.
const otherHalf = byPath.get(thePath);
if (otherHalf) {
// The second half. Complete the paired diff, or fail if they have unexpected statuses or modes.
const [otherDiff, otherIndex] = otherHalf;
actions[otherIndex] = () => dualDiffFilePatch(diff, otherDiff, layeredBuffer);
byPath.delete(thePath);
} else {
// The first half we've seen.
byPath.set(thePath, [diff, index]);
index++;
}
} else {
actions[index] = () => singleDiffFilePatch(diff, layeredBuffer);
index++;
}
}
// Populate unpaired diffs that looked like they could be part of a pair, but weren't.
for (const [unpairedDiff, originalIndex] of byPath.values()) {
actions[originalIndex] = () => singleDiffFilePatch(unpairedDiff, layeredBuffer);
}
const filePatches = actions.map(action => action());
// Fix markers for patches with no hunks.
// Head position was moved everytime lines were appended.
filePatches.forEach(filePatch => {
if (filePatch.getHunks().length === 0) {
const marker = filePatch.getMarker();
marker.setHeadPosition(marker.getTailPosition());
}
});
return new MultiFilePatch({filePatches, ...layeredBuffer});
}
function emptyDiffFilePatch() {
return FilePatch.createNull();
}
function singleDiffFilePatch(diff, layeredBuffer) {
const wasSymlink = diff.oldMode === File.modes.SYMLINK;
const isSymlink = diff.newMode === File.modes.SYMLINK;
const [hunks, patchMarker] = buildHunks(diff, layeredBuffer);
let oldSymlink = null;
let newSymlink = null;
if (wasSymlink && !isSymlink) {
oldSymlink = diff.hunks[0].lines[0].slice(1);
} else if (!wasSymlink && isSymlink) {
newSymlink = diff.hunks[0].lines[0].slice(1);
} else if (wasSymlink && isSymlink) {
oldSymlink = diff.hunks[0].lines[0].slice(1);
newSymlink = diff.hunks[0].lines[2].slice(1);
}
const oldFile = diff.oldPath !== null || diff.oldMode !== null
? new File({path: diff.oldPath, mode: diff.oldMode, symlink: oldSymlink})
: nullFile;
const newFile = diff.newPath !== null || diff.newMode !== null
? new File({path: diff.newPath, mode: diff.newMode, symlink: newSymlink})
: nullFile;
const patch = new Patch({status: diff.status, hunks, marker: patchMarker, buffer: layeredBuffer.buffer});
return new FilePatch(oldFile, newFile, patch);
}
function dualDiffFilePatch(diff1, diff2, layeredBuffer) {
let modeChangeDiff, contentChangeDiff;
if (diff1.oldMode === File.modes.SYMLINK || diff1.newMode === File.modes.SYMLINK) {
modeChangeDiff = diff1;
contentChangeDiff = diff2;
} else {
modeChangeDiff = diff2;
contentChangeDiff = diff1;
}
const [hunks, patchMarker] = buildHunks(contentChangeDiff, layeredBuffer);
const filePath = contentChangeDiff.oldPath || contentChangeDiff.newPath;
const symlink = modeChangeDiff.hunks[0].lines[0].slice(1);
let status;
let oldMode, newMode;
let oldSymlink = null;
let newSymlink = null;
if (modeChangeDiff.status === 'added') {
// contents were deleted and replaced with symlink
status = 'deleted';
oldMode = contentChangeDiff.oldMode;
newMode = modeChangeDiff.newMode;
newSymlink = symlink;
} else if (modeChangeDiff.status === 'deleted') {
// contents were added after symlink was deleted
status = 'added';
oldMode = modeChangeDiff.oldMode;
oldSymlink = symlink;
newMode = contentChangeDiff.newMode;
} else {
throw new Error(`Invalid mode change diff status: ${modeChangeDiff.status}`);
}
const oldFile = new File({path: filePath, mode: oldMode, symlink: oldSymlink});
const newFile = new File({path: filePath, mode: newMode, symlink: newSymlink});
const patch = new Patch({status, hunks, marker: patchMarker, buffer: layeredBuffer.buffer});
return new FilePatch(oldFile, newFile, patch);
}
const CHANGEKIND = {
'+': Addition,
'-': Deletion,
' ': Unchanged,
'\\': NoNewline,
};
function initializeBuffer() {
const buffer = new TextBuffer();
buffer.retain();
const layers = ['patch', 'hunk', 'unchanged', 'addition', 'deletion', 'noNewline'].reduce((obj, key) => {
obj[key] = buffer.addMarkerLayer();
return obj;
}, {});
return {buffer, layers};
}
function buildHunks(diff, {buffer, layers}) {
const layersByKind = new Map([
[Unchanged, layers.unchanged],
[Addition, layers.addition],
[Deletion, layers.deletion],
[NoNewline, layers.noNewline],
]);
const hunks = [];
const patchStartRow = buffer.getLastRow();
let bufferRow = patchStartRow;
let nextLineLength = 0;
if (diff.hunks.length === 0) {
const patchMarker = layers.patch.markPosition(
[patchStartRow, 0],
{invalidate: 'never', exclusive: false},
);
return [hunks, patchMarker];
}
for (const hunkData of diff.hunks) {
const bufferStartRow = bufferRow;
const regions = [];
let LastChangeKind = null;
let currentRangeStart = bufferRow;
let lastLineLength = 0;
const finishCurrentRange = () => {
if (currentRangeStart === bufferRow) {
return;
}
regions.push(
new LastChangeKind(
layersByKind.get(LastChangeKind).markRange(
[[currentRangeStart, 0], [bufferRow - 1, lastLineLength]],
{invalidate: 'never', exclusive: false},
),
),
);
currentRangeStart = bufferRow;
};
for (const lineText of hunkData.lines) {
const bufferLine = lineText.slice(1) + '\n';
nextLineLength = lineText.length - 1;
buffer.append(bufferLine);
const ChangeKind = CHANGEKIND[lineText[0]];
if (ChangeKind === undefined) {
throw new Error(`Unknown diff status character: "${lineText[0]}"`);
}
if (ChangeKind !== LastChangeKind) {
finishCurrentRange();
}
LastChangeKind = ChangeKind;
bufferRow++;
lastLineLength = nextLineLength;
}
finishCurrentRange();
hunks.push(new Hunk({
oldStartRow: hunkData.oldStartLine,
newStartRow: hunkData.newStartLine,
oldRowCount: hunkData.oldLineCount,
newRowCount: hunkData.newLineCount,
sectionHeading: hunkData.heading,
marker: layers.hunk.markRange(
[[bufferStartRow, 0], [bufferRow - 1, nextLineLength]],
{invalidate: 'never', exclusive: false},
),
regions,
}));
}
const patchMarker = layers.patch.markRange(
[[patchStartRow, 0], [bufferRow - 1, nextLineLength]],
{invalidate: 'never', exclusive: false},
);
return [hunks, patchMarker];
}