Fix: Git mentions w/ no commits in workspace repo (#3179)

* fix for git mentions with no commits in repo
This commit is contained in:
canvrno authored and GitHub committed 2025-04-30 19:15:35 -07:00
1 parent b0961f4538
commit 41ae7326c0
2 files changed
+35 -2

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"claude-dev": patch
---
Fix for git commit mentions in repos with no git commits
+30 -2
View File
@@ -30,6 +30,15 @@ async function checkGitInstalled(): Promise<boolean> {
}
}
async function checkGitRepoHasCommits(cwd: string): Promise<boolean> {
try {
await execAsync("git rev-parse HEAD", { cwd })
return true
} catch (error) {
return false
}
}
export async function searchCommits(query: string, cwd: string): Promise<GitCommit[]> {
try {
const isInstalled = await checkGitInstalled()
@@ -44,6 +53,12 @@ export async function searchCommits(query: string, cwd: string): Promise<GitComm
return []
}
// Check if repo has any commits
if (!(await checkGitRepoHasCommits(cwd))) {
// No commits yet in the repository
return []
}
// Search commits by hash or message, limiting to 10 results
const { stdout } = await execAsync(
`git log -n 10 --format="%H%n%h%n%s%n%an%n%ad" --date=short ` + `--grep="${query}" --regexp-ignore-case`,
@@ -100,6 +115,11 @@ export async function getCommitInfo(hash: string, cwd: string): Promise<string>
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<string> {
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) {