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

Re: [PATCH 4/5] status: show the ref that is checked out, even if it's detached

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 3, 2013, 22:25 UTC
Message-ID
<7vy5e49l3i.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1362303681-6585-5-git-send-email-pclouds@gmail.com>
Show 5 quoted lines
> When a remote ref or a tag is checked out, HEAD is automatically
> detached. There is no user friendly way to find out what ref is
> checked out in this case. This patch digs in reflog for this
> information and shows "Detached from origin/master" or "Detached from
> v1.8.0" instead of "Currently not on any branch".
"Detached from" is a nice attempt to compromise in the phrasing.

We usually say you detach HEAD at v1.8.0, but what is shown is what started from such a state but then the user may have built more history on top of it and may no longer be at v1.8.0, so obviously we do not want to say "Detached at". We are in a "detached at v1.8.0 and then possibly built one or more commits on top" state (would it be helpful to differentiate the "nothing built on top" and "some commits have been built on top" cases, I wonder).

Also I wonder if you could do a bit more to help the users who do:
    $ git checkout $(git merge-base HEAD nd/branch-show-rebase-bisect-state)
aka
    $ git checkout ...nd/branch-show-rebase-bisect-state
and then do one or more commits on top.

Instead of punting to "Currently not on any branch", would it help to show the place you first detached at, so that the user can then grab that commit object name and run

    $ git log --oneline $that_commit..
or something?
Show 24 quoted lines
> +static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
> +			   const char *email, unsigned long timestamp, int tz,
> +			   const char *message, void *cb_data)
> +{
> +	struct grab_1st_switch_cbdata *cb = cb_data;
> +	const char *target = NULL;
> +
> +	if (prefixcmp(message, "checkout: moving from "))
> +		return 0;
> +	message += strlen("checkout: moving from ");
> +	target = strstr(message, " to ");
> +	if (!target)
> +		return 0;
> +	target += strlen(" to ");
> +	strbuf_reset(&cb->buf);
> +	hashcpy(cb->sha1, nsha1);
> +	if (!prefixcmp(target, "refs/")) {
> +		const char *end = target;
> +		while (*end && *end != '\n')
> +			end++;
> +		strbuf_add(&cb->buf, target, end - target);
> +	}
> +	return 0;
> +}

Can't this be done by generalizing grab_nth_branch_switch() and then exposing it as part of the general API?

I also feel uneasy about two issues this and the previous change introduces:

 1) It is somewhat unnerving that this step reads what comes after
    "to", while nth_branch_switch() reads what comes between "from"
    and "to", but it probably cannot be avoided because this series
    wants to know what we switched "to" earlier, while "checkout -"
    wants to know what we switched "from"
 2) The previous one records this sequence in a funny way:
        : start from branch A
        git checkout B
        git checkout C
    The resulting reflog entries result in
        checkout: moving from A to refs/heads/B
        checkout: moving from B to refs/heads/C
    even though existing code and tools are expecting to read
        checkout: moving from A to B
        checkout: moving from B to C

By the way, even though the title of this patch is "status: show the ref that is checked out, even if it's detached", a quick check with

        $ cd ../linux-3.0
        $ git describe
        v3.8-rc7
        $ ../git.git/git-checkout v3.8-rc7
        $ tail -n 1 .git/logs/HEAD | sed -e 's/.*checkout/checkout/'
        checkout: moving from master to refs/tags/v3.8-rc7
        $ ../git.git/git-status | head -n 1
        # Not currently on any branch.
        $ ../git.git/git-branch -v
        * (no branch) 836dc9e Linux 3.8-rc7
          master      836dc9e Linux 3.8-rc7
does not seem to give me anything more helpful.
Previous: Nguyễn Thái Ngọc DuyNext: Duy Nguyen
Message 6 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.