| 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 | /** The commit both refs descend from, or null when their histories are unrelated. */ |
| 33 | export async function mergeBase |
| 34 | gitDir, |
| 35 | alternates, |
| 36 | a, |
| 37 | b, |
| 38 | }: { |
| 39 | gitDir: string |
| 40 | alternates?: string |
| 41 | a: string |
| 42 | b: string |
| 43 | }): Promise<string |
| 44 | const raw = |
| 45 | args: ['merge-base', |
| 46 | gitDir, |
| 47 | env: alternatesEnv(alternates |
| 48 | }).catch(() |
| 49 | |
| 50 | return raw.trim |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Paths changed between two commits, with line counts. |
| 55 | * |
| 56 | * `--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. |
| 57 | */ |
| 58 | export async function listDiffFiles |
| 59 | gitDir, |
| 60 | alternates, |
| 61 | from, |
| 62 | to, |
| 63 | }: DiffRange) |
| 64 | const env = |
| 65 | const range = |
| 66 | |
| 67 | const [numstat, |
| 68 | runGit({ args: |
| 69 | runGit({ args: |
| 70 | ]); |
| 71 | |
| 72 | // git writes "-\t-" for the counts of a binary file |
| 73 | const counts = |
| 74 | for (const record |
| 75 | const [additions, |
| 76 | if (changed === |
| 77 | counts.set(changed |
| 78 | additions: Number(additions |
| 79 | deletions: Number(deletions |
| 80 | binary: additions === |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | const parts = |
| 85 | const files: |
| 86 | for (let i |
| 87 | const changed = |
| 88 | files.push({ |
| 89 | status: parts[i |
| 90 | path: changed, |
| 91 | additions: 0, |
| 92 | deletions: 0, |
| 93 | binary: false, |
| 94 | ...counts.get( |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | return files; |
| 99 | } |
| 100 | |
| 101 | /** The patch text itself, streamed - a large review diff should not be buffered. */ |
| 102 | export function streamDiffPatch({ |
| 103 | gitDir, |
| 104 | alternates, |
| 105 | from, |
| 106 | to, |
| 107 | path: only, |
| 108 | }: DiffRange |
| 109 | return runGitReadable({ |
| 110 | args: [ |
| 111 | 'diff', |
| 112 | '--no-renames', |
| 113 | '--end-of-options', |
| 114 | from, |
| 115 | to, |
| 116 | ...(only ? |
| 117 | ], |
| 118 | gitDir, |
| 119 | env: alternatesEnv(alternates |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | export interface DiffLine |
| 124 | kind: ' |
| 125 | /** Line number on the base side; for an added line, the base line it comes before. */ |
| 126 | old: number; |
| 127 | /** Line number on the head side; for a deleted line, the head line it comes before. */ |
| 128 | new: number; |
| 129 | text: string; |
| 130 | } |
| 131 | |
| 132 | export interface Hunk |
| 133 | /** First and last line each side shows, context included. */ |
| 134 | deletions: [number |
| 135 | additions: [number |
| 136 | lines: DiffLine |
| 137 | } |
| 138 | |
| 139 | /** One file's diff as hunks of numbered lines. Empty for a binary file or a path the diff does not touch. */ |
| 140 | export async function fileHunks |
| 141 | gitDir, |
| 142 | alternates, |
| 143 | from, |
| 144 | to, |
| 145 | path: only, |
| 146 | }: DiffRange |
| 147 | const patch = |
| 148 | args: [ |
| 149 | 'diff', |
| 150 | '--no-renames', |
| 151 | '--end-of-options', |
| 152 | from, |
| 153 | to, |
| 154 | '--', |
| 155 | `:(literal)${only}`, |
| 156 | ], |
| 157 | gitDir, |
| 158 | env: alternatesEnv(alternates |
| 159 | }); |
| 160 | return parseHunks(patch |
| 161 | } |
| 162 | |
| 163 | /** Hunk bodies are read by their header's counts rather than by prefix, since a removed line reading `-- x` looks like a file header. */ |
| 164 | export function parseHunks(patch |
| 165 | const hunks: |
| 166 | const rows = |
| 167 | for (let index |
| 168 | const header = |
| 169 | rows[index], |
| 170 | ); |
| 171 | if (!header) |
| 172 | |
| 173 | let [old, |
| 174 | Number(header[1]) |
| 175 | Number(header[2] |
| 176 | Number(header[3]) |
| 177 | Number(header[4] |
| 178 | ]; |
| 179 | const hunk: |
| 180 | deletions: [old, |
| 181 | additions: [next, |
| 182 | lines: [], |
| 183 | }; |
| 184 | while ((remainingOld > |
| 185 | const row = |
| 186 | const kind = |
| 187 | if (kind !== |
| 188 | hunk.lines.push |
| 189 | if (kind !== |
| 190 | if (kind !== |
| 191 | } |
| 192 | hunks.push(hunk |
| 193 | } |
| 194 | return hunks; |
| 195 | } |