| 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 {Button} from 'isl-components/Button'; |
| b69ab31 | | | 9 | import {Checkbox} from 'isl-components/Checkbox'; |
| b69ab31 | | | 10 | import {Divider} from 'isl-components/Divider'; |
| b69ab31 | | | 11 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 12 | import {Kbd} from 'isl-components/Kbd'; |
| b69ab31 | | | 13 | import {KeyCode, Modifier} from 'isl-components/KeyboardShortcuts'; |
| b69ab31 | | | 14 | import {TextField} from 'isl-components/TextField'; |
| b69ab31 | | | 15 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 16 | import {useAtom} from 'jotai'; |
| b69ab31 | | | 17 | import {useEffect, useRef, useState} from 'react'; |
| b69ab31 | | | 18 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 19 | import {Collapsable} from './Collapsable'; |
| b69ab31 | | | 20 | import {CommitCloudInfo} from './CommitCloud'; |
| b69ab31 | | | 21 | import {DropdownFields} from './DropdownFields'; |
| b69ab31 | | | 22 | import {GotoTimeContent} from './GotoTimeMenu'; |
| b69ab31 | | | 23 | import {useCommandEvent} from './ISLShortcuts'; |
| b69ab31 | | | 24 | import {Internal} from './Internal'; |
| b69ab31 | | | 25 | import {tracker} from './analytics'; |
| b69ab31 | | | 26 | import {findPublicBaseAncestor} from './getCommitTree'; |
| b69ab31 | | | 27 | import {t, T} from './i18n'; |
| b69ab31 | | | 28 | import {configBackedAtom, readAtom} from './jotaiUtils'; |
| b69ab31 | | | 29 | import {GotoOperation} from './operations/GotoOperation'; |
| b69ab31 | | | 30 | import {GraftOperation} from './operations/GraftOperation'; |
| b69ab31 | | | 31 | import {PullRevOperation} from './operations/PullRevOperation'; |
| b69ab31 | | | 32 | import {RebaseKeepOperation} from './operations/RebaseKeepOperation'; |
| b69ab31 | | | 33 | import {RebaseOperation} from './operations/RebaseOperation'; |
| b69ab31 | | | 34 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 35 | import {dagWithPreviews} from './previews'; |
| b69ab31 | | | 36 | import {forceFetchCommit} from './serverAPIState'; |
| b69ab31 | | | 37 | import {exactRevset, succeedableRevset} from './types'; |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | import './DownloadCommitsMenu.css'; |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | export function DownloadCommitsTooltipButton() { |
| b69ab31 | | | 42 | const additionalToggles = useCommandEvent('ToggleDownloadCommitsDropdown'); |
| b69ab31 | | | 43 | return ( |
| b69ab31 | | | 44 | <Tooltip |
| b69ab31 | | | 45 | trigger="click" |
| b69ab31 | | | 46 | component={dismiss => <DownloadCommitsTooltip dismiss={dismiss} />} |
| b69ab31 | | | 47 | placement="bottom" |
| b69ab31 | | | 48 | additionalToggles={additionalToggles.asEventTarget()} |
| b69ab31 | | | 49 | group="topbar" |
| b69ab31 | | | 50 | title={ |
| b69ab31 | | | 51 | <div> |
| b69ab31 | | | 52 | <T replace={{$shortcut: <Kbd modifiers={[Modifier.ALT]} keycode={KeyCode.D} />}}> |
| b69ab31 | | | 53 | Download commits and diffs ($shortcut) |
| b69ab31 | | | 54 | </T> |
| b69ab31 | | | 55 | </div> |
| b69ab31 | | | 56 | }> |
| b69ab31 | | | 57 | <Button icon data-testid="download-commits-tooltip-button"> |
| b69ab31 | | | 58 | <Icon icon="cloud-download" /> |
| b69ab31 | | | 59 | </Button> |
| b69ab31 | | | 60 | </Tooltip> |
| b69ab31 | | | 61 | ); |
| b69ab31 | | | 62 | } |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | const downloadCommitRebaseType = configBackedAtom<'rebase_base' | 'rebase_ontop' | null>( |
| b69ab31 | | | 65 | 'isl.download-commit-rebase-type', |
| b69ab31 | | | 66 | null, |
| b69ab31 | | | 67 | ); |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | const downloadCommitShouldGoto = configBackedAtom<boolean>( |
| b69ab31 | | | 70 | 'isl.download-commit-should-goto', |
| b69ab31 | | | 71 | false, |
| b69ab31 | | | 72 | ); |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | function maybeSupportedByPull(name: string): boolean { |
| b69ab31 | | | 75 | return ( |
| b69ab31 | | | 76 | !name.includes('(') /* likely a revset */ && |
| b69ab31 | | | 77 | name.length < 42 /* likely an expression or rREPOhash Phabricator callsign */ |
| b69ab31 | | | 78 | ); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | function DownloadCommitsTooltip({dismiss}: {dismiss: () => unknown}) { |
| b69ab31 | | | 82 | const [enteredRevset, setEnteredRevset] = useState(''); |
| b69ab31 | | | 83 | const runOperation = useRunOperation(); |
| b69ab31 | | | 84 | const supportsDiffDownload = Internal.diffDownloadOperation != null; |
| b69ab31 | | | 85 | const downloadDiffTextArea = useRef(null); |
| b69ab31 | | | 86 | useEffect(() => { |
| b69ab31 | | | 87 | if (downloadDiffTextArea.current) { |
| b69ab31 | | | 88 | (downloadDiffTextArea.current as HTMLTextAreaElement).focus(); |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | }, [downloadDiffTextArea]); |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | const [rebaseType, setRebaseType] = useAtom(downloadCommitRebaseType); |
| b69ab31 | | | 93 | const [shouldGoto, setShouldGoto] = useAtom(downloadCommitShouldGoto); |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | const doCommitDownload = async () => { |
| b69ab31 | | | 96 | tracker.track('ClickPullButton', { |
| b69ab31 | | | 97 | extras: { |
| b69ab31 | | | 98 | rebaseType, |
| b69ab31 | | | 99 | shouldGoto, |
| b69ab31 | | | 100 | }, |
| b69ab31 | | | 101 | }); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | // We need to dismiss the tooltip now, since we don't want to leave it up until after the operations are run. |
| b69ab31 | | | 104 | dismiss(); |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | // Typically, we'd just immediately use runOperation to queue up additional operations. |
| b69ab31 | | | 107 | // Unfortunately, we don't know if the result will be public or not, |
| b69ab31 | | | 108 | // and that changes how we'll rebase/graft the result. This means we can't use the queueing system. |
| b69ab31 | | | 109 | // This is not a correctness issue because we show no optimistically downloaded result to act on. |
| b69ab31 | | | 110 | // Worst case, the rebase/goto will be queued after some other unrelated actions which should be fine. |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | if (maybeSupportedByPull(enteredRevset)) { |
| b69ab31 | | | 113 | try { |
| b69ab31 | | | 114 | await runOperation( |
| b69ab31 | | | 115 | new PullRevOperation(exactRevset(enteredRevset)), |
| b69ab31 | | | 116 | /* throwOnError */ true, |
| b69ab31 | | | 117 | ); |
| b69ab31 | | | 118 | } catch (err) { |
| b69ab31 | | | 119 | if (Internal.diffDownloadOperation != null) { |
| b69ab31 | | | 120 | // Note: try backup diff download system internally |
| b69ab31 | | | 121 | await runOperation( |
| b69ab31 | | | 122 | Internal.diffDownloadOperation(exactRevset(enteredRevset)), |
| b69ab31 | | | 123 | /* throwOnError */ true, |
| b69ab31 | | | 124 | ); |
| b69ab31 | | | 125 | } else { |
| b69ab31 | | | 126 | // If there's no backup operation, respect the error and don't try further actions |
| b69ab31 | | | 127 | throw err; |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | } |
| b69ab31 | | | 130 | } |
| b69ab31 | | | 131 | |
| b69ab31 | | | 132 | // Lookup the result of the pull |
| b69ab31 | | | 133 | const latest = await forceFetchCommit(enteredRevset).catch(() => null); |
| b69ab31 | | | 134 | if (!latest) { |
| b69ab31 | | | 135 | // We can't continue with the rebase/goto if the lookup failed. |
| b69ab31 | | | 136 | return; |
| b69ab31 | | | 137 | } |
| b69ab31 | | | 138 | |
| b69ab31 | | | 139 | // Now we CAN queue up additional actions |
| b69ab31 | | | 140 | |
| b69ab31 | | | 141 | const isPublic = latest?.phase === 'public'; |
| b69ab31 | | | 142 | if (rebaseType != null) { |
| b69ab31 | | | 143 | const Op = isPublic |
| b69ab31 | | | 144 | ? // "graft" implicitly does "goto", "rebase --keep" does not |
| b69ab31 | | | 145 | shouldGoto |
| b69ab31 | | | 146 | ? GraftOperation |
| b69ab31 | | | 147 | : RebaseKeepOperation |
| b69ab31 | | | 148 | : RebaseOperation; |
| b69ab31 | | | 149 | const dest = |
| b69ab31 | | | 150 | rebaseType === 'rebase_ontop' |
| b69ab31 | | | 151 | ? '.' |
| b69ab31 | | | 152 | : nullthrows(findPublicBaseAncestor(readAtom(dagWithPreviews))?.hash); |
| b69ab31 | | | 153 | // Use exact revsets for sources, so that you can type a specific hash to download and not be surprised by succession. |
| b69ab31 | | | 154 | // Only use succession for destination, which may be in flux at the moment you start the download. |
| b69ab31 | | | 155 | runOperation(new Op(exactRevset(enteredRevset), succeedableRevset(dest))); |
| b69ab31 | | | 156 | } |
| b69ab31 | | | 157 | |
| b69ab31 | | | 158 | if ( |
| b69ab31 | | | 159 | shouldGoto && |
| b69ab31 | | | 160 | // Goto for public commits will be handled by Graft, if a rebase/graft was performed. |
| b69ab31 | | | 161 | // Goto on max(latest_successors(revset)) would just yield the existing public commit, |
| b69ab31 | | | 162 | // but for non-landed commits, using succeedableRevset allows goto the newly rebased commit. |
| b69ab31 | | | 163 | // If no rebase was performed, we will use goto even for public commits. |
| b69ab31 | | | 164 | (!isPublic || rebaseType === null) |
| b69ab31 | | | 165 | ) { |
| b69ab31 | | | 166 | runOperation( |
| b69ab31 | | | 167 | new GotoOperation( |
| b69ab31 | | | 168 | // if not rebasing, just use the exact revset. |
| b69ab31 | | | 169 | rebaseType == null ? exactRevset(enteredRevset) : succeedableRevset(enteredRevset), |
| b69ab31 | | | 170 | ), |
| b69ab31 | | | 171 | ); |
| b69ab31 | | | 172 | } |
| b69ab31 | | | 173 | |
| b69ab31 | | | 174 | const fullRepoBranchGraftOperation = Internal.getFullRepoBranchGraftOperation?.( |
| b69ab31 | | | 175 | dagWithPreviews, |
| b69ab31 | | | 176 | latest, |
| b69ab31 | | | 177 | rebaseType, |
| b69ab31 | | | 178 | ); |
| b69ab31 | | | 179 | if (fullRepoBranchGraftOperation != null) { |
| b69ab31 | | | 180 | runOperation(fullRepoBranchGraftOperation); |
| b69ab31 | | | 181 | } |
| b69ab31 | | | 182 | }; |
| b69ab31 | | | 183 | |
| b69ab31 | | | 184 | return ( |
| b69ab31 | | | 185 | <DropdownFields |
| b69ab31 | | | 186 | title={<T>Download Commits</T>} |
| b69ab31 | | | 187 | icon="cloud-download" |
| b69ab31 | | | 188 | data-testid="download-commits-dropdown"> |
| b69ab31 | | | 189 | <div className="download-commits-content"> |
| b69ab31 | | | 190 | <div className="download-commits-input-row"> |
| b69ab31 | | | 191 | <TextField |
| b69ab31 | | | 192 | width="100%" |
| b69ab31 | | | 193 | placeholder={ |
| b69ab31 | | | 194 | supportsDiffDownload ? t('Hash, Diff Number, ...') : t('Hash, revset, pr123, ...') |
| b69ab31 | | | 195 | } |
| b69ab31 | | | 196 | value={enteredRevset} |
| b69ab31 | | | 197 | data-testid="download-commits-input" |
| b69ab31 | | | 198 | onInput={e => setEnteredRevset((e.target as unknown as {value: string})?.value ?? '')} |
| b69ab31 | | | 199 | onKeyDown={e => { |
| b69ab31 | | | 200 | if (e.key === 'Enter') { |
| b69ab31 | | | 201 | if (enteredRevset.trim().length > 0) { |
| b69ab31 | | | 202 | doCommitDownload(); |
| b69ab31 | | | 203 | } |
| b69ab31 | | | 204 | } |
| b69ab31 | | | 205 | }} |
| b69ab31 | | | 206 | ref={downloadDiffTextArea} |
| b69ab31 | | | 207 | /> |
| b69ab31 | | | 208 | <Button |
| b69ab31 | | | 209 | data-testid="download-commit-button" |
| b69ab31 | | | 210 | disabled={enteredRevset.trim().length === 0} |
| b69ab31 | | | 211 | onClick={doCommitDownload}> |
| b69ab31 | | | 212 | <T>Pull</T> |
| b69ab31 | | | 213 | </Button> |
| b69ab31 | | | 214 | </div> |
| b69ab31 | | | 215 | <div className="download-commits-input-row"> |
| b69ab31 | | | 216 | <Tooltip title={t('After downloading this commit, also go there')}> |
| b69ab31 | | | 217 | <Checkbox checked={shouldGoto} onChange={setShouldGoto}> |
| b69ab31 | | | 218 | <T>Go to</T> |
| b69ab31 | | | 219 | </Checkbox> |
| b69ab31 | | | 220 | </Tooltip> |
| b69ab31 | | | 221 | <Tooltip |
| b69ab31 | | | 222 | title={t( |
| b69ab31 | | | 223 | 'After downloading this commit, rebase it onto the public base of the current stack. Public commits will be copied instead of moved.', |
| b69ab31 | | | 224 | )}> |
| b69ab31 | | | 225 | <Checkbox |
| b69ab31 | | | 226 | checked={rebaseType === 'rebase_base'} |
| b69ab31 | | | 227 | onChange={checked => { |
| b69ab31 | | | 228 | setRebaseType(checked ? 'rebase_base' : null); |
| b69ab31 | | | 229 | }}> |
| b69ab31 | | | 230 | <T>Rebase to Stack Base</T> |
| b69ab31 | | | 231 | </Checkbox> |
| b69ab31 | | | 232 | </Tooltip> |
| b69ab31 | | | 233 | <Tooltip |
| b69ab31 | | | 234 | title={t( |
| b69ab31 | | | 235 | 'After downloading this commit, rebase it on top of the current commit. Public commits will be copied instead of moved.', |
| b69ab31 | | | 236 | )}> |
| b69ab31 | | | 237 | <Checkbox |
| b69ab31 | | | 238 | checked={rebaseType === 'rebase_ontop'} |
| b69ab31 | | | 239 | onChange={checked => { |
| b69ab31 | | | 240 | setRebaseType(checked ? 'rebase_ontop' : null); |
| b69ab31 | | | 241 | }}> |
| b69ab31 | | | 242 | <T>Rebase onto Stack</T> |
| b69ab31 | | | 243 | </Checkbox> |
| b69ab31 | | | 244 | </Tooltip> |
| b69ab31 | | | 245 | </div> |
| b69ab31 | | | 246 | </div> |
| b69ab31 | | | 247 | |
| b69ab31 | | | 248 | <Collapsable |
| b69ab31 | | | 249 | title={ |
| b69ab31 | | | 250 | <> |
| b69ab31 | | | 251 | <Icon icon="clock" /> |
| b69ab31 | | | 252 | <T>Go to time</T> |
| b69ab31 | | | 253 | </> |
| b69ab31 | | | 254 | } |
| b69ab31 | | | 255 | className="download-commits-expander"> |
| b69ab31 | | | 256 | <GotoTimeContent dismiss={dismiss} /> |
| b69ab31 | | | 257 | </Collapsable> |
| b69ab31 | | | 258 | |
| b69ab31 | | | 259 | {Internal.supportsCommitCloud && ( |
| b69ab31 | | | 260 | <> |
| b69ab31 | | | 261 | <Divider /> |
| b69ab31 | | | 262 | <CommitCloudInfo /> |
| b69ab31 | | | 263 | </> |
| b69ab31 | | | 264 | )} |
| b69ab31 | | | 265 | </DropdownFields> |
| b69ab31 | | | 266 | ); |
| b69ab31 | | | 267 | } |