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

[PATCH RESEND 2/2] status: improve info for detached HEAD after clone

From
RERoy Eldar <royeldar0@gmail.com>
Date
Mar 8, 2023, 19:20 UTC
Message-ID
<20230308192050.1291-3-royeldar0@gmail.com>
In-Reply-To
<20230308192050.1291-1-royeldar0@gmail.com>

When a remote ref or a tag is checked out, HEAD is automatically detached, and "git status" says 'HEAD detached at ...', instead of 'Not currently on any branch.'; this is done by traversing the reflog and parsing an entry like 'checkout: moving from ... to ...'.

In certain situations, HEAD can be detached after "git clone": for example, when "--branch" specifies a non-branch (e.g. a tag). It is preferable to avoid displaying 'Not currently on any branch.', so 'HEAD detached at $sha1' is shown instead.

Signed-off-by: Roy Eldar <royeldar0@gmail.com>
---
 t/t7508-status.sh | 2 +-
 wt-status.c       | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index d279157d28..0ab5bdc1e0 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -894,7 +894,7 @@ test_expect_success 'status shows detached HEAD properly after cloning a reposit
 
 	git clone -b test_tag upstream downstream &&
 	git -C downstream status >actual &&
-	grep -E "Not currently on any branch." actual
+	grep -E "HEAD detached at [0-9a-f]+" actual
 '
 
 test_expect_success 'setup status submodule summary' '
diff --git a/wt-status.c b/wt-status.c
index 3162241a57..f0a5fb578a 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1632,6 +1632,13 @@ static int grab_1st_switch(struct object_id *ooid UNUSED,
 	struct grab_1st_switch_cbdata *cb = cb_data;
 	const char *target = NULL, *end;
 
+	if (skip_prefix(message, "clone: from ", &message)) {
+		oidcpy(&cb->noid, noid);
+		strbuf_reset(&cb->buf);
+		strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
+		return 1;
+	}
+
 	if (!skip_prefix(message, "checkout: moving from ", &message))
 		return 0;
 	target = strstr(message, " to ");
-- 
2.30.2
Previous: Roy EldarNext: Junio C Hamano
Message 3 of 5 in “status: improve info for detached HEAD”
  1. 0/2 status: improve info for detached HEADRoy Eldar, Mar 8, 2023
  2. 1/2 t7508: test status output for detached HEAD after cloneRoy Eldar, Mar 8, 2023
  3. 2/2 status: improve info for detached HEAD after cloneRoy Eldar, Mar 8, 2023
  4. Junio C HamanoMar 8, 2023
  5. Roy EMar 10, 2023

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.