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

Re: [PATCH] clone: allow detached checkout when --branch takes a tag

From
Jeff King <peff@peff.net>
Date
Jan 5, 2012, 14:18 UTC
Message-ID
<20120105141844.GA26771@sigill.intra.peff.net>
In-Reply-To
<1325771380-18862-1-git-send-email-pclouds@gmail.com>
On Thu, Jan 05, 2012 at 08:49:40PM +0700, Nguyen Thai Ngoc Duy wrote:
Show 9 quoted lines
> This allows you to do "git clone --branch=v1.7.8 git.git" and work
> right away from there. No big deal, just one more convenient step, I
> think. --branch taking a tag may be confusing though.
> 
> We can still have master in this case instead of detached HEAD, which
> may make more sense because we use --branch. I don't care much which
> way should be used.
> 
> Like? Dislike?

Seems like a reasonable goal to me. I agree that "--branch=v1.7.8" is a little confusing, but not the end of the world. If we were designing it from scratch, I might call it "--head" or "--checkout" or something to indicate that it is what we are putting in HEAD. But I don't know that it is worth renaming the option or adding a new option.

Show 12 quoted lines
> @@ -721,6 +722,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>  			strbuf_release(&head);
>  
>  			if (!our_head_points_at) {
> +				strbuf_addstr(&head, "refs/tags/");
> +				strbuf_addstr(&head, option_branch);
> +				our_head_points_at =
> +					find_ref_by_name(mapped_refs, head.buf);
> +				strbuf_release(&head);
> +			}
> +
> +			if (!our_head_points_at) {
Hmm. The context just above your patch that got snipped does this:
    strbuf_addstr(&head, src_ref_prefix);
    strbuf_addstr(&head, option_branch);
    our_head_points_at =
        find_ref_by_name(mapped_refs, head.buf);

where src_ref_prefix typically is "refs/heads/", and clearly you are meaning to do the same thing for tags. But the use of "src_ref_prefix" is interesting.

It is always "refs/heads/" unless we are cloning into a bare mirror, in which case it is "refs/". So with your patch in the non-mirror case, doing "--branch=foo" would try "refs/heads/foo" followed by "refs/tags/foo". Which makes sense. But in the mirror case, it will try "refs/foo" followed by "refs/tags/foo", which is kind of odd.

I wonder, though, if the original code makes any sense. By using "refs/", I would have to say "--branch=heads/foo", which is kind of weird and undocumented. I think it should probably always be "refs/heads/", no matter if we are mirroring or not.

Show 7 quoted lines
> @@ -750,7 +759,16 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>  			      reflog_msg.buf);
>  	}
>  
> -	if (our_head_points_at) {
> +	if (our_head_points_at &&
> +	    !prefixcmp(our_head_points_at->name, "refs/tags/")) {
I think I would prefer this check to be:
  prefixcmp(our_head_points_at->name, "refs/heads/")

which more closely matches the rules for what is allowed to go in HEAD as a symbolic ref. It's pretty hard to get something other than heads or tags, but you can do it with "git clone --bare --mirror --branch=foo/bar". I did argue above for doing away with that "feature", but I still think it future-proofs this section of code to handle anything.

Show 7 quoted lines
> +		const struct ref *ref = our_head_points_at;
> +		struct object *o;
> +
> +		/* Detached HEAD */
> +		o = deref_tag(parse_object(ref->old_sha1), NULL, 0);
> +		update_ref(reflog_msg.buf, "HEAD", o->sha1, NULL,
> +			   REF_NODEREF, DIE_ON_ERR);

It's unlikely, but deref_tag can return NULL, in which case this will segfault (ditto with parse_object, I think). I suspect that is a problem in lots of places, though. I wonder if deref_tag should simply die if we have a missing object (and we can add a _gently form for things like fsck which want to handle the error condition).

Also, any reason the "warn" flag to deref_tag should not be 1?
Other than those minor complaints, the patch looks good to me.
-Peff
Previous: Nguyễn Thái Ngọc DuyNext: Nguyen Thai Ngoc Duy
Message 2 of 68 in “clone: allow detached checkout when --branch takes a tag”
  1. clone: allow detached checkout when --branch takes a tagNguyễn Thái Ngọc Duy, Jan 5, 2012
  2. Jeff KingJan 5, 2012
  3. Nguyen Thai Ngoc DuyJan 6, 2012
  4. Jeff KingJan 6, 2012
  5. Junio C HamanoJan 5, 2012
  6. Nguyen Thai Ngoc DuyJan 6, 2012
  7. 1/6 t5601: add &&Nguyễn Thái Ngọc Duy, Jan 8, 2012
  8. 2/6 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 8, 2012
  9. 3/6 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 8, 2012
  10. Junio C HamanoJan 10, 2012
  11. Nguyen Thai Ngoc DuyJan 10, 2012
  12. Junio C HamanoJan 10, 2012
  13. Nguyen Thai Ngoc DuyJan 10, 2012
  14. 4/6 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 8, 2012
  15. Junio C HamanoJan 10, 2012
  16. 5/6 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 8, 2012
  17. 6/6 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 8, 2012
  18. Junio C HamanoJan 10, 2012
  19. Nguyen Thai Ngoc DuyJan 10, 2012
  20. Junio C HamanoJan 10, 2012
  21. Nguyen Thai Ngoc DuyJan 10, 2012
  22. 00/10 nd/clone-detachedNguyễn Thái Ngọc Duy, Jan 10, 2012
  23. 00/10 nd/clone-detachedNguyễn Thái Ngọc Duy, Jan 13, 2012
  24. Junio C HamanoJan 13, 2012
  25. Nguyen Thai Ngoc DuyJan 14, 2012
  26. Junio C HamanoJan 14, 2012
  27. Nguyen Thai Ngoc DuyJan 14, 2012
  28. Junio C HamanoJan 15, 2012
  29. 00/10 nd/clone-detached rebase onto nd/clone-single-branchNguyễn Thái Ngọc Duy, Jan 16, 2012
  30. 01/10 t5601: add missing && cascadeNguyễn Thái Ngọc Duy, Jan 16, 2012
  31. 02/10 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 16, 2012
  32. 03/10 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 16, 2012
  33. 04/10 clone: factor out HEAD update codeNguyễn Thái Ngọc Duy, Jan 16, 2012
  34. 05/10 clone: factor out remote ref writingNguyễn Thái Ngọc Duy, Jan 16, 2012
  35. 06/10 clone: delay cloning until after remote HEAD checkingNguyễn Thái Ngọc Duy, Jan 16, 2012
  36. 07/10 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 16, 2012
  37. 08/10 clone: refuse to clone if --branch points to bogus refNguyễn Thái Ngọc Duy, Jan 16, 2012
  38. 09/10 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 16, 2012
  39. 10/10 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 16, 2012
  40. 01/10 t5601: add missing && cascadeNguyễn Thái Ngọc Duy, Jan 13, 2012
  41. 02/10 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 13, 2012
  42. 03/10 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 13, 2012
  43. 04/10 clone: factor out HEAD update codeNguyễn Thái Ngọc Duy, Jan 13, 2012
  44. 05/10 clone: factor out remote ref writingNguyễn Thái Ngọc Duy, Jan 13, 2012
  45. 06/10 clone: delay cloning until after remote HEAD checkingNguyễn Thái Ngọc Duy, Jan 13, 2012
  46. 07/10 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 13, 2012
  47. 08/10 clone: refuse to clone if --branch points to bogus refNguyễn Thái Ngọc Duy, Jan 13, 2012
  48. 09/10 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 13, 2012
  49. 10/10 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 13, 2012
  50. 01/10 t5601: add missing && cascadeNguyễn Thái Ngọc Duy, Jan 10, 2012
  51. 02/10 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 10, 2012
  52. 03/10 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 10, 2012
  53. 04/10 clone: factor out HEAD update codeNguyễn Thái Ngọc Duy, Jan 10, 2012
  54. 05/10 clone: factor out remote ref writingNguyễn Thái Ngọc Duy, Jan 10, 2012
  55. 06/10 clone: delay cloning until after remote HEAD checkingNguyễn Thái Ngọc Duy, Jan 10, 2012
  56. Junio C HamanoJan 11, 2012
  57. Nguyen Thai Ngoc DuyJan 12, 2012
  58. Junio C HamanoJan 24, 2012
  59. Junio C HamanoJan 24, 2012
  60. Junio C HamanoJan 24, 2012
  61. Nguyen Thai Ngoc DuyJan 24, 2012
  62. 07/10 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 10, 2012
  63. Junio C HamanoJan 11, 2012
  64. Nguyen Thai Ngoc DuyJan 12, 2012
  65. 08/10 clone: refuse to clone if --branch points to bogus refNguyễn Thái Ngọc Duy, Jan 10, 2012
  66. 09/10 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 10, 2012
  67. Junio C HamanoJan 11, 2012
  68. 10/10 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 10, 2012

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.