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