git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2+ 4/4] branch: show more information when HEAD is detached

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 6, 2013, 12:26 UTC
Message-ID
<1362572773-20809-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1362572773-20809-1-git-send-email-pclouds@gmail.com>

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>
---
 Oops, the previous one did not do
  s/\(get_head_description\)()$/\1(void)/
 
 This one does.
 builtin/branch.c            | 25 ++++++++++++++++++++++++-
 t/t3203-branch-output.sh    |  6 +++---
 t/t6030-bisect-porcelain.sh |  2 +-
 3 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 6371bf9..02dee0d 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -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]"),
@@ -550,6 +551,28 @@ 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;
+	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)
 {
@@ -557,7 +580,7 @@ static void show_detached(struct ref_list *ref_list)
 
 	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;
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index 76fe7e0..ba4f98e 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -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
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index 3e0e15f..9b6f0d0 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -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' '
-- 
1.8.1.2.536.gf441e6d
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 24 of 38 in “nd/branch-show-rebase-bisect-state updates”
  1. 0/5 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 3, 2013
  2. 1/5 checkout: record full target ref in reflogNguyễn Thái Ngọc Duy, Mar 3, 2013
  3. 2/5 wt-status: split wt_status_state parsing function outNguyễn Thái Ngọc Duy, Mar 3, 2013
  4. 3/5 wt-status: move wt_status_get_state() out to wt_status_print()Nguyễn Thái Ngọc Duy, Mar 3, 2013
  5. 4/5 status: show the ref that is checked out, even if it's detachedNguyễn Thái Ngọc Duy, Mar 3, 2013
  6. Junio C HamanoMar 3, 2013
  7. Duy NguyenMar 4, 2013
  8. Junio C HamanoMar 4, 2013
  9. Duy NguyenMar 5, 2013
  10. Matthieu MoyMar 5, 2013
  11. 5/5 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 3, 2013
  12. Junio C HamanoMar 3, 2013
  13. 0/4 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 6, 2013
  14. 1/4 wt-status: split wt_status_state parsing function outNguyễn Thái Ngọc Duy, Mar 6, 2013
  15. Junio C HamanoMar 6, 2013
  16. Junio C HamanoMar 6, 2013
  17. 2/4 wt-status: move wt_status_get_state() out to wt_status_print()Nguyễn Thái Ngọc Duy, Mar 6, 2013
  18. 3/4 status: show more info than "currently not on any branch"Nguyễn Thái Ngọc Duy, Mar 6, 2013
  19. Junio C HamanoMar 6, 2013
  20. Duy NguyenMar 8, 2013
  21. Junio C HamanoMar 8, 2013
  22. 4/4 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 6, 2013
  23. 0/4 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 6, 2013
  24. 4/4 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 6, 2013
  25. 0/5 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 13, 2013
  26. 1/5 wt-status: move strbuf into read_and_strip_branch()Nguyễn Thái Ngọc Duy, Mar 13, 2013
  27. Junio C HamanoMar 13, 2013
  28. 1/5 wt-status: move strbuf into read_and_strip_branch()Nguyễn Thái Ngọc Duy, Mar 16, 2013
  29. 2/5 wt-status: split wt_status_state parsing function outNguyễn Thái Ngọc Duy, Mar 13, 2013
  30. 3/5 wt-status: move wt_status_get_state() out to wt_status_print()Nguyễn Thái Ngọc Duy, Mar 13, 2013
  31. 4/5 status: show more info than "currently not on any branch"Nguyễn Thái Ngọc Duy, Mar 13, 2013
  32. Junio C HamanoMar 13, 2013
  33. 5/5 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 13, 2013
  34. Junio C HamanoMar 19, 2013
  35. Duy NguyenMar 20, 2013
  36. Junio C HamanoMar 20, 2013
  37. status, branch: fix the misleading "bisecting" messageNguyễn Thái Ngọc Duy, Mar 23, 2013
  38. Junio C HamanoMar 24, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.