-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.ts
More file actions
176 lines (156 loc) · 5.38 KB
/
git.ts
File metadata and controls
176 lines (156 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { spawn, spawnSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
/** Parallel git subprocesses for inventory rows and git_status submodule rows. */
export const GIT_SUBPROCESS_PARALLELISM = 4;
// ---------------------------------------------------------------------------
// Git on PATH (lazy probe)
// ---------------------------------------------------------------------------
type GitPathState = "unknown" | "ok" | "missing";
let gitPathState: GitPathState = "unknown";
const GIT_NOT_FOUND_BODY: Record<string, unknown> = {
error: "git_not_found",
};
export function gateGit(): { ok: true } | { ok: false; body: Record<string, unknown> } {
if (gitPathState === "ok") {
return { ok: true };
}
if (gitPathState === "missing") {
return {
ok: false,
body: GIT_NOT_FOUND_BODY,
};
}
const r = spawnSync("git", ["--version"], { encoding: "utf8" });
if (r.status !== 0) {
gitPathState = "missing";
return {
ok: false,
body: GIT_NOT_FOUND_BODY,
};
}
gitPathState = "ok";
return { ok: true };
}
// ---------------------------------------------------------------------------
// Git helpers (sync — used where async batching not needed)
// ---------------------------------------------------------------------------
export function gitTopLevel(cwd: string): string | null {
const r = spawnSync("git", ["rev-parse", "--show-toplevel"], {
cwd,
encoding: "utf8",
});
if (r.status !== 0) return null;
return r.stdout.trim();
}
export function gitRevParseGitDir(cwd: string): boolean {
const r = spawnSync("git", ["rev-parse", "--git-dir"], {
cwd,
encoding: "utf8",
});
return r.status === 0;
}
export function gitRevParseHead(cwd: string): { ok: boolean; sha?: string; text: string } {
const r = spawnSync("git", ["rev-parse", "HEAD"], {
cwd,
encoding: "utf8",
});
if (r.status !== 0) {
return { ok: false, text: (r.stderr || r.stdout || "git rev-parse HEAD failed").trim() };
}
return { ok: true, sha: r.stdout.trim(), text: r.stdout.trim() };
}
export function parseGitSubmodulePaths(gitRoot: string): string[] {
const f = join(gitRoot, ".gitmodules");
if (!existsSync(f)) return [];
const text = readFileSync(f, "utf8");
const paths: string[] = [];
for (const line of text.split("\n")) {
const m = /^\s*path\s*=\s*(.+)\s*$/.exec(line);
if (m?.[1]) paths.push(m[1].trim());
}
return paths;
}
export function hasGitMetadata(dir: string): boolean {
return existsSync(join(dir, ".git"));
}
/** Conservative checks for remote/branch strings passed into git rev-parse / rev-list argv. */
export function isSafeGitUpstreamToken(s: string): boolean {
const t = s.trim();
if (t.length === 0 || t.length > 256) return false;
if (t.includes("..")) return false;
return /^(?!-)[A-Za-z0-9_./+-]+$/.test(t);
}
// ---------------------------------------------------------------------------
// Async pool for parallel git (inventory)
// ---------------------------------------------------------------------------
export async function asyncPool<T, R>(
items: T[],
concurrency: number,
fn: (item: T) => Promise<R>,
): Promise<R[]> {
const results = new Array<R>(items.length);
let next = 0;
async function worker() {
for (;;) {
const i = next++;
if (i >= items.length) break;
const item = items[i];
if (item === undefined) break;
results[i] = await fn(item);
}
}
const n = Math.min(Math.max(1, concurrency), Math.max(1, items.length));
await Promise.all(Array.from({ length: n }, () => worker()));
return results;
}
export function spawnGitAsync(
cwd: string,
args: string[],
): Promise<{ ok: boolean; stdout: string; stderr: string }> {
return new Promise((resolveP) => {
const child = spawn("git", args, { cwd, stdio: ["ignore", "pipe", "pipe"] });
let stdout = "";
let stderr = "";
child.stdout?.setEncoding("utf8");
child.stderr?.setEncoding("utf8");
child.stdout?.on("data", (c: string) => {
stdout += c;
});
child.stderr?.on("data", (c: string) => {
stderr += c;
});
child.on("error", () => resolveP({ ok: false, stdout, stderr }));
child.on("close", (code) => resolveP({ ok: code === 0, stdout, stderr }));
});
}
function gitStatusFailText(r: { stderr: string; stdout: string }): string {
return (r.stderr || r.stdout || "git status failed").trim();
}
export async function gitStatusSnapshotAsync(cwd: string): Promise<{
branchLine: string;
branchOk: boolean;
}> {
const r = await spawnGitAsync(cwd, ["status", "--short", "-b"]);
if (!r.ok) {
return { branchOk: false, branchLine: gitStatusFailText(r) };
}
return { branchOk: true, branchLine: r.stdout.trimEnd() };
}
export async function gitStatusShortBranchAsync(
cwd: string,
): Promise<{ ok: boolean; text: string }> {
const s = await gitStatusSnapshotAsync(cwd);
return { ok: s.branchOk, text: s.branchLine };
}
export async function fetchAheadBehind(
absPath: string,
upstreamSpec: string,
): Promise<{ ahead: string | null; behind: string | null }> {
const aheadR = await spawnGitAsync(absPath, ["rev-list", "--count", `${upstreamSpec}..HEAD`]);
const behindR = await spawnGitAsync(absPath, ["rev-list", "--count", `HEAD..${upstreamSpec}`]);
return {
ahead: aheadR.ok ? aheadR.stdout.trim() : null,
behind: behindR.ok ? behindR.stdout.trim() : null,
};
}