feat(git): Handle detached HEAD in git status and git branch --list

This commit is contained in:
Sam Atkins
2024-06-20 14:31:09 +01:00
committed by Eric Dubé
parent d6dd1a5bb0
commit 2c9b1a3ffc
2 changed files with 18 additions and 2 deletions
+5
View File
@@ -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}`));
+13 -2
View File
@@ -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',
@@ -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:');