addons/isl/src/successionUtils.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 './dag/dag';
b69ab319import type {ExactRevset, OptimisticRevset} from './types';
b69ab3110
b69ab3111import {
b69ab3112 exactRevset,
b69ab3113 optimisticRevset,
b69ab3114 succeedableRevset,
b69ab3115 type CommitInfo,
b69ab3116 type SucceedableRevset,
b69ab3117} from './types';
b69ab3118
b69ab3119/**
b69ab3120 * Get the latest successor hash of the given hash,
b69ab3121 * traversing multiple successions if necessary.
b69ab3122 * Returns original hash if no successors were found.
b69ab3123 *
b69ab3124 * Useful for previews to ensure they're working with the latest version of a commit,
b69ab3125 * given that they may have been queued up while another operation ran and eventually caused succession.
b69ab3126 *
b69ab3127 * Note: if an ExactRevset is passed, don't look up the successor.
b69ab3128 */
b69ab3129export function latestSuccessor(
b69ab3130 ctx: Dag,
b69ab3131 oldRevset: SucceedableRevset | ExactRevset | OptimisticRevset,
b69ab3132): string {
b69ab3133 let hash = oldRevset.type === 'optimistic-revset' ? oldRevset.fake : oldRevset.revset;
b69ab3134 if (oldRevset.type === 'exact-revset') {
b69ab3135 return hash;
b69ab3136 }
b69ab3137 hash = ctx.followSuccessors(hash).toHashes().first() ?? hash;
b69ab3138 return hash;
b69ab3139}
b69ab3140
b69ab3141/**
b69ab3142 * Typically we want to use succeedable revsets everywhere, to maximize support for queued commands.
b69ab3143 * But if you see and act on a visibly obsolete commit in the UI, we should use its exact hash,
b69ab3144 * so that you don't suddenly act on a seemingly unrelated commit.
b69ab3145 */
b69ab3146export function latestSuccessorUnlessExplicitlyObsolete(
b69ab3147 commit: Readonly<CommitInfo>,
b69ab3148): SucceedableRevset | ExactRevset | OptimisticRevset {
b69ab3149 if (commit.optimisticRevset != null) {
b69ab3150 return optimisticRevset(commit.optimisticRevset, commit.hash);
b69ab3151 }
b69ab3152 if (commit.successorInfo?.type != null) {
b69ab3153 return exactRevset(commit.hash);
b69ab3154 }
b69ab3155 return succeedableRevset(commit.hash);
b69ab3156}