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:
@@ -18,6 +18,7 @@ import {
|
||||
__setGhPathForTests,
|
||||
__setPullRequestStatusCacheTtlForTests,
|
||||
commitAll,
|
||||
getCurrentBranch,
|
||||
getCheckoutDiff,
|
||||
getCheckoutShortstat,
|
||||
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 () => {
|
||||
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> {
|
||||
const { stdout } = await runGitCommand(["rev-parse", "--abbrev-ref", "HEAD"], {
|
||||
cwd,
|
||||
env: READ_ONLY_GIT_ENV,
|
||||
});
|
||||
const branch = stdout.trim();
|
||||
return branch.length > 0 ? branch : null;
|
||||
try {
|
||||
const { stdout } = await runGitCommand(["rev-parse", "--abbrev-ref", "HEAD"], {
|
||||
cwd,
|
||||
env: READ_ONLY_GIT_ENV,
|
||||
});
|
||||
const branch = stdout.trim();
|
||||
return branch.length > 0 ? branch : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function getWorktreeRoot(cwd: string): Promise<string | null> {
|
||||
|
||||
Reference in New Issue
Block a user