| 1 | import path from 'node:path'; |
| 2 | import type { Readable |
| 3 | |
| 4 | import { runGit, runGitReadable |
| 5 | |
| 6 | export interface DiffFile |
| 7 | /** `A` added, `M` modified, `D` deleted. */ |
| 8 | status: string; |
| 9 | path: string; |
| 10 | additions: number; |
| 11 | deletions: number; |
| 12 | binary: boolean; |
| 13 | } |
| 14 | |
| 15 | export interface DiffRange |
| 16 | gitDir: string; |
| 17 | /** Object stores to read alongside `gitDir`'s own. A fork request spans two caches, and lending the objects beats copying them between the two. */ |
| 18 | alternates?: string |
| 19 | from: string; |
| 20 | to: string; |
| 21 | } |
| 22 | |
| 23 | export function alternatesEnv(alternates |
| 24 | if (alternates.length |
| 25 | return { |
| 26 | GIT_ALTERNATE_OBJECT_DIRECTORIES: alternates |
| 27 | .map((directory) |
| 28 | .join(path.delimiter |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | /** Whether `descendant` contains `ancestor`. False as well when either object is gone, since a caller cannot build on a commit it cannot read. */ |
| 33 | export function isAncestor( |
| 34 | gitDir: string, |
| 35 | ancestor: string, |
| 36 | descendant: string, |
| 37 | ) { |
| 38 | return runGit({ |
| 39 | args: ['merge-base', |
| 40 | gitDir, |
| 41 | }).then( |
| 42 | () => true, |
| 43 | () => false, |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** The commit both refs descend from, or null when their histories are unrelated. */ |
| 48 | export async function mergeBase |
| 49 | gitDir, |
| 50 | alternates, |
| 51 | a, |
| 52 | b, |
| 53 | }: { |
| 54 | gitDir: string |
| 55 | alternates?: string |
| 56 | a: string |
| 57 | b: string |
| 58 | }): Promise<string |
| 59 | const raw = |
| 60 | args: ['merge-base', |
| 61 | gitDir, |
| 62 | env: alternatesEnv(alternates |
| 63 | }).catch(() |
| 64 | |
| 65 | return raw.trim |
| 66 | } |
| 67 | |
| 68 | /** `--no-renames` keeps both records two fields wide, so a rename reads as a delete and an add rather than needing a third parse shape. */ |
| 69 | export async function listDiffFiles |
| 70 | gitDir, |
| 71 | alternates, |
| 72 | from, |
| 73 | to, |
| 74 | }: DiffRange) |
| 75 | const env = |
| 76 | const range = |
| 77 | |
| 78 | const [numstat, |
| 79 | runGit({ args: |
| 80 | runGit({ args: |
| 81 | ]); |
| 82 | |
| 83 | // git writes "-\t-" for the counts of a binary file |
| 84 | const counts = |
| 85 | for (const record |
| 86 | const [additions, |
| 87 | if (changed === |
| 88 | counts.set(changed |
| 89 | additions: Number(additions |
| 90 | deletions: Number(deletions |
| 91 | binary: additions === |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | const parts = |
| 96 | const files: |
| 97 | for (let i |
| 98 | const changed = |
| 99 | files.push({ |
| 100 | status: parts[i |
| 101 | path: changed, |
| 102 | additions: 0, |
| 103 | deletions: 0, |
| 104 | binary: false, |
| 105 | ...counts.get( |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | return files; |
| 110 | } |
| 111 | |
| 112 | /** `git diff` of `from..to` as a patch, limited to one literal path when given. */ |
| 113 | function patchArgs(from: |
| 114 | return [ |
| 115 | 'diff', |
| 116 | '--no-renames', |
| 117 | '--end-of-options', |
| 118 | from, |
| 119 | to, |
| 120 | ...(path ? |
| 121 | ]; |
| 122 | } |
| 123 | |
| 124 | /** The patch text itself, streamed - a large review diff should not be buffered. */ |
| 125 | export function streamDiffPatch({ |
| 126 | gitDir, |
| 127 | alternates, |
| 128 | from, |
| 129 | to, |
| 130 | path: only, |
| 131 | }: DiffRange |
| 132 | return runGitReadable({ |
| 133 | args: patchArgs(from |
| 134 | gitDir, |
| 135 | env: alternatesEnv(alternates |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | /** `from..to` as one mailbox-style patch per commit, oldest first, as `git format-patch` writes it and `git am` reads it. */ |
| 140 | export function streamFormatPatch({ |
| 141 | gitDir, |
| 142 | alternates, |
| 143 | from, |
| 144 | to, |
| 145 | }: DiffRange) |
| 146 | return runGitReadable({ |
| 147 | args: [ |
| 148 | 'format-patch', |
| 149 | '--stdout', |
| 150 | '--no-renames', |
| 151 | '--end-of-options', |
| 152 | `${from}..${to |
| 153 | ], |
| 154 | gitDir, |
| 155 | env: alternatesEnv(alternates |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | export interface DiffLine |
| 160 | kind: ' |
| 161 | /** Line number on the base side; for an added line, the base line it comes before. */ |
| 162 | old: number; |
| 163 | /** Line number on the head side; for a deleted line, the head line it comes before. */ |
| 164 | new: number; |
| 165 | text: string; |
| 166 | } |
| 167 | |
| 168 | export interface Hunk |
| 169 | /** First and last line each side shows, context included. */ |
| 170 | deletions: [number |
| 171 | additions: [number |
| 172 | lines: DiffLine |
| 173 | } |
| 174 | |
| 175 | /** One file's diff as hunks of numbered lines. Empty for a binary file or a path the diff does not touch. */ |
| 176 | export async function fileHunks |
| 177 | gitDir, |
| 178 | alternates, |
| 179 | from, |
| 180 | to, |
| 181 | path: only, |
| 182 | }: DiffRange |
| 183 | const patch = |
| 184 | args: patchArgs(from |
| 185 | gitDir, |
| 186 | env: alternatesEnv(alternates |
| 187 | }); |
| 188 | return parseHunks(patch |
| 189 | } |
| 190 | |
| 191 | /** Hunk bodies are read by their header's counts rather than by prefix, since a removed line reading `-- x` looks like a file header. */ |
| 192 | export function parseHunks(patch |
| 193 | const hunks: |
| 194 | const rows = |
| 195 | for (let index |
| 196 | const header = |
| 197 | rows[index], |
| 198 | ); |
| 199 | if (!header) |
| 200 | |
| 201 | let [old, |
| 202 | Number(header[1]) |
| 203 | Number(header[2] |
| 204 | Number(header[3]) |
| 205 | Number(header[4] |
| 206 | ]; |
| 207 | const hunk: |
| 208 | deletions: [old, |
| 209 | additions: [next, |
| 210 | lines: [], |
| 211 | }; |
| 212 | while ((remainingOld > |
| 213 | const row = |
| 214 | const kind = |
| 215 | if (kind !== |
| 216 | hunk.lines.push |
| 217 | if (kind !== |
| 218 | if (kind !== |
| 219 | } |
| 220 | hunks.push(hunk |
| 221 | } |
| 222 | return hunks; |
| 223 | } |