1.7 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 {CommandArg, RepoRelativePath, UncommittedChanges} from '../types';
13
14import {Operation} from './Operation';
15
16/**
17 * This deletes untracked files from disk. Often used in conjunction with "Discard" aka `goto --clean .`
18 * If an array of files is provided, only purge those files.
19 */
20export class PurgeOperation extends Operation {
21 static opName = 'Purge';
22
23 constructor(private files: Array<RepoRelativePath> = []) {
24 super('PurgeOperation');
25 }
26
27 getArgs() {
28 const args: Array<CommandArg> = ['purge', '--files', '--abort-on-err'];
29 if (this.files.length > 0) {
30 // Tag file arguments specially so the remote repo can convert them to the proper cwd-relative format.
31 args.push({
32 type: 'repo-relative-file-list' as const,
33 paths: this.files,
34 });
35 }
36 return args;
37 }
38
39 makeOptimisticUncommittedChangesApplier?(
40 context: UncommittedChangesPreviewContext,
41 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
42 const filesToHide = new Set(this.files);
43 if (
44 context.uncommittedChanges.length === 0 ||
45 // no untracked files should be left
46 context.uncommittedChanges.every(change => !filesToHide.has(change.path))
47 ) {
48 return undefined;
49 }
50
51 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
52 // remove all untracked files
53 return changes.filter(change => !filesToHide.has(change.path));
54 };
55 return func;
56 }
57}
58