| 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 | ApplyMergeConflictsPreviewsFuncType, |
| 10 | ApplyUncommittedChangesPreviewsFuncType, |
| 11 | MergeConflictsPreviewContext, |
| 12 | UncommittedChangesPreviewContext, |
| 13 | } from '../previews'; |
| 14 | import type {CommandArg, MergeConflicts, RepoRelativePath, UncommittedChanges} from '../types'; |
| 15 | |
| 16 | import {Operation} from './Operation'; |
| 17 | |
| 18 | export enum ResolveTool { |
| 19 | mark = 'mark', |
| 20 | unmark = 'unmark', |
| 21 | both = 'internal:union', |
| 22 | local = 'internal:merge-local', |
| 23 | other = 'internal:merge-other', |
| 24 | } |
| 25 | |
| 26 | export class ResolveOperation extends Operation { |
| 27 | constructor( |
| 28 | private filePath: RepoRelativePath, |
| 29 | private tool: ResolveTool, |
| 30 | ) { |
| 31 | super('ResolveOperation'); |
| 32 | } |
| 33 | |
| 34 | static opName = 'Resolve'; |
| 35 | |
| 36 | getArgs() { |
| 37 | const args: Array<CommandArg> = ['resolve']; |
| 38 | |
| 39 | switch (this.tool) { |
| 40 | case ResolveTool.mark: |
| 41 | args.push('--mark'); |
| 42 | break; |
| 43 | case ResolveTool.unmark: |
| 44 | args.push('--unmark'); |
| 45 | break; |
| 46 | case ResolveTool.both: |
| 47 | case ResolveTool.local: |
| 48 | case ResolveTool.other: |
| 49 | args.push('--tool', this.tool); |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | args.push({ |
| 54 | type: 'repo-relative-file' as const, |
| 55 | path: this.filePath, |
| 56 | }); |
| 57 | return args; |
| 58 | } |
| 59 | |
| 60 | makeOptimisticUncommittedChangesApplier?( |
| 61 | context: UncommittedChangesPreviewContext, |
| 62 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| 63 | if ( |
| 64 | context.uncommittedChanges.some( |
| 65 | change => change.path === this.filePath && change.status !== 'U', |
| 66 | ) |
| 67 | ) { |
| 68 | return undefined; |
| 69 | } |
| 70 | |
| 71 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| 72 | return changes.map(change => |
| 73 | change.path === this.filePath ? {...change, status: 'Resolved'} : change, |
| 74 | ); |
| 75 | }; |
| 76 | return func; |
| 77 | } |
| 78 | |
| 79 | makeOptimisticMergeConflictsApplier?( |
| 80 | context: MergeConflictsPreviewContext, |
| 81 | ): ApplyMergeConflictsPreviewsFuncType | undefined { |
| 82 | if ( |
| 83 | context.conflicts?.files?.some( |
| 84 | change => change.path === this.filePath && change.status !== 'U', |
| 85 | ) === true |
| 86 | ) { |
| 87 | return undefined; |
| 88 | } |
| 89 | |
| 90 | const func: ApplyMergeConflictsPreviewsFuncType = (conflicts?: MergeConflicts) => { |
| 91 | if (conflicts?.state !== 'loaded') { |
| 92 | return conflicts; |
| 93 | } |
| 94 | return { |
| 95 | ...conflicts, |
| 96 | files: |
| 97 | conflicts?.files?.map(change => |
| 98 | change.path === this.filePath ? {...change, status: 'Resolved' as const} : change, |
| 99 | ) ?? [], |
| 100 | }; |
| 101 | }; |
| 102 | return func; |
| 103 | } |
| 104 | } |
| 105 | |