Skip to content

Commit

Permalink
branch: show more information when HEAD is detached
Browse files Browse the repository at this point in the history
This prints more helpful info when HEAD is detached: is it detached
because of bisect or rebase? What is the original branch name in those
cases? Is it detached because the user checks out a remote ref or a
tag (and which one)?

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Nguyễn Thái Ngọc Duy authored and Junio C Hamano committed Mar 17, 2013
1 parent b397ea4 commit c8183cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
26 changes: 25 additions & 1 deletion builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "string-list.h"
#include "column.h"
#include "utf8.h"
#include "wt-status.h"

static const char * const builtin_branch_usage[] = {
N_("git branch [options] [-r | -a] [--merged | --no-merged]"),
Expand Down Expand Up @@ -550,14 +551,37 @@ static int calc_maxwidth(struct ref_list *refs)
return w;
}

static char *get_head_description(void)
{
struct strbuf desc = STRBUF_INIT;
struct wt_status_state state;
memset(&state, 0, sizeof(state));
wt_status_get_state(&state, 1);
if (state.rebase_in_progress ||
state.rebase_interactive_in_progress)
strbuf_addf(&desc, _("(no branch, rebasing %s)"),
state.branch);
else if (state.bisect_in_progress)
strbuf_addf(&desc, _("(no branch, bisecting %s)"),
state.branch);
else if (state.detached_from)
strbuf_addf(&desc, _("(detached from %s)"),
state.detached_from);
else
strbuf_addstr(&desc, _("(no branch)"));
free(state.branch);
free(state.onto);
free(state.detached_from);
return strbuf_detach(&desc, NULL);
}

static void show_detached(struct ref_list *ref_list)
{
struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);

if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
struct ref_item item;
item.name = xstrdup(_("(no branch)"));
item.name = get_head_description();
item.width = utf8_strwidth(item.name);
item.kind = REF_LOCAL_BRANCH;
item.dest = NULL;
Expand Down
6 changes: 3 additions & 3 deletions t/t3203-branch-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ test_expect_success 'git branch -v pattern does not show branch summaries' '
test_must_fail git branch -v branch*
'

cat >expect <<'EOF'
* (no branch)
test_expect_success 'git branch shows detached HEAD properly' '
cat >expect <<EOF &&
* (detached from $(git rev-parse --short HEAD^0))
branch-one
branch-two
master
EOF
test_expect_success 'git branch shows detached HEAD properly' '
git checkout HEAD^0 &&
git branch >actual &&
test_i18ncmp expect actual
Expand Down
2 changes: 1 addition & 1 deletion t/t6030-bisect-porcelain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if
cp .git/BISECT_START saved &&
test_must_fail git bisect start $HASH4 foo -- &&
git branch > branch.output &&
test_i18ngrep "* (no branch)" branch.output > /dev/null &&
test_i18ngrep "* (no branch, bisecting other)" branch.output > /dev/null &&
test_cmp saved .git/BISECT_START
'
test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
Expand Down

0 comments on commit c8183cd

Please sign in to comment.