| 1 | import { stat } from 'node:fs/promises'; |
| 2 | |
| 3 | import { alternatesEnv } |
| 4 | import { GitCommandFailedError } |
| 5 | import { runGit } |
| 6 | |
| 7 | export interface MergeContext |
| 8 | gitDir: string; |
| 9 | alternates?: string |
| 10 | } |
| 11 | |
| 12 | /** Merges two commits into a tree without a worktree or an index, so a bare cache can answer "does this conflict, and where" and build the result from one call. The tree of a conflicted merge holds conflict markers and must not be committed. */ |
| 13 | export async function mergeTree |
| 14 | gitDir, |
| 15 | alternates, |
| 16 | base, |
| 17 | head, |
| 18 | mergeBase, |
| 19 | }: MergeContext |
| 20 | base: string |
| 21 | head: string |
| 22 | /** Overrides the computed merge base, which turns the merge into a cherry-pick of `head` when given its parent. */ |
| 23 | mergeBase?: string |
| 24 | }): Promise<{ |
| 25 | const args = |
| 26 | 'merge-tree', |
| 27 | '--write-tree', |
| 28 | '--name-only', |
| 29 | '-z', |
| 30 | ...(mergeBase ? |
| 31 | '--end-of-options', |
| 32 | base, |
| 33 | head, |
| 34 | ]; |
| 35 | try { |
| 36 | return { |
| 37 | ...parseMergeTree( |
| 38 | await runGit({ |
| 39 | ), |
| 40 | clean: true, |
| 41 | }; |
| 42 | } catch (error) |
| 43 | // A conflict and an unreadable commit both exit 1. Only the conflict keeps stderr empty, writing its tree and the conflicted paths to stdout, so a fork whose objects were never lent must not read as "merges cleanly". |
| 44 | if ( |
| 45 | error instanceof GitCommandFailedError |
| 46 | error.exitCode === |
| 47 | error.stderr === |
| 48 | ) { |
| 49 | // The exit status decides cleanliness; the path list only names what it can. |
| 50 | return { ...parseMergeTree |
| 51 | } |
| 52 | throw error; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** `-z` output: the tree, then one conflicted path per entry, then an empty entry before the messages. */ |
| 57 | function parseMergeTree(raw: |
| 58 | const [tree, |
| 59 | const end = |
| 60 | return { tree, |
| 61 | } |
| 62 | |
| 63 | export async function commitTree |
| 64 | gitDir, |
| 65 | alternates, |
| 66 | tree, |
| 67 | parents, |
| 68 | message, |
| 69 | author, |
| 70 | committer = author, |
| 71 | }: MergeContext |
| 72 | tree: string |
| 73 | parents: string |
| 74 | message: string |
| 75 | /** `date` is git's raw `<seconds> <offset>`, kept when a commit is replayed; without it git stamps the current time. */ |
| 76 | author: { |
| 77 | committer?: { |
| 78 | }): Promise<string |
| 79 | const raw = |
| 80 | args: [ |
| 81 | 'commit-tree', |
| 82 | tree, |
| 83 | ...parents.flatMap( |
| 84 | '-m', |
| 85 | message, |
| 86 | ], |
| 87 | gitDir, |
| 88 | env: { |
| 89 | ...alternatesEnv(alternates |
| 90 | // a bare cache has no configured identity, and git refuses to guess one |
| 91 | GIT_AUTHOR_NAME: author.name |
| 92 | GIT_AUTHOR_EMAIL: author.email |
| 93 | ...(author.date |
| 94 | GIT_COMMITTER_NAME: committer.name |
| 95 | GIT_COMMITTER_EMAIL: committer.email |
| 96 | }, |
| 97 | }); |
| 98 | |
| 99 | return raw.trim |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Replays every non-merge commit in `from..to` onto `onto`, oldest first, keeping each one's author and message; merge commits are dropped, the way `git rebase` linearizes a branch. |
| 104 | * |
| 105 | * Stops at the first commit that conflicts and names its paths. Nothing is written but objects, so a conflict leaves no trace a caller has to undo. |
| 106 | */ |
| 107 | export async function rebaseCommits |
| 108 | gitDir, |
| 109 | alternates, |
| 110 | onto, |
| 111 | from, |
| 112 | to, |
| 113 | committer, |
| 114 | }: MergeContext |
| 115 | onto: string |
| 116 | from: string |
| 117 | to: string |
| 118 | committer: { |
| 119 | }): Promise< |
| 120 | { clean: |
| 121 | > { |
| 122 | const raw = |
| 123 | args: [ |
| 124 | 'log', |
| 125 | '--reverse', |
| 126 | '--no-merges', |
| 127 | '-z', |
| 128 | '--date=raw', |
| 129 | '--format=%H%x00%an%x00%ae%x00%ad%x00%B', |
| 130 | '--end-of-options', |
| 131 | to, |
| 132 | `^${from}`, |
| 133 | `^${onto}`, |
| 134 | ], |
| 135 | gitDir, |
| 136 | env: alternatesEnv(alternates |
| 137 | }); |
| 138 | |
| 139 | // -z ends each record with a NUL too, so every commit is exactly five fields. |
| 140 | const fields = |
| 141 | let tip = |
| 142 | for (let i |
| 143 | const [sha, |
| 144 | const merged = |
| 145 | gitDir, |
| 146 | alternates, |
| 147 | base: tip, |
| 148 | head: sha, |
| 149 | mergeBase: `${sha} |
| 150 | }); |
| 151 | if (!merged.clean |
| 152 | |
| 153 | tip = await |
| 154 | gitDir, |
| 155 | alternates, |
| 156 | tree: merged.tree |
| 157 | parents: [tip], |
| 158 | message, |
| 159 | author: { name, |
| 160 | committer, |
| 161 | }); |
| 162 | } |
| 163 | return { clean: |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * A packfile holding everything reachable from `include` that `exclude` does not already cover. |
| 168 | * |
| 169 | * `exclude` must be what the *target* log already holds, never the head commit: a fork's commits live in another repository's log, so excluding them would write an entry whose merge commit no node could ever replay. |
| 170 | */ |
| 171 | export async function packRange |
| 172 | gitDir, |
| 173 | alternates, |
| 174 | include, |
| 175 | exclude, |
| 176 | prefix, |
| 177 | }: MergeContext |
| 178 | include: string |
| 179 | exclude: string |
| 180 | prefix: string |
| 181 | }): Promise<{ |
| 182 | const name = |
| 183 | args: ['pack-objects', |
| 184 | gitDir, |
| 185 | env: alternatesEnv(alternates |
| 186 | input: Buffer.from |
| 187 | [...include, ... |
| 188 | 'utf8', |
| 189 | ), |
| 190 | }); |
| 191 | |
| 192 | const path = |
| 193 | return { path, |
| 194 | } |