addons/isl/src/operations/AmendMessageOperation.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 {Dag} from '../previews';
b69ab319import type {CommandArg, ExactRevset, Hash, OptimisticRevset, SucceedableRevset} from '../types';
b69ab3110
b69ab3111import {Operation} from './Operation';
b69ab3112
b69ab3113export class AmendMessageOperation extends Operation {
b69ab3114 constructor(
b69ab3115 public revset: SucceedableRevset | ExactRevset | OptimisticRevset,
b69ab3116 public message: string,
b69ab3117 public author?: string,
b69ab3118 ) {
b69ab3119 super('AmendMessageOperation');
b69ab3120 }
b69ab3121
b69ab3122 static opName = 'Metaedit';
b69ab3123
b69ab3124 /** If the input revset refers to a specific commit hash, return it */
b69ab3125 getCommitHash(): Hash | undefined {
b69ab3126 if (this.revset.type === 'optimistic-revset') {
b69ab3127 return this.revset.fake;
b69ab3128 }
b69ab3129 if (/[a-fA-F0-9]{12,40}/.test(this.revset.revset)) {
b69ab3130 return this.revset.revset;
b69ab3131 }
b69ab3132 return undefined;
b69ab3133 }
b69ab3134
b69ab3135 getArgs() {
b69ab3136 const args: Array<CommandArg> = ['metaedit', '--rev', this.revset, '--message', this.message];
b69ab3137 if (this.author) {
b69ab3138 args.push('--user', this.author);
b69ab3139 }
b69ab3140 return args;
b69ab3141 }
b69ab3142
b69ab3143 optimisticDag(dag: Dag): Dag {
b69ab3144 const hash = this.revset.revset;
b69ab3145 return dag.touch(hash).replaceWith(hash, (_h, c) => {
b69ab3146 if (c === undefined) {
b69ab3147 // metaedit succeeds when we no longer see original commit
b69ab3148 // Note: this assumes we always restack children and never render old commit as obsolete.
b69ab3149 return c;
b69ab3150 }
b69ab3151 const [title] = this.message.split(/\n+/, 1);
b69ab3152 const description = this.message.slice(title.length);
b69ab3153 return c?.merge({title, description});
b69ab3154 });
b69ab3155 }
b69ab3156}