mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-13 16:38:56 +02:00
fix(next-version): git fallback queries the live remote, never mutates, and keeps 3-digit width
The degraded path counted every remote-tracking ref on every remote — stale experiment branches and second remotes inflated version allocation, and a failed base read flipped 3-digit repos to 4-digit slots. Now: ls-remote --heads origin first (GIT_TERMINAL_PROMPT=0, 5s timeout, zero local ref mutation); on failure, local refs/remotes/origin ONLY with an explicit stale-refs warning; a failed base read zeroes at the LOCAL version file's width so a 3-digit repo allocates 0.0.1, not 0.0.1.0. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
7f749f94fe
commit
bdc0b11664
+95
-32
@@ -147,19 +147,36 @@ function detectHost(): "github" | "gitlab" | "unknown" {
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
function readBaseVersion(base: string, versionPath: string, warnings: string[]): string {
|
||||
// When the base-version read fails we assume a zero base — but a literal
|
||||
// "0.0.0.0" is 4-digit, which flips versionWidth() to 4 and hands a 3-digit
|
||||
// repo a 4-digit slot (the exact width class of bug #2501 fixed in parsing).
|
||||
// The LOCAL version file at versionPath knows the repo's own width; shape the
|
||||
// zero from it. No local file either → keep the 4-digit default.
|
||||
function zeroBaseAtLocalWidth(versionPath: string, repoRoot: string): string {
|
||||
try {
|
||||
const local = extractVersion(readFileSync(join(repoRoot, versionPath), "utf8"), versionPath);
|
||||
if (local && parseVersion(local) && versionWidth(local) === 3) return "0.0.0";
|
||||
} catch {
|
||||
// unreadable/absent local version file — 4-digit default below
|
||||
}
|
||||
return "0.0.0.0";
|
||||
}
|
||||
|
||||
function readBaseVersion(base: string, versionPath: string, repoRoot: string, warnings: string[]): string {
|
||||
// git fetch is best-effort; we tolerate failure and fall back to whatever
|
||||
// origin/<base> currently points at.
|
||||
runCommand("git", ["fetch", "origin", base, "--quiet"], 10000);
|
||||
const r = runCommand("git", ["show", `origin/${base}:${versionPath}`]);
|
||||
if (!r.ok) {
|
||||
warnings.push(`could not read ${versionPath} at origin/${base}; assuming 0.0.0.0`);
|
||||
return "0.0.0.0";
|
||||
const assumed = zeroBaseAtLocalWidth(versionPath, repoRoot);
|
||||
warnings.push(`could not read ${versionPath} at origin/${base}; assuming ${assumed}`);
|
||||
return assumed;
|
||||
}
|
||||
const v = extractVersion(r.stdout, versionPath);
|
||||
if (!v) {
|
||||
warnings.push(`${versionPath} at origin/${base} has no readable version; assuming 0.0.0.0`);
|
||||
return "0.0.0.0";
|
||||
const assumed = zeroBaseAtLocalWidth(versionPath, repoRoot);
|
||||
warnings.push(`${versionPath} at origin/${base} has no readable version; assuming ${assumed}`);
|
||||
return assumed;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
@@ -460,40 +477,85 @@ function autoDetectExcludePR(): number | null {
|
||||
// v0.1.57.0. Auditing that repo's history found FOUR such pairs going back
|
||||
// three weeks, so the silent fallback had been mis-allocating for a while.
|
||||
//
|
||||
// Git already knows what the API was asked for. Remote-tracking refs carry
|
||||
// each branch's VERSION file, and the base's own history records every version
|
||||
// already shipped. Neither needs a token, a network round-trip, or a working
|
||||
// `gh`. So "offline" degrades the QUEUE VIEW (no PR numbers, no draft status)
|
||||
// without degrading the ALLOCATION.
|
||||
// Git already knows what the API was asked for. `git ls-remote --heads origin`
|
||||
// returns the remote's LIVE branch list with zero local mutation (no fetch, no
|
||||
// ref updates), each branch's VERSION file is readable from the local object
|
||||
// store, and the base's own history records every version already shipped.
|
||||
// None of it needs a token or a working `gh`. So "offline" degrades the QUEUE
|
||||
// VIEW (no PR numbers, no draft status) without degrading the ALLOCATION.
|
||||
function fetchGitClaimed(
|
||||
base: string,
|
||||
versionPath: string,
|
||||
warnings: string[],
|
||||
): ClaimedPR[] {
|
||||
const claims: ClaimedPR[] = [];
|
||||
const baseShort = base.replace(/^origin\//, "");
|
||||
|
||||
// 1. Every remote-tracking branch's VERSION file. These are the open PRs'
|
||||
// branches, whether or not the API can be reached to enumerate them.
|
||||
// Read through extractVersion so a JSON version-path (#2501) resolves on
|
||||
// remote refs too, and the branch's own width is preserved in the claim.
|
||||
const refs = runCommand("git", [
|
||||
"for-each-ref",
|
||||
"--format=%(refname:short)",
|
||||
"refs/remotes",
|
||||
]);
|
||||
if (refs.ok) {
|
||||
const baseShort = base.replace(/^origin\//, "");
|
||||
for (const ref of refs.stdout.split("\n").map((r) => r.trim()).filter(Boolean)) {
|
||||
if (ref.endsWith("/HEAD")) continue;
|
||||
if (ref === base || ref.replace(/^origin\//, "") === baseShort) continue;
|
||||
const show = runCommand("git", ["show", `${ref}:${versionPath}`]);
|
||||
if (!show.ok) continue;
|
||||
const raw = extractVersion(show.stdout, versionPath);
|
||||
if (!raw || !parseVersion(raw)) continue;
|
||||
claims.push({ pr: 0, branch: ref, version: raw });
|
||||
// 1. The version-claim branches. FIRST try `git ls-remote --heads origin`:
|
||||
// fresh remote data, zero local mutation. This scopes claims to branches
|
||||
// that actually EXIST on origin right now — the previous implementation
|
||||
// counted every remote-tracking ref on EVERY remote, so stale local refs
|
||||
// (deleted PR branches, an unrelated `upstream` remote) inflated the
|
||||
// claim set and pushed the allocation further than the real queue.
|
||||
// GIT_TERMINAL_PROMPT=0 + a 5s timeout keep a dead/credential-prompting
|
||||
// remote from hanging the allocator.
|
||||
const lsRemote = spawnSync("git", ["ls-remote", "--heads", "origin"], {
|
||||
encoding: "utf8",
|
||||
timeout: 5000,
|
||||
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
||||
});
|
||||
const lsOk = lsRemote.status === 0 && !lsRemote.error;
|
||||
|
||||
// Each candidate carries the LIVE tip sha when it came from ls-remote, so
|
||||
// the VERSION read prefers the fresh commit (present locally after any
|
||||
// prior fetch/clone) and only falls back to the local remote-tracking ref.
|
||||
const candidates: { branch: string; sha?: string }[] = [];
|
||||
if (lsOk) {
|
||||
for (const line of (lsRemote.stdout ?? "").split("\n")) {
|
||||
const m = line.trim().match(/^([0-9a-f]{40,64})\trefs\/heads\/(.+)$/);
|
||||
if (!m) continue;
|
||||
if (m[2] === baseShort) continue;
|
||||
candidates.push({ branch: m[2], sha: m[1] });
|
||||
}
|
||||
} else {
|
||||
warnings.push("git for-each-ref failed; branch claims unavailable");
|
||||
// Degraded twice over: no host API AND no reachable remote. Fall back to
|
||||
// the LOCAL refs/remotes/origin snapshot ONLY (never other remotes — an
|
||||
// `upstream` remote's branches are not claims against OUR queue).
|
||||
warnings.push(
|
||||
"git ls-remote origin failed; using stale local refs/remotes/origin — " +
|
||||
"branches deleted on the remote may still be counted as claims (run `git fetch --prune origin` to refresh)",
|
||||
);
|
||||
const refs = runCommand("git", [
|
||||
"for-each-ref",
|
||||
"--format=%(refname:short)",
|
||||
"refs/remotes/origin",
|
||||
]);
|
||||
if (refs.ok) {
|
||||
for (const ref of refs.stdout.split("\n").map((r) => r.trim()).filter(Boolean)) {
|
||||
if (ref.endsWith("/HEAD")) continue;
|
||||
const branch = ref.replace(/^origin\//, "");
|
||||
if (branch === baseShort) continue;
|
||||
candidates.push({ branch });
|
||||
}
|
||||
} else {
|
||||
warnings.push("git for-each-ref failed; branch claims unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
// Read each candidate's VERSION through extractVersion so a JSON
|
||||
// version-path (#2501) resolves on remote refs too, and the branch's own
|
||||
// width is preserved in the claim.
|
||||
for (const { branch, sha } of candidates) {
|
||||
let show = sha ? runCommand("git", ["show", `${sha}:${versionPath}`]) : { ok: false, stdout: "", stderr: "" };
|
||||
if (!show.ok) {
|
||||
// Live tip not fetched yet (or no sha in the fallback path): best-effort
|
||||
// read from the local remote-tracking ref.
|
||||
show = runCommand("git", ["show", `refs/remotes/origin/${branch}:${versionPath}`]);
|
||||
}
|
||||
if (!show.ok) continue;
|
||||
const raw = extractVersion(show.stdout, versionPath);
|
||||
if (!raw || !parseVersion(raw)) continue;
|
||||
claims.push({ pr: 0, branch: `origin/${branch}`, version: raw });
|
||||
}
|
||||
|
||||
// 2. Versions already shipped, read from the base's commit subjects. Catches
|
||||
@@ -534,8 +596,9 @@ async function main() {
|
||||
}
|
||||
const warnings: string[] = [];
|
||||
const host = detectHost();
|
||||
const versionPath = resolveVersionPath(args.versionPath, repoToplevel());
|
||||
const baseVersion = args.current || readBaseVersion(args.base, versionPath, warnings);
|
||||
const repoRoot = repoToplevel();
|
||||
const versionPath = resolveVersionPath(args.versionPath, repoRoot);
|
||||
const baseVersion = args.current || readBaseVersion(args.base, versionPath, repoRoot, warnings);
|
||||
const baseParsed = parseVersion(baseVersion);
|
||||
if (!baseParsed) {
|
||||
console.error(`Error: could not parse base version '${baseVersion}'`);
|
||||
|
||||
Reference in New Issue
Block a user