| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {Dag} from '../previews'; |
| 9 | import type {CommandArg, ExactRevset, Hash, OptimisticRevset, SucceedableRevset} from '../types'; |
| 10 | |
| 11 | import {Operation} from './Operation'; |
| 12 | |
| 13 | export class AmendMessageOperation extends Operation { |
| 14 | constructor( |
| 15 | public revset: SucceedableRevset | ExactRevset | OptimisticRevset, |
| 16 | public message: string, |
| 17 | public author?: string, |
| 18 | ) { |
| 19 | super('AmendMessageOperation'); |
| 20 | } |
| 21 | |
| 22 | static opName = 'Metaedit'; |
| 23 | |
| 24 | /** If the input revset refers to a specific commit hash, return it */ |
| 25 | getCommitHash(): Hash | undefined { |
| 26 | if (this.revset.type === 'optimistic-revset') { |
| 27 | return this.revset.fake; |
| 28 | } |
| 29 | if (/[a-fA-F0-9]{12,40}/.test(this.revset.revset)) { |
| 30 | return this.revset.revset; |
| 31 | } |
| 32 | return undefined; |
| 33 | } |
| 34 | |
| 35 | getArgs() { |
| 36 | const args: Array<CommandArg> = ['metaedit', '--rev', this.revset, '--message', this.message]; |
| 37 | if (this.author) { |
| 38 | args.push('--user', this.author); |
| 39 | } |
| 40 | return args; |
| 41 | } |
| 42 | |
| 43 | optimisticDag(dag: Dag): Dag { |
| 44 | const hash = this.revset.revset; |
| 45 | return dag.touch(hash).replaceWith(hash, (_h, c) => { |
| 46 | if (c === undefined) { |
| 47 | // metaedit succeeds when we no longer see original commit |
| 48 | // Note: this assumes we always restack children and never render old commit as obsolete. |
| 49 | return c; |
| 50 | } |
| 51 | const [title] = this.message.split(/\n+/, 1); |
| 52 | const description = this.message.slice(title.length); |
| 53 | return c?.merge({title, description}); |
| 54 | }); |
| 55 | } |
| 56 | } |
| 57 | |