fix(server): silence non-Git workspace warnings

Expected rev-parse failures were logged like unexpected discovery errors after Git detection became fail-open. Keep ordinary non-Git results silent while retaining diagnostics for genuine process failures.
This commit is contained in:
Mohamed Boudra
2026-07-21 15:06:42 +02:00
parent fd4e13735c
commit 5605b2aa94
2 changed files with 46 additions and 18 deletions
+36 -13
View File
@@ -315,18 +315,28 @@ describe("checkout git utilities", () => {
}); });
}); });
it("reports a real non-git directory as non-git", async () => { it("reports an ordinary directory as non-git without warning", async () => {
const nonGitDir = join(tempDir, "not-git-status"); const nonGitDir = join(tempDir, "not-git-status");
mkdirSync(nonGitDir, { recursive: true }); mkdirSync(nonGitDir, { recursive: true });
const records: unknown[] = [];
const logger = pino(
{ level: "warn" },
{
write(line: string) {
records.push(JSON.parse(line));
},
},
);
await expect(getCheckoutStatus(nonGitDir)).resolves.toEqual({ isGit: false }); await expect(getCheckoutStatus(nonGitDir, { logger })).resolves.toEqual({ isGit: false });
expect(records).toEqual([]);
}); });
it.runIf( it.runIf(
process.platform !== "win32" && process.platform !== "win32" &&
existsSync("/dev/shm") && existsSync("/dev/shm") &&
statSync("/dev").dev !== statSync("/dev/shm").dev, statSync("/dev").dev !== statSync("/dev/shm").dev,
)("warns and reports a non-git directory at a filesystem boundary as non-git", async () => { )("does not warn for a non-git directory at a filesystem boundary", async () => {
const nonGitDir = realpathSync.native(mkdtempSync("/dev/shm/checkout-git-boundary-test-")); const nonGitDir = realpathSync.native(mkdtempSync("/dev/shm/checkout-git-boundary-test-"));
const records: unknown[] = []; const records: unknown[] = [];
const logger = pino( const logger = pino(
@@ -339,21 +349,34 @@ describe("checkout git utilities", () => {
); );
try { try {
await expect(getCheckoutStatus(nonGitDir, { logger })).resolves.toEqual({ isGit: false }); await expect(getCheckoutStatus(nonGitDir, { logger })).resolves.toEqual({ isGit: false });
expect(records).toEqual([ expect(records).toEqual([]);
expect.objectContaining({
level: 40,
cwd: nonGitDir,
msg: "Git worktree discovery failed; treating directory as non-Git",
err: expect.objectContaining({
message: expect.stringContaining("Stopping at filesystem boundary"),
}),
}),
]);
} finally { } finally {
rmSync(nonGitDir, { recursive: true, force: true }); rmSync(nonGitDir, { recursive: true, force: true });
} }
}); });
it("warns when git discovery fails unexpectedly", async () => {
const missingDir = join(tempDir, "missing-git-cwd");
const records: unknown[] = [];
const logger = pino(
{ level: "warn" },
{
write(line: string) {
records.push(JSON.parse(line));
},
},
);
await expect(getCheckoutStatus(missingDir, { logger })).resolves.toEqual({ isGit: false });
expect(records).toEqual([
expect.objectContaining({
level: 40,
cwd: missingDir,
msg: "Git worktree discovery failed; treating directory as non-Git",
}),
]);
});
it("returns null for getCurrentBranch in a repo with no commits", async () => { it("returns null for getCurrentBranch in a repo with no commits", async () => {
const emptyRepo = join(tempDir, "empty-repo"); const emptyRepo = join(tempDir, "empty-repo");
mkdirSync(emptyRepo, { recursive: true }); mkdirSync(emptyRepo, { recursive: true });
+10 -5
View File
@@ -810,6 +810,10 @@ export type CheckoutSnapshotFacts =
pullRequestLookupTarget: PullRequestStatusLookupTarget | null; pullRequestLookupTarget: PullRequestStatusLookupTarget | null;
}; };
function isNotGitRepositoryError(error: unknown): boolean {
return error instanceof Error && /not a git repository/i.test(error.message);
}
async function requireGitRepo(cwd: string): Promise<void> { async function requireGitRepo(cwd: string): Promise<void> {
try { try {
await runGitCommand(["rev-parse", "--git-dir"], { cwd, envOverlay: READ_ONLY_GIT_ENV }); await runGitCommand(["rev-parse", "--git-dir"], { cwd, envOverlay: READ_ONLY_GIT_ENV });
@@ -897,11 +901,12 @@ async function getWorktreeRoot(cwd: string, context?: CheckoutContext): Promise<
}); });
return parseGitRevParsePath(stdout); return parseGitRevParsePath(stdout);
} catch (error) { } catch (error) {
// Git discovery is fail-open: keep the directory usable as non-Git and retain the diagnostic. if (!isNotGitRepositoryError(error)) {
context?.logger?.warn( context?.logger?.warn(
{ err: error, cwd }, { err: error, cwd },
"Git worktree discovery failed; treating directory as non-Git", "Git worktree discovery failed; treating directory as non-Git",
); );
}
return null; return null;
} }
} }