addons/isl/src/operations/ResolveOperation.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {
b69ab319 ApplyMergeConflictsPreviewsFuncType,
b69ab3110 ApplyUncommittedChangesPreviewsFuncType,
b69ab3111 MergeConflictsPreviewContext,
b69ab3112 UncommittedChangesPreviewContext,
b69ab3113} from '../previews';
b69ab3114import type {CommandArg, MergeConflicts, RepoRelativePath, UncommittedChanges} from '../types';
b69ab3115
b69ab3116import {Operation} from './Operation';
b69ab3117
b69ab3118export enum ResolveTool {
b69ab3119 mark = 'mark',
b69ab3120 unmark = 'unmark',
b69ab3121 both = 'internal:union',
b69ab3122 local = 'internal:merge-local',
b69ab3123 other = 'internal:merge-other',
b69ab3124}
b69ab3125
b69ab3126export class ResolveOperation extends Operation {
b69ab3127 constructor(
b69ab3128 private filePath: RepoRelativePath,
b69ab3129 private tool: ResolveTool,
b69ab3130 ) {
b69ab3131 super('ResolveOperation');
b69ab3132 }
b69ab3133
b69ab3134 static opName = 'Resolve';
b69ab3135
b69ab3136 getArgs() {
b69ab3137 const args: Array<CommandArg> = ['resolve'];
b69ab3138
b69ab3139 switch (this.tool) {
b69ab3140 case ResolveTool.mark:
b69ab3141 args.push('--mark');
b69ab3142 break;
b69ab3143 case ResolveTool.unmark:
b69ab3144 args.push('--unmark');
b69ab3145 break;
b69ab3146 case ResolveTool.both:
b69ab3147 case ResolveTool.local:
b69ab3148 case ResolveTool.other:
b69ab3149 args.push('--tool', this.tool);
b69ab3150 break;
b69ab3151 }
b69ab3152
b69ab3153 args.push({
b69ab3154 type: 'repo-relative-file' as const,
b69ab3155 path: this.filePath,
b69ab3156 });
b69ab3157 return args;
b69ab3158 }
b69ab3159
b69ab3160 makeOptimisticUncommittedChangesApplier?(
b69ab3161 context: UncommittedChangesPreviewContext,
b69ab3162 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3163 if (
b69ab3164 context.uncommittedChanges.some(
b69ab3165 change => change.path === this.filePath && change.status !== 'U',
b69ab3166 )
b69ab3167 ) {
b69ab3168 return undefined;
b69ab3169 }
b69ab3170
b69ab3171 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3172 return changes.map(change =>
b69ab3173 change.path === this.filePath ? {...change, status: 'Resolved'} : change,
b69ab3174 );
b69ab3175 };
b69ab3176 return func;
b69ab3177 }
b69ab3178
b69ab3179 makeOptimisticMergeConflictsApplier?(
b69ab3180 context: MergeConflictsPreviewContext,
b69ab3181 ): ApplyMergeConflictsPreviewsFuncType | undefined {
b69ab3182 if (
b69ab3183 context.conflicts?.files?.some(
b69ab3184 change => change.path === this.filePath && change.status !== 'U',
b69ab3185 ) === true
b69ab3186 ) {
b69ab3187 return undefined;
b69ab3188 }
b69ab3189
b69ab3190 const func: ApplyMergeConflictsPreviewsFuncType = (conflicts?: MergeConflicts) => {
b69ab3191 if (conflicts?.state !== 'loaded') {
b69ab3192 return conflicts;
b69ab3193 }
b69ab3194 return {
b69ab3195 ...conflicts,
b69ab3196 files:
b69ab3197 conflicts?.files?.map(change =>
b69ab3198 change.path === this.filePath ? {...change, status: 'Resolved' as const} : change,
b69ab3199 ) ?? [],
b69ab31100 };
b69ab31101 };
b69ab31102 return func;
b69ab31103 }
b69ab31104}