| 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 { |
| b69ab31 | | | 9 | ApplyUncommittedChangesPreviewsFuncType, |
| b69ab31 | | | 10 | UncommittedChangesPreviewContext, |
| b69ab31 | | | 11 | } from '../previews'; |
| b69ab31 | | | 12 | import type { |
| b69ab31 | | | 13 | CommandArg, |
| b69ab31 | | | 14 | ExactRevset, |
| b69ab31 | | | 15 | OptimisticRevset, |
| b69ab31 | | | 16 | RepoRelativePath, |
| b69ab31 | | | 17 | SucceedableRevset, |
| b69ab31 | | | 18 | UncommittedChanges, |
| b69ab31 | | | 19 | } from '../types'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | import {Operation} from './Operation'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | export class AmendToOperation extends Operation { |
| b69ab31 | | | 24 | /** |
| b69ab31 | | | 25 | * @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. |
| b69ab31 | | | 26 | * @param message if provided, update commit description to use this title & description |
| b69ab31 | | | 27 | */ |
| b69ab31 | | | 28 | constructor( |
| b69ab31 | | | 29 | private commit: SucceedableRevset | ExactRevset | OptimisticRevset, |
| b69ab31 | | | 30 | private filePathsToAmend?: Array<RepoRelativePath>, |
| b69ab31 | | | 31 | ) { |
| b69ab31 | | | 32 | super('AmendToOperation'); |
| b69ab31 | | | 33 | } |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | static opName = 'AmendTo'; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | getArgs() { |
| b69ab31 | | | 38 | const args: Array<CommandArg> = ['amend', '--to', this.commit]; |
| b69ab31 | | | 39 | if (this.filePathsToAmend) { |
| b69ab31 | | | 40 | args.push( |
| b69ab31 | | | 41 | ...this.filePathsToAmend.map(file => |
| b69ab31 | | | 42 | // tag file arguments specially so the remote repo can convert them to the proper cwd-relative format. |
| b69ab31 | | | 43 | ({ |
| b69ab31 | | | 44 | type: 'repo-relative-file' as const, |
| b69ab31 | | | 45 | path: file, |
| b69ab31 | | | 46 | }), |
| b69ab31 | | | 47 | ), |
| b69ab31 | | | 48 | ); |
| b69ab31 | | | 49 | } |
| b69ab31 | | | 50 | return args; |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 54 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 55 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 56 | const filesToAmend = new Set(this.filePathsToAmend); |
| b69ab31 | | | 57 | if ( |
| b69ab31 | | | 58 | context.uncommittedChanges.length === 0 || |
| b69ab31 | | | 59 | (filesToAmend.size > 0 && |
| b69ab31 | | | 60 | context.uncommittedChanges.every(change => !filesToAmend.has(change.path))) |
| b69ab31 | | | 61 | ) { |
| b69ab31 | | | 62 | return undefined; |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 66 | if (this.filePathsToAmend != null) { |
| b69ab31 | | | 67 | return changes.filter(change => !filesToAmend.has(change.path)); |
| b69ab31 | | | 68 | } else { |
| b69ab31 | | | 69 | return []; |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | }; |
| b69ab31 | | | 72 | return func; |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | } |