addons/isl/src/operations/AddRemoveOperation.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 {RepoRelativePath, UncommittedChanges} from '../types';
b69ab3113
b69ab3114import {Operation} from './Operation';
b69ab3115
b69ab3116/**
b69ab3117 * `sl addremove` adds all untracked files, and forgets all missing files.
b69ab3118 * If filepaths are passed, only those file paths will be used, like doing a bulk `sl add` or `sl forget`.
b69ab3119 * If filepaths is empty array, all untracked/missing files will be affected.
b69ab3120 */
b69ab3121export class AddRemoveOperation extends Operation {
b69ab3122 constructor(private paths: Array<RepoRelativePath>) {
b69ab3123 super('AddRemoveOperation');
b69ab3124 }
b69ab3125
b69ab3126 static opName = 'AddRemove';
b69ab3127
b69ab3128 getArgs() {
b69ab3129 return [
b69ab3130 'addremove',
b69ab3131 ...this.paths.map(path => ({
b69ab3132 type: 'repo-relative-file' as const,
b69ab3133 path,
b69ab3134 })),
b69ab3135 ];
b69ab3136 }
b69ab3137
b69ab3138 makeOptimisticUncommittedChangesApplier?(
b69ab3139 context: UncommittedChangesPreviewContext,
b69ab3140 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3141 const allFiles = this.paths.length === 0;
b69ab3142 if (
b69ab3143 context.uncommittedChanges.every(
b69ab3144 change =>
b69ab3145 (allFiles || this.paths.includes(change.path)) &&
b69ab3146 change.status !== '?' &&
b69ab3147 change.status !== '!',
b69ab3148 )
b69ab3149 ) {
b69ab3150 return undefined;
b69ab3151 }
b69ab3152
b69ab3153 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3154 return changes.map(change =>
b69ab3155 allFiles || this.paths.includes(change.path)
b69ab3156 ? {
b69ab3157 path: change.path,
b69ab3158 status: change.status === '?' ? 'A' : change.status === '!' ? 'R' : change.status,
b69ab3159 }
b69ab3160 : change,
b69ab3161 );
b69ab3162 };
b69ab3163 return func;
b69ab3164 }
b69ab3165}