addons/isl/src/operations/GotoOperation.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 {Dag} from '../previews';
b69ab319import type {ExactRevset, OptimisticRevset, SucceedableRevset} from '../types';
b69ab3110
b69ab3111import {YOU_ARE_HERE_VIRTUAL_COMMIT} from '../dag/virtualCommit';
b69ab3112import {t} from '../i18n';
b69ab3113import {CommitPreview} from '../previews';
b69ab3114import {latestSuccessor} from '../successionUtils';
b69ab3115import {GotoBaseOperation} from './GotoBaseOperation';
b69ab3116
b69ab3117export class GotoOperation extends GotoBaseOperation {
b69ab3118 constructor(protected destination: SucceedableRevset | ExactRevset | OptimisticRevset) {
b69ab3119 super(destination);
b69ab3120 }
b69ab3121
b69ab3122 getInitialInlineProgress(): [hash: string, message: string][] {
b69ab3123 return [[YOU_ARE_HERE_VIRTUAL_COMMIT.hash, t('moving...')]];
b69ab3124 }
b69ab3125
b69ab3126 optimisticDag(dag: Dag): Dag {
b69ab3127 const headCommitHash = dag.resolve('.')?.hash;
b69ab3128 if (headCommitHash == null) {
b69ab3129 return dag;
b69ab3130 }
b69ab3131 const dest = dag.resolve(latestSuccessor(dag, this.destination));
b69ab3132 const src = dag.get(headCommitHash);
b69ab3133 if (dest == null || src == null || dest.hash === src.hash) {
b69ab3134 return dag;
b69ab3135 }
b69ab3136 return dag.replaceWith([src.hash, dest.hash], (h, c) => {
b69ab3137 const isDest = h === dest.hash;
b69ab3138 const previewType = isDest
b69ab3139 ? CommitPreview.GOTO_DESTINATION
b69ab3140 : CommitPreview.GOTO_PREVIOUS_LOCATION;
b69ab3141 return c?.merge({isDot: isDest, previewType});
b69ab3142 });
b69ab3143 }
b69ab3144}