| 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 {CommitTreeWithPreviews} from './getCommitTree'; |
| b69ab31 | | | 10 | import type {Operation} from './operations/Operation'; |
| b69ab31 | | | 11 | import type {OperationInfo, OperationList} from './operationsState'; |
| b69ab31 | | | 12 | import type {ChangedFile, CommitInfo, Hash, MergeConflicts, UncommittedChanges} from './types'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import {atom, useAtom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 15 | import {useEffect} from 'react'; |
| b69ab31 | | | 16 | import {notEmpty, nullthrows} from 'shared/utils'; |
| b69ab31 | | | 17 | import {latestSuccessorsMapAtom} from './SuccessionTracker'; |
| b69ab31 | | | 18 | import {getTracker} from './analytics/globalTracker'; |
| b69ab31 | | | 19 | import {focusMode} from './atoms/FocusModeState'; |
| b69ab31 | | | 20 | import {YOU_ARE_HERE_VIRTUAL_COMMIT} from './dag/virtualCommit'; |
| b69ab31 | | | 21 | import {getCommitTree, walkTreePostorder} from './getCommitTree'; |
| b69ab31 | | | 22 | import {getOpName} from './operations/Operation'; |
| b69ab31 | | | 23 | import {operationBeingPreviewed, operationList, queuedOperations} from './operationsState'; |
| b69ab31 | | | 24 | import { |
| b69ab31 | | | 25 | latestCommits, |
| b69ab31 | | | 26 | latestCommitsData, |
| b69ab31 | | | 27 | latestDag, |
| b69ab31 | | | 28 | latestHeadCommit, |
| b69ab31 | | | 29 | latestUncommittedChanges, |
| b69ab31 | | | 30 | latestUncommittedChangesData, |
| b69ab31 | | | 31 | mergeConflicts, |
| b69ab31 | | | 32 | } from './serverAPIState'; |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | export enum CommitPreview { |
| b69ab31 | | | 35 | REBASE_ROOT = 'rebase-root', |
| b69ab31 | | | 36 | REBASE_DESCENDANT = 'rebase-descendant', |
| b69ab31 | | | 37 | REBASE_OLD = 'rebase-old', |
| b69ab31 | | | 38 | REBASE_OPTIMISTIC_ROOT = 'rebase-optimistic-root', |
| b69ab31 | | | 39 | REBASE_OPTIMISTIC_DESCENDANT = 'rebase-optimistic-descendant', |
| b69ab31 | | | 40 | GOTO_DESTINATION = 'goto-destination', |
| b69ab31 | | | 41 | GOTO_PREVIOUS_LOCATION = 'goto-previous-location', |
| b69ab31 | | | 42 | HIDDEN_ROOT = 'hidden-root', |
| b69ab31 | | | 43 | HIDDEN_DESCENDANT = 'hidden-descendant', |
| b69ab31 | | | 44 | STACK_EDIT_ROOT = 'stack-edit-root', |
| b69ab31 | | | 45 | STACK_EDIT_DESCENDANT = 'stack-edit-descendant', |
| b69ab31 | | | 46 | FOLD_PREVIEW = 'fold-preview', |
| b69ab31 | | | 47 | FOLD = 'fold', |
| b69ab31 | | | 48 | // Commit being rendered in some other context than the commit tree, |
| b69ab31 | | | 49 | // such as the commit info sidebar |
| b69ab31 | | | 50 | NON_ACTIONABLE_COMMIT = 'non-actionable-commit', |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | /** |
| b69ab31 | | | 54 | * Alter the set of Uncommitted Changes. |
| b69ab31 | | | 55 | */ |
| b69ab31 | | | 56 | export type ApplyUncommittedChangesPreviewsFuncType = ( |
| b69ab31 | | | 57 | changes: UncommittedChanges, |
| b69ab31 | | | 58 | ) => UncommittedChanges; |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | /** |
| b69ab31 | | | 61 | * Alter the set of Merge Conflicts. |
| b69ab31 | | | 62 | */ |
| b69ab31 | | | 63 | export type ApplyMergeConflictsPreviewsFuncType = ( |
| b69ab31 | | | 64 | conflicts: MergeConflicts | undefined, |
| b69ab31 | | | 65 | ) => MergeConflicts | undefined; |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | function applyPreviewsToChangedFiles( |
| b69ab31 | | | 68 | files: Array<ChangedFile>, |
| b69ab31 | | | 69 | list: OperationList, |
| b69ab31 | | | 70 | queued: Array<Operation>, |
| b69ab31 | | | 71 | ): Array<ChangedFile> { |
| b69ab31 | | | 72 | const currentOperation = list.currentOperation; |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | // gather operations from past, current, and queued commands which could have optimistic state appliers |
| b69ab31 | | | 75 | type Applier = ( |
| b69ab31 | | | 76 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 77 | ) => ApplyUncommittedChangesPreviewsFuncType | undefined; |
| b69ab31 | | | 78 | const appliersSources: Array<Applier> = []; |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | // previous commands |
| b69ab31 | | | 81 | for (const op of list.operationHistory) { |
| b69ab31 | | | 82 | if (op != null && !op.hasCompletedUncommittedChangesOptimisticState) { |
| b69ab31 | | | 83 | if (op.operation.makeOptimisticUncommittedChangesApplier != null) { |
| b69ab31 | | | 84 | appliersSources.push( |
| b69ab31 | | | 85 | op.operation.makeOptimisticUncommittedChangesApplier.bind(op.operation), |
| b69ab31 | | | 86 | ); |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | // currently running/last command |
| b69ab31 | | | 92 | if ( |
| b69ab31 | | | 93 | currentOperation != null && |
| b69ab31 | | | 94 | !currentOperation.hasCompletedUncommittedChangesOptimisticState && |
| b69ab31 | | | 95 | // don't show optimistic state if we hit an error |
| b69ab31 | | | 96 | (currentOperation.exitCode == null || currentOperation.exitCode === 0) |
| b69ab31 | | | 97 | ) { |
| b69ab31 | | | 98 | if (currentOperation.operation.makeOptimisticUncommittedChangesApplier != null) { |
| b69ab31 | | | 99 | appliersSources.push( |
| b69ab31 | | | 100 | currentOperation.operation.makeOptimisticUncommittedChangesApplier.bind( |
| b69ab31 | | | 101 | currentOperation.operation, |
| b69ab31 | | | 102 | ), |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | } |
| b69ab31 | | | 105 | } |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | // queued commands |
| b69ab31 | | | 108 | for (const op of queued) { |
| b69ab31 | | | 109 | if (op != null) { |
| b69ab31 | | | 110 | if (op.makeOptimisticUncommittedChangesApplier != null) { |
| b69ab31 | | | 111 | appliersSources.push(op.makeOptimisticUncommittedChangesApplier.bind(op)); |
| b69ab31 | | | 112 | } |
| b69ab31 | | | 113 | } |
| b69ab31 | | | 114 | } |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | // apply in order |
| b69ab31 | | | 117 | if (appliersSources.length) { |
| b69ab31 | | | 118 | let finalChanges = files; |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | for (const applierSource of appliersSources) { |
| b69ab31 | | | 121 | const context: UncommittedChangesPreviewContext = { |
| b69ab31 | | | 122 | uncommittedChanges: files, |
| b69ab31 | | | 123 | }; |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | const applier = applierSource(context); |
| b69ab31 | | | 126 | if (applier == null) { |
| b69ab31 | | | 127 | continue; |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | |
| b69ab31 | | | 130 | finalChanges = applier(finalChanges); |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | return finalChanges; |
| b69ab31 | | | 133 | } |
| b69ab31 | | | 134 | |
| b69ab31 | | | 135 | return files; |
| b69ab31 | | | 136 | } |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | function applyPreviewsToMergeConflicts( |
| b69ab31 | | | 139 | conflicts: MergeConflicts, |
| b69ab31 | | | 140 | list: OperationList, |
| b69ab31 | | | 141 | queued: Array<Operation>, |
| b69ab31 | | | 142 | ): MergeConflicts | undefined { |
| b69ab31 | | | 143 | const currentOperation = list.currentOperation; |
| b69ab31 | | | 144 | if (conflicts.state !== 'loaded') { |
| b69ab31 | | | 145 | return conflicts; |
| b69ab31 | | | 146 | } |
| b69ab31 | | | 147 | |
| b69ab31 | | | 148 | // gather operations from past, current, and queued commands which could have optimistic state appliers |
| b69ab31 | | | 149 | type Applier = ( |
| b69ab31 | | | 150 | context: MergeConflictsPreviewContext, |
| b69ab31 | | | 151 | ) => ApplyMergeConflictsPreviewsFuncType | undefined; |
| b69ab31 | | | 152 | const appliersSources: Array<Applier> = []; |
| b69ab31 | | | 153 | |
| b69ab31 | | | 154 | // previous commands |
| b69ab31 | | | 155 | for (const op of list.operationHistory) { |
| b69ab31 | | | 156 | if (op != null && !op.hasCompletedMergeConflictsOptimisticState) { |
| b69ab31 | | | 157 | if (op.operation.makeOptimisticMergeConflictsApplier != null) { |
| b69ab31 | | | 158 | appliersSources.push(op.operation.makeOptimisticMergeConflictsApplier.bind(op.operation)); |
| b69ab31 | | | 159 | } |
| b69ab31 | | | 160 | } |
| b69ab31 | | | 161 | } |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | // currently running/last command |
| b69ab31 | | | 164 | if ( |
| b69ab31 | | | 165 | currentOperation != null && |
| b69ab31 | | | 166 | !currentOperation.hasCompletedMergeConflictsOptimisticState && |
| b69ab31 | | | 167 | // don't show optimistic state if we hit an error |
| b69ab31 | | | 168 | (currentOperation.exitCode == null || currentOperation.exitCode === 0) |
| b69ab31 | | | 169 | ) { |
| b69ab31 | | | 170 | if (currentOperation.operation.makeOptimisticMergeConflictsApplier != null) { |
| b69ab31 | | | 171 | appliersSources.push( |
| b69ab31 | | | 172 | currentOperation.operation.makeOptimisticMergeConflictsApplier.bind( |
| b69ab31 | | | 173 | currentOperation.operation, |
| b69ab31 | | | 174 | ), |
| b69ab31 | | | 175 | ); |
| b69ab31 | | | 176 | } |
| b69ab31 | | | 177 | } |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | // queued commands |
| b69ab31 | | | 180 | for (const op of queued) { |
| b69ab31 | | | 181 | if (op != null) { |
| b69ab31 | | | 182 | if (op.makeOptimisticMergeConflictsApplier != null) { |
| b69ab31 | | | 183 | appliersSources.push(op.makeOptimisticMergeConflictsApplier.bind(op)); |
| b69ab31 | | | 184 | } |
| b69ab31 | | | 185 | } |
| b69ab31 | | | 186 | } |
| b69ab31 | | | 187 | |
| b69ab31 | | | 188 | // apply in order |
| b69ab31 | | | 189 | if (appliersSources.length) { |
| b69ab31 | | | 190 | let finalChanges: MergeConflicts | undefined = conflicts; |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | for (const applierSource of appliersSources) { |
| b69ab31 | | | 193 | const context: MergeConflictsPreviewContext = { |
| b69ab31 | | | 194 | conflicts, |
| b69ab31 | | | 195 | }; |
| b69ab31 | | | 196 | |
| b69ab31 | | | 197 | const applier = applierSource(context); |
| b69ab31 | | | 198 | if (applier == null) { |
| b69ab31 | | | 199 | continue; |
| b69ab31 | | | 200 | } |
| b69ab31 | | | 201 | |
| b69ab31 | | | 202 | finalChanges = applier(finalChanges); |
| b69ab31 | | | 203 | } |
| b69ab31 | | | 204 | return finalChanges; |
| b69ab31 | | | 205 | } |
| b69ab31 | | | 206 | return conflicts; |
| b69ab31 | | | 207 | } |
| b69ab31 | | | 208 | |
| b69ab31 | | | 209 | export const uncommittedChangesWithPreviews = atom<Array<ChangedFile>>(get => { |
| b69ab31 | | | 210 | const list = get(operationList); |
| b69ab31 | | | 211 | const queued = get(queuedOperations); |
| b69ab31 | | | 212 | const uncommittedChanges = get(latestUncommittedChanges); |
| b69ab31 | | | 213 | |
| b69ab31 | | | 214 | return applyPreviewsToChangedFiles(uncommittedChanges, list, queued); |
| b69ab31 | | | 215 | }); |
| b69ab31 | | | 216 | |
| b69ab31 | | | 217 | export const optimisticMergeConflicts = atom(get => { |
| b69ab31 | | | 218 | const list = get(operationList); |
| b69ab31 | | | 219 | const queued = get(queuedOperations); |
| b69ab31 | | | 220 | const conflicts = get(mergeConflicts); |
| b69ab31 | | | 221 | if (conflicts?.files == null) { |
| b69ab31 | | | 222 | return conflicts; |
| b69ab31 | | | 223 | } |
| b69ab31 | | | 224 | |
| b69ab31 | | | 225 | return applyPreviewsToMergeConflicts(conflicts, list, queued); |
| b69ab31 | | | 226 | }); |
| b69ab31 | | | 227 | |
| b69ab31 | | | 228 | export type TreeWithPreviews = { |
| b69ab31 | | | 229 | trees: Array<CommitTreeWithPreviews>; |
| b69ab31 | | | 230 | treeMap: Map<Hash, CommitTreeWithPreviews>; |
| b69ab31 | | | 231 | headCommit?: CommitInfo; |
| b69ab31 | | | 232 | }; |
| b69ab31 | | | 233 | |
| b69ab31 | | | 234 | export type WithPreviewType = { |
| b69ab31 | | | 235 | previewType?: CommitPreview; |
| b69ab31 | | | 236 | /** |
| b69ab31 | | | 237 | * Insertion batch. Larger: later inserted. |
| b69ab31 | | | 238 | * All 'sl log' commits share a same initial number. |
| b69ab31 | | | 239 | * Later previews might have larger numbers. |
| b69ab31 | | | 240 | * Used for sorting. |
| b69ab31 | | | 241 | */ |
| b69ab31 | | | 242 | seqNumber?: number; |
| b69ab31 | | | 243 | }; |
| b69ab31 | | | 244 | |
| b69ab31 | | | 245 | export type {Dag}; |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | export const dagWithPreviews = atom(get => { |
| b69ab31 | | | 248 | const originalDag = get(latestDag); |
| b69ab31 | | | 249 | const list = get(operationList); |
| b69ab31 | | | 250 | const queued = get(queuedOperations); |
| b69ab31 | | | 251 | const currentOperation = list.currentOperation; |
| b69ab31 | | | 252 | const history = list.operationHistory; |
| b69ab31 | | | 253 | const currentPreview = get(operationBeingPreviewed); |
| b69ab31 | | | 254 | let dag = originalDag; |
| b69ab31 | | | 255 | |
| b69ab31 | | | 256 | const focus = get(focusMode); |
| b69ab31 | | | 257 | if (focus) { |
| b69ab31 | | | 258 | const current = dag.resolve('.'); |
| b69ab31 | | | 259 | if (current) { |
| b69ab31 | | | 260 | const currentStack = dag.descendants( |
| b69ab31 | | | 261 | dag.ancestors(dag.draft(current.hash), {within: dag.draft()}), |
| b69ab31 | | | 262 | ); |
| b69ab31 | | | 263 | const related = dag.descendants( |
| b69ab31 | | | 264 | dag.successors(currentStack).union(dag.predecessors(currentStack)), |
| b69ab31 | | | 265 | ); |
| b69ab31 | | | 266 | const toKeep = currentStack |
| b69ab31 | | | 267 | .union(YOU_ARE_HERE_VIRTUAL_COMMIT.hash) // ensure we always show "You Are Here" |
| b69ab31 | | | 268 | .union(related); |
| b69ab31 | | | 269 | const toRemove = dag.draft().subtract(toKeep); |
| b69ab31 | | | 270 | dag = dag.remove(toRemove); |
| b69ab31 | | | 271 | } |
| b69ab31 | | | 272 | } |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | for (const op of optimisticOperations({history, queued, currentOperation})) { |
| b69ab31 | | | 275 | dag = op.optimisticDag(dag); |
| b69ab31 | | | 276 | } |
| b69ab31 | | | 277 | if (currentPreview) { |
| b69ab31 | | | 278 | dag = currentPreview.previewDag(dag); |
| b69ab31 | | | 279 | } |
| b69ab31 | | | 280 | return dag; |
| b69ab31 | | | 281 | }); |
| b69ab31 | | | 282 | |
| b69ab31 | | | 283 | export const treeWithPreviews = atom(get => { |
| b69ab31 | | | 284 | const dag = get(dagWithPreviews); |
| b69ab31 | | | 285 | const commits = [...dag.values()]; |
| b69ab31 | | | 286 | const trees = getCommitTree(commits); |
| b69ab31 | | | 287 | |
| b69ab31 | | | 288 | let headCommit = get(latestHeadCommit); |
| b69ab31 | | | 289 | // The headCommit might be changed by dag previews. Double check. |
| b69ab31 | | | 290 | if (headCommit && !dag.get(headCommit.hash)?.isDot) { |
| b69ab31 | | | 291 | headCommit = dag.resolve('.'); |
| b69ab31 | | | 292 | } |
| b69ab31 | | | 293 | // Open-code latestCommitTreeMap to pick up tree changes done by `dag`. |
| b69ab31 | | | 294 | const treeMap = new Map<Hash, CommitTreeWithPreviews>(); |
| b69ab31 | | | 295 | for (const tree of walkTreePostorder(trees)) { |
| b69ab31 | | | 296 | treeMap.set(tree.info.hash, tree); |
| b69ab31 | | | 297 | } |
| b69ab31 | | | 298 | |
| b69ab31 | | | 299 | return {trees, treeMap, headCommit}; |
| b69ab31 | | | 300 | }); |
| b69ab31 | | | 301 | |
| b69ab31 | | | 302 | /** Yield operations that might need optimistic state. */ |
| b69ab31 | | | 303 | function* optimisticOperations(props: { |
| b69ab31 | | | 304 | history: OperationInfo[]; |
| b69ab31 | | | 305 | queued: Operation[]; |
| b69ab31 | | | 306 | currentOperation?: OperationInfo; |
| b69ab31 | | | 307 | }): Generator<Operation> { |
| b69ab31 | | | 308 | const {history, queued, currentOperation} = props; |
| b69ab31 | | | 309 | |
| b69ab31 | | | 310 | // previous commands |
| b69ab31 | | | 311 | for (const op of history) { |
| b69ab31 | | | 312 | if (op != null && !op.hasCompletedOptimisticState) { |
| b69ab31 | | | 313 | yield op.operation; |
| b69ab31 | | | 314 | } |
| b69ab31 | | | 315 | } |
| b69ab31 | | | 316 | |
| b69ab31 | | | 317 | // currently running/last command |
| b69ab31 | | | 318 | if ( |
| b69ab31 | | | 319 | currentOperation != null && |
| b69ab31 | | | 320 | !currentOperation.hasCompletedOptimisticState && |
| b69ab31 | | | 321 | // don't show optimistic state if we hit an error |
| b69ab31 | | | 322 | (currentOperation.exitCode == null || currentOperation.exitCode === 0) |
| b69ab31 | | | 323 | ) { |
| b69ab31 | | | 324 | yield currentOperation.operation; |
| b69ab31 | | | 325 | } |
| b69ab31 | | | 326 | |
| b69ab31 | | | 327 | // queued commands |
| b69ab31 | | | 328 | for (const op of queued) { |
| b69ab31 | | | 329 | if (op != null) { |
| b69ab31 | | | 330 | yield op; |
| b69ab31 | | | 331 | } |
| b69ab31 | | | 332 | } |
| b69ab31 | | | 333 | } |
| b69ab31 | | | 334 | |
| b69ab31 | | | 335 | /** |
| b69ab31 | | | 336 | * Mark operations as completed when their optimistic applier is no longer needed. |
| b69ab31 | | | 337 | * Similarly marks uncommitted changes optimistic state resolved. |
| b69ab31 | | | 338 | * n.b. this must be a useEffect since React doesn't like setCurrentOperation getting called during render |
| b69ab31 | | | 339 | * when ongoingOperation is used elsewhere in the tree |
| b69ab31 | | | 340 | */ |
| b69ab31 | | | 341 | export function useMarkOperationsCompleted(): void { |
| b69ab31 | | | 342 | const fetchedCommits = useAtomValue(latestCommitsData); |
| b69ab31 | | | 343 | const commits = useAtomValue(latestCommits); |
| b69ab31 | | | 344 | const uncommittedChanges = useAtomValue(latestUncommittedChangesData); |
| b69ab31 | | | 345 | const conflicts = useAtomValue(mergeConflicts); |
| b69ab31 | | | 346 | const successorMap = useAtomValue(latestSuccessorsMapAtom); |
| b69ab31 | | | 347 | |
| b69ab31 | | | 348 | const [list, setOperationList] = useAtom(operationList); |
| b69ab31 | | | 349 | |
| b69ab31 | | | 350 | // Mark operations as completed when their optimistic applier is no longer needed |
| b69ab31 | | | 351 | // n.b. this must be a useEffect since React doesn't like setCurrentOperation getting called during render |
| b69ab31 | | | 352 | // when ongoingOperation is used elsewhere in the tree |
| b69ab31 | | | 353 | useEffect(() => { |
| b69ab31 | | | 354 | const toMarkResolved: Array<ReturnType<typeof shouldMarkOptimisticChangesResolved>> = []; |
| b69ab31 | | | 355 | const uncommittedContext = { |
| b69ab31 | | | 356 | uncommittedChanges: uncommittedChanges.files ?? [], |
| b69ab31 | | | 357 | }; |
| b69ab31 | | | 358 | const mergeConflictsContext = { |
| b69ab31 | | | 359 | conflicts, |
| b69ab31 | | | 360 | }; |
| b69ab31 | | | 361 | const currentOperation = list.currentOperation; |
| b69ab31 | | | 362 | |
| b69ab31 | | | 363 | for (const operation of [...list.operationHistory, currentOperation]) { |
| b69ab31 | | | 364 | if (operation) { |
| b69ab31 | | | 365 | toMarkResolved.push( |
| b69ab31 | | | 366 | shouldMarkOptimisticChangesResolved(operation, uncommittedContext, mergeConflictsContext), |
| b69ab31 | | | 367 | ); |
| b69ab31 | | | 368 | } |
| b69ab31 | | | 369 | } |
| b69ab31 | | | 370 | if (toMarkResolved.some(notEmpty)) { |
| b69ab31 | | | 371 | const operationHistory = [...list.operationHistory]; |
| b69ab31 | | | 372 | const currentOperation = |
| b69ab31 | | | 373 | list.currentOperation == null ? undefined : {...list.currentOperation}; |
| b69ab31 | | | 374 | for (let i = 0; i < toMarkResolved.length - 1; i++) { |
| b69ab31 | | | 375 | if (toMarkResolved[i]?.commits) { |
| b69ab31 | | | 376 | operationHistory[i] = { |
| b69ab31 | | | 377 | ...operationHistory[i], |
| b69ab31 | | | 378 | hasCompletedOptimisticState: true, |
| b69ab31 | | | 379 | }; |
| b69ab31 | | | 380 | } |
| b69ab31 | | | 381 | if (toMarkResolved[i]?.files) { |
| b69ab31 | | | 382 | operationHistory[i] = { |
| b69ab31 | | | 383 | ...operationHistory[i], |
| b69ab31 | | | 384 | hasCompletedUncommittedChangesOptimisticState: true, |
| b69ab31 | | | 385 | }; |
| b69ab31 | | | 386 | } |
| b69ab31 | | | 387 | if (toMarkResolved[i]?.conflicts) { |
| b69ab31 | | | 388 | operationHistory[i] = { |
| b69ab31 | | | 389 | ...operationHistory[i], |
| b69ab31 | | | 390 | hasCompletedMergeConflictsOptimisticState: true, |
| b69ab31 | | | 391 | }; |
| b69ab31 | | | 392 | } |
| b69ab31 | | | 393 | } |
| b69ab31 | | | 394 | const markCurrentOpResolved = toMarkResolved[toMarkResolved.length - 1]; |
| b69ab31 | | | 395 | if (markCurrentOpResolved && currentOperation != null) { |
| b69ab31 | | | 396 | if (markCurrentOpResolved.commits) { |
| b69ab31 | | | 397 | currentOperation.hasCompletedOptimisticState = true; |
| b69ab31 | | | 398 | } |
| b69ab31 | | | 399 | if (markCurrentOpResolved.files) { |
| b69ab31 | | | 400 | currentOperation.hasCompletedUncommittedChangesOptimisticState = true; |
| b69ab31 | | | 401 | } |
| b69ab31 | | | 402 | if (markCurrentOpResolved.conflicts) { |
| b69ab31 | | | 403 | currentOperation.hasCompletedMergeConflictsOptimisticState = true; |
| b69ab31 | | | 404 | } |
| b69ab31 | | | 405 | } |
| b69ab31 | | | 406 | setOperationList({operationHistory, currentOperation}); |
| b69ab31 | | | 407 | } |
| b69ab31 | | | 408 | |
| b69ab31 | | | 409 | function shouldMarkOptimisticChangesResolved( |
| b69ab31 | | | 410 | operation: OperationInfo, |
| b69ab31 | | | 411 | uncommittedChangesContext: UncommittedChangesPreviewContext, |
| b69ab31 | | | 412 | mergeConflictsContext: MergeConflictsPreviewContext, |
| b69ab31 | | | 413 | ): {commits: boolean; files: boolean; conflicts: boolean} | undefined { |
| b69ab31 | | | 414 | let files = false; |
| b69ab31 | | | 415 | let commits = false; |
| b69ab31 | | | 416 | let conflicts = false; |
| b69ab31 | | | 417 | |
| b69ab31 | | | 418 | if (operation != null && !operation.hasCompletedUncommittedChangesOptimisticState) { |
| b69ab31 | | | 419 | if (operation.operation.makeOptimisticUncommittedChangesApplier != null) { |
| b69ab31 | | | 420 | const optimisticApplier = |
| b69ab31 | | | 421 | operation.operation.makeOptimisticUncommittedChangesApplier(uncommittedChangesContext); |
| b69ab31 | | | 422 | if (operation.exitCode != null) { |
| b69ab31 | | | 423 | if (optimisticApplier == null || operation.exitCode !== 0) { |
| b69ab31 | | | 424 | files = true; |
| b69ab31 | | | 425 | } else if ( |
| b69ab31 | | | 426 | uncommittedChanges.fetchStartTimestamp > nullthrows(operation.endTime).valueOf() |
| b69ab31 | | | 427 | ) { |
| b69ab31 | | | 428 | getTracker()?.track('OptimisticFilesStateForceResolved', {extras: {}}); |
| b69ab31 | | | 429 | files = true; |
| b69ab31 | | | 430 | } |
| b69ab31 | | | 431 | } |
| b69ab31 | | | 432 | } else if (operation.exitCode != null) { |
| b69ab31 | | | 433 | files = true; |
| b69ab31 | | | 434 | } |
| b69ab31 | | | 435 | } |
| b69ab31 | | | 436 | |
| b69ab31 | | | 437 | if (operation != null && !operation.hasCompletedMergeConflictsOptimisticState) { |
| b69ab31 | | | 438 | if (operation.operation.makeOptimisticMergeConflictsApplier != null) { |
| b69ab31 | | | 439 | const optimisticApplier = |
| b69ab31 | | | 440 | operation.operation.makeOptimisticMergeConflictsApplier(mergeConflictsContext); |
| b69ab31 | | | 441 | if (operation.exitCode != null) { |
| b69ab31 | | | 442 | if (optimisticApplier == null || operation.exitCode !== 0) { |
| b69ab31 | | | 443 | conflicts = true; |
| b69ab31 | | | 444 | } else if ( |
| b69ab31 | | | 445 | (mergeConflictsContext.conflicts?.fetchStartTimestamp ?? 0) > |
| b69ab31 | | | 446 | nullthrows(operation.endTime).valueOf() |
| b69ab31 | | | 447 | ) { |
| b69ab31 | | | 448 | getTracker()?.track('OptimisticConflictsStateForceResolved', { |
| b69ab31 | | | 449 | extras: {operation: getOpName(operation.operation)}, |
| b69ab31 | | | 450 | }); |
| b69ab31 | | | 451 | conflicts = true; |
| b69ab31 | | | 452 | } |
| b69ab31 | | | 453 | } |
| b69ab31 | | | 454 | } else if (operation.exitCode != null) { |
| b69ab31 | | | 455 | conflicts = true; |
| b69ab31 | | | 456 | } |
| b69ab31 | | | 457 | } |
| b69ab31 | | | 458 | |
| b69ab31 | | | 459 | if (operation != null && !operation.hasCompletedOptimisticState) { |
| b69ab31 | | | 460 | const endTime = operation.endTime?.valueOf(); |
| b69ab31 | | | 461 | if (endTime && fetchedCommits.fetchStartTimestamp >= endTime) { |
| b69ab31 | | | 462 | commits = true; |
| b69ab31 | | | 463 | } |
| b69ab31 | | | 464 | } |
| b69ab31 | | | 465 | |
| b69ab31 | | | 466 | if (commits || files || conflicts) { |
| b69ab31 | | | 467 | return {commits, files, conflicts}; |
| b69ab31 | | | 468 | } |
| b69ab31 | | | 469 | return undefined; |
| b69ab31 | | | 470 | } |
| b69ab31 | | | 471 | }, [ |
| b69ab31 | | | 472 | list, |
| b69ab31 | | | 473 | setOperationList, |
| b69ab31 | | | 474 | commits, |
| b69ab31 | | | 475 | uncommittedChanges, |
| b69ab31 | | | 476 | conflicts, |
| b69ab31 | | | 477 | fetchedCommits, |
| b69ab31 | | | 478 | successorMap, |
| b69ab31 | | | 479 | ]); |
| b69ab31 | | | 480 | } |
| b69ab31 | | | 481 | |
| b69ab31 | | | 482 | export type UncommittedChangesPreviewContext = { |
| b69ab31 | | | 483 | uncommittedChanges: UncommittedChanges; |
| b69ab31 | | | 484 | }; |
| b69ab31 | | | 485 | |
| b69ab31 | | | 486 | export type MergeConflictsPreviewContext = { |
| b69ab31 | | | 487 | conflicts: MergeConflicts | undefined; |
| b69ab31 | | | 488 | }; |
| b69ab31 | | | 489 | |
| b69ab31 | | | 490 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| b69ab31 | | | 491 | type Class<T> = new (...args: any[]) => T; |
| b69ab31 | | | 492 | /** |
| b69ab31 | | | 493 | * React hook which looks in operation queue and history to see if a |
| b69ab31 | | | 494 | * particular operation is running or queued to run. |
| b69ab31 | | | 495 | * ``` |
| b69ab31 | | | 496 | * const isRunning = useIsOperationRunningOrQueued(PullOperation); |
| b69ab31 | | | 497 | * ``` |
| b69ab31 | | | 498 | */ |
| b69ab31 | | | 499 | export function useIsOperationRunningOrQueued( |
| b69ab31 | | | 500 | cls: Class<Operation>, |
| b69ab31 | | | 501 | ): 'running' | 'queued' | undefined { |
| b69ab31 | | | 502 | const list = useAtomValue(operationList); |
| b69ab31 | | | 503 | const queued = useAtomValue(queuedOperations); |
| b69ab31 | | | 504 | if (list.currentOperation?.operation instanceof cls && list.currentOperation?.exitCode == null) { |
| b69ab31 | | | 505 | return 'running'; |
| b69ab31 | | | 506 | } else if (queued.some(op => op instanceof cls)) { |
| b69ab31 | | | 507 | return 'queued'; |
| b69ab31 | | | 508 | } |
| b69ab31 | | | 509 | return undefined; |
| b69ab31 | | | 510 | } |
| b69ab31 | | | 511 | |
| b69ab31 | | | 512 | export function useMostRecentPendingOperation(): Operation | undefined { |
| b69ab31 | | | 513 | const list = useAtomValue(operationList); |
| b69ab31 | | | 514 | const queued = useAtomValue(queuedOperations); |
| b69ab31 | | | 515 | if (queued.length > 0) { |
| b69ab31 | | | 516 | return queued.at(-1); |
| b69ab31 | | | 517 | } |
| b69ab31 | | | 518 | if (list.currentOperation?.exitCode == null) { |
| b69ab31 | | | 519 | return list.currentOperation?.operation; |
| b69ab31 | | | 520 | } |
| b69ab31 | | | 521 | return undefined; |
| b69ab31 | | | 522 | } |