Skip to content

Commit

Permalink
feat(git): Handle detached HEAD in git status and git branch --list
Browse files Browse the repository at this point in the history
  • Loading branch information
AtkinsSJ authored and KernelDeimos committed Jun 28, 2024
1 parent d6dd1a5 commit 2c9b1a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/git/src/subcommands/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ const BRANCH = {
throw SHOW_USAGE;
}

if (!current_branch) {
const oid = await git.resolveRef({ fs, dir, gitdir, ref: 'HEAD' });
stdout(`* ${chalk.greenBright(`(HEAD detached at ${shorten_hash(oid)})`)}`);
}

for (const branch of branches) {
if (branch === current_branch) {
stdout(chalk.greenBright(`* ${branch}`));
Expand Down
15 changes: 13 additions & 2 deletions packages/git/src/subcommands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
import git from 'isomorphic-git';
import path from 'path-browserify';
import { find_repo_root } from '../git-helpers.js';
import { find_repo_root, shorten_hash } from '../git-helpers.js';
import chalk from 'chalk';

export default {
name: 'status',
Expand Down Expand Up @@ -107,7 +108,17 @@ export default {
dir,
gitdir,
});
stdout(`On branch ${current_branch}\n`);
if (current_branch) {
stdout(`On branch ${current_branch}`);
// Check if the branch actually exists. If not, this is a fresh repo that's never been committed to.
const actual_current_branch = await git.currentBranch({ fs, dir, gitdir, test: true });
if (!actual_current_branch) {
stdout('\nNo commits yet\n');
}
} else {
const oid = await git.resolveRef({ fs, dir, gitdir, ref: 'HEAD' });
stdout(`${chalk.redBright('HEAD detached at')} ${shorten_hash(oid)}`);
}

if (staged.length) {
stdout('Changes to be committed:');
Expand Down

0 comments on commit 2c9b1a3

Please sign in to comment.