| 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 'shared/types/common'; |
| b69ab31 | | | 9 | import type {ExportStack, ImportCommit, ImportStack, Mark} from 'shared/types/stack'; |
| b69ab31 | | | 10 | import type {Dag} from '../previews'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {DagCommitInfo} from '../dag/dagCommitInfo'; |
| b69ab31 | | | 13 | import {HashSet} from '../dag/set'; |
| b69ab31 | | | 14 | import {t} from '../i18n'; |
| b69ab31 | | | 15 | import {CommitPreview} from '../previews'; |
| b69ab31 | | | 16 | import {Operation} from './Operation'; |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | export class ImportStackOperation extends Operation { |
| b69ab31 | | | 19 | static opName = 'StackEdit'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | // Derived from importStack. |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | /** Commits sorted from the stack bottom to top. */ |
| b69ab31 | | | 24 | private commits: Readonly<ImportCommit>[]; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | /** Parent of the first commit. */ |
| b69ab31 | | | 27 | private firstParent: Hash | null; |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | /** Goto command from the importStack. */ |
| b69ab31 | | | 30 | private gotoMark: Mark | undefined; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | constructor( |
| b69ab31 | | | 33 | private importStack: Readonly<ImportStack>, |
| b69ab31 | | | 34 | protected originalStack: Readonly<ExportStack>, |
| b69ab31 | | | 35 | ) { |
| b69ab31 | | | 36 | super('ImportStackOperation'); |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | let firstParent: Hash | null = null; |
| b69ab31 | | | 39 | const origHashes = new Set<Hash>(); |
| b69ab31 | | | 40 | const gotoMark = importStack |
| b69ab31 | | | 41 | .flatMap(([op, value]) => (op === 'goto' || op === 'reset' ? value.mark : [])) |
| b69ab31 | | | 42 | .at(-1); |
| b69ab31 | | | 43 | const commits = importStack.flatMap(a => (a[0] === 'commit' ? [a[1]] : [])); |
| b69ab31 | | | 44 | commits.forEach(commit => { |
| b69ab31 | | | 45 | if (firstParent == null) { |
| b69ab31 | | | 46 | firstParent = commit.parents.at(0) ?? 'unexpected_parents'; |
| b69ab31 | | | 47 | } |
| b69ab31 | | | 48 | (commit.predecessors ?? []).forEach(pred => { |
| b69ab31 | | | 49 | origHashes.add(pred); |
| b69ab31 | | | 50 | }); |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | this.commits = commits; |
| b69ab31 | | | 54 | this.firstParent = firstParent; |
| b69ab31 | | | 55 | this.gotoMark = gotoMark; |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | getArgs() { |
| b69ab31 | | | 59 | return ['debugimportstack']; |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | getStdin() { |
| b69ab31 | | | 63 | return JSON.stringify(this.importStack); |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | getDescriptionForDisplay() { |
| b69ab31 | | | 67 | return { |
| b69ab31 | | | 68 | description: t('Applying stack changes'), |
| b69ab31 | | | 69 | tooltip: t( |
| b69ab31 | | | 70 | 'This operation does not have a traditional command line equivalent. \n' + |
| b69ab31 | | | 71 | 'You might use commit, amend, histedit, rebase, absorb, fold, or a combination of them for similar functionalities.', |
| b69ab31 | | | 72 | ), |
| b69ab31 | | | 73 | }; |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | optimisticDag(dag: Dag): Dag { |
| b69ab31 | | | 77 | const originalHashes = this.originalStack.map(c => c.node); |
| b69ab31 | | | 78 | // Replace the old stack with the new stack, followed by a rebase. |
| b69ab31 | | | 79 | // Note the rebase is actually not what the operation does, but we always |
| b69ab31 | | | 80 | // follow up with a rebase operation if needed. |
| b69ab31 | | | 81 | const toRebase = dag.descendants(dag.children(originalHashes.at(-1))); |
| b69ab31 | | | 82 | let toRemove = HashSet.fromHashes(originalHashes).subtract(dag.ancestors(this.firstParent)); |
| b69ab31 | | | 83 | // If the "toRemove" part of the original stack is gone, consider as completed. |
| b69ab31 | | | 84 | // Note: We no longer do a rebase in this case, and requires the rebase preview |
| b69ab31 | | | 85 | // to be handled separately. |
| b69ab31 | | | 86 | if (dag.present(toRemove).size === 0) { |
| b69ab31 | | | 87 | return dag; |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | // It's possible that the new stack was actually created but the head commit |
| b69ab31 | | | 90 | // keeps the old stack from disappearing (so the above check returns false). |
| b69ab31 | | | 91 | // In this case, we hide the new stack (by using successors) temporarily. |
| b69ab31 | | | 92 | // Otherwise we need to figure out the "new head", which is not trivial. |
| b69ab31 | | | 93 | toRemove = toRemove.union(dag.successors(toRemove)); |
| b69ab31 | | | 94 | const newStack = this.previewStack(dag); |
| b69ab31 | | | 95 | const newDag = dag.remove(toRemove).add(newStack).rebase(toRebase, newStack.at(-1)?.hash); |
| b69ab31 | | | 96 | return newDag; |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | private previewStack(dag: Dag): Array<DagCommitInfo> { |
| b69ab31 | | | 100 | let parents = this.firstParent ? [this.firstParent] : []; |
| b69ab31 | | | 101 | const usedHashes = new Set<Hash>(); |
| b69ab31 | | | 102 | return this.commits.map(commit => { |
| b69ab31 | | | 103 | const pred = commit.predecessors?.at(-1); |
| b69ab31 | | | 104 | const existingInfo = pred ? dag.get(pred) : undefined; |
| b69ab31 | | | 105 | // Pick a unique hash. |
| b69ab31 | | | 106 | let hash = existingInfo?.hash ?? `fake:${commit.mark}`; |
| b69ab31 | | | 107 | while (usedHashes.has(hash)) { |
| b69ab31 | | | 108 | hash = hash + '_'; |
| b69ab31 | | | 109 | } |
| b69ab31 | | | 110 | usedHashes.add(hash); |
| b69ab31 | | | 111 | // Use existing CommitInfo as the "base" to build a new CommitInfo. |
| b69ab31 | | | 112 | const info = DagCommitInfo.fromCommitInfo({ |
| b69ab31 | | | 113 | // "Default". Might be replaced by existingInfo. |
| b69ab31 | | | 114 | bookmarks: [], |
| b69ab31 | | | 115 | remoteBookmarks: [], |
| b69ab31 | | | 116 | filePathsSample: [], |
| b69ab31 | | | 117 | phase: 'draft', |
| b69ab31 | | | 118 | // Note: using `existingInfo` here might be not accurate. |
| b69ab31 | | | 119 | ...(existingInfo || {}), |
| b69ab31 | | | 120 | // Replace existingInfo. |
| b69ab31 | | | 121 | hash, |
| b69ab31 | | | 122 | parents, |
| b69ab31 | | | 123 | title: commit.text.trimStart().split('\n', 1).at(0) || '', |
| b69ab31 | | | 124 | author: commit.author ?? '', |
| b69ab31 | | | 125 | date: commit.date == null ? new Date() : new Date(commit.date[0] * 1000), |
| b69ab31 | | | 126 | description: commit.text, |
| b69ab31 | | | 127 | isDot: this.gotoMark ? commit.mark === this.gotoMark : (existingInfo?.isDot ?? false), |
| b69ab31 | | | 128 | totalFileCount: Object.keys(commit.files).length, |
| b69ab31 | | | 129 | closestPredecessors: commit.predecessors, |
| b69ab31 | | | 130 | previewType: CommitPreview.STACK_EDIT_DESCENDANT, |
| b69ab31 | | | 131 | }); |
| b69ab31 | | | 132 | parents = [info.hash]; |
| b69ab31 | | | 133 | return info; |
| b69ab31 | | | 134 | }); |
| b69ab31 | | | 135 | } |
| b69ab31 | | | 136 | } |