forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.js
More file actions
544 lines (410 loc) · 12.2 KB
/
Copy pathstate.js
File metadata and controls
544 lines (410 loc) · 12.2 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import {nullCommit} from '../commit';
import BranchSet from '../branch-set';
import RemoteSet from '../remote-set';
import {nullOperationStates} from '../operation-states';
import MultiFilePatch from '../patch/multi-file-patch';
/**
* Map of registered subclasses to allow states to transition to one another without circular dependencies.
* Subclasses of State should call `State.register` to add themselves here.
*/
const stateConstructors = new Map();
/**
* Base class for Repository states. Implements default "null" behavior.
*/
export default class State {
constructor(repository) {
this.repository = repository;
}
static register(Subclass) {
stateConstructors.set(Subclass.name, Subclass);
}
// This state has just been entered. Perform any asynchronous initialization that needs to occur.
start() {
return Promise.resolve();
}
// State probe predicates ////////////////////////////////////////////////////////////////////////////////////////////
// Allow external callers to identify which state a Repository is in if necessary.
isLoadingGuess() {
return false;
}
isAbsentGuess() {
return false;
}
isAbsent() {
return false;
}
isLoading() {
return false;
}
isEmpty() {
return false;
}
isPresent() {
return false;
}
isTooLarge() {
return false;
}
isDestroyed() {
return false;
}
// Behavior probe predicates /////////////////////////////////////////////////////////////////////////////////////////
// Determine specific rendering behavior based on the current state.
isUndetermined() {
return false;
}
showGitTabInit() {
return false;
}
showGitTabInitInProgress() {
return false;
}
showGitTabLoading() {
return false;
}
showStatusBarTiles() {
return false;
}
hasDirectory() {
return true;
}
// Lifecycle actions /////////////////////////////////////////////////////////////////////////////////////////////////
// These generally default to rejecting a Promise with an error.
init() {
return unsupportedOperationPromise(this, 'init');
}
clone(remoteUrl) {
return unsupportedOperationPromise(this, 'clone');
}
destroy() {
return this.transitionTo('Destroyed');
}
/* istanbul ignore next */
refresh() {
// No-op
}
/* istanbul ignore next */
observeFilesystemChange(events) {
this.repository.refresh();
}
/* istanbul ignore next */
updateCommitMessageAfterFileSystemChange() {
// this is only used in unit tests, we don't need no stinkin coverage
this.repository.refresh();
}
// Git operations ////////////////////////////////////////////////////////////////////////////////////////////////////
// These default to rejecting a Promise with an error stating that the operation is not supported in the current
// state.
// Staging and unstaging
stageFiles(paths) {
return unsupportedOperationPromise(this, 'stageFiles');
}
unstageFiles(paths) {
return unsupportedOperationPromise(this, 'unstageFiles');
}
stageFilesFromParentCommit(paths) {
return unsupportedOperationPromise(this, 'stageFilesFromParentCommit');
}
applyPatchToIndex(patch) {
return unsupportedOperationPromise(this, 'applyPatchToIndex');
}
applyPatchToWorkdir(patch) {
return unsupportedOperationPromise(this, 'applyPatchToWorkdir');
}
// Committing
commit(message, options) {
return unsupportedOperationPromise(this, 'commit');
}
// Merging
merge(branchName) {
return unsupportedOperationPromise(this, 'merge');
}
abortMerge() {
return unsupportedOperationPromise(this, 'abortMerge');
}
checkoutSide(side, paths) {
return unsupportedOperationPromise(this, 'checkoutSide');
}
mergeFile(oursPath, commonBasePath, theirsPath, resultPath) {
return unsupportedOperationPromise(this, 'mergeFile');
}
writeMergeConflictToIndex(filePath, commonBaseSha, oursSha, theirsSha) {
return unsupportedOperationPromise(this, 'writeMergeConflictToIndex');
}
// Checkout
checkout(revision, options = {}) {
return unsupportedOperationPromise(this, 'checkout');
}
checkoutPathsAtRevision(paths, revision = 'HEAD') {
return unsupportedOperationPromise(this, 'checkoutPathsAtRevision');
}
// Reset
undoLastCommit() {
return unsupportedOperationPromise(this, 'undoLastCommit');
}
// Remote interactions
fetch(branchName) {
return unsupportedOperationPromise(this, 'fetch');
}
pull(branchName) {
return unsupportedOperationPromise(this, 'pull');
}
push(branchName) {
return unsupportedOperationPromise(this, 'push');
}
// Configuration
setConfig(option, value, {replaceAll} = {}) {
return unsupportedOperationPromise(this, 'setConfig');
}
unsetConfig(option) {
return unsupportedOperationPromise(this, 'unsetConfig');
}
// Direct blob interactions
createBlob({filePath, stdin} = {}) {
return unsupportedOperationPromise(this, 'createBlob');
}
expandBlobToFile(absFilePath, sha) {
return unsupportedOperationPromise(this, 'expandBlobToFile');
}
// Discard history
createDiscardHistoryBlob() {
return unsupportedOperationPromise(this, 'createDiscardHistoryBlob');
}
updateDiscardHistory() {
return unsupportedOperationPromise(this, 'updateDiscardHistory');
}
storeBeforeAndAfterBlobs(filePaths, isSafe, destructiveAction, partialDiscardFilePath = null) {
return unsupportedOperationPromise(this, 'storeBeforeAndAfterBlobs');
}
restoreLastDiscardInTempFiles(isSafe, partialDiscardFilePath = null) {
return unsupportedOperationPromise(this, 'restoreLastDiscardInTempFiles');
}
popDiscardHistory(partialDiscardFilePath = null) {
return unsupportedOperationPromise(this, 'popDiscardHistory');
}
clearDiscardHistory(partialDiscardFilePath = null) {
return unsupportedOperationPromise(this, 'clearDiscardHistory');
}
discardWorkDirChangesForPaths(paths) {
return unsupportedOperationPromise(this, 'discardWorkDirChangesForPaths');
}
// Accessors /////////////////////////////////////////////////////////////////////////////////////////////////////////
// When possible, these default to "empty" results when invoked in states that don't have information available, or
// fail in a way that's consistent with the requested information not being found.
// Index queries
getStatusBundle() {
return Promise.resolve({
stagedFiles: {},
unstagedFiles: {},
mergeConflictFiles: {},
branch: {
oid: null,
head: null,
upstream: null,
aheadBehind: {ahead: null, behind: null},
},
});
}
getStatusesForChangedFiles() {
return Promise.resolve({
stagedFiles: [],
unstagedFiles: [],
mergeConflictFiles: [],
});
}
getFilePatchForPath(filePath, options = {}) {
return Promise.resolve(MultiFilePatch.createNull());
}
getDiffsForFilePath(filePath, options = {}) {
return Promise.resolve([]);
}
getStagedChangesPatch() {
return Promise.resolve(MultiFilePatch.createNull());
}
readFileFromIndex(filePath) {
return Promise.reject(new Error(`fatal: Path ${filePath} does not exist (neither on disk nor in the index).`));
}
// Commit access
getLastCommit() {
return Promise.resolve(nullCommit);
}
getCommit() {
return Promise.resolve(nullCommit);
}
getRecentCommits() {
return Promise.resolve([]);
}
isCommitPushed(sha) {
return false;
}
// Author information
getAuthors() {
return Promise.resolve([]);
}
// Branches
getBranches() {
return Promise.resolve(new BranchSet());
}
getHeadDescription() {
return Promise.resolve('(no repository)');
}
// Merging and rebasing status
isMerging() {
return Promise.resolve(false);
}
isRebasing() {
return Promise.resolve(false);
}
// Remotes
getRemotes() {
return Promise.resolve(new RemoteSet([]));
}
addRemote() {
return unsupportedOperationPromise(this, 'addRemote');
}
getAheadCount(branchName) {
return Promise.resolve(null);
}
getBehindCount(branchName) {
return Promise.resolve(null);
}
getConfig(option, {local} = {}) {
return Promise.resolve(null);
}
// Direct blob access
getBlobContents(sha) {
return Promise.reject(new Error(`fatal: Not a valid object name ${sha}`));
}
// Discard history
hasDiscardHistory(partialDiscardFilePath = null) {
return false;
}
getDiscardHistory(partialDiscardFilePath = null) {
return [];
}
getLastHistorySnapshots(partialDiscardFilePath = null) {
return null;
}
// Atom repo state
getOperationStates() {
return nullOperationStates;
}
setCommitMessage(message) {
return unsupportedOperationPromise(this, 'setCommitMessage');
}
getCommitMessage() {
return '';
}
fetchCommitMessageTemplate() {
return unsupportedOperationPromise(this, 'fetchCommitMessageTemplate');
}
// Cache
getCache() {
return null;
}
// Internal //////////////////////////////////////////////////////////////////////////////////////////////////////////
// Non-delegated methods that provide subclasses with convenient access to Repository properties.
git() {
return this.repository.git;
}
workdir() {
return this.repository.getWorkingDirectoryPath();
}
// Call methods on the active Repository state, even if the state has transitioned beneath you.
// Use this to perform operations within `start()` methods to guard against interrupted state transitions.
current() {
return this.repository.state;
}
// pipeline
executePipelineAction(...args) {
return this.repository.executePipelineAction(...args);
}
// Return a Promise that will resolve once the state transitions from Loading.
getLoadPromise() {
return this.repository.getLoadPromise();
}
getRemoteForBranch(branchName) {
return this.repository.getRemoteForBranch(branchName);
}
saveDiscardHistory() {
return this.repository.saveDiscardHistory();
}
// Initiate a transition to another state.
transitionTo(stateName, ...payload) {
const StateConstructor = stateConstructors.get(stateName);
/* istanbul ignore if */
if (StateConstructor === undefined) {
throw new Error(`Attempt to transition to unrecognized state ${stateName}`);
}
return this.repository.transition(this, StateConstructor, ...payload);
}
// Event broadcast
didDestroy() {
return this.repository.emitter.emit('did-destroy');
}
didUpdate() {
return this.repository.emitter.emit('did-update');
}
// Direct git access
// Non-delegated git operations for internal use within states.
/* istanbul ignore next */
directResolveDotGitDir() {
return Promise.resolve(null);
}
/* istanbul ignore next */
directGetConfig(key, options = {}) {
return Promise.resolve(null);
}
/* istanbul ignore next */
directGetBlobContents() {
return Promise.reject(new Error('Not a valid object name'));
}
/* istanbul ignore next */
directInit() {
return Promise.resolve();
}
/* istanbul ignore next */
directClone(remoteUrl, options) {
return Promise.resolve();
}
// Deferred operations
// Direct raw git operations to the current state, even if the state has been changed. Use these methods within
// start() methods.
resolveDotGitDir() {
return this.current().directResolveDotGitDir();
}
doInit(workdir) {
return this.current().directInit();
}
doClone(remoteUrl, options) {
return this.current().directClone(remoteUrl, options);
}
// Parse a DiscardHistory payload from the SHA recorded in config.
async loadHistoryPayload() {
const historySha = await this.current().directGetConfig('atomGithub.historySha');
if (!historySha) {
return {};
}
let blob;
try {
blob = await this.current().directGetBlobContents(historySha);
} catch (e) {
if (/Not a valid object name/.test(e.stdErr)) {
return {};
}
throw e;
}
try {
return JSON.parse(blob);
} catch (e) {
return {};
}
}
// Debugging assistance.
toString() {
return this.constructor.name;
}
}
function unsupportedOperationPromise(self, opName) {
return Promise.reject(new Error(`${opName} is not available in ${self} state`));
}