Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/detached-head-git-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Show the current commit in git status when the repository is in detached HEAD.
16 changes: 15 additions & 1 deletion apps/kimi-code/src/utils/git/git-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,21 @@ function readBranch(workDir: string): string | null {
});
if (result.status !== 0) return null;
const name = result.stdout.trim();
return name.length > 0 ? name : null;
return name.length > 0 ? name : readDetachedHead(workDir);
} catch {
return null;
}
}

function readDetachedHead(workDir: string): string | null {
try {
const result = spawnSync('git', ['-C', workDir, 'rev-parse', '--short', 'HEAD'], {
encoding: 'utf8',
timeout: SPAWN_TIMEOUT_MS,
});
if (result.status !== 0) return null;
const hash = result.stdout.trim();
return hash.length > 0 ? `detached@${hash}` : null;
} catch {
return null;
}
Expand Down
38 changes: 38 additions & 0 deletions apps/kimi-code/test/utils/git/git-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,44 @@ describe('git status cache', () => {
});
});

it('shows the current commit when the repository is in detached HEAD', async () => {
mocks.execFile.mockImplementation(
(
_cmd: string,
_args: string[],
_options: unknown,
callback: (error: Error | null, stdout: string, stderr: string) => void,
) => {
callback(new Error('no pull request'), '', '');
},
);
mocks.spawnSync.mockImplementation((_cmd: string, args: string[]) => {
if (args.includes('rev-parse') && args.includes('--is-inside-work-tree')) {
return { status: 0, stdout: 'true\n' };
}
if (args.includes('branch')) {
return { status: 0, stdout: '\n' };
}
if (args.includes('rev-parse') && args.includes('--short')) {
return { status: 0, stdout: 'abc1234\n' };
}
if (args.includes('status')) {
return { status: 0, stdout: '## HEAD (no branch)\n' };
}
return { status: 1, stdout: '' };
});

expect(createGitStatusCache('/tmp/repo').getStatus()).toEqual({
branch: 'detached@abc1234',
dirty: false,
ahead: 0,
behind: 0,
diffAdded: 0,
diffDeleted: 0,
pullRequest: null,
});
});

it('returns null when the working directory is not a git repo and formats badges', () => {
mocks.spawnSync.mockReturnValue({ status: 1, stdout: '' });
expect(createGitStatusCache('/tmp/not-a-repo').getStatus()).toBeNull();
Expand Down