addons/isl/src/operations/Uncommit.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 ApplyUncommittedChangesPreviewsFuncType,
b69ab3110 Dag,
b69ab3111 UncommittedChangesPreviewContext,
b69ab3112} from '../previews';
b69ab3113import type {ChangedFile, CommitInfo, UncommittedChanges} from '../types';
b69ab3114
b69ab3115import {Operation} from './Operation';
b69ab3116
b69ab3117export class UncommitOperation extends Operation {
b69ab3118 /**
b69ab3119 * @param originalDotCommit the current dot commit, needed to track when optimistic state is resolved
b69ab3120 * @param changedFiles the files that are in the commit to be uncommitted. Must be fetched before running, as the CommitInfo object itself does not have file statuses.
b69ab3121 */
b69ab3122 constructor(
b69ab3123 private originalDotCommit: CommitInfo,
b69ab3124 private changedFiles: Array<ChangedFile>,
b69ab3125 ) {
b69ab3126 super('UncommitOperation');
b69ab3127 }
b69ab3128
b69ab3129 static opName = 'Uncommit';
b69ab3130
b69ab3131 getArgs() {
b69ab3132 const args = ['uncommit'];
b69ab3133 return args;
b69ab3134 }
b69ab3135
b69ab3136 optimisticDag(dag: Dag): Dag {
b69ab3137 const {hash, parents} = this.originalDotCommit;
b69ab3138 const p1 = parents.at(0);
b69ab3139 const commitHasChildren = (dag.children(hash)?.size ?? 0) > 0;
b69ab3140 if (
b69ab3141 p1 == null || commitHasChildren
b69ab3142 ? // If the commit has children, then we know the uncommit is done when it's no longer the dot commit
b69ab3143 dag.get(hash)?.isDot !== true
b69ab3144 : // If the commit does not have children, if `hash` disappears and `p1` still exists, then uncommit is completed.
b69ab3145 dag.get(hash) == null || dag.get(p1) == null
b69ab3146 ) {
b69ab3147 return dag;
b69ab3148 }
b69ab3149 return commitHasChildren
b69ab3150 ? // Set `isDot` on `p1` and not `hash`
b69ab3151 dag.replaceWith([p1 as string, hash], (h, c) => {
b69ab3152 return c?.set('isDot', h === p1);
b69ab3153 })
b69ab3154 : // Hide `hash` and set `isDot` on `p1`.
b69ab3155 dag.replaceWith([p1 as string, hash], (h, c) => {
b69ab3156 if (h === hash) {
b69ab3157 return undefined;
b69ab3158 } else {
b69ab3159 return c?.set('isDot', true);
b69ab3160 }
b69ab3161 });
b69ab3162 }
b69ab3163
b69ab3164 makeOptimisticUncommittedChangesApplier?(
b69ab3165 context: UncommittedChangesPreviewContext,
b69ab3166 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3167 const preexistingChanges = new Set(context.uncommittedChanges.map(change => change.path));
b69ab3168
b69ab3169 if (this.changedFiles.every(file => preexistingChanges.has(file.path))) {
b69ab3170 // once every file to uncommit appears in the output, the uncommit has reflected in the latest fetch.
b69ab3171 // TODO: we'll eventually limit how many uncommitted changes we pull in. When this happens, it's
b69ab3172 // possible the list of files won't include any of the changes being uncommitted (though this would be rare).
b69ab3173 // We should probably return undefined if the number of uncommitted changes >= max fetched.
b69ab3174 return undefined;
b69ab3175 }
b69ab3176
b69ab3177 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3178 // You could have uncommitted changes before uncommitting, so we need to include
b69ab3179 // files from the commit AND the existing uncommitted changes.
b69ab3180 // But it's also possible to have changed a file changed by the commit, so we need to de-dupe.
b69ab3181 const newChanges = this.changedFiles.filter(file => !preexistingChanges.has(file.path));
b69ab3182 return [...changes, ...newChanges];
b69ab3183 };
b69ab3184 return func;
b69ab3185 }
b69ab3186}