addons/isl/src/operations/AmendToOperation.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 {
b69ab3113 CommandArg,
b69ab3114 ExactRevset,
b69ab3115 OptimisticRevset,
b69ab3116 RepoRelativePath,
b69ab3117 SucceedableRevset,
b69ab3118 UncommittedChanges,
b69ab3119} from '../types';
b69ab3120
b69ab3121import {Operation} from './Operation';
b69ab3122
b69ab3123export class AmendToOperation extends Operation {
b69ab3124 /**
b69ab3125 * @param filePathsToAmend if provided, only these file paths will be included in the amend operation. If undefined, ALL uncommitted changes are included. Paths should be relative to repo root.
b69ab3126 * @param message if provided, update commit description to use this title & description
b69ab3127 */
b69ab3128 constructor(
b69ab3129 private commit: SucceedableRevset | ExactRevset | OptimisticRevset,
b69ab3130 private filePathsToAmend?: Array<RepoRelativePath>,
b69ab3131 ) {
b69ab3132 super('AmendToOperation');
b69ab3133 }
b69ab3134
b69ab3135 static opName = 'AmendTo';
b69ab3136
b69ab3137 getArgs() {
b69ab3138 const args: Array<CommandArg> = ['amend', '--to', this.commit];
b69ab3139 if (this.filePathsToAmend) {
b69ab3140 args.push(
b69ab3141 ...this.filePathsToAmend.map(file =>
b69ab3142 // tag file arguments specially so the remote repo can convert them to the proper cwd-relative format.
b69ab3143 ({
b69ab3144 type: 'repo-relative-file' as const,
b69ab3145 path: file,
b69ab3146 }),
b69ab3147 ),
b69ab3148 );
b69ab3149 }
b69ab3150 return args;
b69ab3151 }
b69ab3152
b69ab3153 makeOptimisticUncommittedChangesApplier?(
b69ab3154 context: UncommittedChangesPreviewContext,
b69ab3155 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3156 const filesToAmend = new Set(this.filePathsToAmend);
b69ab3157 if (
b69ab3158 context.uncommittedChanges.length === 0 ||
b69ab3159 (filesToAmend.size > 0 &&
b69ab3160 context.uncommittedChanges.every(change => !filesToAmend.has(change.path)))
b69ab3161 ) {
b69ab3162 return undefined;
b69ab3163 }
b69ab3164
b69ab3165 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3166 if (this.filePathsToAmend != null) {
b69ab3167 return changes.filter(change => !filesToAmend.has(change.path));
b69ab3168 } else {
b69ab3169 return [];
b69ab3170 }
b69ab3171 };
b69ab3172 return func;
b69ab3173 }
b69ab3174}