From 89de28cb0c3f778afa883b48a352d041a417dc2a Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 28 Nov 2025 16:36:26 +0000 Subject: [PATCH] fix: recovered for detached HEAD git_commit tool was showing a false positive when in reality no commits were changed. there are also other issues in the UI whilst in this state. eg. current branch doesn't show, cannot push or pull, or 'push to nostr' or github or other remote. warning this must be done in combination with reverting d13bc84232ef838acb559ed1c2c985ee8aecd14e or the user will lose all uncomitted changes --- src/lib/tools/GitCommitTool.test.ts | 54 +++++++++++++++++++++++++++++ src/lib/tools/GitCommitTool.ts | 30 ++++++++++------ 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/lib/tools/GitCommitTool.test.ts b/src/lib/tools/GitCommitTool.test.ts index 44252a84..f5d72117 100644 --- a/src/lib/tools/GitCommitTool.test.ts +++ b/src/lib/tools/GitCommitTool.test.ts @@ -185,4 +185,58 @@ describe('GitCommitTool', () => { expect(result).toContain('✅ Successfully committed'); }); + + it('should error when HEAD is detached (currentBranch returns null)', async () => { + // Mock .git directory exists + vi.mocked(mockFS.stat).mockResolvedValue({ + isDirectory: () => true, + isFile: () => false, + }); + + // Mock detached HEAD state - currentBranch returns null + vi.mocked(mockGitInstance.currentBranch).mockResolvedValue(null); + + await expect(tool.execute({ message: 'Test commit' })).rejects.toThrow('❌ Cannot commit: Git is in a detached HEAD state'); + + // Verify commit was never called + expect(mockGitInstance.commit).not.toHaveBeenCalled(); + }); + + it('should error when HEAD is detached (currentBranch returns undefined)', async () => { + // Mock .git directory exists + vi.mocked(mockFS.stat).mockResolvedValue({ + isDirectory: () => true, + isFile: () => false, + }); + + // Mock detached HEAD state - currentBranch returns undefined + vi.mocked(mockGitInstance.currentBranch).mockResolvedValue(undefined); + + await expect(tool.execute({ message: 'Test commit' })).rejects.toThrow('❌ Cannot commit: Git is in a detached HEAD state'); + + // Verify commit was never called + expect(mockGitInstance.commit).not.toHaveBeenCalled(); + }); + + it('should provide helpful instructions when HEAD is detached', async () => { + // Mock .git directory exists + vi.mocked(mockFS.stat).mockResolvedValue({ + isDirectory: () => true, + isFile: () => false, + }); + + // Mock detached HEAD state + vi.mocked(mockGitInstance.currentBranch).mockResolvedValue(null); + + try { + await tool.execute({ message: 'Test commit' }); + expect.fail('Should have thrown an error'); + } catch (error) { + const errorMessage = (error as Error).message; + // Verify the error message contains safe instructions that preserve changes + expect(errorMessage).toContain('git checkout -b temp-branch'); + expect(errorMessage).toContain('try the commit again'); + expect(errorMessage).toContain('preserving all changes'); + } + }); }); \ No newline at end of file diff --git a/src/lib/tools/GitCommitTool.ts b/src/lib/tools/GitCommitTool.ts index 2daebe47..c75fe817 100644 --- a/src/lib/tools/GitCommitTool.ts +++ b/src/lib/tools/GitCommitTool.ts @@ -41,6 +41,26 @@ export class GitCommitTool implements Tool { throw new Error(`❌ Commit message cannot be empty.`); } + // Check for detached HEAD state before doing any work + // currentBranch returns null when HEAD is detached + const currentBranch = await this.git.currentBranch({ + dir: this.cwd, + }); + + if (currentBranch === null || currentBranch === undefined) { + throw new Error( + `❌ Cannot commit: Git is in a detached HEAD state.\n\n` + + `This usually happens after checking out a specific commit. ` + + `Your changes need to be on a branch before committing.\n\n` + + `To fix this and preserve your staged changes:\n` + + ` 1. Run: git checkout -b temp-branch\n` + + ` (This creates a new branch from your current state, preserving all changes)\n` + + ` 2. Then try the commit again\n` + + ` 3. After committing, you can merge to main with:\n` + + ` git checkout main && git merge temp-branch` + ); + } + // Get git status to see what files have changed const statusMatrix = await this.git.statusMatrix({ dir: this.cwd, @@ -79,16 +99,6 @@ export class GitCommitTool implements Tool { } } - // Get current branch name - let currentBranch = 'main'; - try { - currentBranch = await this.git.currentBranch({ - dir: this.cwd, - }) || 'main'; - } catch { - // Default to 'main' if we can't get the current branch - } - // Commit the changes (author/committer will be set automatically by Git class) const commitSha = await this.git.commit({ dir: this.cwd,