From 41ae7326c01bb85e921be415492823137927f8bc Mon Sep 17 00:00:00 2001 From: canvrno <46584286+canvrno@users.noreply.github.com> Date: Wed, 30 Apr 2025 19:15:35 -0700 Subject: [PATCH] Fix: Git mentions w/ no commits in workspace repo (#3179) * fix for git mentions with no commits in repo --- .changeset/large-crews-divide.md | 5 +++++ src/utils/git.ts | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .changeset/large-crews-divide.md diff --git a/.changeset/large-crews-divide.md b/.changeset/large-crews-divide.md new file mode 100644 index 00000000..b1291889 --- /dev/null +++ b/.changeset/large-crews-divide.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Fix for git commit mentions in repos with no git commits diff --git a/src/utils/git.ts b/src/utils/git.ts index 7ab6d67a..55832369 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -30,6 +30,15 @@ async function checkGitInstalled(): Promise { } } +async function checkGitRepoHasCommits(cwd: string): Promise { + try { + await execAsync("git rev-parse HEAD", { cwd }) + return true + } catch (error) { + return false + } +} + export async function searchCommits(query: string, cwd: string): Promise { try { const isInstalled = await checkGitInstalled() @@ -44,6 +53,12 @@ export async function searchCommits(query: string, cwd: string): Promise return "Not a git repository" } + // Check if repo has any commits + if (!(await checkGitRepoHasCommits(cwd))) { + return "Repository has no commits yet" + } + // Get commit info, stats, and diff separately const { stdout: info } = await execAsync(`git show --format="%H%n%h%n%s%n%an%n%ad%n%b" --no-patch ${hash}`, { cwd, @@ -147,8 +167,16 @@ export async function getWorkingState(cwd: string): Promise { return "No changes in working directory" } - // Get all changes (both staged and unstaged) compared to HEAD - const { stdout: diff } = await execAsync("git diff HEAD", { cwd }) + // Check if repo has any commits before trying to diff against HEAD + let diff = "" + if (await checkGitRepoHasCommits(cwd)) { + // Only run git diff if there are commits + const { stdout: diffOutput } = await execAsync("git diff HEAD", { cwd }) + diff = diffOutput + } else { + // No commits yet, use status output only + return `Working directory changes (new repository):\n\n${status}` + } const output = `Working directory changes:\n\n${status}\n\n${diff}`.trim() return truncateOutput(output) } catch (error) {