forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.js
More file actions
326 lines (267 loc) · 8.11 KB
/
patch.js
File metadata and controls
326 lines (267 loc) · 8.11 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
// Builders for classes related to MultiFilePatches.
import {TextBuffer} from 'atom';
import MultiFilePatch from '../../lib/models/patch/multi-file-patch';
import FilePatch from '../../lib/models/patch/file-patch';
import File, {nullFile} from '../../lib/models/patch/file';
import Patch from '../../lib/models/patch/patch';
import Hunk from '../../lib/models/patch/hunk';
import {Unchanged, Addition, Deletion, NoNewline} from '../../lib/models/patch/region';
class LayeredBuffer {
constructor() {
this.buffer = new TextBuffer();
this.layers = ['patch', 'hunk', 'unchanged', 'addition', 'deletion', 'noNewline'].reduce((layers, name) => {
layers[name] = this.buffer.addMarkerLayer();
return layers;
}, {});
}
getInsertionPoint() {
return this.buffer.getEndPosition();
}
getLayer(markerLayerName) {
const layer = this.layers[markerLayerName];
if (!layer) {
throw new Error(`invalid marker layer name: ${markerLayerName}`);
}
return layer;
}
appendMarked(markerLayerName, lines) {
const startPosition = this.buffer.getEndPosition();
const layer = this.getLayer(markerLayerName);
this.buffer.append(lines.join('\n'));
const marker = layer.markRange([startPosition, this.buffer.getEndPosition()], {exclusive: true});
this.buffer.append('\n');
return marker;
}
markFrom(markerLayerName, startPosition) {
const endPosition = this.buffer.getEndPosition().translate([-1, Infinity]);
const layer = this.getLayer(markerLayerName);
return layer.markRange([startPosition, endPosition], {exclusive: true});
}
wrapReturn(object) {
return {
buffer: this.buffer,
layers: this.layers,
...object,
};
}
}
class MultiFilePatchBuilder {
constructor(layeredBuffer = null) {
this.layeredBuffer = layeredBuffer;
this.filePatches = [];
}
addFilePatch(block = () => {}) {
const filePatch = new FilePatchBuilder(this.layeredBuffer);
block(filePatch);
this.filePatches.push(filePatch.build().filePatch);
return this;
}
build() {
return this.layeredBuffer.wrapReturn({
multiFilePatch: new MultiFilePatch({
buffer: this.layeredBuffer.buffer,
layers: this.layeredBuffer.layers,
filePatches: this.filePatches,
}),
});
}
}
class FilePatchBuilder {
constructor(layeredBuffer = null) {
this.layeredBuffer = layeredBuffer;
this.oldFile = new File({path: 'file', mode: File.modes.NORMAL});
this.newFile = null;
this.patchBuilder = new PatchBuilder(this.layeredBuffer);
}
setOldFile(block) {
const file = new FileBuilder();
block(file);
this.oldFile = file.build().file;
return this;
}
nullOldFile() {
this.oldFile = nullFile;
return this;
}
setNewFile(block) {
const file = new FileBuilder();
block(file);
this.newFile = file.build().file;
return this;
}
nullNewFile() {
this.newFile = nullFile;
return this;
}
status(...args) {
this.patchBuilder.status(...args);
return this;
}
addHunk(...args) {
this.patchBuilder.addHunk(...args);
return this;
}
empty() {
this.patchBuilder.empty();
return this;
}
build() {
const {patch} = this.patchBuilder.build();
if (this.newFile === null) {
this.newFile = this.oldFile.clone();
}
return this.layeredBuffer.wrapReturn({
filePatch: new FilePatch(this.oldFile, this.newFile, patch),
});
}
}
class FileBuilder {
constructor() {
this._path = 'file.txt';
this._mode = File.modes.NORMAL;
this._symlink = null;
}
path(thePath) {
this._path = thePath;
return this;
}
mode(theMode) {
this._mode = theMode;
return this;
}
executable() {
return this.mode('100755');
}
symlinkTo(destinationPath) {
this._symlink = destinationPath;
return this.mode('120000');
}
build() {
return {file: new File({path: this._path, mode: this._mode, symlink: this._symlink})};
}
}
class PatchBuilder {
constructor(layeredBuffer = null) {
this.layeredBuffer = layeredBuffer;
this._status = 'modified';
this.hunks = [];
this.patchStart = this.layeredBuffer.getInsertionPoint();
this.drift = 0;
this.explicitlyEmpty = false;
}
status(st) {
if (['modified', 'added', 'deleted'].indexOf(st) === -1) {
throw new Error(`Unrecognized status: ${st} (must be 'modified', 'added' or 'deleted')`);
}
this._status = st;
return this;
}
addHunk(block = () => {}) {
const builder = new HunkBuilder(this.layeredBuffer, this.drift);
block(builder);
const {hunk, drift} = builder.build();
this.hunks.push(hunk);
this.drift = drift;
return this;
}
empty() {
this.explicitlyEmpty = true;
return this;
}
build() {
if (this.hunks.length === 0 && !this.explicitlyEmpty) {
if (this._status === 'modified') {
this.addHunk(hunk => hunk.oldRow(1).unchanged('0000').added('0001').deleted('0002').unchanged('0003'));
this.addHunk(hunk => hunk.oldRow(10).unchanged('0004').added('0005').deleted('0006').unchanged('0007'));
} else if (this._status === 'added') {
this.addHunk(hunk => hunk.oldRow(1).added('0000', '0001', '0002', '0003'));
} else if (this._status === 'deleted') {
this.addHunk(hunk => hunk.oldRow(1).deleted('0000', '0001', '0002', '0003'));
}
}
const marker = this.layeredBuffer.markFrom('patch', this.patchStart);
return this.layeredBuffer.wrapReturn({
patch: new Patch({status: this._status, hunks: this.hunks, marker}),
});
}
}
class HunkBuilder {
constructor(layeredBuffer = null, drift = 0) {
this.layeredBuffer = layeredBuffer;
this.drift = drift;
this.oldStartRow = 0;
this.oldRowCount = null;
this.newStartRow = null;
this.newRowCount = null;
this.sectionHeading = "don't care";
this.hunkStartPoint = this.layeredBuffer.getInsertionPoint();
this.regions = [];
}
oldRow(rowNumber) {
this.oldStartRow = rowNumber;
return this;
}
unchanged(...lines) {
this.regions.push(new Unchanged(this.layeredBuffer.appendMarked('unchanged', lines)));
return this;
}
added(...lines) {
this.regions.push(new Addition(this.layeredBuffer.appendMarked('addition', lines)));
return this;
}
deleted(...lines) {
this.regions.push(new Deletion(this.layeredBuffer.appendMarked('deletion', lines)));
return this;
}
noNewline() {
this.regions.push(new NoNewline(this.layeredBuffer.appendMarked('noNewline', [' No newline at end of file'])));
return this;
}
build() {
if (this.regions.length === 0) {
this.unchanged('0000').added('0001').deleted('0002').unchanged('0003');
}
if (this.oldRowCount === null) {
this.oldRowCount = this.regions.reduce((count, region) => region.when({
unchanged: () => count + region.bufferRowCount(),
deletion: () => count + region.bufferRowCount(),
default: () => count,
}), 0);
}
if (this.newStartRow === null) {
this.newStartRow = this.oldStartRow + this.drift;
}
if (this.newRowCount === null) {
this.newRowCount = this.regions.reduce((count, region) => region.when({
unchanged: () => count + region.bufferRowCount(),
addition: () => count + region.bufferRowCount(),
default: () => count,
}), 0);
}
const marker = this.layeredBuffer.markFrom('hunk', this.hunkStartPoint);
return this.layeredBuffer.wrapReturn({
hunk: new Hunk({
oldStartRow: this.oldStartRow,
oldRowCount: this.oldRowCount,
newStartRow: this.newStartRow,
newRowCount: this.newRowCount,
sectionHeading: this.sectionHeading,
marker,
regions: this.regions,
}),
drift: this.drift + this.newRowCount - this.oldRowCount,
});
}
}
export function multiFilePatchBuilder() {
return new MultiFilePatchBuilder(new LayeredBuffer());
}
export function filePatchBuilder() {
return new FilePatchBuilder(new LayeredBuffer());
}
export function patchBuilder() {
return new PatchBuilder(new LayeredBuffer());
}
export function hunkBuilder() {
return new HunkBuilder(new LayeredBuffer());
}