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
d13bc84232 or the user will lose all
uncomitted changes
This commit is contained in:
DanConwayDev authored and Alex Gleason committed 2025-11-28 11:34:19 -06:00
1 parent c5e5cafb77
commit 89de28cb0c
2 files changed
+74 -10

No files matched your search

+54
View File
@@ -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');
}
});
});
+20 -10
View File
@@ -41,6 +41,26 @@ export class GitCommitTool implements Tool<GitCommitParams> {
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<GitCommitParams> {
}
}
// 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,