| 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 {RepoRelativePath, UncommittedChanges} from '../types'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import {Operation} from './Operation'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | /** |
| b69ab31 | | | 17 | * `sl addremove` adds all untracked files, and forgets all missing files. |
| b69ab31 | | | 18 | * If filepaths are passed, only those file paths will be used, like doing a bulk `sl add` or `sl forget`. |
| b69ab31 | | | 19 | * If filepaths is empty array, all untracked/missing files will be affected. |
| b69ab31 | | | 20 | */ |
| b69ab31 | | | 21 | export class AddRemoveOperation extends Operation { |
| b69ab31 | | | 22 | constructor(private paths: Array<RepoRelativePath>) { |
| b69ab31 | | | 23 | super('AddRemoveOperation'); |
| b69ab31 | | | 24 | } |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | static opName = 'AddRemove'; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | getArgs() { |
| b69ab31 | | | 29 | return [ |
| b69ab31 | | | 30 | 'addremove', |
| b69ab31 | | | 31 | ...this.paths.map(path => ({ |
| b69ab31 | | | 32 | type: 'repo-relative-file' as const, |
| b69ab31 | | | 33 | path, |
| b69ab31 | | | 34 | })), |
| b69ab31 | | | 35 | ]; |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 39 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 40 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 41 | const allFiles = this.paths.length === 0; |
| b69ab31 | | | 42 | if ( |
| b69ab31 | | | 43 | context.uncommittedChanges.every( |
| b69ab31 | | | 44 | change => |
| b69ab31 | | | 45 | (allFiles || this.paths.includes(change.path)) && |
| b69ab31 | | | 46 | change.status !== '?' && |
| b69ab31 | | | 47 | change.status !== '!', |
| b69ab31 | | | 48 | ) |
| b69ab31 | | | 49 | ) { |
| b69ab31 | | | 50 | return undefined; |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 54 | return changes.map(change => |
| b69ab31 | | | 55 | allFiles || this.paths.includes(change.path) |
| b69ab31 | | | 56 | ? { |
| b69ab31 | | | 57 | path: change.path, |
| b69ab31 | | | 58 | status: change.status === '?' ? 'A' : change.status === '!' ? 'R' : change.status, |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | : change, |
| b69ab31 | | | 61 | ); |
| b69ab31 | | | 62 | }; |
| b69ab31 | | | 63 | return func; |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | } |