{"thread":{"id":"40381","subject":"[BUG?] HEAD detached at HEAD","startedAt":"2015-09-18T16:59:15Z","lastAt":"2015-09-21T17:20:13Z","messageCount":9,"participants":["Matthieu Moy","Jacob Keller","Stefan Beller","Michael J Gruber","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"270304","messageId":"vpqk2rnirz0.fsf@anie.imag.fr","threadId":"40381","inReplyTo":null,"subject":"[BUG?] HEAD detached at HEAD","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-18T16:59:15Z","receivedAt":"2015-09-18T16:59:15Z","isPatch":false,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Hi,\n\nI did a \"git checkout --detached\", and now I get:\n\n$ git status           \nHEAD detached at HEAD\nnothing to commit, working directory clean\n$ git branch\n* (HEAD detached at HEAD)\n  master\n\nThe \"HEAD detached at HEAD\" is not very helpful. I thought there was an\nabbreved sha1 here before, but I'm getting the same with latest Git in\nmaster and Git 2.0, so it doesn't seem to be new.\n\nI'm getting it even if there's a tag and/or a branch pointing to the\nsame commit.\n\nAny idea what's going on?\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"270305","messageId":"CA+P7+xoeXiZd=WU460Xfjthe0U5BnAV69_KNKW39p10ZGLHx7g@mail.gmail.com","threadId":"40381","inReplyTo":"vpqk2rnirz0.fsf@anie.imag.fr","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Jacob Keller","fromEmail":"jacob.keller@gmail.com","sentAt":"2015-09-18T17:08:04Z","receivedAt":"2015-09-18T17:08:04Z","isPatch":false,"sender":{"key":"jacob.keller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"On Fri, Sep 18, 2015 at 9:59 AM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> I'm getting it even if there's a tag and/or a branch pointing to the\n> same commit.\n>\n> Any idea what's going on?\n>\n\nAny chance you accidentally made a branch or tag named HEAD?\n\nRegards,\nJake\n"},{"id":"270306","messageId":"vpqeghviqu1.fsf@anie.imag.fr","threadId":"40381","inReplyTo":"CA+P7+xoeXiZd=WU460Xfjthe0U5BnAV69_KNKW39p10ZGLHx7g@mail.gmail.com","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-18T17:23:50Z","receivedAt":"2015-09-18T17:23:50Z","isPatch":false,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Jacob Keller <jacob.keller@gmail.com> writes:\n\n> On Fri, Sep 18, 2015 at 9:59 AM, Matthieu Moy\n> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>> I'm getting it even if there's a tag and/or a branch pointing to the\n>> same commit.\n>>\n>> Any idea what's going on?\n>\n> Any chance you accidentally made a branch or tag named HEAD?\n\nNice try ;-), but no:\n\n$ git status   \nHEAD detached at HEAD\nnothing to commit, working directory clean\n\n$ git for-each-ref\n16aa1796fb487fc7178ceef0525fed80284a4c52 commit\trefs/heads/master\n16aa1796fb487fc7178ceef0525fed80284a4c52 commit\trefs/tags/foo\n\n$ git rev-parse HEAD\n16aa1796fb487fc7178ceef0525fed80284a4c52\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"270307","messageId":"CAGZ79kZxAwMvv6UoZLBd2wTOdj1DFWKQqSPBYL449KSokA8DQQ@mail.gmail.com","threadId":"40381","inReplyTo":"vpqeghviqu1.fsf@anie.imag.fr","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2015-09-18T17:32:09Z","receivedAt":"2015-09-18T17:32:09Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Fri, Sep 18, 2015 at 10:23 AM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> Jacob Keller <jacob.keller@gmail.com> writes:\n>\n>> On Fri, Sep 18, 2015 at 9:59 AM, Matthieu Moy\n>> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>>> I'm getting it even if there's a tag and/or a branch pointing to the\n>>> same commit.\n>>>\n>>> Any idea what's going on?\n>>\n>> Any chance you accidentally made a branch or tag named HEAD?\n>\n> Nice try ;-), but no:\n\nI was playing around with origin/master and origin/pu and\nI cannot reproduce this bug.\n\nHowever maybe Michael as the author of 4b063186\n(2015-03-03 branch: name detached HEAD analogous to status)\ncan help here ?\n\n>\n> $ git status\n> HEAD detached at HEAD\n> nothing to commit, working directory clean\n>\n> $ git for-each-ref\n> 16aa1796fb487fc7178ceef0525fed80284a4c52 commit refs/heads/master\n> 16aa1796fb487fc7178ceef0525fed80284a4c52 commit refs/tags/foo\n>\n> $ git rev-parse HEAD\n> 16aa1796fb487fc7178ceef0525fed80284a4c52\n>\n> --\n> Matthieu Moy\n> http://www-verimag.imag.fr/~moy/\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"},{"id":"270312","messageId":"vpqlhc3h7e7.fsf@anie.imag.fr","threadId":"40381","inReplyTo":"CAGZ79kZxAwMvv6UoZLBd2wTOdj1DFWKQqSPBYL449KSokA8DQQ@mail.gmail.com","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-18T19:09:04Z","receivedAt":"2015-09-18T19:09:04Z","isPatch":false,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Stefan Beller <sbeller@google.com> writes:\n\n> On Fri, Sep 18, 2015 at 10:23 AM, Matthieu Moy\n> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>> Jacob Keller <jacob.keller@gmail.com> writes:\n>>\n>>> On Fri, Sep 18, 2015 at 9:59 AM, Matthieu Moy\n>>> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>>>> I'm getting it even if there's a tag and/or a branch pointing to the\n>>>> same commit.\n>>>>\n>>>> Any idea what's going on?\n>>>\n>>> Any chance you accidentally made a branch or tag named HEAD?\n>>\n>> Nice try ;-), but no:\n>\n> I was playing around with origin/master and origin/pu and\n> I cannot reproduce this bug.\n\nI investigated a bit more. The root of the problem is \"git checkout\n--detach\" and the reflog. Here's a reproduction script:\n\n\nrm -fr test-repo\ngit init test-repo\ncd test-repo\necho foo>bar; git add bar; git commit -m \"foo\"\necho boz>bar; git add bar; git commit -m \"boz\"\ngit checkout --detach\ngit status\ngit branch\nrm -fr .git/logs/\ngit status\ngit branch\n\nThe end of the output is:\n\n  + git checkout --detach\n  + git status\n  HEAD detached at HEAD\n  nothing to commit, working directory clean\n  + git branch\n  * (HEAD detached at HEAD)\n    master\n  + rm -fr .git/logs/\n  + git status\n  Not currently on any branch.\n  nothing to commit, working directory clean\n  + git branch\n  * (no branch)\n    master\n\nIf one replaces \"git checkout --detach\" with \"git checkout HEAD^0\", then\nthe output is the one I expected:\n\n  HEAD detached at cb39b20\n\nThe guilty line in the reflog is:\n\n  checkout: moving from master to HEAD\n\nOne possible fix is to resolve HEAD when encountering it in the reflog,\nlike this:\n\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -1319,6 +1319,13 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,\n        hashcpy(cb->nsha1, nsha1);\n        for (end = target; *end && *end != '\\n'; end++)\n                ;\n+       if (!memcmp(target, \"HEAD\", end - target)) {\n+               /* Don't say \"HEAD detached at HEAD\" */\n+               unsigned char head[GIT_SHA1_RAWSZ];\n+               get_sha1(\"HEAD\", head);\n+               strbuf_addstr(&cb->buf, find_unique_abbrev(head, DEFAULT_ABBREV));\n+               return 1;\n+       }\n        strbuf_add(&cb->buf, target, end - target);\n        return 1;\n }\n\nWhat do you think?\n\nShall I turn this into a proper patch?\n\nThanks,\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"270375","messageId":"55FFB267.3040106@drmicha.warpmail.net","threadId":"40381","inReplyTo":"vpqlhc3h7e7.fsf@anie.imag.fr","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2015-09-21T07:31:51Z","receivedAt":"2015-09-21T07:31:51Z","isPatch":false,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"Matthieu Moy venit, vidit, dixit 18.09.2015 21:09:\n> Stefan Beller <sbeller@google.com> writes:\n> \n>> On Fri, Sep 18, 2015 at 10:23 AM, Matthieu Moy\n>> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>>> Jacob Keller <jacob.keller@gmail.com> writes:\n>>>\n>>>> On Fri, Sep 18, 2015 at 9:59 AM, Matthieu Moy\n>>>> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>>>>> I'm getting it even if there's a tag and/or a branch pointing to the\n>>>>> same commit.\n>>>>>\n>>>>> Any idea what's going on?\n>>>>\n>>>> Any chance you accidentally made a branch or tag named HEAD?\n>>>\n>>> Nice try ;-), but no:\n>>\n>> I was playing around with origin/master and origin/pu and\n>> I cannot reproduce this bug.\n> \n> I investigated a bit more. The root of the problem is \"git checkout\n> --detach\" and the reflog. Here's a reproduction script:\n> \n> \n> rm -fr test-repo\n> git init test-repo\n> cd test-repo\n> echo foo>bar; git add bar; git commit -m \"foo\"\n> echo boz>bar; git add bar; git commit -m \"boz\"\n> git checkout --detach\n> git status\n> git branch\n> rm -fr .git/logs/\n> git status\n> git branch\n> \n> The end of the output is:\n> \n>   + git checkout --detach\n>   + git status\n>   HEAD detached at HEAD\n>   nothing to commit, working directory clean\n>   + git branch\n>   * (HEAD detached at HEAD)\n>     master\n>   + rm -fr .git/logs/\n>   + git status\n>   Not currently on any branch.\n>   nothing to commit, working directory clean\n>   + git branch\n>   * (no branch)\n>     master\n> \n> If one replaces \"git checkout --detach\" with \"git checkout HEAD^0\", then\n> the output is the one I expected:\n> \n>   HEAD detached at cb39b20\n> \n> The guilty line in the reflog is:\n> \n>   checkout: moving from master to HEAD\n> \n> One possible fix is to resolve HEAD when encountering it in the reflog,\n> like this:\n> \n> --- a/wt-status.c\n> +++ b/wt-status.c\n> @@ -1319,6 +1319,13 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,\n>         hashcpy(cb->nsha1, nsha1);\n>         for (end = target; *end && *end != '\\n'; end++)\n>                 ;\n> +       if (!memcmp(target, \"HEAD\", end - target)) {\n> +               /* Don't say \"HEAD detached at HEAD\" */\n> +               unsigned char head[GIT_SHA1_RAWSZ];\n> +               get_sha1(\"HEAD\", head);\n> +               strbuf_addstr(&cb->buf, find_unique_abbrev(head, DEFAULT_ABBREV));\n> +               return 1;\n> +       }\n>         strbuf_add(&cb->buf, target, end - target);\n>         return 1;\n>  }\n> \n> What do you think?\n> \n> Shall I turn this into a proper patch?\n> \n> Thanks,\n> \n\nThanks for finding the root cause.\n\nI think, though, that the reflog is wrong:\n\n\"git checkout --detach\" should be equivalent to \"git checkout HEAD^0\",\nshouldn't it? It becomes clearer with branches:\n\ngit co --detach master\ngit reflog\n3b9153c HEAD@{0}: checkout: moving from master to master\n\nI think that information (\"moving ... to master\") is misleading at best,\nif not wrong (depending on how you read the reflog). In any case you\ncannot distinguish a regular branch checkout (i.e. branch switching)\nfrom a \"--detach\" checkout in the reflog, which is bad.\n\nThe same happens when you \"git checkout origin/next\" and you get\n--detach kicking in automatically. It looks like a regular branch checkout.\n\nSo, maybe we should really fix the reflog, e.g. by amending the\ninformation there?\n\nstatus/branch figure out that they're in the detached case even without\nthe help from reflog, and your patch is correct as long as we don't\ncommit on top of the fork point. But as soon as we do that, resolving\n\"HEAD\" will not give you the fork point, and the output of \"detached\nfrom...\" will be incorrect (based on my reading of the patch and my\nmemory of the workings, no compile test yet, sorry).\n\nMichael\n"},{"id":"270376","messageId":"55FFB43B.5080002@drmicha.warpmail.net","threadId":"40381","inReplyTo":"55FFB267.3040106@drmicha.warpmail.net","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2015-09-21T07:39:39Z","receivedAt":"2015-09-21T07:39:39Z","isPatch":false,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"... in addition to my previous reply, looking at more context:\n\n>> --- a/wt-status.c\n>> +++ b/wt-status.c\n>> @@ -1319,6 +1319,13 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,\n>>         hashcpy(cb->nsha1, nsha1);\n>>         for (end = target; *end && *end != '\\n'; end++)\n>>                 ;\n>> +       if (!memcmp(target, \"HEAD\", end - target)) {\n>> +               /* Don't say \"HEAD detached at HEAD\" */\n>> +               unsigned char head[GIT_SHA1_RAWSZ];\n>> +               get_sha1(\"HEAD\", head);\n>> +               strbuf_addstr(&cb->buf, find_unique_abbrev(head, DEFAULT_ABBREV));\n>> +               return 1;\n>> +       }\n>>         strbuf_add(&cb->buf, target, end - target);\n>>         return 1;\n>>  }\n>>\n>> What do you think?\n\nI think we should return (the hex repr. of) nsha1 instead of resolving\nHEAD at its current state. That should solve the present problem (and\nleave the more difficult reflog issue for the future).\n\nMichael\n"},{"id":"270377","messageId":"vpqk2rkcj67.fsf@anie.imag.fr","threadId":"40381","inReplyTo":"55FFB43B.5080002@drmicha.warpmail.net","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-21T07:42:40Z","receivedAt":"2015-09-21T07:42:40Z","isPatch":false,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Michael J Gruber <git@drmicha.warpmail.net> writes:\n\n> ... in addition to my previous reply, looking at more context:\n>\n>>> --- a/wt-status.c\n>>> +++ b/wt-status.c\n>>> @@ -1319,6 +1319,13 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,\n>>>         hashcpy(cb->nsha1, nsha1);\n>>>         for (end = target; *end && *end != '\\n'; end++)\n>>>                 ;\n>>> +       if (!memcmp(target, \"HEAD\", end - target)) {\n>>> +               /* Don't say \"HEAD detached at HEAD\" */\n>>> +               unsigned char head[GIT_SHA1_RAWSZ];\n>>> +               get_sha1(\"HEAD\", head);\n>>> +               strbuf_addstr(&cb->buf, find_unique_abbrev(head, DEFAULT_ABBREV));\n>>> +               return 1;\n>>> +       }\n>>>         strbuf_add(&cb->buf, target, end - target);\n>>>         return 1;\n>>>  }\n>>>\n>>> What do you think?\n>\n> I think we should return (the hex repr. of) nsha1 instead of resolving\n> HEAD at its current state. That should solve the present problem (and\n> leave the more difficult reflog issue for the future).\n\nIndeed, resolving HEAD to its current state is wrong. I'll submit a\nproper patch soon.\n\nThanks,\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"270409","messageId":"xmqq8u7zbsfm.fsf@gitster.mtv.corp.google.com","threadId":"40381","inReplyTo":"vpqlhc3h7e7.fsf@anie.imag.fr","subject":"Re: [BUG?] HEAD detached at HEAD","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-21T17:20:13Z","receivedAt":"2015-09-21T17:20:13Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:\n\n> I investigated a bit more. The root of the problem is \"git checkout\n> --detach\" and the reflog. Here's a reproduction script:\n> ...\n> If one replaces \"git checkout --detach\" with \"git checkout HEAD^0\", then\n> the output is the one I expected:\n>\n>   HEAD detached at cb39b20\n>\n> The guilty line in the reflog is:\n>\n>   checkout: moving from master to HEAD\n\nYeah, that sounds broken.  --detach should write the same reflog entry\nas \"git checkout HEAD^0\".\n"}]}