| 1 | import { runGit |
| 2 |
| 3 | const RECORD = '\x1e'; |
| 4 | const FIELD = '\x1f'; |
| 5 | const SUMMARY_FORMAT = `--format=%H${FIELD}%ct${FIELD}%s`; |
| 6 | const FORMAT = `--format=${RECORD}%H${FIELD}%an${FIELD}%ae${FIELD}%ct${FIELD}%at${FIELD}%cn${FIELD}%ce${FIELD}%s${FIELD}%b`; |
| 7 |
| 8 | export interface Commit { |
| 9 | sha: string; |
| 10 | authorName: string; |
| 11 | authorEmail: string; |
| 12 | /** Committer timestamp, ISO 8601. */ |
| 13 | committedAt: string; |
| 14 | /** Author timestamp, ISO 8601; differs from `committedAt` once a commit is rebased or amended. */ |
| 15 | authoredAt: string; |
| 16 | committerName: string; |
| 17 | committerEmail: string; |
| 18 | subject: string; |
| 19 | body: string; |
| 20 | } |
| 21 |
| 22 | export interface CommitSummary { |
| 23 | sha: string; |
| 24 | subject: string; |
| 25 | /** Committer timestamp, ISO 8601. */ |
| 26 | committedAt: string; |
| 27 | } |
| 28 |
| 29 | export interface CommitFile { |
| 30 | /** A for added, M for modified, D for deleted. */ |
| 31 | status: string; |
| 32 | path: string; |
| 33 | } |
| 34 |
| 35 | /** A commit-ish a caller supplied, safe to hand git as a revision. */ |
| 36 | export function isSha(value: string) { |
| 37 | return /^[0-9a-f]{4,40}$/.test(value); |
| 38 | } |
| 39 |
| 40 | function parse(record: string): Commit | null { |
| 41 | const [ |
| 42 | sha, |
| 43 | authorName, |
| 44 | authorEmail, |
| 45 | committedAt, |
| 46 | authoredAt, |
| 47 | committerName, |
| 48 | committerEmail, |
| 49 | subject, |
| 50 | body, |
| 51 | ] = record.split(FIELD); |
| 52 | if (!sha) return null; |
| 53 |
| 54 | return { |
| 55 | sha, |
| 56 | authorName, |
| 57 | authorEmail, |
| 58 | committedAt: new Date(Number(committedAt) * 1000).toISOString(), |
| 59 | authoredAt: new Date(Number(authoredAt) * 1000).toISOString(), |
| 60 | committerName, |
| 61 | committerEmail, |
| 62 | subject, |
| 63 | body: (body ?? '').trim(), |
| 64 | }; |
| 65 | } |
| 66 |
| 67 | /** Newest-first history of a ref, one page at a time. `cursor` is the sha to resume from, which stays correct as new commits land on top. */ |
| 68 | export async function listCommits({ |
| 69 | gitDir, |
| 70 | env, |
| 71 | ref, |
| 72 | path, |
| 73 | limit, |
| 74 | cursor, |
| 75 | skip, |
| 76 | }: { |
| 77 | gitDir: string; |
| 78 | env?: Record<string, string>; |
| 79 | /** A revision, or several such as `[tip, '^excluded']`. */ |
| 80 | ref: string | string[]; |
| 81 | path?: string; |
| 82 | limit: number; |
| 83 | cursor?: string; |
| 84 | /** Commits to pass over first, newest first; how a caller reaches the oldest ones, since `--reverse` only reorders after `-n` has cut. */ |
| 85 | skip?: number; |
| 86 | }): Promise<{ commits: Commit[]; nextCursor: string | null }> { |
| 87 | const args = [ |
| 88 | 'log', |
| 89 | FORMAT, |
| 90 | // one extra row answers "is there another page" without a second walk |
| 91 | '-n', |
| 92 | String(limit + 1), |
| 93 | ...(skip ? [`--skip=${skip}`] : []), |
| 94 | '--end-of-options', |
| 95 | ...(cursor ? [cursor] : [ref].flat()), |
| 96 | ]; |
| 97 | if (path) args.push('--', path); |
| 98 |
| 99 | const raw = await runGit({ args, gitDir, env }); |
| 100 | const commits = raw.split(RECORD).flatMap((record) => parse(record) ?? []); |
| 101 |
| 102 | return { |
| 103 | commits: commits.slice(0, limit), |
| 104 | nextCursor: commits.length > limit ? commits[limit].sha : null, |
| 105 | }; |
| 106 | } |
| 107 |
| 108 | /** One commit with the paths it changed, or null when the sha is unknown. */ |
| 109 | export async function readCommit({ |
| 110 | gitDir, |
| 111 | sha, |
| 112 | }: { |
| 113 | gitDir: string; |
| 114 | sha: string; |
| 115 | }): Promise<(Commit & { files: CommitFile[] }) | null> { |
| 116 | const raw = await runGit({ |
| 117 | args: ['show', '-s', FORMAT, '--end-of-options', sha], |
| 118 | gitDir, |
| 119 | }).catch(() => ''); |
| 120 |
| 121 | const commit = parse(raw.split(RECORD)[1] ?? ''); |
| 122 | if (!commit) return null; |
| 123 |
| 124 | const changes = await runGit({ |
| 125 | args: [ |
| 126 | 'show', |
| 127 | '--name-status', |
| 128 | '--format=', |
| 129 | '-z', |
| 130 | '--no-renames', |
| 131 | // a merge shows nothing without this, and its first-parent diff is what a reader expects to see |
| 132 | '--first-parent', |
| 133 | '--end-of-options', |
| 134 | commit.sha, |
| 135 | ], |
| 136 | gitDir, |
| 137 | }); |
| 138 |
| 139 | const parts = changes.split('\0').filter(Boolean); |
| 140 | const files: CommitFile[] = []; |
| 141 | for (let i = 0; i + 1 < parts.length; i += 2) { |
| 142 | files.push({ status: parts[i], path: parts[i + 1] }); |
| 143 | } |
| 144 |
| 145 | return { ...commit, files }; |
| 146 | } |
| 147 |
| 148 | /** The newest commit touching `path`, or the tip of `ref` when no path is given. Null when the ref has no history. */ |
| 149 | export async function readCommitSummary({ |
| 150 | gitDir, |
| 151 | ref, |
| 152 | path, |
| 153 | }: { |
| 154 | gitDir: string; |
| 155 | ref: string; |
| 156 | path?: string; |
| 157 | }): Promise<CommitSummary | null> { |
| 158 | const args = ['log', '-1', SUMMARY_FORMAT, '--end-of-options', ref]; |
| 159 | if (path) args.push('--', path); |
| 160 |
| 161 | const raw = await runGit({ args, gitDir }).catch(() => ''); |
| 162 | const [sha, committedAt, subject] = raw.trim().split(FIELD); |
| 163 | if (!sha) return null; |
| 164 |
| 165 | return { |
| 166 | sha, |
| 167 | subject, |
| 168 | committedAt: new Date(Number(committedAt) * 1000).toISOString(), |
| 169 | }; |
| 170 | } |
| 171 |
| 172 | export async function countCommits({ |
| 173 | gitDir, |
| 174 | range, |
| 175 | path, |
| 176 | env, |
| 177 | }: { |
| 178 | gitDir: string; |
| 179 | range: string; |
| 180 | path?: string; |
| 181 | env?: Record<string, string>; |
| 182 | }) { |
| 183 | const count = await runGit({ |
| 184 | args: [ |
| 185 | 'rev-list', |
| 186 | '--count', |
| 187 | '--end-of-options', |
| 188 | range, |
| 189 | ...(path ? ['--', path] : []), |
| 190 | ], |
| 191 | gitDir, |
| 192 | env, |
| 193 | }); |
| 194 | return Number(count.trim()) || 0; |
| 195 | } |