| 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 {Hash} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 12 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 13 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 14 | import {HighlightCommitsWhileHovering} from './HighlightedCommits'; |
| b69ab31 | | | 15 | import {Internal} from './Internal'; |
| b69ab31 | | | 16 | import {tracker} from './analytics'; |
| b69ab31 | | | 17 | import {useFeatureFlagSync} from './featureFlags'; |
| b69ab31 | | | 18 | import {t, T} from './i18n'; |
| b69ab31 | | | 19 | import {atomFamilyWeak} from './jotaiUtils'; |
| b69ab31 | | | 20 | import {RebaseOperation} from './operations/RebaseOperation'; |
| b69ab31 | | | 21 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 22 | import {dagWithPreviews} from './previews'; |
| b69ab31 | | | 23 | import {succeedableRevset} from './types'; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | /** |
| b69ab31 | | | 26 | * For a given obsolete commit, returns info about its orphaned (non-obsolete) |
| b69ab31 | | | 27 | * children and the latest successor to rebase them onto, or null if the button |
| b69ab31 | | | 28 | * should not be shown. |
| b69ab31 | | | 29 | */ |
| b69ab31 | | | 30 | export const orphanedChildrenForCommit = atomFamilyWeak((hash: Hash) => |
| b69ab31 | | | 31 | atom(get => { |
| b69ab31 | | | 32 | const dag = get(dagWithPreviews); |
| b69ab31 | | | 33 | const commit = dag.get(hash); |
| b69ab31 | | | 34 | if (commit == null || commit.successorInfo == null) { |
| b69ab31 | | | 35 | return null; |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | // Don't show for landed commits — they use the Cleanup workflow instead. |
| b69ab31 | | | 39 | const successorType = commit.successorInfo.type; |
| b69ab31 | | | 40 | if (successorType === 'land' || successorType === 'pushrebase') { |
| b69ab31 | | | 41 | return null; |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | // Follow the successor chain to find the latest non-obsolete successor. |
| b69ab31 | | | 45 | const successors = dag.followSuccessors(hash); |
| b69ab31 | | | 46 | // followSuccessors returns a set; remove the original hash to get just successors. |
| b69ab31 | | | 47 | const successorHashes = successors |
| b69ab31 | | | 48 | .toHashes() |
| b69ab31 | | | 49 | .toArray() |
| b69ab31 | | | 50 | .filter(h => h !== hash); |
| b69ab31 | | | 51 | if (successorHashes.length === 0) { |
| b69ab31 | | | 52 | return null; |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | // Pick the first successor. If it's not in the DAG or is itself obsolete, bail out. |
| b69ab31 | | | 56 | const successorHash = successorHashes[0]; |
| b69ab31 | | | 57 | const successorCommit = dag.get(successorHash); |
| b69ab31 | | | 58 | if (successorCommit == null || successorCommit.successorInfo != null) { |
| b69ab31 | | | 59 | return null; |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | // Find non-obsolete children of this obsolete commit. |
| b69ab31 | | | 63 | const children = dag.children(hash); |
| b69ab31 | | | 64 | const nonObsoleteChildren = dag.nonObsolete(children); |
| b69ab31 | | | 65 | if (nonObsoleteChildren.size === 0) { |
| b69ab31 | | | 66 | return null; |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | return { |
| b69ab31 | | | 70 | orphanedChildren: nonObsoleteChildren.toHashes().toArray(), |
| b69ab31 | | | 71 | successorHash, |
| b69ab31 | | | 72 | }; |
| b69ab31 | | | 73 | }), |
| b69ab31 | | | 74 | ); |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | /** |
| b69ab31 | | | 77 | * For a given stack root hash, aggregates all orphaned children across all |
| b69ab31 | | | 78 | * obsolete commits in the stack. Returns the list of rebase operations needed |
| b69ab31 | | | 79 | * (each mapping an orphaned child to its target successor), or null if no |
| b69ab31 | | | 80 | * orphaned commits exist in the stack. |
| b69ab31 | | | 81 | */ |
| b69ab31 | | | 82 | export const orphanedChildrenForStack = atomFamilyWeak((hash: Hash) => |
| b69ab31 | | | 83 | atom(get => { |
| b69ab31 | | | 84 | const dag = get(dagWithPreviews); |
| b69ab31 | | | 85 | const stackHashes = dag.descendants(hash).toHashes().toArray(); |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | const rebaseEntries: Array<{orphanedChild: Hash; successorHash: Hash}> = []; |
| b69ab31 | | | 88 | const allOrphaned: Hash[] = []; |
| b69ab31 | | | 89 | const allSuccessors: Hash[] = []; |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | for (const h of stackHashes) { |
| b69ab31 | | | 92 | const info = get(orphanedChildrenForCommit(h)); |
| b69ab31 | | | 93 | if (info != null) { |
| b69ab31 | | | 94 | for (const child of info.orphanedChildren) { |
| b69ab31 | | | 95 | rebaseEntries.push({orphanedChild: child, successorHash: info.successorHash}); |
| b69ab31 | | | 96 | allOrphaned.push(child); |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | if (!allSuccessors.includes(info.successorHash)) { |
| b69ab31 | | | 99 | allSuccessors.push(info.successorHash); |
| b69ab31 | | | 100 | } |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | } |
| b69ab31 | | | 103 | |
| b69ab31 | | | 104 | if (rebaseEntries.length === 0) { |
| b69ab31 | | | 105 | return null; |
| b69ab31 | | | 106 | } |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | return { |
| b69ab31 | | | 109 | rebaseEntries, |
| b69ab31 | | | 110 | allOrphaned, |
| b69ab31 | | | 111 | allSuccessors, |
| b69ab31 | | | 112 | }; |
| b69ab31 | | | 113 | }), |
| b69ab31 | | | 114 | ); |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | export function RebaseOrphanedStackButton({hash}: {hash: Hash}) { |
| b69ab31 | | | 117 | const featureEnabled = useFeatureFlagSync(Internal.featureFlags?.RebaseOntoSuccessor); |
| b69ab31 | | | 118 | const info = useAtomValue(orphanedChildrenForStack(hash)); |
| b69ab31 | | | 119 | const runOperation = useRunOperation(); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | if (!featureEnabled) { |
| b69ab31 | | | 122 | return null; |
| b69ab31 | | | 123 | } |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | if (info == null) { |
| b69ab31 | | | 126 | return null; |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | const {rebaseEntries, allOrphaned, allSuccessors} = info; |
| b69ab31 | | | 130 | |
| b69ab31 | | | 131 | const handleClick = async () => { |
| b69ab31 | | | 132 | tracker.track('ClickRebaseOntoSuccessor', { |
| b69ab31 | | | 133 | extras: { |
| b69ab31 | | | 134 | sources: allOrphaned, |
| b69ab31 | | | 135 | dest: allSuccessors.join(','), |
| b69ab31 | | | 136 | numOrphans: allOrphaned.length, |
| b69ab31 | | | 137 | }, |
| b69ab31 | | | 138 | }); |
| b69ab31 | | | 139 | for (const {orphanedChild, successorHash} of rebaseEntries) { |
| b69ab31 | | | 140 | runOperation( |
| b69ab31 | | | 141 | new RebaseOperation(succeedableRevset(orphanedChild), succeedableRevset(successorHash)), |
| b69ab31 | | | 142 | ); |
| b69ab31 | | | 143 | } |
| b69ab31 | | | 144 | }; |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | return ( |
| b69ab31 | | | 147 | <HighlightCommitsWhileHovering toHighlight={[...allOrphaned, ...allSuccessors]}> |
| b69ab31 | | | 148 | <Tooltip |
| b69ab31 | | | 149 | title={t('Rebase orphaned commits onto the latest successors of their obsolete parents')} |
| b69ab31 | | | 150 | placement="bottom"> |
| b69ab31 | | | 151 | <Button icon onClick={handleClick} data-testid="rebase-onto-successor-button"> |
| b69ab31 | | | 152 | <Icon icon="git-pull-request" slot="start" /> |
| b69ab31 | | | 153 | <T>Rebase orphaned commits</T> |
| b69ab31 | | | 154 | </Button> |
| b69ab31 | | | 155 | </Tooltip> |
| b69ab31 | | | 156 | </HighlightCommitsWhileHovering> |
| b69ab31 | | | 157 | ); |
| b69ab31 | | | 158 | } |