| 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 RevertOperation extends Operation { |
| b69ab31 | | | 24 | static opName = 'Revert'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | constructor( |
| b69ab31 | | | 27 | private files: Array<RepoRelativePath>, |
| b69ab31 | | | 28 | private revset?: SucceedableRevset | ExactRevset | OptimisticRevset, |
| b69ab31 | | | 29 | ) { |
| b69ab31 | | | 30 | super('RevertOperation'); |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | getArgs() { |
| b69ab31 | | | 34 | const args: Array<CommandArg> = ['revert']; |
| b69ab31 | | | 35 | if (this.revset != null) { |
| b69ab31 | | | 36 | args.push('--rev', this.revset); |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | if (this.files.length > 0) { |
| b69ab31 | | | 39 | // Tag file arguments specially so the remote repo can convert them to the proper cwd-relative format. |
| b69ab31 | | | 40 | args.push({ |
| b69ab31 | | | 41 | type: 'repo-relative-file-list' as const, |
| b69ab31 | | | 42 | paths: this.files, |
| b69ab31 | | | 43 | }); |
| b69ab31 | | | 44 | } |
| b69ab31 | | | 45 | return args; |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 49 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 50 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 51 | if (this.revset == null) { |
| b69ab31 | | | 52 | const filesToHide = new Set(this.files); |
| b69ab31 | | | 53 | if (context.uncommittedChanges.every(change => !filesToHide.has(change.path))) { |
| b69ab31 | | | 54 | return undefined; |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 58 | return changes.filter(change => !filesToHide.has(change.path)); |
| b69ab31 | | | 59 | }; |
| b69ab31 | | | 60 | return func; |
| b69ab31 | | | 61 | } else { |
| b69ab31 | | | 62 | // If reverting back to a specific commit, the file will probably become 'M', not disappear. |
| b69ab31 | | | 63 | // Note: this is just a guess, in reality the file could do any number of things. |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | const filesToMarkChanged = new Set(this.files); |
| b69ab31 | | | 66 | if (context.uncommittedChanges.find(change => filesToMarkChanged.has(change.path)) != null) { |
| b69ab31 | | | 67 | return undefined; |
| b69ab31 | | | 68 | } |
| b69ab31 | | | 69 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 70 | const existingChanges = new Set(changes.map(change => change.path)); |
| b69ab31 | | | 71 | const revertedChangesToInsert = this.files.filter(file => !existingChanges.has(file)); |
| b69ab31 | | | 72 | return [ |
| b69ab31 | | | 73 | ...changes.map(change => |
| b69ab31 | | | 74 | filesToMarkChanged.has(change.path) ? {...change, status: 'M' as const} : change, |
| b69ab31 | | | 75 | ), |
| b69ab31 | | | 76 | ...revertedChangesToInsert.map(path => ({path, status: 'M' as const})), |
| b69ab31 | | | 77 | ]; |
| b69ab31 | | | 78 | }; |
| b69ab31 | | | 79 | return func; |
| b69ab31 | | | 80 | } |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | } |