| 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 '../previews'; |
| b69ab31 | | | 9 | import type {ExactRevset, OptimisticRevset, SucceedableRevset} from '../types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {YOU_ARE_HERE_VIRTUAL_COMMIT} from '../dag/virtualCommit'; |
| b69ab31 | | | 12 | import {t} from '../i18n'; |
| b69ab31 | | | 13 | import {CommitPreview} from '../previews'; |
| b69ab31 | | | 14 | import {latestSuccessor} from '../successionUtils'; |
| b69ab31 | | | 15 | import {GotoBaseOperation} from './GotoBaseOperation'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | export class GotoOperation extends GotoBaseOperation { |
| b69ab31 | | | 18 | constructor(protected destination: SucceedableRevset | ExactRevset | OptimisticRevset) { |
| b69ab31 | | | 19 | super(destination); |
| b69ab31 | | | 20 | } |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | getInitialInlineProgress(): [hash: string, message: string][] { |
| b69ab31 | | | 23 | return [[YOU_ARE_HERE_VIRTUAL_COMMIT.hash, t('moving...')]]; |
| b69ab31 | | | 24 | } |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | optimisticDag(dag: Dag): Dag { |
| b69ab31 | | | 27 | const headCommitHash = dag.resolve('.')?.hash; |
| b69ab31 | | | 28 | if (headCommitHash == null) { |
| b69ab31 | | | 29 | return dag; |
| b69ab31 | | | 30 | } |
| b69ab31 | | | 31 | const dest = dag.resolve(latestSuccessor(dag, this.destination)); |
| b69ab31 | | | 32 | const src = dag.get(headCommitHash); |
| b69ab31 | | | 33 | if (dest == null || src == null || dest.hash === src.hash) { |
| b69ab31 | | | 34 | return dag; |
| b69ab31 | | | 35 | } |
| b69ab31 | | | 36 | return dag.replaceWith([src.hash, dest.hash], (h, c) => { |
| b69ab31 | | | 37 | const isDest = h === dest.hash; |
| b69ab31 | | | 38 | const previewType = isDest |
| b69ab31 | | | 39 | ? CommitPreview.GOTO_DESTINATION |
| b69ab31 | | | 40 | : CommitPreview.GOTO_PREVIOUS_LOCATION; |
| b69ab31 | | | 41 | return c?.merge({isDot: isDest, previewType}); |
| b69ab31 | | | 42 | }); |
| b69ab31 | | | 43 | } |
| b69ab31 | | | 44 | } |