addons/isl/src/operations/DiscardOperation.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 {ImportStack} from 'shared/types/stack';
b69ab319import type {PartialSelection} from '../partialSelection';
b69ab3110import type {
b69ab3111 ApplyUncommittedChangesPreviewsFuncType,
b69ab3112 UncommittedChangesPreviewContext,
b69ab3113} from '../previews';
b69ab3114import type {RepoRelativePath, UncommittedChanges} from '../types';
b69ab3115
b69ab3116import {t} from '../i18n';
b69ab3117import {Operation} from './Operation';
b69ab3118
b69ab3119/**
b69ab3120 * "Discard" is not an actual command, but the effect of removing all uncommitted changes is accomplished by `goto --clean .`
b69ab3121 * This leaves behind untracked files, which may be separately removed by `purge --files`.
b69ab3122 */
b69ab3123export class DiscardOperation extends Operation {
b69ab3124 static opName = 'Discard';
b69ab3125
b69ab3126 constructor() {
b69ab3127 super('DiscardOperation');
b69ab3128 }
b69ab3129
b69ab3130 getArgs() {
b69ab3131 const args = ['goto', '--clean', '.'];
b69ab3132 return args;
b69ab3133 }
b69ab3134
b69ab3135 makeOptimisticUncommittedChangesApplier?(
b69ab3136 context: UncommittedChangesPreviewContext,
b69ab3137 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3138 const trackedChangeTypes = ['M', 'A', 'R', '!'];
b69ab3139 if (
b69ab3140 context.uncommittedChanges.length === 0 ||
b69ab3141 // some files may become untracked after clean goto
b69ab3142 context.uncommittedChanges.every(change => !trackedChangeTypes.includes(change.status))
b69ab3143 ) {
b69ab3144 return undefined;
b69ab3145 }
b69ab3146
b69ab3147 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3148 // clean goto leaves behind untracked files
b69ab3149 return changes.filter(change => !trackedChangeTypes.includes(change.status));
b69ab3150 };
b69ab3151 return func;
b69ab3152 }
b69ab3153}
b69ab3154
b69ab3155/**
b69ab3156 * Removes selected uncommitted changes to make them disappare from `status` or `diff`.
b69ab3157 * - Delete selected `A` or `?` files, so they disappear from `status`
b69ab3158 * - Restore selected `R` or `!` files, so they disappear from `status`.
b69ab3159 * - Revert selected `M` files, so they disappear from status.
b69ab3160 * - For partially selected files, their content will be reverted by dropping
b69ab3161 * the selected changes (line insertions or deletions), similar to `revert -i`.
b69ab3162 * The selected changes will disappear from `diff` output.
b69ab3163 *
b69ab3164 * This might replace DiscardOperation in the future. For now they might still be different,
b69ab3165 * since this operation only writes the working copy without changing the dirstate.
b69ab3166 */
b69ab3167export class PartialDiscardOperation extends Operation {
b69ab3168 static opName = 'Discard';
b69ab3169
b69ab3170 constructor(
b69ab3171 private selection: PartialSelection,
b69ab3172 private allFiles: Array<RepoRelativePath>,
b69ab3173 ) {
b69ab3174 super('PartialDiscardOperation');
b69ab3175 }
b69ab3176
b69ab3177 getArgs() {
b69ab3178 return ['debugimportstack'];
b69ab3179 }
b69ab3180
b69ab3181 getStdin(): string | undefined {
b69ab3182 const inverse = true;
b69ab3183 const files = this.selection.calculateImportStackFiles(this.allFiles, inverse);
b69ab3184 const importStack: ImportStack = [['write', files]];
b69ab3185 return JSON.stringify(importStack);
b69ab3186 }
b69ab3187
b69ab3188 getDescriptionForDisplay() {
b69ab3189 return {
b69ab3190 description: t('Discarding selected changes'),
b69ab3191 tooltip: t(
b69ab3192 'This operation does not have a traditional command line equivalent. \n' +
b69ab3193 'You can use `revert -i`, `goto --clean`, `purge` for similar effects.',
b69ab3194 ),
b69ab3195 };
b69ab3196 }
b69ab3197
b69ab3198 makeOptimisticUncommittedChangesApplier?(
b69ab3199 _context: UncommittedChangesPreviewContext,
b69ab31100 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab31101 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab31102 return changes.filter(change => !this.selection.isFullySelected(change.path));
b69ab31103 };
b69ab31104 return func;
b69ab31105 }
b69ab31106}