Skip to content

Commit c760f9c

Browse files
anandgupta42claude
andcommitted
fix: [#830] address coderabbit heavy-lift — distinguish degraded-git from clean non-repo
Reviewer correctly flagged that non-zero rev-parse exit was being bucketed as a clean "not a repo" signal, potentially masking corrupted- .git or permission-denied failures. The previous implementation had three states (git missing / valid git / not a repo) collapsing into two display branches. Added a `gitError?: { exitCode, stderr }` field on GitInfo so callers can distinguish: isRepo=false, gitAvailable=true, gitError=undefined → clean non-repo isRepo=false, gitAvailable=true, gitError=<exit> → degraded git isRepo=false, gitAvailable=false → git missing Exit 128 (git's standard "fatal" for not-in-a-repo) remains the clean non-repo signal. Any other non-zero exit now surfaces via gitError, gets recorded in degraded.add("git"), and renders as "✗ git error (exit N): <stderr-snippet>" instead of silently bucketing as "Not a git repository". Test added to pin: clean non-repo dir has gitError undefined. The degraded-git path is hard to force in a unit test (would require seeding a malformed .git), but the contract is small enough that the type and the comment carry the burden. 84/84 tests pass; no upstream-shared file changes. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 03cdb68 commit c760f9c

2 files changed

Lines changed: 65 additions & 8 deletions

File tree

‎packages/opencode/src/altimate/tools/project-scan.ts‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export interface GitInfo {
2323
* Undefined for callers that don't care.
2424
*/
2525
gitAvailable?: boolean
26+
/**
27+
* Captures the case where git ran but produced a non-standard error
28+
* (corrupted .git, permission denied, etc. — anything that isn't a
29+
* clean "not a repo" exit-128). Lets callers distinguish:
30+
* isRepo=false, gitAvailable=true, gitError=undefined → clean non-repo
31+
* isRepo=false, gitAvailable=true, gitError=<exit> → degraded git
32+
* isRepo=false, gitAvailable=false → git missing
33+
* Undefined when irrelevant.
34+
*/
35+
gitError?: { exitCode: number; stderr?: string }
2636
}
2737

2838
export interface DbtProjectInfo {
@@ -81,7 +91,7 @@ export interface ConfigFileInfo {
8191
export function safeSpawnSync(
8292
args: string[],
8393
opts?: { timeout?: number },
84-
): { exitCode: number; stdout: string } | null {
94+
): { exitCode: number; stdout: string; stderr: string } | null {
8595
try {
8696
const result = Bun.spawnSync(args, {
8797
stdout: "pipe",
@@ -91,6 +101,7 @@ export function safeSpawnSync(
91101
return {
92102
exitCode: result.exitCode ?? 1,
93103
stdout: result.stdout?.toString() ?? "",
104+
stderr: result.stderr?.toString() ?? "",
94105
}
95106
} catch {
96107
// Binary missing from PATH, permission denied, etc. — treat as
@@ -102,17 +113,30 @@ export function safeSpawnSync(
102113

103114
export async function detectGit(): Promise<GitInfo> {
104115
const isRepoResult = safeSpawnSync(["git", "rev-parse", "--is-inside-work-tree"])
105-
// Distinguish "git binary missing" from "valid git, not a repo" — both
106-
// produce isRepo=false but the caller wants to record "git" in the
107-
// degraded list only for the first case. Collapsing both into a bare
108-
// `{ isRepo: false }` would re-introduce the same opacity that was the
109-
// original 437-user telemetry symptom — "Executable not found in
116+
// Distinguish three cases that all produce isRepo=false:
117+
// 1. Git binary missing (null result) — record "git" in degraded.
118+
// 2. Git ran with exit 128 — clean "not a repo" signal.
119+
// 3. Git ran with non-zero, non-128 — degraded git (corrupted .git,
120+
// permission denied, etc.); still record "git" in degraded so the
121+
// caller doesn't silently bucket a real failure as "not a repo".
122+
// Collapsing all three into a bare `{ isRepo: false }` would re-introduce
123+
// the original 437-user telemetry opacity — "Executable not found in
110124
// $PATH: ?" masked to look like a generic shell error.
111125
if (!isRepoResult) {
112126
return { isRepo: false, gitAvailable: false }
113127
}
114128
if (isRepoResult.exitCode !== 0) {
115-
return { isRepo: false, gitAvailable: true }
129+
// Exit 128 is git's standard "fatal" for not-in-a-repo. Anything else
130+
// is unusual — surface it via gitError so the caller can decide
131+
// whether to record it in degraded.
132+
if (isRepoResult.exitCode === 128) {
133+
return { isRepo: false, gitAvailable: true }
134+
}
135+
return {
136+
isRepo: false,
137+
gitAvailable: true,
138+
gitError: { exitCode: isRepoResult.exitCode, stderr: isRepoResult.stderr?.trim() || undefined },
139+
}
116140
}
117141

118142
const branchResult = safeSpawnSync(["git", "branch", "--show-current"])
@@ -620,6 +644,14 @@ export const ProjectScanTool = Tool.define("project_scan", {
620644
// this case and only this explicit add() records it.
621645
lines.push("✗ git binary not found in PATH (install git or add it to PATH)")
622646
degraded.add("git")
647+
} else if (git.gitError) {
648+
// Git ran but produced a non-128 error — corrupted .git, permission
649+
// denied, etc. Surface the exit code + stderr (truncated) so the user
650+
// can self-diagnose. Record in degraded so this case doesn't silently
651+
// bucket as a clean "not a repo".
652+
const stderrSuffix = git.gitError.stderr ? `: ${git.gitError.stderr.slice(0, 120)}` : ""
653+
lines.push(`✗ git error (exit ${git.gitError.exitCode})${stderrSuffix}`)
654+
degraded.add("git")
623655
} else {
624656
lines.push("✗ Not a git repository")
625657
}
@@ -847,7 +879,12 @@ export const ProjectScanTool = Tool.define("project_scan", {
847879
title: `Scan: ${totalConnections} connection(s), ${dbtProject.found ? "dbt found" : "no dbt"}${degradedSuffix}`,
848880
metadata: {
849881
engine_healthy: engineHealth.healthy,
850-
git: { isRepo: git.isRepo, branch: git.branch, gitAvailable: git.gitAvailable },
882+
git: {
883+
isRepo: git.isRepo,
884+
branch: git.branch,
885+
gitAvailable: git.gitAvailable,
886+
gitError: git.gitError,
887+
},
851888
dbt: {
852889
found: dbtProject.found,
853890
name: dbtProject.name,

‎packages/opencode/test/tool/project-scan.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ describe("detectGit", () => {
141141
expect(currentRepoResult.isRepo).toBe(true)
142142
})
143143

144+
test("detectGit on a clean non-repo dir has no gitError", async () => {
145+
// Reviewer (coderabbit) flagged: any non-zero rev-parse exit was being
146+
// bucketed as "not a repo", potentially masking corrupted-.git failures.
147+
// Pin the contract that a clean non-repo (exit 128) leaves gitError
148+
// undefined so callers can distinguish "clean non-repo" from "degraded
149+
// git" by checking gitError presence.
150+
const dir = nextTmpDir()
151+
await fsp.mkdir(dir, { recursive: true })
152+
const originalCwd = process.cwd()
153+
process.chdir(dir)
154+
try {
155+
const result = await detectGit()
156+
expect(result.isRepo).toBe(false)
157+
expect(result.gitAvailable).toBe(true)
158+
expect(result.gitError).toBeUndefined()
159+
} finally {
160+
process.chdir(originalCwd)
161+
}
162+
})
163+
144164
afterAll(async () => {
145165
await fsp.rm(tmpRoot, { recursive: true, force: true }).catch(() => {})
146166
})

0 commit comments

Comments
 (0)