1.8 KB58 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {
9 ApplyUncommittedChangesPreviewsFuncType,
10 UncommittedChangesPreviewContext,
11} from '../previews';
12import type {ShelvedChange, UncommittedChanges} from '../types';
13
14import {Operation} from './Operation';
15
16export class UnshelveOperation extends Operation {
17 constructor(
18 private shelvedChange: ShelvedChange,
19 private keep: boolean,
20 ) {
21 super('UnshelveOperation');
22 }
23
24 static opName = 'Unshelve';
25
26 getArgs() {
27 const args = ['unshelve'];
28 if (this.keep) {
29 args.push('--keep');
30 }
31 args.push('--name', this.shelvedChange.name);
32 return args;
33 }
34
35 makeOptimisticUncommittedChangesApplier?(
36 context: UncommittedChangesPreviewContext,
37 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
38 const shelvedChangedFiles = this.shelvedChange.filesSample;
39 const preexistingChanges = new Set(context.uncommittedChanges.map(change => change.path));
40
41 if (shelvedChangedFiles.every(file => preexistingChanges.has(file.path))) {
42 // once every file to unshelve appears in the output, the unshelve has reflected in the latest fetch.
43 return undefined;
44 }
45
46 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
47 // You could have uncommitted changes before unshelving, so we need to include
48 // shelved changes AND the existing uncommitted changes.
49 // But it's also possible to have changed a file changed by the commit, so we need to de-dupe.
50 const newChanges = this.shelvedChange.filesSample.filter(
51 file => !preexistingChanges.has(file.path),
52 );
53 return [...changes, ...newChanges];
54 };
55 return func;
56 }
57}
58