| 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 {DagCommitInfo} from '../dag/dagCommitInfo'; |
| b69ab31 | | | 9 | import type {Dag} from '../previews'; |
| b69ab31 | | | 10 | import type {ExactRevset, Hash, OptimisticRevset, SucceedableRevset} from '../types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import deepEqual from 'fast-deep-equal'; |
| b69ab31 | | | 13 | import {t} from '../i18n'; |
| b69ab31 | | | 14 | import {CommitPreview} from '../previews'; |
| b69ab31 | | | 15 | import {latestSuccessor} from '../successionUtils'; |
| b69ab31 | | | 16 | import {Operation} from './Operation'; |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | export class RebaseOperation extends Operation { |
| b69ab31 | | | 19 | constructor( |
| b69ab31 | | | 20 | private source: SucceedableRevset | ExactRevset | OptimisticRevset, |
| b69ab31 | | | 21 | private destination: SucceedableRevset | ExactRevset | OptimisticRevset, |
| b69ab31 | | | 22 | ) { |
| b69ab31 | | | 23 | super('RebaseOperation'); |
| b69ab31 | | | 24 | } |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | static opName = 'Rebase'; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | equals(other?: Operation | null): boolean { |
| b69ab31 | | | 29 | return ( |
| b69ab31 | | | 30 | other instanceof RebaseOperation && |
| b69ab31 | | | 31 | deepEqual([this.source, this.destination], [other.source, other.destination]) |
| b69ab31 | | | 32 | ); |
| b69ab31 | | | 33 | } |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | getArgs() { |
| b69ab31 | | | 36 | return ['rebase', '-s', this.source, '-d', this.destination]; |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | getInitialInlineProgress(): Array<[string, string]> { |
| b69ab31 | | | 40 | // TODO: successions |
| b69ab31 | | | 41 | return [[this.source.revset, t('rebasing...')]]; |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | previewDag(dag: Dag): Dag { |
| b69ab31 | | | 45 | const srcHash = dag.resolve(latestSuccessor(dag, this.source))?.hash; |
| b69ab31 | | | 46 | const destHash = dag.resolve(latestSuccessor(dag, this.destination))?.hash; |
| b69ab31 | | | 47 | if (srcHash == null || destHash == null) { |
| b69ab31 | | | 48 | return dag; |
| b69ab31 | | | 49 | } |
| b69ab31 | | | 50 | const src = dag.descendants(srcHash); |
| b69ab31 | | | 51 | const srcHashes = src.toHashes().toArray(); |
| b69ab31 | | | 52 | const prefix = `${REBASE_PREVIEW_HASH_PREFIX}:${destHash}:`; |
| b69ab31 | | | 53 | const rewriteHash = (h: Hash) => (src.contains(h) ? prefix + h : h); |
| b69ab31 | | | 54 | const date = new Date(); |
| b69ab31 | | | 55 | const newCommits = srcHashes.flatMap(h => { |
| b69ab31 | | | 56 | const info = dag.get(h); |
| b69ab31 | | | 57 | if (info == null) { |
| b69ab31 | | | 58 | return []; |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | const isRoot = info.hash === srcHash; |
| b69ab31 | | | 61 | const newInfo: DagCommitInfo = info.merge({ |
| b69ab31 | | | 62 | parents: isRoot ? [destHash] : info.parents, |
| b69ab31 | | | 63 | date, |
| b69ab31 | | | 64 | seqNumber: undefined, |
| b69ab31 | | | 65 | previewType: isRoot ? CommitPreview.REBASE_ROOT : CommitPreview.REBASE_DESCENDANT, |
| b69ab31 | | | 66 | }); |
| b69ab31 | | | 67 | return [newInfo]; |
| b69ab31 | | | 68 | }); |
| b69ab31 | | | 69 | // Rewrite REBASE_OLD commits to use fake hash so they won't conflict with |
| b69ab31 | | | 70 | // the rebased commits. Insert new commits with the original hash so they |
| b69ab31 | | | 71 | // can be interacted. |
| b69ab31 | | | 72 | return dag |
| b69ab31 | | | 73 | .replaceWith(src, (h, c) => |
| b69ab31 | | | 74 | c?.merge({ |
| b69ab31 | | | 75 | hash: rewriteHash(h), |
| b69ab31 | | | 76 | parents: c.parents.map(rewriteHash), |
| b69ab31 | | | 77 | previewType: CommitPreview.REBASE_OLD, |
| b69ab31 | | | 78 | }), |
| b69ab31 | | | 79 | ) |
| b69ab31 | | | 80 | .add(newCommits); |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | optimisticDag(dag: Dag): Dag { |
| b69ab31 | | | 84 | const src = dag.resolve(latestSuccessor(dag, this.source))?.hash; |
| b69ab31 | | | 85 | const dest = dag.resolve(latestSuccessor(dag, this.destination))?.hash; |
| b69ab31 | | | 86 | // src already on dest? |
| b69ab31 | | | 87 | if (src && dest && dag.parentHashes(src).includes(dest)) { |
| b69ab31 | | | 88 | // The stack might be partially rebased while the rebase is ongoing. |
| b69ab31 | | | 89 | // For example, graph |
| b69ab31 | | | 90 | // a--b--c--d--e z |
| b69ab31 | | | 91 | // Rebasing b (and descendants) to z, we might observe: |
| b69ab31 | | | 92 | // a--bOld--cOld--d--e z--bNew--cNew |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | // bOld |
| b69ab31 | | | 95 | const srcOrig = dag.resolve(latestSuccessor(dag.remove(src), this.source))?.hash; |
| b69ab31 | | | 96 | // bOld + cOld + d + e |
| b69ab31 | | | 97 | const toRebase = dag.descendants(srcOrig); |
| b69ab31 | | | 98 | // bNew + cNew + d + e |
| b69ab31 | | | 99 | const successors = dag.followSuccessors(toRebase); |
| b69ab31 | | | 100 | // d + e |
| b69ab31 | | | 101 | const remainingToRebase = toRebase.intersect(successors); |
| b69ab31 | | | 102 | // cNew (simplified, does not handle merges) |
| b69ab31 | | | 103 | const newDest = dag.heads(dag.descendants(src).intersect(successors)).toHashes().first(); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | // To test this in a real repo, try adding these configs to slow down rebases: |
| b69ab31 | | | 106 | // --config "hooks.pretxncommit.slow=sleep 3" |
| b69ab31 | | | 107 | // --config "rebase.experimental.inmemory=false" |
| b69ab31 | | | 108 | // and goto the src stack top before rebasing. |
| b69ab31 | | | 109 | return dag.rebase(remainingToRebase, newDest); |
| b69ab31 | | | 110 | } |
| b69ab31 | | | 111 | return dag.rebase(dag.descendants(src), dest); |
| b69ab31 | | | 112 | } |
| b69ab31 | | | 113 | } |
| b69ab31 | | | 114 | |
| b69ab31 | | | 115 | export const REBASE_PREVIEW_HASH_PREFIX = 'OPTIMISTIC_REBASE_PREVIEW'; |