fix: handle repos with no commits in getCurrentBranch
git rev-parse --abbrev-ref HEAD fails with exit code 128 on freshly initialized repos with no commits. This error bubbled up through workspace listing and showed a toast error on every app launch for users with empty git repos.
This commit is contained in:
@@ -21,7 +21,7 @@ import type { WorkspaceGitService } from "./workspace-git-service.js";
|
|||||||
import { normalizeWorkspaceId as normalizePersistedWorkspaceId } from "./workspace-registry-model.js";
|
import { normalizeWorkspaceId as normalizePersistedWorkspaceId } from "./workspace-registry-model.js";
|
||||||
import { createAgentWorktree } from "./worktree-bootstrap.js";
|
import { createAgentWorktree } from "./worktree-bootstrap.js";
|
||||||
import type { TerminalManager } from "../terminal/terminal-manager.js";
|
import type { TerminalManager } from "../terminal/terminal-manager.js";
|
||||||
import { resolveRepositoryDefaultBranch } from "../utils/checkout-git.js";
|
import { getCurrentBranch, resolveRepositoryDefaultBranch } from "../utils/checkout-git.js";
|
||||||
import { expandTilde } from "../utils/path.js";
|
import { expandTilde } from "../utils/path.js";
|
||||||
import {
|
import {
|
||||||
computeWorktreePath,
|
computeWorktreePath,
|
||||||
@@ -35,8 +35,7 @@ import {
|
|||||||
validateBranchSlug,
|
validateBranchSlug,
|
||||||
type WorktreeConfig,
|
type WorktreeConfig,
|
||||||
} from "../utils/worktree.js";
|
} from "../utils/worktree.js";
|
||||||
import { runGitCommand } from "../utils/run-git-command.js";
|
import { toCheckoutError } from "./checkout-git-utils.js";
|
||||||
import { READ_ONLY_GIT_ENV, toCheckoutError } from "./checkout-git-utils.js";
|
|
||||||
const SAFE_GIT_REF_PATTERN = /^[A-Za-z0-9._\/-]+$/;
|
const SAFE_GIT_REF_PATTERN = /^[A-Za-z0-9._\/-]+$/;
|
||||||
|
|
||||||
export type NormalizedGitOptions = {
|
export type NormalizedGitOptions = {
|
||||||
@@ -153,11 +152,7 @@ export async function buildAgentSessionConfig(
|
|||||||
if (normalized.createNewBranch) {
|
if (normalized.createNewBranch) {
|
||||||
targetBranch = normalized.newBranchName!;
|
targetBranch = normalized.newBranchName!;
|
||||||
} else {
|
} else {
|
||||||
const { stdout } = await runGitCommand(["rev-parse", "--abbrev-ref", "HEAD"], {
|
targetBranch = (await getCurrentBranch(cwd)) ?? "";
|
||||||
cwd,
|
|
||||||
env: READ_ONLY_GIT_ENV,
|
|
||||||
});
|
|
||||||
targetBranch = stdout.trim();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!targetBranch) {
|
if (!targetBranch) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
__setGhPathForTests,
|
__setGhPathForTests,
|
||||||
__setPullRequestStatusCacheTtlForTests,
|
__setPullRequestStatusCacheTtlForTests,
|
||||||
commitAll,
|
commitAll,
|
||||||
|
getCurrentBranch,
|
||||||
getCheckoutDiff,
|
getCheckoutDiff,
|
||||||
getCheckoutShortstat,
|
getCheckoutShortstat,
|
||||||
getPullRequestStatus,
|
getPullRequestStatus,
|
||||||
@@ -84,6 +85,15 @@ describe("checkout git utilities", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns null for getCurrentBranch in a repo with no commits", async () => {
|
||||||
|
const emptyRepo = join(tempDir, "empty-repo");
|
||||||
|
execSync(`mkdir -p ${emptyRepo}`);
|
||||||
|
execSync("git init -b main", { cwd: emptyRepo });
|
||||||
|
|
||||||
|
const branch = await getCurrentBranch(emptyRepo);
|
||||||
|
expect(branch).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
it("handles status/diff/commit in a normal repo", async () => {
|
it("handles status/diff/commit in a normal repo", async () => {
|
||||||
writeFileSync(join(repoDir, "file.txt"), "updated\n");
|
writeFileSync(join(repoDir, "file.txt"), "updated\n");
|
||||||
|
|
||||||
|
|||||||
@@ -525,12 +525,16 @@ async function requireGitRepo(cwd: string): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getCurrentBranch(cwd: string): Promise<string | null> {
|
export async function getCurrentBranch(cwd: string): Promise<string | null> {
|
||||||
const { stdout } = await runGitCommand(["rev-parse", "--abbrev-ref", "HEAD"], {
|
try {
|
||||||
cwd,
|
const { stdout } = await runGitCommand(["rev-parse", "--abbrev-ref", "HEAD"], {
|
||||||
env: READ_ONLY_GIT_ENV,
|
cwd,
|
||||||
});
|
env: READ_ONLY_GIT_ENV,
|
||||||
const branch = stdout.trim();
|
});
|
||||||
return branch.length > 0 ? branch : null;
|
const branch = stdout.trim();
|
||||||
|
return branch.length > 0 ? branch : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getWorktreeRoot(cwd: string): Promise<string | null> {
|
async function getWorktreeRoot(cwd: string): Promise<string | null> {
|
||||||
|
|||||||
Reference in New Issue
Block a user