-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.ts
More file actions
147 lines (135 loc) · 4.64 KB
/
inventory.ts
File metadata and controls
147 lines (135 loc) · 4.64 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
import { assertRelativePathUnderTop, resolvePathForRepo } from "../repo-paths.js";
import { fetchAheadBehind, gitStatusSnapshotAsync, spawnGitAsync } from "./git.js";
export const MAX_INVENTORY_ROOTS_DEFAULT = 64;
export type InventoryEntryJson = {
label: string;
path: string;
upstreamMode: "auto" | "fixed";
branchStatus?: string;
detached?: true;
headAbbrev?: string;
upstreamRef?: string;
ahead?: string;
behind?: string;
upstreamNote?: string;
skipReason?: string;
};
export function validateRepoPath(rel: string, gitTop: string): { abs: string; underTop: boolean } {
const abs = resolvePathForRepo(rel, gitTop);
return { abs, underTop: assertRelativePathUnderTop(rel, abs, gitTop) };
}
export function makeSkipEntry(
label: string,
abs: string,
upstreamMode: "auto" | "fixed",
skipReason: string,
): InventoryEntryJson {
return { label, path: abs, upstreamMode, skipReason };
}
export function buildInventorySectionMarkdown(e: InventoryEntryJson): string[] {
const header = `## ${e.label} — ${e.path}`;
if (e.skipReason) {
return ["", header, e.skipReason];
}
const lines: string[] = [e.branchStatus || "(clean)"];
if (e.detached) lines.push("detached HEAD");
if (e.ahead !== undefined && e.behind !== undefined && e.upstreamRef) {
lines.push(`${e.upstreamRef}: ahead ${e.ahead}, behind ${e.behind}`);
} else if (e.upstreamNote) {
lines.push(`upstream: ${e.upstreamNote}`);
}
const single = lines.length === 1 ? lines[0] : undefined;
if (single !== undefined && !single.includes("\n")) {
return ["", header, single];
}
return ["", header, "```text", lines.join("\n"), "```"];
}
function upstreamNoteFor(ref: string, hasCounts: boolean): string {
return hasCounts ? `tracking ${ref}` : `upstream ${ref} (counts unreadable)`;
}
function buildEntry(params: {
label: string;
absPath: string;
branchStatus: string;
detached: boolean;
headAbbrev: string;
upstreamMode: "auto" | "fixed";
upstreamRef: string | null;
ahead: string | null;
behind: string | null;
upstreamNote: string;
}): InventoryEntryJson {
const out: InventoryEntryJson = {
label: params.label,
path: params.absPath,
upstreamMode: params.upstreamMode,
};
if (params.branchStatus) out.branchStatus = params.branchStatus;
if (params.detached) out.detached = true;
if (params.headAbbrev) out.headAbbrev = params.headAbbrev;
if (params.upstreamRef !== null) out.upstreamRef = params.upstreamRef;
if (params.ahead !== null) out.ahead = params.ahead;
if (params.behind !== null) out.behind = params.behind;
if (params.upstreamNote) out.upstreamNote = params.upstreamNote;
return out;
}
export async function collectInventoryEntry(
label: string,
absPath: string,
fixedRemote: string | undefined,
fixedBranch: string | undefined,
): Promise<InventoryEntryJson> {
const [snap, headR] = await Promise.all([
gitStatusSnapshotAsync(absPath),
spawnGitAsync(absPath, ["rev-parse", "--abbrev-ref", "HEAD"]),
]);
const branchStatus = snap.branchLine;
const headAbbrev = headR.ok ? headR.stdout.trim() : "";
const detached = !headR.ok || headAbbrev === "HEAD" || headAbbrev.endsWith("/HEAD");
const base = { label, absPath, branchStatus, detached, headAbbrev };
if (fixedRemote !== undefined && fixedBranch !== undefined) {
const ref = `${fixedRemote}/${fixedBranch}`;
const verify = await spawnGitAsync(absPath, ["rev-parse", "--verify", ref]);
if (!verify.ok) {
return buildEntry({
...base,
upstreamMode: "fixed",
upstreamRef: ref,
ahead: null,
behind: null,
upstreamNote: `(no local ref ${ref} or unreadable)`,
});
}
const { ahead, behind } = await fetchAheadBehind(absPath, ref);
return buildEntry({
...base,
upstreamMode: "fixed",
upstreamRef: ref,
ahead,
behind,
upstreamNote: upstreamNoteFor(ref, ahead != null && behind != null),
});
}
const upVerify = await spawnGitAsync(absPath, ["rev-parse", "--verify", "@{u}"]);
if (!upVerify.ok) {
return buildEntry({
...base,
upstreamMode: "auto",
upstreamRef: null,
ahead: null,
behind: null,
upstreamNote: detached ? "detached HEAD — no upstream" : "no upstream configured",
});
}
const abbrevR = await spawnGitAsync(absPath, ["rev-parse", "--abbrev-ref", "@{u}"]);
const upstreamRef = abbrevR.ok ? abbrevR.stdout.trim() : "@{u}";
const { ahead, behind } = await fetchAheadBehind(absPath, "@{u}");
return buildEntry({
...base,
upstreamMode: "auto",
upstreamRef,
ahead,
behind,
upstreamNote: upstreamNoteFor(upstreamRef, ahead != null && behind != null),
});
}