addons/isl/src/operations/UnshelveOperation.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 {ShelvedChange, UncommittedChanges} from '../types';
b69ab3113
b69ab3114import {Operation} from './Operation';
b69ab3115
b69ab3116export class UnshelveOperation extends Operation {
b69ab3117 constructor(
b69ab3118 private shelvedChange: ShelvedChange,
b69ab3119 private keep: boolean,
b69ab3120 ) {
b69ab3121 super('UnshelveOperation');
b69ab3122 }
b69ab3123
b69ab3124 static opName = 'Unshelve';
b69ab3125
b69ab3126 getArgs() {
b69ab3127 const args = ['unshelve'];
b69ab3128 if (this.keep) {
b69ab3129 args.push('--keep');
b69ab3130 }
b69ab3131 args.push('--name', this.shelvedChange.name);
b69ab3132 return args;
b69ab3133 }
b69ab3134
b69ab3135 makeOptimisticUncommittedChangesApplier?(
b69ab3136 context: UncommittedChangesPreviewContext,
b69ab3137 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3138 const shelvedChangedFiles = this.shelvedChange.filesSample;
b69ab3139 const preexistingChanges = new Set(context.uncommittedChanges.map(change => change.path));
b69ab3140
b69ab3141 if (shelvedChangedFiles.every(file => preexistingChanges.has(file.path))) {
b69ab3142 // once every file to unshelve appears in the output, the unshelve has reflected in the latest fetch.
b69ab3143 return undefined;
b69ab3144 }
b69ab3145
b69ab3146 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3147 // You could have uncommitted changes before unshelving, so we need to include
b69ab3148 // shelved changes AND the existing uncommitted changes.
b69ab3149 // But it's also possible to have changed a file changed by the commit, so we need to de-dupe.
b69ab3150 const newChanges = this.shelvedChange.filesSample.filter(
b69ab3151 file => !preexistingChanges.has(file.path),
b69ab3152 );
b69ab3153 return [...changes, ...newChanges];
b69ab3154 };
b69ab3155 return func;
b69ab3156 }
b69ab3157}