| 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 {Operation} from './operations/Operation'; |
| b69ab31 | | | 9 | import type {CommitInfo, ExactRevset, Hash, OptimisticRevset, SucceedableRevset} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 12 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 13 | import {Kbd} from 'isl-components/Kbd'; |
| b69ab31 | | | 14 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 15 | import {atom} from 'jotai'; |
| b69ab31 | | | 16 | import React from 'react'; |
| b69ab31 | | | 17 | import {useContextMenu} from 'shared/ContextMenu'; |
| b69ab31 | | | 18 | import {allCommands} from './ISLShortcuts'; |
| b69ab31 | | | 19 | import './SuggestedRebase.css'; |
| b69ab31 | | | 20 | import {tracker} from './analytics'; |
| b69ab31 | | | 21 | import {findPublicBaseAncestor} from './getCommitTree'; |
| b69ab31 | | | 22 | import {T} from './i18n'; |
| b69ab31 | | | 23 | import {atomFamilyWeak, readAtom} from './jotaiUtils'; |
| b69ab31 | | | 24 | import {BulkRebaseOperation} from './operations/BulkRebaseOperation'; |
| b69ab31 | | | 25 | import {RebaseAllDraftCommitsOperation} from './operations/RebaseAllDraftCommitsOperation'; |
| b69ab31 | | | 26 | import {RebaseOperation} from './operations/RebaseOperation'; |
| b69ab31 | | | 27 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 28 | import {dagWithPreviews} from './previews'; |
| b69ab31 | | | 29 | import {RelativeDate} from './relativeDate'; |
| b69ab31 | | | 30 | import {commitsShownRange, latestDag} from './serverAPIState'; |
| b69ab31 | | | 31 | import {succeedableRevset} from './types'; |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | /** |
| b69ab31 | | | 34 | * Whether a given stack (from its base hash) is eligible for currently suggested rebase. |
| b69ab31 | | | 35 | * Determined by if the stack is old enough and worth rebasing (i.e. not obsolete or closed) |
| b69ab31 | | | 36 | */ |
| b69ab31 | | | 37 | export const showSuggestedRebaseForStack = atomFamilyWeak((hash: Hash) => |
| b69ab31 | | | 38 | atom(get => { |
| b69ab31 | | | 39 | const dag = get(dagWithPreviews); |
| b69ab31 | | | 40 | const commit = dag.get(hash); |
| b69ab31 | | | 41 | if (commit == null) { |
| b69ab31 | | | 42 | return false; |
| b69ab31 | | | 43 | } |
| b69ab31 | | | 44 | const parentHash = commit.parents.at(0); |
| b69ab31 | | | 45 | const stackBase = dag.get(parentHash); |
| b69ab31 | | | 46 | if (stackBase == null) { |
| b69ab31 | | | 47 | return false; |
| b69ab31 | | | 48 | } |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | // If the public base is already on a remote bookmark or a stable commit, don't suggest rebasing it. |
| b69ab31 | | | 51 | if ( |
| b69ab31 | | | 52 | stackBase.remoteBookmarks.length > 0 || |
| b69ab31 | | | 53 | stackBase.bookmarks.length > 0 || |
| b69ab31 | | | 54 | (stackBase.stableCommitMetadata?.length ?? 0) > 0 |
| b69ab31 | | | 55 | ) { |
| b69ab31 | | | 56 | return false; |
| b69ab31 | | | 57 | } |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | // If all commits are obsoleted, do not suggest rebasing (but should suggest cleanup). |
| b69ab31 | | | 60 | const stack = dag.descendants(hash); |
| b69ab31 | | | 61 | if (stack.size === dag.obsolete(stack).size) { |
| b69ab31 | | | 62 | return false; |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | return true; |
| b69ab31 | | | 66 | }), |
| b69ab31 | | | 67 | ); |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | export const suggestedRebaseDestinations = atom(get => { |
| b69ab31 | | | 70 | const dag = get(latestDag); |
| b69ab31 | | | 71 | const publicBase = findPublicBaseAncestor(get(dagWithPreviews)); |
| b69ab31 | | | 72 | const destinations: Array<[CommitInfo, React.ReactNode]> = dag |
| b69ab31 | | | 73 | .getBatch(dag.public_().toArray()) |
| b69ab31 | | | 74 | .filter( |
| b69ab31 | | | 75 | commit => commit.remoteBookmarks.length > 0 || (commit.stableCommitMetadata?.length ?? 0) > 0, |
| b69ab31 | | | 76 | ) |
| b69ab31 | | | 77 | .map((commit): [CommitInfo, string] => [ |
| b69ab31 | | | 78 | commit, |
| b69ab31 | | | 79 | [ |
| b69ab31 | | | 80 | ...commit.remoteBookmarks, |
| b69ab31 | | | 81 | ...(commit.stableCommitMetadata?.map(s => s.value) ?? []), |
| b69ab31 | | | 82 | ...commit.bookmarks, |
| b69ab31 | | | 83 | ].join(', '), |
| b69ab31 | | | 84 | ]) |
| b69ab31 | | | 85 | .filter(([_commit, label]) => label.length > 0); |
| b69ab31 | | | 86 | if (publicBase) { |
| b69ab31 | | | 87 | const [modifiers, keycode] = allCommands.RebaseOntoCurrentStackBase; |
| b69ab31 | | | 88 | const publicBaseLabel = ( |
| b69ab31 | | | 89 | <T |
| b69ab31 | | | 90 | replace={{ |
| b69ab31 | | | 91 | $shortcut: ( |
| b69ab31 | | | 92 | <Kbd modifiers={Array.isArray(modifiers) ? modifiers : [modifiers]} keycode={keycode} /> |
| b69ab31 | | | 93 | ), |
| b69ab31 | | | 94 | }}> |
| b69ab31 | | | 95 | Current Stack Base ($shortcut) |
| b69ab31 | | | 96 | </T> |
| b69ab31 | | | 97 | ); |
| b69ab31 | | | 98 | const existing = destinations.find(dest => dest[0].hash === publicBase.hash); |
| b69ab31 | | | 99 | if (existing != null) { |
| b69ab31 | | | 100 | existing[1] = ( |
| b69ab31 | | | 101 | <> |
| b69ab31 | | | 102 | {publicBaseLabel}, {existing[1]} |
| b69ab31 | | | 103 | </> |
| b69ab31 | | | 104 | ); |
| b69ab31 | | | 105 | } else { |
| b69ab31 | | | 106 | destinations.push([publicBase, publicBaseLabel]); |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | } |
| b69ab31 | | | 109 | destinations.sort((a, b) => b[0].date.valueOf() - a[0].date.valueOf()); |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | return destinations; |
| b69ab31 | | | 112 | }); |
| b69ab31 | | | 113 | |
| b69ab31 | | | 114 | export function SuggestedRebaseButton({ |
| b69ab31 | | | 115 | source, |
| b69ab31 | | | 116 | sources, |
| b69ab31 | | | 117 | afterRun, |
| b69ab31 | | | 118 | }: |
| b69ab31 | | | 119 | | { |
| b69ab31 | | | 120 | source: SucceedableRevset | ExactRevset | OptimisticRevset; |
| b69ab31 | | | 121 | sources?: undefined; |
| b69ab31 | | | 122 | afterRun?: () => unknown; |
| b69ab31 | | | 123 | } |
| b69ab31 | | | 124 | | { |
| b69ab31 | | | 125 | source?: undefined; |
| b69ab31 | | | 126 | sources: Array<SucceedableRevset>; |
| b69ab31 | | | 127 | afterRun?: () => unknown; |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | | { |
| b69ab31 | | | 130 | source?: undefined; |
| b69ab31 | | | 131 | sources?: undefined; |
| b69ab31 | | | 132 | afterRun?: () => unknown; |
| b69ab31 | | | 133 | }) { |
| b69ab31 | | | 134 | const runOperation = useRunOperation(); |
| b69ab31 | | | 135 | const isBulk = source == null; |
| b69ab31 | | | 136 | const isAllDraftCommits = sources == null && source == null; |
| b69ab31 | | | 137 | const showContextMenu = useContextMenu(() => { |
| b69ab31 | | | 138 | const destinations = readAtom(suggestedRebaseDestinations); |
| b69ab31 | | | 139 | return ( |
| b69ab31 | | | 140 | destinations?.map(([dest, label]) => { |
| b69ab31 | | | 141 | return { |
| b69ab31 | | | 142 | label: ( |
| b69ab31 | | | 143 | <span className="suggested-rebase-context-menu-option"> |
| b69ab31 | | | 144 | <span>{label}</span> |
| b69ab31 | | | 145 | <Subtle> |
| b69ab31 | | | 146 | <RelativeDate date={dest.date} /> |
| b69ab31 | | | 147 | </Subtle> |
| b69ab31 | | | 148 | </span> |
| b69ab31 | | | 149 | ), |
| b69ab31 | | | 150 | onClick: () => { |
| b69ab31 | | | 151 | runOperation(getSuggestedRebaseOperation(dest, source ?? sources)); |
| b69ab31 | | | 152 | afterRun?.(); |
| b69ab31 | | | 153 | }, |
| b69ab31 | | | 154 | }; |
| b69ab31 | | | 155 | }) ?? [] |
| b69ab31 | | | 156 | ); |
| b69ab31 | | | 157 | }); |
| b69ab31 | | | 158 | return ( |
| b69ab31 | | | 159 | <Button icon={!isBulk} onClick={showContextMenu}> |
| b69ab31 | | | 160 | <Icon icon="git-pull-request" slot="start" /> |
| b69ab31 | | | 161 | {isAllDraftCommits ? ( |
| b69ab31 | | | 162 | <T>Rebase all onto…</T> |
| b69ab31 | | | 163 | ) : isBulk ? ( |
| b69ab31 | | | 164 | <T>Rebase selected commits onto...</T> |
| b69ab31 | | | 165 | ) : ( |
| b69ab31 | | | 166 | <T>Rebase onto…</T> |
| b69ab31 | | | 167 | )} |
| b69ab31 | | | 168 | </Button> |
| b69ab31 | | | 169 | ); |
| b69ab31 | | | 170 | } |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | /** |
| b69ab31 | | | 173 | * Returns an operation that will rebase the given source onto the given destination. |
| b69ab31 | | | 174 | * If source is undefined, rebase all draft commits. |
| b69ab31 | | | 175 | * If source is an Array of revsets, bulk rebase those commits. |
| b69ab31 | | | 176 | * If source is a single revset, rebase that commit. |
| b69ab31 | | | 177 | */ |
| b69ab31 | | | 178 | export function getSuggestedRebaseOperation( |
| b69ab31 | | | 179 | dest: CommitInfo, |
| b69ab31 | | | 180 | source: SucceedableRevset | ExactRevset | OptimisticRevset | Array<SucceedableRevset> | undefined, |
| b69ab31 | | | 181 | ): Operation { |
| b69ab31 | | | 182 | const destination = dest.remoteBookmarks?.[0] ?? dest.hash; |
| b69ab31 | | | 183 | const isBulk = source != null && Array.isArray(source); |
| b69ab31 | | | 184 | tracker.track('ClickSuggestedRebase', { |
| b69ab31 | | | 185 | extras: {destination, isBulk, locations: dest.stableCommitMetadata?.map(s => s.value)}, |
| b69ab31 | | | 186 | }); |
| b69ab31 | | | 187 | |
| b69ab31 | | | 188 | const operation = |
| b69ab31 | | | 189 | source == null |
| b69ab31 | | | 190 | ? new RebaseAllDraftCommitsOperation( |
| b69ab31 | | | 191 | readAtom(commitsShownRange), |
| b69ab31 | | | 192 | succeedableRevset(destination), |
| b69ab31 | | | 193 | ) |
| b69ab31 | | | 194 | : Array.isArray(source) |
| b69ab31 | | | 195 | ? new BulkRebaseOperation(source, succeedableRevset(destination)) |
| b69ab31 | | | 196 | : new RebaseOperation(source, succeedableRevset(destination)); |
| b69ab31 | | | 197 | |
| b69ab31 | | | 198 | return operation; |
| b69ab31 | | | 199 | } |