| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Dag} from './dag/dag'; |
| b69ab31 | | | 9 | import type {ExactRevset, OptimisticRevset} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import { |
| b69ab31 | | | 12 | exactRevset, |
| b69ab31 | | | 13 | optimisticRevset, |
| b69ab31 | | | 14 | succeedableRevset, |
| b69ab31 | | | 15 | type CommitInfo, |
| b69ab31 | | | 16 | type SucceedableRevset, |
| b69ab31 | | | 17 | } from './types'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | /** |
| b69ab31 | | | 20 | * Get the latest successor hash of the given hash, |
| b69ab31 | | | 21 | * traversing multiple successions if necessary. |
| b69ab31 | | | 22 | * Returns original hash if no successors were found. |
| b69ab31 | | | 23 | * |
| b69ab31 | | | 24 | * Useful for previews to ensure they're working with the latest version of a commit, |
| b69ab31 | | | 25 | * given that they may have been queued up while another operation ran and eventually caused succession. |
| b69ab31 | | | 26 | * |
| b69ab31 | | | 27 | * Note: if an ExactRevset is passed, don't look up the successor. |
| b69ab31 | | | 28 | */ |
| b69ab31 | | | 29 | export function latestSuccessor( |
| b69ab31 | | | 30 | ctx: Dag, |
| b69ab31 | | | 31 | oldRevset: SucceedableRevset | ExactRevset | OptimisticRevset, |
| b69ab31 | | | 32 | ): string { |
| b69ab31 | | | 33 | let hash = oldRevset.type === 'optimistic-revset' ? oldRevset.fake : oldRevset.revset; |
| b69ab31 | | | 34 | if (oldRevset.type === 'exact-revset') { |
| b69ab31 | | | 35 | return hash; |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | hash = ctx.followSuccessors(hash).toHashes().first() ?? hash; |
| b69ab31 | | | 38 | return hash; |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | /** |
| b69ab31 | | | 42 | * Typically we want to use succeedable revsets everywhere, to maximize support for queued commands. |
| b69ab31 | | | 43 | * But if you see and act on a visibly obsolete commit in the UI, we should use its exact hash, |
| b69ab31 | | | 44 | * so that you don't suddenly act on a seemingly unrelated commit. |
| b69ab31 | | | 45 | */ |
| b69ab31 | | | 46 | export function latestSuccessorUnlessExplicitlyObsolete( |
| b69ab31 | | | 47 | commit: Readonly<CommitInfo>, |
| b69ab31 | | | 48 | ): SucceedableRevset | ExactRevset | OptimisticRevset { |
| b69ab31 | | | 49 | if (commit.optimisticRevset != null) { |
| b69ab31 | | | 50 | return optimisticRevset(commit.optimisticRevset, commit.hash); |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | if (commit.successorInfo?.type != null) { |
| b69ab31 | | | 53 | return exactRevset(commit.hash); |
| b69ab31 | | | 54 | } |
| b69ab31 | | | 55 | return succeedableRevset(commit.hash); |
| b69ab31 | | | 56 | } |