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