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