Open History to see the last commit
1import type { ProjectCloneStage } from "@t3tools/contracts";
2
3export interface GitCloneProgressLine {
4 readonly stage: ProjectCloneStage;
5 readonly percent: number | null;
6 readonly detail: string | null;
7}
8
9const STAGE_PREFIXES: ReadonlyArray<readonly [RegExp, ProjectCloneStage]> = [
10 [/^remote: Enumerating objects/, "counting"],
11 [/^remote: Counting objects/, "counting"],
12 [/^remote: Compressing objects/, "counting"],
13 [/^Receiving objects/, "receiving"],
14 [/^Resolving deltas/, "resolving"],
15 [/^Updating files/, "checkout"],
16 [/^Checking out files/, "checkout"],
17];
18
19const PERCENT = /:\s+(\d+)%\s+\((\d+)\/(\d+)\)(?:,\s*(.*?))?\s*(?:,\s*done\.)?\s*$/;
20
21/**
22 * Git redraws each counter with a bare `\r`, so callers hand over each redraw
23 * as its own line.
24 */
25export function parseGitCloneProgressLine(line: string): GitCloneProgressLine | null {
26 const trimmed = line.trim();
27 const stageEntry = STAGE_PREFIXES.find(([pattern]) => pattern.test(trimmed));
28 if (!stageEntry) return null;
29 const stage = stageEntry[1];
30 const match = PERCENT.exec(trimmed);
31 if (!match) return { stage, percent: null, detail: null };
32 const percent = Number(match[1]);
33 const rawDetail = match[4]?.trim() ?? "";
34 const detail = rawDetail.length > 0 && rawDetail !== "done." ? rawDetail : null;
35 return {
36 stage,
37 percent: Number.isFinite(percent) ? Math.max(0, Math.min(100, percent)) : null,
38 detail,
39 };
40}