1.8 KB66 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 {RepoRelativePath, UncommittedChanges} from '../types';
13
14import {Operation} from './Operation';
15
16/**
17 * `sl addremove` adds all untracked files, and forgets all missing files.
18 * If filepaths are passed, only those file paths will be used, like doing a bulk `sl add` or `sl forget`.
19 * If filepaths is empty array, all untracked/missing files will be affected.
20 */
21export class AddRemoveOperation extends Operation {
22 constructor(private paths: Array<RepoRelativePath>) {
23 super('AddRemoveOperation');
24 }
25
26 static opName = 'AddRemove';
27
28 getArgs() {
29 return [
30 'addremove',
31 ...this.paths.map(path => ({
32 type: 'repo-relative-file' as const,
33 path,
34 })),
35 ];
36 }
37
38 makeOptimisticUncommittedChangesApplier?(
39 context: UncommittedChangesPreviewContext,
40 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
41 const allFiles = this.paths.length === 0;
42 if (
43 context.uncommittedChanges.every(
44 change =>
45 (allFiles || this.paths.includes(change.path)) &&
46 change.status !== '?' &&
47 change.status !== '!',
48 )
49 ) {
50 return undefined;
51 }
52
53 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
54 return changes.map(change =>
55 allFiles || this.paths.includes(change.path)
56 ? {
57 path: change.path,
58 status: change.status === '?' ? 'A' : change.status === '!' ? 'R' : change.status,
59 }
60 : change,
61 );
62 };
63 return func;
64 }
65}
66