| 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 {ImportStack} from 'shared/types/stack'; |
| b69ab31 | | | 9 | import type {PartialSelection} from '../partialSelection'; |
| b69ab31 | | | 10 | import type { |
| b69ab31 | | | 11 | ApplyUncommittedChangesPreviewsFuncType, |
| b69ab31 | | | 12 | UncommittedChangesPreviewContext, |
| b69ab31 | | | 13 | } from '../previews'; |
| b69ab31 | | | 14 | import type {RepoRelativePath, UncommittedChanges} from '../types'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | import {t} from '../i18n'; |
| b69ab31 | | | 17 | import {Operation} from './Operation'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | /** |
| b69ab31 | | | 20 | * "Discard" is not an actual command, but the effect of removing all uncommitted changes is accomplished by `goto --clean .` |
| b69ab31 | | | 21 | * This leaves behind untracked files, which may be separately removed by `purge --files`. |
| b69ab31 | | | 22 | */ |
| b69ab31 | | | 23 | export class DiscardOperation extends Operation { |
| b69ab31 | | | 24 | static opName = 'Discard'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | constructor() { |
| b69ab31 | | | 27 | super('DiscardOperation'); |
| b69ab31 | | | 28 | } |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | getArgs() { |
| b69ab31 | | | 31 | const args = ['goto', '--clean', '.']; |
| b69ab31 | | | 32 | return args; |
| b69ab31 | | | 33 | } |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 36 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 37 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 38 | const trackedChangeTypes = ['M', 'A', 'R', '!']; |
| b69ab31 | | | 39 | if ( |
| b69ab31 | | | 40 | context.uncommittedChanges.length === 0 || |
| b69ab31 | | | 41 | // some files may become untracked after clean goto |
| b69ab31 | | | 42 | context.uncommittedChanges.every(change => !trackedChangeTypes.includes(change.status)) |
| b69ab31 | | | 43 | ) { |
| b69ab31 | | | 44 | return undefined; |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 48 | // clean goto leaves behind untracked files |
| b69ab31 | | | 49 | return changes.filter(change => !trackedChangeTypes.includes(change.status)); |
| b69ab31 | | | 50 | }; |
| b69ab31 | | | 51 | return func; |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | /** |
| b69ab31 | | | 56 | * Removes selected uncommitted changes to make them disappare from `status` or `diff`. |
| b69ab31 | | | 57 | * - Delete selected `A` or `?` files, so they disappear from `status` |
| b69ab31 | | | 58 | * - Restore selected `R` or `!` files, so they disappear from `status`. |
| b69ab31 | | | 59 | * - Revert selected `M` files, so they disappear from status. |
| b69ab31 | | | 60 | * - For partially selected files, their content will be reverted by dropping |
| b69ab31 | | | 61 | * the selected changes (line insertions or deletions), similar to `revert -i`. |
| b69ab31 | | | 62 | * The selected changes will disappear from `diff` output. |
| b69ab31 | | | 63 | * |
| b69ab31 | | | 64 | * This might replace DiscardOperation in the future. For now they might still be different, |
| b69ab31 | | | 65 | * since this operation only writes the working copy without changing the dirstate. |
| b69ab31 | | | 66 | */ |
| b69ab31 | | | 67 | export class PartialDiscardOperation extends Operation { |
| b69ab31 | | | 68 | static opName = 'Discard'; |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | constructor( |
| b69ab31 | | | 71 | private selection: PartialSelection, |
| b69ab31 | | | 72 | private allFiles: Array<RepoRelativePath>, |
| b69ab31 | | | 73 | ) { |
| b69ab31 | | | 74 | super('PartialDiscardOperation'); |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | getArgs() { |
| b69ab31 | | | 78 | return ['debugimportstack']; |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | getStdin(): string | undefined { |
| b69ab31 | | | 82 | const inverse = true; |
| b69ab31 | | | 83 | const files = this.selection.calculateImportStackFiles(this.allFiles, inverse); |
| b69ab31 | | | 84 | const importStack: ImportStack = [['write', files]]; |
| b69ab31 | | | 85 | return JSON.stringify(importStack); |
| b69ab31 | | | 86 | } |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | getDescriptionForDisplay() { |
| b69ab31 | | | 89 | return { |
| b69ab31 | | | 90 | description: t('Discarding selected changes'), |
| b69ab31 | | | 91 | tooltip: t( |
| b69ab31 | | | 92 | 'This operation does not have a traditional command line equivalent. \n' + |
| b69ab31 | | | 93 | 'You can use `revert -i`, `goto --clean`, `purge` for similar effects.', |
| b69ab31 | | | 94 | ), |
| b69ab31 | | | 95 | }; |
| b69ab31 | | | 96 | } |
| b69ab31 | | | 97 | |
| b69ab31 | | | 98 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 99 | _context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 100 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 101 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 102 | return changes.filter(change => !this.selection.isFullySelected(change.path)); |
| b69ab31 | | | 103 | }; |
| b69ab31 | | | 104 | return func; |
| b69ab31 | | | 105 | } |
| b69ab31 | | | 106 | } |