addons/isl/src/operations/PurgeOperation.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {
b69ab319 ApplyUncommittedChangesPreviewsFuncType,
b69ab3110 UncommittedChangesPreviewContext,
b69ab3111} from '../previews';
b69ab3112import type {CommandArg, RepoRelativePath, UncommittedChanges} from '../types';
b69ab3113
b69ab3114import {Operation} from './Operation';
b69ab3115
b69ab3116/**
b69ab3117 * This deletes untracked files from disk. Often used in conjunction with "Discard" aka `goto --clean .`
b69ab3118 * If an array of files is provided, only purge those files.
b69ab3119 */
b69ab3120export class PurgeOperation extends Operation {
b69ab3121 static opName = 'Purge';
b69ab3122
b69ab3123 constructor(private files: Array<RepoRelativePath> = []) {
b69ab3124 super('PurgeOperation');
b69ab3125 }
b69ab3126
b69ab3127 getArgs() {
b69ab3128 const args: Array<CommandArg> = ['purge', '--files', '--abort-on-err'];
b69ab3129 if (this.files.length > 0) {
b69ab3130 // Tag file arguments specially so the remote repo can convert them to the proper cwd-relative format.
b69ab3131 args.push({
b69ab3132 type: 'repo-relative-file-list' as const,
b69ab3133 paths: this.files,
b69ab3134 });
b69ab3135 }
b69ab3136 return args;
b69ab3137 }
b69ab3138
b69ab3139 makeOptimisticUncommittedChangesApplier?(
b69ab3140 context: UncommittedChangesPreviewContext,
b69ab3141 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3142 const filesToHide = new Set(this.files);
b69ab3143 if (
b69ab3144 context.uncommittedChanges.length === 0 ||
b69ab3145 // no untracked files should be left
b69ab3146 context.uncommittedChanges.every(change => !filesToHide.has(change.path))
b69ab3147 ) {
b69ab3148 return undefined;
b69ab3149 }
b69ab3150
b69ab3151 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3152 // remove all untracked files
b69ab3153 return changes.filter(change => !filesToHide.has(change.path));
b69ab3154 };
b69ab3155 return func;
b69ab3156 }
b69ab3157}