| 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 {PartialSelection} from '../partialSelection'; |
| b69ab31 | | | 9 | import type { |
| b69ab31 | | | 10 | ApplyUncommittedChangesPreviewsFuncType, |
| b69ab31 | | | 11 | UncommittedChangesPreviewContext, |
| b69ab31 | | | 12 | } from '../previews'; |
| b69ab31 | | | 13 | import type {CommandArg, RepoRelativePath, UncommittedChanges} from '../types'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | import {Operation} from './Operation'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | export class ShelveOperation extends Operation { |
| b69ab31 | | | 18 | /** |
| b69ab31 | | | 19 | * @param name the name of the shelved changes. This makes it easier to find the change later. |
| b69ab31 | | | 20 | * @param filesPathsToCommit if provided, only these file paths will be included in the shelve operation. If undefined, ALL uncommitted changes are included. Paths should be relative to repo root. |
| b69ab31 | | | 21 | */ |
| b69ab31 | | | 22 | constructor( |
| b69ab31 | | | 23 | private name?: string, |
| b69ab31 | | | 24 | private filesPathsToCommit?: Array<RepoRelativePath>, |
| b69ab31 | | | 25 | ) { |
| b69ab31 | | | 26 | super('ShelveOperation'); |
| b69ab31 | | | 27 | } |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | static opName = 'Shelve'; |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | getArgs() { |
| b69ab31 | | | 32 | const args: Array<CommandArg> = ['shelve', '--unknown']; |
| b69ab31 | | | 33 | if (this.name) { |
| b69ab31 | | | 34 | args.push('--name', this.name); |
| b69ab31 | | | 35 | } |
| b69ab31 | | | 36 | if (this.filesPathsToCommit) { |
| b69ab31 | | | 37 | args.push( |
| b69ab31 | | | 38 | ...this.filesPathsToCommit.map(file => |
| b69ab31 | | | 39 | // tag file arguments specially so the remote repo can convert them to the proper cwd-relative format. |
| b69ab31 | | | 40 | ({ |
| b69ab31 | | | 41 | type: 'repo-relative-file' as const, |
| b69ab31 | | | 42 | path: file, |
| b69ab31 | | | 43 | }), |
| b69ab31 | | | 44 | ), |
| b69ab31 | | | 45 | ); |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | return args; |
| b69ab31 | | | 48 | } |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 51 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 52 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 53 | const filesToCommit = new Set(this.filesPathsToCommit); |
| b69ab31 | | | 54 | // optimistic state is over when there's no uncommitted changes that we wanted to shelve left |
| b69ab31 | | | 55 | if ( |
| b69ab31 | | | 56 | context.uncommittedChanges.length === 0 || |
| b69ab31 | | | 57 | (filesToCommit.size > 0 && |
| b69ab31 | | | 58 | context.uncommittedChanges.every(change => !filesToCommit.has(change.path))) |
| b69ab31 | | | 59 | ) { |
| b69ab31 | | | 60 | return undefined; |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 64 | if (this.filesPathsToCommit != null) { |
| b69ab31 | | | 65 | return changes.filter(change => !filesToCommit.has(change.path)); |
| b69ab31 | | | 66 | } else { |
| b69ab31 | | | 67 | return []; |
| b69ab31 | | | 68 | } |
| b69ab31 | | | 69 | }; |
| b69ab31 | | | 70 | return func; |
| b69ab31 | | | 71 | } |
| b69ab31 | | | 72 | } |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | /** Find appropriate ShelveOperation based on selection. */ |
| b69ab31 | | | 75 | export function getShelveOperation( |
| b69ab31 | | | 76 | name: string | undefined, |
| b69ab31 | | | 77 | selection: PartialSelection, |
| b69ab31 | | | 78 | allFiles: Array<RepoRelativePath>, |
| b69ab31 | | | 79 | ): ShelveOperation { |
| b69ab31 | | | 80 | if (selection.hasChunkSelection()) { |
| b69ab31 | | | 81 | throw new Error('Cannot shelve partially selected hunks.'); |
| b69ab31 | | | 82 | } else if (selection.isEverythingSelected(() => allFiles)) { |
| b69ab31 | | | 83 | return new ShelveOperation(name); |
| b69ab31 | | | 84 | } else { |
| b69ab31 | | | 85 | const selectedFiles = allFiles.filter(path => selection.isFullyOrPartiallySelected(path)); |
| b69ab31 | | | 86 | return new ShelveOperation(name, selectedFiles); |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | } |