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

[RFC/PATCH] branch: name detached HEAD analogous to status

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Feb 22, 2015, 17:38 UTC
Message-ID
<71fc137d8015f6e81ab91cfcbcad4ec0fa0dc3e6.1424626271.git.git@drmicha.warpmail.net>

"git status" carefully names a detached HEAD "at" resp. "from" a rev or ref depending on whether the detached HEAD has moved since. "git branch" always uses "from", which can be confusing, because a status-aware user would interpret this as moved detached HEAD.

Make "git branch" use the same logic and wording.
Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
Notes:
    The wording is still different:
    
    HEAD detached at %s
    * (detached at %s)
    
    for status (line 1) resp. branch (line 2). Maybe it's worthwhile to use the
    exact same string so that l10n output is guaranteed to be the same? E.g.
    
    a)
    HEAD detached at %s
    * (HEAD detached at %s)
    
    or
    b)
    HEAD (detached at %s)
    * (detached at %s)
    
    In case b), "git status" strings would need to change, in case a)
    only "git branch" strings which change anyway by this patch.
 builtin/branch.c         | 13 ++++++++++---
 t/t3203-branch-output.sh | 39 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index d8949cb..be391ee 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -589,9 +589,16 @@ static char *get_head_description(void)
 	else if (state.bisect_in_progress)
 		strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
 			    state.branch);
-	else if (state.detached_from)
-		strbuf_addf(&desc, _("(detached from %s)"),
-			    state.detached_from);
+	else if (state.detached_from) {
+		unsigned char sha1[20];
+		if (!get_sha1("HEAD", sha1) &&
+		    !hashcmp(sha1, state.detached_sha1))
+			strbuf_addf(&desc, _("(detached at %s)"),
+				state.detached_from);
+		else
+			strbuf_addf(&desc, _("(detached from %s)"),
+				state.detached_from);
+	}
 	else
 		strbuf_addstr(&desc, _("(no branch)"));
 	free(state.branch);
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index ba4f98e..aaff885 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -96,7 +96,7 @@ test_expect_success 'git branch -v pattern does not show branch summaries' '
 
 test_expect_success 'git branch shows detached HEAD properly' '
 	cat >expect <<EOF &&
-* (detached from $(git rev-parse --short HEAD^0))
+* (detached at $(git rev-parse --short HEAD^0))
   branch-one
   branch-two
   master
@@ -106,4 +106,41 @@ EOF
 	test_i18ncmp expect actual
 '
 
+test_expect_success 'git branch shows detached HEAD properly after moving' '
+	cat >expect <<EOF &&
+* (detached from $(git rev-parse --short HEAD))
+  branch-one
+  branch-two
+  master
+EOF
+	git reset --hard HEAD^1 &&
+	git branch >actual &&
+	test_i18ncmp expect actual
+'
+
+test_expect_success 'git branch shows detached HEAD properly from tag' '
+	cat >expect <<EOF &&
+* (detached at fromtag)
+  branch-one
+  branch-two
+  master
+EOF
+	git tag fromtag master &&
+	git checkout fromtag &&
+	git branch >actual &&
+	test_i18ncmp expect actual
+'
+
+test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
+	cat >expect <<EOF &&
+* (detached from fromtag)
+  branch-one
+  branch-two
+  master
+EOF
+	git reset --hard HEAD^1 &&
+	git branch >actual &&
+	test_i18ncmp expect actual
+'
+
 test_done
-- 
2.3.0.296.g32c87e1
Next: Junio C Hamano
Message 1 of 11 in “branch: name detached HEAD analogous to status”
  1. branch: name detached HEAD analogous to statusMichael J Gruber, Feb 22, 2015
  2. Junio C HamanoFeb 22, 2015
  3. Michael J GruberFeb 23, 2015
  4. Marc BranchaudFeb 23, 2015
  5. 0/2 branch output for detached HEADMichael J Gruber, Mar 6, 2015
  6. 1/2 wt-status: refactor detached HEAD analysisMichael J Gruber, Mar 6, 2015
  7. 2/2 branch: name detached HEAD analogous to statusMichael J Gruber, Mar 6, 2015
  8. Junio C HamanoMar 6, 2015
  9. Marc BranchaudFeb 23, 2015
  10. Michael J GruberFeb 23, 2015
  11. Marc BranchaudFeb 23, 2015

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.