{"thread":{"id":"44255","subject":"Bug? git worktree fails with master on bare repo","startedAt":"2016-10-09T00:30:43Z","lastAt":"2016-10-13T10:32:00Z","messageCount":14,"participants":["Michael Tutty","Kevin Daudt","Dennis Kaarsemaker","Duy Nguyen","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"303650","messageId":"CAOi_75+2SG2WYHBMQhfGj96eKsZ66niJzOevVGM5eJv-qqrVNg@mail.gmail.com","threadId":"44255","inReplyTo":null,"subject":"Bug? git worktree fails with master on bare repo","fromName":"Michael Tutty","fromEmail":"mtutty@gforgegroup.com","sentAt":"2016-10-09T00:30:36Z","receivedAt":"2016-10-09T00:30:43Z","isPatch":false,"sender":{"key":"mtutty@gforgegroup.com","avatar":"https://gravatar.com/avatar/81d83fba0cdbb4867c76c103ab581a3958579798c2dfe91541e89824681073a0?d=mp&s=160"},"body":"Hey all,\nI'm working on some server-side software to do a merge. By using git\nworktree it's possible to check out a given branch for a bare repo and\nmerge another branch into it. It's very fast, even with large\nrepositories.\n\nThe only exception seems to be merging to master. When I do git\nworktree add /tmp/path/to/worktree master I get an error:\n\n[fatal: 'master' is already checked out at '/path/to/bare/repo']\n\nBut this is clearly not true, git worktree list gives:\n\n[/path/to/bare/repo (bare)]\n\n...and of course, there is no work tree at that path, just the bare\nrepo files you'd expect.\n"},{"id":"303658","messageId":"20161009065046.GD25613@ikke.info","threadId":"44255","inReplyTo":"CAOi_75+2SG2WYHBMQhfGj96eKsZ66niJzOevVGM5eJv-qqrVNg@mail.gmail.com","subject":"Re: Bug? git worktree fails with master on bare repo","fromName":"Kevin Daudt","fromEmail":"me@ikke.info","sentAt":"2016-10-09T06:50:46Z","receivedAt":"2016-10-09T06:51:19Z","isPatch":false,"sender":{"key":"me@ikke.info","avatar":"https://avatars.githubusercontent.com/u/135698?v=4"},"body":"On Sat, Oct 08, 2016 at 07:30:36PM -0500, Michael Tutty wrote:\n> Hey all,\n> I'm working on some server-side software to do a merge. By using git\n> worktree it's possible to check out a given branch for a bare repo and\n> merge another branch into it. It's very fast, even with large\n> repositories.\n> \n> The only exception seems to be merging to master. When I do git\n> worktree add /tmp/path/to/worktree master I get an error:\n> \n> [fatal: 'master' is already checked out at '/path/to/bare/repo']\n> \n> But this is clearly not true, git worktree list gives:\n> \n> [/path/to/bare/repo (bare)]\n> \n> ...and of course, there is no work tree at that path, just the bare\n> repo files you'd expect.\n\nA bare repo still has a HEAD, which by default points to\nrefs/heads/master. That's what's it complaining about.\n\nSo the question is, should there be an exception for the branch 'checked\nout' on a bare reposity.\n"},{"id":"303659","messageId":"1475999513.7410.8.camel@kaarsemaker.net","threadId":"44255","inReplyTo":"CAOi_75+2SG2WYHBMQhfGj96eKsZ66niJzOevVGM5eJv-qqrVNg@mail.gmail.com","subject":"Re: Bug? git worktree fails with master on bare repo","fromName":"Dennis Kaarsemaker","fromEmail":"dennis@kaarsemaker.net","sentAt":"2016-10-09T07:51:53Z","receivedAt":"2016-10-09T08:07:48Z","isPatch":false,"sender":{"key":"dennis@kaarsemaker.net","avatar":"https://avatars.githubusercontent.com/u/200649?v=4"},"body":"On Sat, 2016-10-08 at 19:30 -0500, Michael Tutty wrote:\n> Hey all,\n> I'm working on some server-side software to do a merge. By using git\n> worktree it's possible to check out a given branch for a bare repo and\n> merge another branch into it. It's very fast, even with large\n> repositories.\n> \n> The only exception seems to be merging to master. When I do git\n> worktree add /tmp/path/to/worktree master I get an error:\n> \n> [fatal: 'master' is already checked out at '/path/to/bare/repo']\n> \n> But this is clearly not true, git worktree list gives:\n> \n> [/path/to/bare/repo (bare)]\n> \n> ...and of course, there is no work tree at that path, just the bare\n> repo files you'd expect.\n\nThe worktree code treats the base repo as a worktree, even if it's\nbare. For the purpose of being able to do a checkout of the main branch\nof a bare repo, this patch should do:\n\ndiff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\nindex 4bcc335..b618d6b 100755\n--- a/t/t2025-worktree-add.sh\n+++ b/t/t2025-worktree-add.sh\n@@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n        )\n '\n \n+test_expect_success '\"add\" default branch of a bare repo' '\n+       (\n+               git clone --bare . bare2 &&\n+               cd bare2 &&\n+               git worktree add ../there3 master\n+       )\n+'\n+\n test_expect_success 'checkout with grafts' '\n        test_when_finished rm .git/info/grafts &&\n        test_commit abc &&\ndiff --git a/worktree.c b/worktree.c\nindex 5acfe4c..35e95b7 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,\n \n        for (i = 0; worktrees[i]; i++) {\n                struct worktree *wt = worktrees[i];\n+               if(wt->is_bare)\n+                       continue;\n \n                if (wt->is_detached && !strcmp(symref, \"HEAD\")) {\n                        if (is_worktree_being_rebased(wt, target)) {\n\n\nBut I'm wondering why the worktree code does this. A bare repo isn't a\nworktree and I think it shouldn't treat it as one. A patch that rips\nout this feature and updates the tests to match would look like this:\n\n\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 5c4854d..3600530 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -382,15 +382,11 @@ static int add(int ac, const char **av, const char *prefix)\n static void show_worktree_porcelain(struct worktree *wt)\n {\n \tprintf(\"worktree %s\\n\", wt->path);\n-\tif (wt->is_bare)\n-\t\tprintf(\"bare\\n\");\n-\telse {\n-\t\tprintf(\"HEAD %s\\n\", sha1_to_hex(wt->head_sha1));\n-\t\tif (wt->is_detached)\n-\t\t\tprintf(\"detached\\n\");\n-\t\telse\n-\t\t\tprintf(\"branch %s\\n\", wt->head_ref);\n-\t}\n+\tprintf(\"HEAD %s\\n\", sha1_to_hex(wt->head_sha1));\n+\tif (wt->is_detached)\n+\t\tprintf(\"detached\\n\");\n+\telse\n+\t\tprintf(\"branch %s\\n\", wt->head_ref);\n \tprintf(\"\\n\");\n }\n \n@@ -401,16 +397,12 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)\n \tint path_adj = cur_path_len - utf8_strwidth(wt->path);\n \n \tstrbuf_addf(&sb, \"%-*s \", 1 + path_maxlen + path_adj, wt->path);\n-\tif (wt->is_bare)\n-\t\tstrbuf_addstr(&sb, \"(bare)\");\n-\telse {\n-\t\tstrbuf_addf(&sb, \"%-*s \", abbrev_len,\n-\t\t\t\tfind_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));\n-\t\tif (!wt->is_detached)\n-\t\t\tstrbuf_addf(&sb, \"[%s]\", shorten_unambiguous_ref(wt->head_ref, 0));\n-\t\telse\n-\t\t\tstrbuf_addstr(&sb, \"(detached HEAD)\");\n-\t}\n+\tstrbuf_addf(&sb, \"%-*s \", abbrev_len,\n+\t\t\tfind_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));\n+\tif (!wt->is_detached)\n+\t\tstrbuf_addf(&sb, \"[%s]\", shorten_unambiguous_ref(wt->head_ref, 0));\n+\telse\n+\t\tstrbuf_addstr(&sb, \"(detached HEAD)\");\n \tprintf(\"%s\\n\", sb.buf);\n \n \tstrbuf_release(&sb);\ndiff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\nindex 4bcc335..b618d6b 100755\n--- a/t/t2025-worktree-add.sh\n+++ b/t/t2025-worktree-add.sh\n@@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n \t)\n '\n \n+test_expect_success '\"add\" default branch of a bare repo' '\n+\t(\n+\t\tgit clone --bare . bare2 &&\n+\t\tcd bare2 &&\n+\t\tgit worktree add ../there3 master\n+\t)\n+'\n+\n test_expect_success 'checkout with grafts' '\n \ttest_when_finished rm .git/info/grafts &&\n \ttest_commit abc &&\ndiff --git a/t/t2027-worktree-list.sh b/t/t2027-worktree-list.sh\nindex 1b1b65a..842e9d9 100755\n--- a/t/t2027-worktree-list.sh\n+++ b/t/t2027-worktree-list.sh\n@@ -62,9 +62,8 @@ test_expect_success 'bare repo setup' '\n \n test_expect_success '\"list\" all worktrees from bare main' '\n \ttest_when_finished \"rm -rf there && git -C bare1 worktree prune\" &&\n-\tgit -C bare1 worktree add --detach ../there master &&\n-\techo \"$(pwd)/bare1 (bare)\" >expect &&\n-\techo \"$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)\" >>expect &&\n+\tgit -C bare1 worktree add ../there master &&\n+\techo \"$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) [master]\" >expect &&\n \tgit -C bare1 worktree list | sed \"s/  */ /g\" >actual &&\n \ttest_cmp expect actual\n '\n@@ -72,10 +71,7 @@ test_expect_success '\"list\" all worktrees from bare main' '\n test_expect_success '\"list\" all worktrees --porcelain from bare main' '\n \ttest_when_finished \"rm -rf there && git -C bare1 worktree prune\" &&\n \tgit -C bare1 worktree add --detach ../there master &&\n-\techo \"worktree $(pwd)/bare1\" >expect &&\n-\techo \"bare\" >>expect &&\n-\techo >>expect &&\n-\techo \"worktree $(git -C there rev-parse --show-toplevel)\" >>expect &&\n+\techo \"worktree $(git -C there rev-parse --show-toplevel)\" >expect &&\n \techo \"HEAD $(git -C there rev-parse HEAD)\" >>expect &&\n \techo \"detached\" >>expect &&\n \techo >>expect &&\n@@ -85,9 +81,8 @@ test_expect_success '\"list\" all worktrees --porcelain from bare main' '\n \n test_expect_success '\"list\" all worktrees from linked with a bare main' '\n \ttest_when_finished \"rm -rf there && git -C bare1 worktree prune\" &&\n-\tgit -C bare1 worktree add --detach ../there master &&\n-\techo \"$(pwd)/bare1 (bare)\" >expect &&\n-\techo \"$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)\" >>expect &&\n+\tgit -C bare1 worktree add ../there master &&\n+\techo \"$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) [master]\" >expect &&\n \tgit -C there worktree list | sed \"s/  */ /g\" >actual &&\n \ttest_cmp expect actual\n '\ndiff --git a/worktree.c b/worktree.c\nindex 5acfe4c..d4dbaab 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -84,7 +84,7 @@ static struct worktree *get_main_worktree(void)\n \tstrbuf_add_absolute_path(&worktree_path, get_git_common_dir());\n \tis_bare = !strbuf_strip_suffix(&worktree_path, \"/.git\");\n \tif (is_bare)\n-\t\tstrbuf_strip_suffix(&worktree_path, \"/.\");\n+\t\tgoto done;\n \n \tstrbuf_addf(&path, \"%s/HEAD\", get_git_common_dir());\n \n@@ -94,7 +94,6 @@ static struct worktree *get_main_worktree(void)\n \tworktree = xmalloc(sizeof(struct worktree));\n \tworktree->path = strbuf_detach(&worktree_path, NULL);\n \tworktree->id = NULL;\n-\tworktree->is_bare = is_bare;\n \tworktree->head_ref = NULL;\n \tworktree->is_detached = is_detached;\n \tworktree->is_current = 0;\n@@ -141,7 +140,6 @@ static struct worktree *get_linked_worktree(const char *id)\n \tworktree = xmalloc(sizeof(struct worktree));\n \tworktree->path = strbuf_detach(&worktree_path, NULL);\n \tworktree->id = xstrdup(id);\n-\tworktree->is_bare = 0;\n \tworktree->head_ref = NULL;\n \tworktree->is_detached = is_detached;\n \tworktree->is_current = 0;\ndiff --git a/worktree.h b/worktree.h\nindex 90e1311..04a75e8 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -8,7 +8,6 @@ struct worktree {\n \tchar *lock_reason;\t/* internal use */\n \tunsigned char head_sha1[20];\n \tint is_detached;\n-\tint is_bare;\n \tint is_current;\n \tint lock_reason_valid;\n };\n"},{"id":"303667","messageId":"CACsJy8CmgmGLEi0xQUY9Eo-4FkA4eDNk9WJ2LtEDVFQBjbFdCA@mail.gmail.com","threadId":"44255","inReplyTo":"1475999513.7410.8.camel@kaarsemaker.net","subject":"Re: Bug? git worktree fails with master on bare repo","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-10-09T10:52:11Z","receivedAt":"2016-10-09T10:52:48Z","isPatch":false,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Oct 9, 2016 at 2:51 PM, Dennis Kaarsemaker\n<dennis@kaarsemaker.net> wrote:\n> On Sat, 2016-10-08 at 19:30 -0500, Michael Tutty wrote:\n>> Hey all,\n>> I'm working on some server-side software to do a merge. By using git\n>> worktree it's possible to check out a given branch for a bare repo and\n>> merge another branch into it. It's very fast, even with large\n>> repositories.\n>>\n>> The only exception seems to be merging to master. When I do git\n>> worktree add /tmp/path/to/worktree master I get an error:\n>>\n>> [fatal: 'master' is already checked out at '/path/to/bare/repo']\n>>\n>> But this is clearly not true, git worktree list gives:\n>>\n>> [/path/to/bare/repo (bare)]\n>>\n>> ...and of course, there is no work tree at that path, just the bare\n>> repo files you'd expect.\n>\n> The worktree code treats the base repo as a worktree, even if it's\n> bare. For the purpose of being able to do a checkout of the main branch\n> of a bare repo, this patch should do:\n>\n> diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\n> index 4bcc335..b618d6b 100755\n> --- a/t/t2025-worktree-add.sh\n> +++ b/t/t2025-worktree-add.sh\n> @@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n>         )\n>  '\n>\n> +test_expect_success '\"add\" default branch of a bare repo' '\n> +       (\n> +               git clone --bare . bare2 &&\n> +               cd bare2 &&\n> +               git worktree add ../there3 master\n> +       )\n> +'\n> +\n>  test_expect_success 'checkout with grafts' '\n>         test_when_finished rm .git/info/grafts &&\n>         test_commit abc &&\n> diff --git a/worktree.c b/worktree.c\n> index 5acfe4c..35e95b7 100644\n> --- a/worktree.c\n> +++ b/worktree.c\n> @@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,\n>\n>         for (i = 0; worktrees[i]; i++) {\n>                 struct worktree *wt = worktrees[i];\n> +               if(wt->is_bare)\n> +                       continue;\n>\n>                 if (wt->is_detached && !strcmp(symref, \"HEAD\")) {\n>                         if (is_worktree_being_rebased(wt, target)) {\n>\n>\n\nYou're fast :) I'm still studying  8d9fdd7 (worktree.c: check whether\nbranch is rebased in another worktree - 2016-04-22). But yeah that\nshould fix it.\n\n> But I'm wondering why the worktree code does this. A bare repo isn't a\n> worktree and I think it shouldn't treat it as one. A patch that rips\n> out this feature and updates the tests to match would look like this:\n>\n>\n> diff --git a/builtin/worktree.c b/builtin/worktree.c\n> index 5c4854d..3600530 100644\n> --- a/builtin/worktree.c\n> +++ b/builtin/worktree.c\n> @@ -382,15 +382,11 @@ static int add(int ac, const char **av, const char *prefix)\n>  static void show_worktree_porcelain(struct worktree *wt)\n>  {\n>         printf(\"worktree %s\\n\", wt->path);\n> -       if (wt->is_bare)\n> -               printf(\"bare\\n\");\n> -       else {\n> -               printf(\"HEAD %s\\n\", sha1_to_hex(wt->head_sha1));\n> -               if (wt->is_detached)\n> -                       printf(\"detached\\n\");\n> -               else\n> -                       printf(\"branch %s\\n\", wt->head_ref);\n> -       }\n> +       printf(\"HEAD %s\\n\", sha1_to_hex(wt->head_sha1));\n> +       if (wt->is_detached)\n> +               printf(\"detached\\n\");\n> +       else\n> +               printf(\"branch %s\\n\", wt->head_ref);\n>         printf(\"\\n\");\n>  }\n\nThis goes back to the first very first commit of \"git worktree list\":\nbb9c03b (worktree: add 'list' command - 2015-10-08) and was sort of\npointed out during review [1] but nobody answered it.\n\nA bare repo does not have an associated worktree. However only main\nworktree can be bare. If we take this out, \"git worktree list\"'s first\nline will no longer be about the main worktree (because it does not\nexist). That may cause trouble since we promised in \"git-worktree.txt\"\nthat the main worktree is listed first. I don't think we have any way\nelse to determine if the main worktree exists. Showing \"bare\" may be\nthe way to see if we have a main worktree or not. So we probably want\nto keep this function unchanged.\n\n[1] https://public-inbox.org/git/%3CCANoM8SWeqxD2vWLQmEfxxxn8Dz4yPfjGOoOH=Azn1A3So+wz2Q@mail.gmail.com%3E/\n-- \nDuy\n"},{"id":"303677","messageId":"CAOi_75+Zoeeq5FD1HKgG4MjeL9LkBshsSfdrMOTi8m-Mt-OQNQ@mail.gmail.com","threadId":"44255","inReplyTo":"CACsJy8CmgmGLEi0xQUY9Eo-4FkA4eDNk9WJ2LtEDVFQBjbFdCA@mail.gmail.com","subject":"Re: Bug? git worktree fails with master on bare repo","fromName":"Michael Tutty","fromEmail":"mtutty@gforgegroup.com","sentAt":"2016-10-09T13:42:55Z","receivedAt":"2016-10-09T13:43:04Z","isPatch":false,"sender":{"key":"mtutty@gforgegroup.com","avatar":"https://gravatar.com/avatar/81d83fba0cdbb4867c76c103ab581a3958579798c2dfe91541e89824681073a0?d=mp&s=160"},"body":"Dennis,\nThanks for the great response, and for spending time on my issue.\nI'll try that first patch and see what happens.\n\nIn the meantime, it got weirder...\n\nI created a brand-new (bare) repo and was able to git add worktree\n/path master.  I was able to do this repeatedly, even using the\nworktree to merge other branches to master.  I didn't find any\ncondition or step that caused some kind of orphan master work tree,\nwhich was what I thought the underlying problem might be.\n\nSo, on the one hand, you found code validating my initial experience.\nBut on the other hand, I found a test case that didn't appear to have\nthat problem.\n\nWAT.\n      M.\n\nOn Sun, Oct 9, 2016 at 5:52 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Oct 9, 2016 at 2:51 PM, Dennis Kaarsemaker\n> <dennis@kaarsemaker.net> wrote:\n>> On Sat, 2016-10-08 at 19:30 -0500, Michael Tutty wrote:\n>>> Hey all,\n>>> I'm working on some server-side software to do a merge. By using git\n>>> worktree it's possible to check out a given branch for a bare repo and\n>>> merge another branch into it. It's very fast, even with large\n>>> repositories.\n>>>\n>>> The only exception seems to be merging to master. When I do git\n>>> worktree add /tmp/path/to/worktree master I get an error:\n>>>\n>>> [fatal: 'master' is already checked out at '/path/to/bare/repo']\n>>>\n>>> But this is clearly not true, git worktree list gives:\n>>>\n>>> [/path/to/bare/repo (bare)]\n>>>\n>>> ...and of course, there is no work tree at that path, just the bare\n>>> repo files you'd expect.\n>>\n>> The worktree code treats the base repo as a worktree, even if it's\n>> bare. For the purpose of being able to do a checkout of the main branch\n>> of a bare repo, this patch should do:\n>>\n>> diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\n>> index 4bcc335..b618d6b 100755\n>> --- a/t/t2025-worktree-add.sh\n>> +++ b/t/t2025-worktree-add.sh\n>> @@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n>>         )\n>>  '\n>>\n>> +test_expect_success '\"add\" default branch of a bare repo' '\n>> +       (\n>> +               git clone --bare . bare2 &&\n>> +               cd bare2 &&\n>> +               git worktree add ../there3 master\n>> +       )\n>> +'\n>> +\n>>  test_expect_success 'checkout with grafts' '\n>>         test_when_finished rm .git/info/grafts &&\n>>         test_commit abc &&\n>> diff --git a/worktree.c b/worktree.c\n>> index 5acfe4c..35e95b7 100644\n>> --- a/worktree.c\n>> +++ b/worktree.c\n>> @@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,\n>>\n>>         for (i = 0; worktrees[i]; i++) {\n>>                 struct worktree *wt = worktrees[i];\n>> +               if(wt->is_bare)\n>> +                       continue;\n>>\n>>                 if (wt->is_detached && !strcmp(symref, \"HEAD\")) {\n>>                         if (is_worktree_being_rebased(wt, target)) {\n>>\n>>\n>\n> You're fast :) I'm still studying  8d9fdd7 (worktree.c: check whether\n> branch is rebased in another worktree - 2016-04-22). But yeah that\n> should fix it.\n>\n>> But I'm wondering why the worktree code does this. A bare repo isn't a\n>> worktree and I think it shouldn't treat it as one. A patch that rips\n>> out this feature and updates the tests to match would look like this:\n>>\n>>\n>> diff --git a/builtin/worktree.c b/builtin/worktree.c\n>> index 5c4854d..3600530 100644\n>> --- a/builtin/worktree.c\n>> +++ b/builtin/worktree.c\n>> @@ -382,15 +382,11 @@ static int add(int ac, const char **av, const char *prefix)\n>>  static void show_worktree_porcelain(struct worktree *wt)\n>>  {\n>>         printf(\"worktree %s\\n\", wt->path);\n>> -       if (wt->is_bare)\n>> -               printf(\"bare\\n\");\n>> -       else {\n>> -               printf(\"HEAD %s\\n\", sha1_to_hex(wt->head_sha1));\n>> -               if (wt->is_detached)\n>> -                       printf(\"detached\\n\");\n>> -               else\n>> -                       printf(\"branch %s\\n\", wt->head_ref);\n>> -       }\n>> +       printf(\"HEAD %s\\n\", sha1_to_hex(wt->head_sha1));\n>> +       if (wt->is_detached)\n>> +               printf(\"detached\\n\");\n>> +       else\n>> +               printf(\"branch %s\\n\", wt->head_ref);\n>>         printf(\"\\n\");\n>>  }\n>\n> This goes back to the first very first commit of \"git worktree list\":\n> bb9c03b (worktree: add 'list' command - 2015-10-08) and was sort of\n> pointed out during review [1] but nobody answered it.\n>\n> A bare repo does not have an associated worktree. However only main\n> worktree can be bare. If we take this out, \"git worktree list\"'s first\n> line will no longer be about the main worktree (because it does not\n> exist). That may cause trouble since we promised in \"git-worktree.txt\"\n> that the main worktree is listed first. I don't think we have any way\n> else to determine if the main worktree exists. Showing \"bare\" may be\n> the way to see if we have a main worktree or not. So we probably want\n> to keep this function unchanged.\n>\n> [1] https://public-inbox.org/git/%3CCANoM8SWeqxD2vWLQmEfxxxn8Dz4yPfjGOoOH=Azn1A3So+wz2Q@mail.gmail.com%3E/\n> --\n> Duy\n\n\n\n-- \nMichael Tutty, CTO\n\ne: mtutty@gforgegroup.com\nt: @mtutty, @gforgegroup\nv: 515-789-0772\nw: http://gforgegroup.com, http://gforge.com\n"},{"id":"303700","messageId":"CACsJy8DMKWeZ+DuQ0uoY6rdPfusq8D1SfBCkPyn+6X9S589ncg@mail.gmail.com","threadId":"44255","inReplyTo":"CAOi_75+Zoeeq5FD1HKgG4MjeL9LkBshsSfdrMOTi8m-Mt-OQNQ@mail.gmail.com","subject":"Re: Bug? git worktree fails with master on bare repo","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-10-10T09:45:01Z","receivedAt":"2016-10-10T09:45:37Z","isPatch":false,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Oct 9, 2016 at 8:42 PM, Michael Tutty <mtutty@gforgegroup.com> wrote:\n> Dennis,\n> Thanks for the great response, and for spending time on my issue.\n> I'll try that first patch and see what happens.\n>\n> In the meantime, it got weirder...\n>\n> I created a brand-new (bare) repo\n\nElaboration needed here. If I create a bare _clone_, then \"HEAD\" could\nbe detached, or point to some branch, depending on where \"HEAD\" is in\nthe source repo. If source repo's HEAD is \"master\", I got the same\nbehavior (worktree add fails). If it's detached or points to some\nother branch, it's ok. If this is \"git init --bare\" then I got \"fatal:\ninvalid reference: master\".\n\n> and was able to git add worktree\n> /path master.  I was able to do this repeatedly, even using the\n> worktree to merge other branches to master.  I didn't find any\n> condition or step that caused some kind of orphan master work tree,\n> which was what I thought the underlying problem might be.\n-- \nDuy\n"},{"id":"303720","messageId":"CAOi_75KR+AEAD540L=GtQ7jH5ngTFdHMq53+FzjzAfsZtTqfwg@mail.gmail.com","threadId":"44255","inReplyTo":"CACsJy8DMKWeZ+DuQ0uoY6rdPfusq8D1SfBCkPyn+6X9S589ncg@mail.gmail.com","subject":"Re: Bug? git worktree fails with master on bare repo","fromName":"Michael Tutty","fromEmail":"mtutty@gforgegroup.com","sentAt":"2016-10-10T13:06:47Z","receivedAt":"2016-10-10T13:09:23Z","isPatch":false,"sender":{"key":"mtutty@gforgegroup.com","avatar":"https://gravatar.com/avatar/81d83fba0cdbb4867c76c103ab581a3958579798c2dfe91541e89824681073a0?d=mp&s=160"},"body":"> If I create a bare _clone_, then \"HEAD\" could be detached, or point to some branch, depending on where \"HEAD\" is in the source repo\n\nI didn't mean a clone, I meant a brand-new (bare) repo.  Then I would\nclone it somewhere, add commits and branches, and push them to the\nbare repo.\n\n\n> If source repo's HEAD is \"master\", I got the same behavior (worktree add fails)\n\nSo if it's possible for a bare repo to have HEAD pointing at master,\nis there a safe way for me to change this (e.g., as a cleanup step\nbefore doing my actual merge process)?\n\nOn Mon, Oct 10, 2016 at 4:45 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Oct 9, 2016 at 8:42 PM, Michael Tutty <mtutty@gforgegroup.com> wrote:\n>> Dennis,\n>> Thanks for the great response, and for spending time on my issue.\n>> I'll try that first patch and see what happens.\n>>\n>> In the meantime, it got weirder...\n>>\n>> I created a brand-new (bare) repo\n>\n> Elaboration needed here. If I create a bare _clone_, then \"HEAD\" could\n> be detached, or point to some branch, depending on where \"HEAD\" is in\n> the source repo. If source repo's HEAD is \"master\", I got the same\n> behavior (worktree add fails). If it's detached or points to some\n> other branch, it's ok. If this is \"git init --bare\" then I got \"fatal:\n> invalid reference: master\".\n>\n>> and was able to git add worktree\n>> /path master.  I was able to do this repeatedly, even using the\n>> worktree to merge other branches to master.  I didn't find any\n>> condition or step that caused some kind of orphan master work tree,\n>> which was what I thought the underlying problem might be.\n> --\n> Duy\n\n\n\n-- \nMichael Tutty, CTO\n\ne: mtutty@gforgegroup.com\nt: @mtutty, @gforgegroup\nv: 515-789-0772\nw: http://gforgegroup.com, http://gforge.com\n"},{"id":"303875","messageId":"20161011154137.GA20160@ikke.info","threadId":"44255","inReplyTo":"CAOi_75KR+AEAD540L=GtQ7jH5ngTFdHMq53+FzjzAfsZtTqfwg@mail.gmail.com","subject":"Re: Bug? git worktree fails with master on bare repo","fromName":"Kevin Daudt","fromEmail":"me@ikke.info","sentAt":"2016-10-11T15:41:37Z","receivedAt":"2016-10-11T15:41:48Z","isPatch":false,"sender":{"key":"me@ikke.info","avatar":"https://avatars.githubusercontent.com/u/135698?v=4"},"body":"On Mon, Oct 10, 2016 at 08:06:47AM -0500, Michael Tutty wrote:\n> \n> > If source repo's HEAD is \"master\", I got the same behavior (worktree add fails)\n> \n> So if it's possible for a bare repo to have HEAD pointing at master,\n> is there a safe way for me to change this (e.g., as a cleanup step\n> before doing my actual merge process)?\n\nYou can change where HEAD points to in a bare repositor with `git\nsymbolic-ref HEAD <branch>`, but note that this changes what branch gets\nchecked out when cloning a repository. \n\nWhere users would normally check out master by default, after changing\nwhat HEAD points to, it would be that branch (or detached when HEAD\ndoesn't even point at a branch).\n"},{"id":"304010","messageId":"20161012164104.zcxpcwqrp5h65qmc@hurricane","threadId":"44255","inReplyTo":"CACsJy8CmgmGLEi0xQUY9Eo-4FkA4eDNk9WJ2LtEDVFQBjbFdCA@mail.gmail.com","subject":"[PATCH] worktree: allow the main brach of a bare repository to be checked out","fromName":"Dennis Kaarsemaker","fromEmail":"dennis@kaarsemaker.net","sentAt":"2016-10-12T16:41:07Z","receivedAt":"2016-10-12T16:41:16Z","isPatch":true,"sender":{"key":"dennis@kaarsemaker.net","avatar":"https://avatars.githubusercontent.com/u/200649?v=4"},"body":"In bare repositories, get_worktrees() still returns the main repository,\nso git worktree list can show it. ignore it in find_shared_symref so we\ncan still check out the main branch.\n\nSigned-off-by: Dennis Kaarsemaker <dennis@kaarsemaker.net>\n---\n t/t2025-worktree-add.sh | 8 ++++++++\n worktree.c              | 2 ++\n 2 files changed, 10 insertions(+)\n\nOn Sun, 2016-10-09 at 17:52 +0700, Duy Nguyen wrote:\n> On Sun, Oct 9, 2016 at 2:51 PM, Dennis Kaarsemaker > <dennis@kaarsemaker.net> wrote:\n> > On Sat, 2016-10-08 at 19:30 -0500, Michael Tutty wrote:\n> > > \n> > > The only exception seems to be merging to master. When I do git\n> > > worktree add /tmp/path/to/worktree master I get an error:\n> > > \n> > > [fatal: 'master' is already checked out at '/path/to/bare/repo']\n> > > \n> > \n> > The worktree code treats the base repo as a worktree, even if it's\n> > bare. For the purpose of being able to do a checkout of the main branch\n> > of a bare repo, this patch should do:\n> > \n> --snip--\n> \n> You're fast :) I'm still studying  8d9fdd7 (worktree.c: check whether\n> branch is rebased in another worktree - 2016-04-22). But yeah that\n> should fix it.\n\nOK, so here it is as a proper patch.\n\nD.\n\ndiff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\nindex 4bcc335..2996c38 100755\n--- a/t/t2025-worktree-add.sh\n+++ b/t/t2025-worktree-add.sh\n@@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n \t)\n '\n \n++test_expect_success '\"add\" default branch of a bare repo' '\n+\t(\n+\t\tgit clone --bare . bare2 &&\n+\t\tcd bare2 &&\n+\t\tgit worktree add ../there3 master\n+\t)\n+'\n+\n test_expect_success 'checkout with grafts' '\n \ttest_when_finished rm .git/info/grafts &&\n \ttest_commit abc &&\ndiff --git a/worktree.c b/worktree.c\nindex 5acfe4c..35e95b7 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,\n \n \tfor (i = 0; worktrees[i]; i++) {\n \t\tstruct worktree *wt = worktrees[i];\n+\t\tif(wt->is_bare)\n+\t\t\tcontinue;\n \n \t\tif (wt->is_detached && !strcmp(symref, \"HEAD\")) {\n \t\t\tif (is_worktree_being_rebased(wt, target)) {\n-- \n2.10.1-356-g947a599\n\n\n-- \nDennis Kaarsemaker <dennis@kaarsemaker.net>\nhttp://twitter.com/seveas\n"},{"id":"304017","messageId":"CAOi_75JKPuOhL4BqE8nYkCAN9-wzDkD65TUvN6YDJWkB6HqSAA@mail.gmail.com","threadId":"44255","inReplyTo":"20161012164104.zcxpcwqrp5h65qmc@hurricane","subject":"Re: [PATCH] worktree: allow the main brach of a bare repository to be checked out","fromName":"Michael Tutty","fromEmail":"mtutty@gforgegroup.com","sentAt":"2016-10-12T17:35:56Z","receivedAt":"2016-10-12T18:01:27Z","isPatch":true,"sender":{"key":"mtutty@gforgegroup.com","avatar":"https://gravatar.com/avatar/81d83fba0cdbb4867c76c103ab581a3958579798c2dfe91541e89824681073a0?d=mp&s=160"},"body":"I have tested this successfully with git 2.10.1 built from source\n(failing), then patched as above (passing).\nThanks!\n     M.\n\n\nOn Wed, Oct 12, 2016 at 11:41 AM, Dennis Kaarsemaker\n<dennis@kaarsemaker.net> wrote:\n> In bare repositories, get_worktrees() still returns the main repository,\n> so git worktree list can show it. ignore it in find_shared_symref so we\n> can still check out the main branch.\n>\n> Signed-off-by: Dennis Kaarsemaker <dennis@kaarsemaker.net>\n> ---\n>  t/t2025-worktree-add.sh | 8 ++++++++\n>  worktree.c              | 2 ++\n>  2 files changed, 10 insertions(+)\n>\n> On Sun, 2016-10-09 at 17:52 +0700, Duy Nguyen wrote:\n>> On Sun, Oct 9, 2016 at 2:51 PM, Dennis Kaarsemaker > <dennis@kaarsemaker.net> wrote:\n>> > On Sat, 2016-10-08 at 19:30 -0500, Michael Tutty wrote:\n>> > >\n>> > > The only exception seems to be merging to master. When I do git\n>> > > worktree add /tmp/path/to/worktree master I get an error:\n>> > >\n>> > > [fatal: 'master' is already checked out at '/path/to/bare/repo']\n>> > >\n>> >\n>> > The worktree code treats the base repo as a worktree, even if it's\n>> > bare. For the purpose of being able to do a checkout of the main branch\n>> > of a bare repo, this patch should do:\n>> >\n>> --snip--\n>>\n>> You're fast :) I'm still studying  8d9fdd7 (worktree.c: check whether\n>> branch is rebased in another worktree - 2016-04-22). But yeah that\n>> should fix it.\n>\n> OK, so here it is as a proper patch.\n>\n> D.\n>\n> diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\n> index 4bcc335..2996c38 100755\n> --- a/t/t2025-worktree-add.sh\n> +++ b/t/t2025-worktree-add.sh\n> @@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n>         )\n>  '\n>\n> ++test_expect_success '\"add\" default branch of a bare repo' '\n> +       (\n> +               git clone --bare . bare2 &&\n> +               cd bare2 &&\n> +               git worktree add ../there3 master\n> +       )\n> +'\n> +\n>  test_expect_success 'checkout with grafts' '\n>         test_when_finished rm .git/info/grafts &&\n>         test_commit abc &&\n> diff --git a/worktree.c b/worktree.c\n> index 5acfe4c..35e95b7 100644\n> --- a/worktree.c\n> +++ b/worktree.c\n> @@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,\n>\n>         for (i = 0; worktrees[i]; i++) {\n>                 struct worktree *wt = worktrees[i];\n> +               if(wt->is_bare)\n> +                       continue;\n>\n>                 if (wt->is_detached && !strcmp(symref, \"HEAD\")) {\n>                         if (is_worktree_being_rebased(wt, target)) {\n> --\n> 2.10.1-356-g947a599\n>\n>\n> --\n> Dennis Kaarsemaker <dennis@kaarsemaker.net>\n> http://twitter.com/seveas\n\n\n\n-- \nMichael Tutty, CTO\n\ne: mtutty@gforgegroup.com\nt: @mtutty, @gforgegroup\nv: 515-789-0772\nw: http://gforgegroup.com, http://gforge.com\n"},{"id":"304020","messageId":"xmqqfuo14dnr.fsf@gitster.mtv.corp.google.com","threadId":"44255","inReplyTo":"20161012164104.zcxpcwqrp5h65qmc@hurricane","subject":"Re: [PATCH] worktree: allow the main brach of a bare repository to be checked out","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-12T18:37:12Z","receivedAt":"2016-10-12T18:37:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dennis Kaarsemaker <dennis@kaarsemaker.net> writes:\n\n> OK, so here it is as a proper patch.\n>\n> D.\n>\n> diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\n> index 4bcc335..2996c38 100755\n> --- a/t/t2025-worktree-add.sh\n> +++ b/t/t2025-worktree-add.sh\n> @@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n>  \t)\n>  '\n>  \n> ++test_expect_success '\"add\" default branch of a bare repo' '\n\nHuh?\n\n> +\t(\n> +\t\tgit clone --bare . bare2 &&\n> +\t\tcd bare2 &&\n> +\t\tgit worktree add ../there3 master\n> +\t)\n> +'\n> +\n>  test_expect_success 'checkout with grafts' '\n>  \ttest_when_finished rm .git/info/grafts &&\n>  \ttest_commit abc &&\n> diff --git a/worktree.c b/worktree.c\n> index 5acfe4c..35e95b7 100644\n> --- a/worktree.c\n> +++ b/worktree.c\n> @@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,\n>  \n>  \tfor (i = 0; worktrees[i]; i++) {\n>  \t\tstruct worktree *wt = worktrees[i];\n> +\t\tif(wt->is_bare)\n> +\t\t\tcontinue;\n>  \n>  \t\tif (wt->is_detached && !strcmp(symref, \"HEAD\")) {\n>  \t\t\tif (is_worktree_being_rebased(wt, target)) {\n> -- \n> 2.10.1-356-g947a599\n"},{"id":"304021","messageId":"xmqqbmyp4d28.fsf@gitster.mtv.corp.google.com","threadId":"44255","inReplyTo":"xmqqfuo14dnr.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH] worktree: allow the main brach of a bare repository to be checked out","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-12T18:50:07Z","receivedAt":"2016-10-12T18:51:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Dennis Kaarsemaker <dennis@kaarsemaker.net> writes:\n>\n>> OK, so here it is as a proper patch.\n\nHere is what I queued.  Duy, what do you think?  It seems OK to me.\n\nThanks.\n\n-- >8 --\nFrom: Dennis Kaarsemaker <dennis@kaarsemaker.net>\nDate: Wed, 12 Oct 2016 18:41:07 +0200\nSubject: [PATCH] worktree: allow the main brach of a bare repository to be\n checked out\n\nIn bare repositories, get_worktrees() still returns the main repository,\nso git worktree list can show it. ignore it in find_shared_symref so we\ncan still check out the main branch.\n\nSigned-off-by: Dennis Kaarsemaker <dennis@kaarsemaker.net>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t2025-worktree-add.sh | 8 ++++++++\n worktree.c              | 2 ++\n 2 files changed, 10 insertions(+)\n\ndiff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh\nindex 4bcc335a19..b618d6be21 100755\n--- a/t/t2025-worktree-add.sh\n+++ b/t/t2025-worktree-add.sh\n@@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without \"add\"' '\n \t)\n '\n \n+test_expect_success '\"add\" default branch of a bare repo' '\n+\t(\n+\t\tgit clone --bare . bare2 &&\n+\t\tcd bare2 &&\n+\t\tgit worktree add ../there3 master\n+\t)\n+'\n+\n test_expect_success 'checkout with grafts' '\n \ttest_when_finished rm .git/info/grafts &&\n \ttest_commit abc &&\ndiff --git a/worktree.c b/worktree.c\nindex 5acfe4cd64..f7869f8d60 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,\n \n \tfor (i = 0; worktrees[i]; i++) {\n \t\tstruct worktree *wt = worktrees[i];\n+\t\tif (wt->is_bare)\n+\t\t\tcontinue;\n \n \t\tif (wt->is_detached && !strcmp(symref, \"HEAD\")) {\n \t\t\tif (is_worktree_being_rebased(wt, target)) {\n-- \n2.10.1-591-g271c03b70f\n\n"},{"id":"304047","messageId":"1476300189.3876.18.camel@kaarsemaker.net","threadId":"44255","inReplyTo":"xmqqfuo14dnr.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH] worktree: allow the main brach of a bare repository to be checked out","fromName":"Dennis Kaarsemaker","fromEmail":"dennis@kaarsemaker.net","sentAt":"2016-10-12T19:23:09Z","receivedAt":"2016-10-12T23:37:53Z","isPatch":true,"sender":{"key":"dennis@kaarsemaker.net","avatar":"https://avatars.githubusercontent.com/u/200649?v=4"},"body":"On Wed, 2016-10-12 at 11:37 -0700, Junio C Hamano wrote:\n> > ++test_expect_success '\"add\" default branch of a bare repo' '\n> \n> Huh?\n\nCopy paste error. And I missed\n\nok 17 - checkout from a bare repo without \"add\"\n./t2025-worktree-add.sh: 141: ./t2025-worktree-add.sh: +test_expect_success: not found\n\nin the output of 'make test'. Thanks for fixing up!\n\nD.\n"},{"id":"304067","messageId":"CACsJy8BUhASP0t7bm1_zGttSY+V5PuLGNBRXdiNK4x0Bdib9Ng@mail.gmail.com","threadId":"44255","inReplyTo":"xmqqbmyp4d28.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH] worktree: allow the main brach of a bare repository to be checked out","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-10-13T10:31:07Z","receivedAt":"2016-10-13T10:32:00Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, Oct 13, 2016 at 1:50 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n>> Dennis Kaarsemaker <dennis@kaarsemaker.net> writes:\n>>\n>>> OK, so here it is as a proper patch.\n>\n> Here is what I queued.  Duy, what do you think?  It seems OK to me.\n\nAck. Thanks both.\n-- \nDuy\n"}]}