{"thread":{"id":"62494","subject":"[PATCH] worktree: detect from secondary worktree if main worktree is bare","startedAt":"2024-11-15T06:52:31Z","lastAt":"2025-02-05T06:30:18Z","messageCount":19,"participants":["Olga Pilipenco via GitGitGadget","Eric Sunshine","Olga Pilipenco","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"507318","messageId":"pull.1829.git.1731653548549.gitgitgadget@gmail.com","threadId":"62494","inReplyTo":null,"subject":"[PATCH] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-11-15T06:52:28Z","receivedAt":"2024-11-15T06:52:31Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"From: Olga Pilipenco <olga.pilipenco@shopify.com>\n\nSetup:\n1. Have a bare repo with core.bare = true in config.worktree\n2. Create a new worktree\n\nBehavior:\nFrom the secondary worktree the main worktree appears as non-bare.\n\nExpected:\nFrom the secondary worktree the main worktree should appear as bare.\n\nWhy current behavior is not good?\nIf the main worktree is detected as not bare it doesn't allow\nchecking out the branch of the main worktree. There are possibly\nother problems associated with that behavior.\n\nWhy is it happening?\nWhile we're inside the secondary worktree we don't initialize the main\nworktree's repository with its configuration.\n\nHow is it fixed?\nLoad actual configs of the main worktree. Also, skip the config loading\nstep if we're already inside the current worktree because in that case we\nrely on is_bare_repository() to return the correct result.\n\nOther solutions considered:\nAlternatively, instead of incorrectly always using\n`the_repository` as the main worktree's repository, we can detect\nand load the actual repository of the main worktree and then use\nthat repository's `is_bare` value extracted from correct configs.\nHowever, this approach is a bit riskier and could also affect\nperformance. Since we had the assignment `worktree->repo =\nthe_repository` for a long time already, I decided it's safe to\nkeep it as it is for now; it can be still fixed separately from\nthis change.\n\nReal life use case:\n1. Have a bare repo\n2. Create a worktree from the bare repo\n3. In the secondary worktree enable sparse-checkout - this enables\nextensions.worktreeConfig and keeps core.bare=true setting in\nconfig.worktree of the bare worktree\n4. The secondary worktree or any other non-bare worktree created\nwon't be able to use branch main (not even once), but it should be\nable to.\n\nSigned-off-by: Olga Pilipenco <olga.pilipenco@shopify.com>\n---\n    worktree: detect from secondary worktree if main worktree is bare\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1829%2Folga-mcbfe%2Ffix-bare-repo-detection-with-worktree-config-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1829/olga-mcbfe/fix-bare-repo-detection-with-worktree-config-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1829\n\n t/t3200-branch.sh | 14 ++++++++++++++\n worktree.c        | 38 +++++++++++++++++++++++++++++---------\n 2 files changed, 43 insertions(+), 9 deletions(-)\n\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex ccfa6a720d0..819228a3344 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -411,6 +411,20 @@ test_expect_success 'bare main worktree has HEAD at branch deleted by secondary\n \tgit -C secondary branch -D main\n '\n \n+test_expect_success 'secondary worktree can switch to main if common dir is bare worktree' '\n+\ttest_when_finished \"rm -rf bare_repo non_bare_repo secondary_worktree\" &&\n+\tgit init -b main non_bare_repo &&\n+\ttest_commit -C non_bare_repo x &&\n+\n+\tgit clone --bare non_bare_repo bare_repo &&\n+\tgit -C bare_repo config extensions.worktreeConfig true &&\n+\tgit -C bare_repo config unset core.bare &&\n+\tgit -C bare_repo config --worktree core.bare true &&\n+\n+\tgit -C bare_repo worktree add ../secondary_worktree &&\n+\tgit -C secondary_worktree checkout main\n+'\n+\n test_expect_success 'git branch --list -v with --abbrev' '\n \ttest_when_finished \"git branch -D t\" &&\n \tgit branch t &&\ndiff --git a/worktree.c b/worktree.c\nindex 77ff484d3ec..1bce505bdd7 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -64,6 +64,28 @@ static int is_current_worktree(struct worktree *wt)\n \treturn is_current;\n }\n \n+static int is_bare_git_dir(const char *git_dir)\n+{\n+\tint bare = 0;\n+\tstruct config_set cs = { { 0 } };\n+\tchar *config_file;\n+\tchar *worktree_config_file;\n+\n+\tconfig_file = xstrfmt(\"%s/config\", git_dir);\n+\tworktree_config_file = xstrfmt(\"%s/config.worktree\",  git_dir);\n+\n+\tgit_configset_init(&cs);\n+\tgit_configset_add_file(&cs, config_file);\n+\tgit_configset_add_file(&cs, worktree_config_file);\n+\n+\tgit_configset_get_bool(&cs, \"core.bare\", &bare);\n+\n+\tgit_configset_clear(&cs);\n+\tfree(config_file);\n+\tfree(worktree_config_file);\n+\treturn bare;\n+}\n+\n /**\n  * get the main worktree\n  */\n@@ -76,18 +98,16 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n \tstrbuf_strip_suffix(&worktree_path, \"/.git\");\n \n \tCALLOC_ARRAY(worktree, 1);\n+\t/*\n+\t * NEEDSWORK: the_repository is not always main worktree's repository\n+\t*/\n \tworktree->repo = the_repository;\n \tworktree->path = strbuf_detach(&worktree_path, NULL);\n-\t/*\n-\t * NEEDSWORK: If this function is called from a secondary worktree and\n-\t * config.worktree is present, is_bare_repository_cfg will reflect the\n-\t * contents of config.worktree, not the contents of the main worktree.\n-\t * This means that worktree->is_bare may be set to 0 even if the main\n-\t * worktree is configured to be bare.\n-\t */\n-\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n-\t\tis_bare_repository();\n \tworktree->is_current = is_current_worktree(worktree);\n+\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n+\t\tis_bare_repository() ||\n+\t\t(!worktree->is_current && is_bare_git_dir(repo_get_common_dir(the_repository)));\n+\n \tif (!skip_reading_head)\n \t\tadd_head_info(worktree);\n \treturn worktree;\n\nbase-commit: 25b0f41288718625b18495de23cc066394c09a92\n-- \ngitgitgadget\n"},{"id":"510733","messageId":"pull.1829.v2.git.1737063335673.gitgitgadget@gmail.com","threadId":"62494","inReplyTo":"pull.1829.git.1731653548549.gitgitgadget@gmail.com","subject":"[PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-01-16T21:35:35Z","receivedAt":"2025-01-16T21:35:39Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"From: Olga Pilipenco <olga.pilipenco@shopify.com>\n\nSetup:\n1. Have a bare repo with core.bare = true in config.worktree\n2. Create a new worktree\n\nBehavior:\nFrom the secondary worktree the main worktree appears as non-bare.\n\nExpected:\nFrom the secondary worktree the main worktree should appear as bare.\n\nWhy current behavior is not good?\nIf the main worktree is detected as not bare it doesn't allow\nchecking out the branch of the main worktree. There are possibly\nother problems associated with that behavior.\n\nWhy is it happening?\nWhile we're inside the secondary worktree we don't initialize the main\nworktree's repository with its configuration.\n\nHow is it fixed?\nLoad actual configs of the main worktree. Also, skip the config loading\nstep if we're already inside the current worktree because in that case we\nrely on is_bare_repository() to return the correct result.\n\nOther solutions considered:\nAlternatively, instead of incorrectly always using\n`the_repository` as the main worktree's repository, we can detect\nand load the actual repository of the main worktree and then use\nthat repository's `is_bare` value extracted from correct configs.\nHowever, this approach is a bit riskier and could also affect\nperformance. Since we had the assignment `worktree->repo =\nthe_repository` for a long time already, I decided it's safe to\nkeep it as it is for now; it can be still fixed separately from\nthis change.\n\nReal life use case:\n1. Have a bare repo\n2. Create a worktree from the bare repo\n3. In the secondary worktree enable sparse-checkout - this enables\nextensions.worktreeConfig and keeps core.bare=true setting in\nconfig.worktree of the bare worktree\n4. The secondary worktree or any other non-bare worktree created\nwon't be able to use branch main (not even once), but it should be\nable to.\n\nSigned-off-by: Olga Pilipenco <olga.pilipenco@shopify.com>\n---\n    worktree: detect from secondary worktree if main worktree is bare\n    \n    Changes since v1:\n    \n     * no code changes\n     * rebased with maint\n     * CC added\n    \n    Existing broken functionality forces our project to use hacks on bare\n    repo that we'd like to avoid. I would really appreciate reviews of this\n    patch to move closer towards fixing the issue. This is my first\n    contribution to git/git, I apologize if I got lost in the instructions,\n    but I tried my best to follow the rules.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1829%2Folga-mcbfe%2Ffix-bare-repo-detection-with-worktree-config-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1829/olga-mcbfe/fix-bare-repo-detection-with-worktree-config-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/1829\n\nRange-diff vs v1:\n\n 1:  9e7170f07fc = 1:  17f4b24d1da worktree: detect from secondary worktree if main worktree is bare\n\n\n t/t3200-branch.sh | 14 ++++++++++++++\n worktree.c        | 38 +++++++++++++++++++++++++++++---------\n 2 files changed, 43 insertions(+), 9 deletions(-)\n\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex a3a21c54cf6..7ca50c9a78d 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -410,6 +410,20 @@ test_expect_success 'bare main worktree has HEAD at branch deleted by secondary\n \tgit -C secondary branch -D main\n '\n \n+test_expect_success 'secondary worktree can switch to main if common dir is bare worktree' '\n+\ttest_when_finished \"rm -rf bare_repo non_bare_repo secondary_worktree\" &&\n+\tgit init -b main non_bare_repo &&\n+\ttest_commit -C non_bare_repo x &&\n+\n+\tgit clone --bare non_bare_repo bare_repo &&\n+\tgit -C bare_repo config extensions.worktreeConfig true &&\n+\tgit -C bare_repo config unset core.bare &&\n+\tgit -C bare_repo config --worktree core.bare true &&\n+\n+\tgit -C bare_repo worktree add ../secondary_worktree &&\n+\tgit -C secondary_worktree checkout main\n+'\n+\n test_expect_success 'git branch --list -v with --abbrev' '\n \ttest_when_finished \"git branch -D t\" &&\n \tgit branch t &&\ndiff --git a/worktree.c b/worktree.c\nindex 248bbb39d43..a7726c67747 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -65,6 +65,28 @@ static int is_current_worktree(struct worktree *wt)\n \treturn is_current;\n }\n \n+static int is_bare_git_dir(const char *git_dir)\n+{\n+\tint bare = 0;\n+\tstruct config_set cs = { { 0 } };\n+\tchar *config_file;\n+\tchar *worktree_config_file;\n+\n+\tconfig_file = xstrfmt(\"%s/config\", git_dir);\n+\tworktree_config_file = xstrfmt(\"%s/config.worktree\",  git_dir);\n+\n+\tgit_configset_init(&cs);\n+\tgit_configset_add_file(&cs, config_file);\n+\tgit_configset_add_file(&cs, worktree_config_file);\n+\n+\tgit_configset_get_bool(&cs, \"core.bare\", &bare);\n+\n+\tgit_configset_clear(&cs);\n+\tfree(config_file);\n+\tfree(worktree_config_file);\n+\treturn bare;\n+}\n+\n /**\n  * get the main worktree\n  */\n@@ -77,18 +99,16 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n \tstrbuf_strip_suffix(&worktree_path, \"/.git\");\n \n \tCALLOC_ARRAY(worktree, 1);\n+\t/*\n+\t * NEEDSWORK: the_repository is not always main worktree's repository\n+\t*/\n \tworktree->repo = the_repository;\n \tworktree->path = strbuf_detach(&worktree_path, NULL);\n-\t/*\n-\t * NEEDSWORK: If this function is called from a secondary worktree and\n-\t * config.worktree is present, is_bare_repository_cfg will reflect the\n-\t * contents of config.worktree, not the contents of the main worktree.\n-\t * This means that worktree->is_bare may be set to 0 even if the main\n-\t * worktree is configured to be bare.\n-\t */\n-\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n-\t\tis_bare_repository();\n \tworktree->is_current = is_current_worktree(worktree);\n+\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n+\t\tis_bare_repository() ||\n+\t\t(!worktree->is_current && is_bare_git_dir(repo_get_common_dir(the_repository)));\n+\n \tif (!skip_reading_head)\n \t\tadd_head_info(worktree);\n \treturn worktree;\n\nbase-commit: f93ff170b93a1782659637824b25923245ac9dd1\n-- \ngitgitgadget\n"},{"id":"510913","messageId":"CAPig+cRpKKpVHT8x6nOx1KNjWR=hywz-nHZga9fhiXMXD7KOSw@mail.gmail.com","threadId":"62494","inReplyTo":"pull.1829.v2.git.1737063335673.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2025-01-19T22:30:05Z","receivedAt":"2025-01-19T22:30:17Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 16, 2025 at 4:35 PM Olga Pilipenco via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n> Setup:\n> 1. Have a bare repo with core.bare = true in config.worktree\n> 2. Create a new worktree\n>\n> Behavior:\n> From the secondary worktree the main worktree appears as non-bare.\n>\n> Expected:\n> From the secondary worktree the main worktree should appear as bare.\n>\n> Why current behavior is not good?\n> If the main worktree is detected as not bare it doesn't allow\n> checking out the branch of the main worktree. There are possibly\n> other problems associated with that behavior.\n>\n> Why is it happening?\n> While we're inside the secondary worktree we don't initialize the main\n> worktree's repository with its configuration.\n\nOkay, this is clearly a very real problem and explains this comment\nadded by f3534c98e4 (worktree: update is_bare heuristics, 2019-04-19):\n\n    NEEDSWORK: If this function is called from a secondary worktree and\n    config.worktree is present, is_bare_repository_cfg will reflect the\n    contents of config.worktree, not the contents of the main worktree.\n    This means that worktree->is_bare may be set to 0 even if the main\n    worktree is configured to be bare.\n\n(Aside: I recall reading this comment when Jonathan added it but\nwasn't able to dig into it at the time to really understand it, and\nnever got back around to it. Now, after studying your patch, I\nunderstand what it was about.\n\n> How is it fixed?\n> Load actual configs of the main worktree. Also, skip the config loading\n> step if we're already inside the current worktree because in that case we\n> rely on is_bare_repository() to return the correct result.\n\nI found that I had to dig around a bit to fully understand the problem\nexpressed by this commit message. Perhaps adding a bit more detail\nwould help? Here's my attempt at rewriting the above (also in a way\nwhich is more idiomatic to this project):\n\n    When extensions.worktreeConfig is true and the main worktree is\n    bare -- that is, its config.worktree file contains core.bare=true\n    -- commands run from secondary worktrees incorrectly see the main\n    worktree as not bare. As such, those commands incorrectly think\n    that the repository's default branch (typically \"main\" or\n    \"master\") is checked out in the bare repository even though it's\n    not. This makes it impossible, for instance, to checkout or delete\n    the default branch from a secondary worktree, among other\n    shortcomings.\n\n    This problem occurs because, when extensions.worktreeConfig is\n    true, commands run in secondary worktrees only consult\n    $commondir/config and $commondir/worktrees/<id>/config.worktree,\n    thus they never see the main worktree's core.bare=true setting in\n    $commondir/config.worktree.\n\n    Fix this problem by consulting the main worktree's config.worktree\n    file when checking whether it is bare. (This extra work is\n    performed only when running from a secondary worktree.)\n\n> Other solutions considered:\n> Alternatively, instead of incorrectly always using\n> `the_repository` as the main worktree's repository, we can detect\n> and load the actual repository of the main worktree and then use\n> that repository's `is_bare` value extracted from correct configs.\n> However, this approach is a bit riskier and could also affect\n> performance. Since we had the assignment `worktree->repo =\n> the_repository` for a long time already, I decided it's safe to\n> keep it as it is for now; it can be still fixed separately from\n> this change.\n\nI found this paragraph somewhat confusing because it seems to conflate\na repository (i.e. the shared object database) with the `struct\nrepository` type, and the configuration which happens to get loaded\nand stored (as one of *many* members) of the repository structure. I\nhad to read it several times to understand that this was talking about\ninstantiating a separate `struct repository` initialized from the main\nworktree configuration. I agree that doing so would likely be overkill\nand could impact performance negatively. I understand that you added\nthis paragraph because SubmittingPatches suggests to do so, but I\nthink it can probably be omitted in this case unless it can be\nrewritten to be more clear (but even then I doubt it is necessary to\nkeep it).\n\n> Real life use case:\n> 1. Have a bare repo\n> 2. Create a worktree from the bare repo\n> 3. In the secondary worktree enable sparse-checkout - this enables\n> extensions.worktreeConfig and keeps core.bare=true setting in\n> config.worktree of the bare worktree\n> 4. The secondary worktree or any other non-bare worktree created\n> won't be able to use branch main (not even once), but it should be\n> able to.\n\nThis is mostly repeating what was said earlier, thus probably isn't\nadding any value to the commit message. I'd probably drop it.\n\n> Signed-off-by: Olga Pilipenco <olga.pilipenco@shopify.com>\n> ---\n>     Changes since v1:\n>\n>      * no code changes\n>      * rebased with maint\n>      * CC added\n\nSorry. I've had your v1 sitting in my ever-increasingly-large backlog\nof patches to look at, but have been extra busy the last many months\nand never managed to get to it.\n\n>     Existing broken functionality forces our project to use hacks on bare\n>     repo that we'd like to avoid. I would really appreciate reviews of this\n>     patch to move closer towards fixing the issue. This is my first\n>     contribution to git/git, I apologize if I got lost in the instructions,\n>     but I tried my best to follow the rules.\n\nYour submission is fine. Unfortunately, the project has a lack of\nreviewers but no lack of submitters, so sometimes patches get\noverlooked or simply buried.\n\n> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\n> @@ -410,6 +410,20 @@ test_expect_success 'bare main worktree has HEAD at branch deleted by secondary\n> +test_expect_success 'secondary worktree can switch to main if common dir is bare worktree' '\n\nThe use of \"common dir\" is a bit confusing. Also, this patch is fixing\nthe more general problem that secondary worktrees think that the bare\nmain worktree has a branch checked out. So, perhaps a better title\nwould be:\n\n    secondary worktrees recognize core.bare=true in main config.worktree\n\nor something?\n\n> +       test_when_finished \"rm -rf bare_repo non_bare_repo secondary_worktree\" &&\n> +       git init -b main non_bare_repo &&\n> +       test_commit -C non_bare_repo x &&\n> +\n> +       git clone --bare non_bare_repo bare_repo &&\n> +       git -C bare_repo config extensions.worktreeConfig true &&\n> +       git -C bare_repo config unset core.bare &&\n> +       git -C bare_repo config --worktree core.bare true &&\n> +\n> +       git -C bare_repo worktree add ../secondary_worktree &&\n> +       git -C secondary_worktree checkout main\n> +'\n\nVery straightforward and exactly what I expected to see once I\nunderstood the problem.\n\n> diff --git a/worktree.c b/worktree.c\n> @@ -65,6 +65,28 @@ static int is_current_worktree(struct worktree *wt)\n> +static int is_bare_git_dir(const char *git_dir)\n\nNit: I wonder if a name such as is_main_worktree_bare() would clue\nreaders in a bit more?\n\n> +{\n> +       int bare = 0;\n> +       struct config_set cs = { { 0 } };\n\nThis is not your fault since this construct is used elsewhere in this\nfile (from which I presume you copied it), but project consensus is\nthat using the notation `{{0}}` to work around a complaint from the\nApple compiler (and only the Apple compiler) should be avoided, and\nthat `{0}` is preferred. So, if you reroll, changing this to `{0}` may\nmake other reviewers happy (or you can leave it as is to be consistent\nwith existing precedence in this file; I don't feel strongly about\nit).\n\n> +       char *config_file;\n> +       char *worktree_config_file;\n> +\n> +       config_file = xstrfmt(\"%s/config\", git_dir);\n> +       worktree_config_file = xstrfmt(\"%s/config.worktree\",  git_dir);\n> +\n> +       git_configset_init(&cs);\n> +       git_configset_add_file(&cs, config_file);\n> +       git_configset_add_file(&cs, worktree_config_file);\n\nGenuine question: I haven't thought too deeply about it, but do we\ngain anything by loading $commondir/config here -- which is shared by\nthe main worktree and all secondary worktrees -- considering that it\nwas already loaded and consulted by the earlier is-bare check before\nthis function was even called?\n\n> +       git_configset_get_bool(&cs, \"core.bare\", &bare);\n> +\n> +       git_configset_clear(&cs);\n> +       free(config_file);\n> +       free(worktree_config_file);\n> +       return bare;\n\nEverything gets cleaned up correctly. Good.\n\n> @@ -77,18 +99,16 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n> +       /*\n> +        * NEEDSWORK: the_repository is not always main worktree's repository\n> +       */\n>         worktree->repo = the_repository;\n>         worktree->path = strbuf_detach(&worktree_path, NULL);\n\nI found this new NEEDSWORK comment rather confusing the first several\ntimes I read the patch. It wasn't until I finally realized that the\nreference to `the_repository` here is the same reference to\n`the_repository` in the commit message -- which confused me, as well\n-- that I understood what this was trying to say. The actual problem,\nof course, is that the _configuration_ stored in `the_repository` is\nthe secondary worktree's configuration, not the main worktree's\nconfiguration. Considering that this patch addresses that problem, I'd\nprobably just drop this new comment altogether (unless, perhaps, you\nrewrite it to talk about the _configuration_ stored in\n`the_repository`).\n\n> -       /*\n> -        * NEEDSWORK: If this function is called from a secondary worktree and\n> -        * config.worktree is present, is_bare_repository_cfg will reflect the\n> -        * contents of config.worktree, not the contents of the main worktree.\n> -        * This means that worktree->is_bare may be set to 0 even if the main\n> -        * worktree is configured to be bare.\n> -        */\n> -       worktree->is_bare = (is_bare_repository_cfg == 1) ||\n> -               is_bare_repository();\n>         worktree->is_current = is_current_worktree(worktree);\n> +       worktree->is_bare = (is_bare_repository_cfg == 1) ||\n> +               is_bare_repository() ||\n> +               (!worktree->is_current && is_bare_git_dir(repo_get_common_dir(the_repository)));\n\nThis is performing the expensive check only if the earlier checks left\nthe question unanswered. Good.\n"},{"id":"511371","messageId":"116C27A8-EF7B-42E1-9606-815FDA3CF94C@shopify.com","threadId":"62494","inReplyTo":"CAPig+cRpKKpVHT8x6nOx1KNjWR=hywz-nHZga9fhiXMXD7KOSw@mail.gmail.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco","fromEmail":"olga.pilipenco@shopify.com","sentAt":"2025-01-28T21:44:47Z","receivedAt":"2025-01-28T21:45:01Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"Thank you for the review, I totally understand the delay in the review process and appreciate your time spent on this.\n\n> On Jan 19, 2025, at 3:30 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> \n> On Thu, Jan 16, 2025 at 4:35 PM Olga Pilipenco via GitGitGadget\n> <gitgitgadget@gmail.com> wrote:\n>> Setup:\n>> 1. Have a bare repo with core.bare = true in config.worktree\n>> 2. Create a new worktree\n>> \n>> Behavior:\n>> From the secondary worktree the main worktree appears as non-bare.\n>> \n>> Expected:\n>> From the secondary worktree the main worktree should appear as bare.\n>> \n>> Why current behavior is not good?\n>> If the main worktree is detected as not bare it doesn't allow\n>> checking out the branch of the main worktree. There are possibly\n>> other problems associated with that behavior.\n>> \n>> Why is it happening?\n>> While we're inside the secondary worktree we don't initialize the main\n>> worktree's repository with its configuration.\n> \n> Okay, this is clearly a very real problem and explains this comment\n> added by f3534c98e4 (worktree: update is_bare heuristics, 2019-04-19):\n> \n>    NEEDSWORK: If this function is called from a secondary worktree and\n>    config.worktree is present, is_bare_repository_cfg will reflect the\n>    contents of config.worktree, not the contents of the main worktree.\n>    This means that worktree->is_bare may be set to 0 even if the main\n>    worktree is configured to be bare.\n> \n> (Aside: I recall reading this comment when Jonathan added it but\n> wasn't able to dig into it at the time to really understand it, and\n> never got back around to it. Now, after studying your patch, I\n> understand what it was about.\n> \n>> How is it fixed?\n>> Load actual configs of the main worktree. Also, skip the config loading\n>> step if we're already inside the current worktree because in that case we\n>> rely on is_bare_repository() to return the correct result.\n> \n> I found that I had to dig around a bit to fully understand the problem\n> expressed by this commit message. Perhaps adding a bit more detail\n> would help? Here's my attempt at rewriting the above (also in a way\n> which is more idiomatic to this project):\n> \n>    When extensions.worktreeConfig is true and the main worktree is\n>    bare -- that is, its config.worktree file contains core.bare=true\n>    -- commands run from secondary worktrees incorrectly see the main\n>    worktree as not bare. As such, those commands incorrectly think\n>    that the repository's default branch (typically \"main\" or\n>    \"master\") is checked out in the bare repository even though it's\n>    not. This makes it impossible, for instance, to checkout or delete\n>    the default branch from a secondary worktree, among other\n>    shortcomings.\n> \n>    This problem occurs because, when extensions.worktreeConfig is\n>    true, commands run in secondary worktrees only consult\n>    $commondir/config and $commondir/worktrees/<id>/config.worktree,\n>    thus they never see the main worktree's core.bare=true setting in\n>    $commondir/config.worktree.\n> \n>    Fix this problem by consulting the main worktree's config.worktree\n>    file when checking whether it is bare. (This extra work is\n>    performed only when running from a secondary worktree.)\n\n\nWow, your explanation is so much better than mine.Thank you for “translating\" it for the world :) I’m still trying to get used to the terminology used in this codebase.\nI’ll steal your description for sure (if you don’t mind).\n\n> \n>> Other solutions considered:\n>> Alternatively, instead of incorrectly always using\n>> `the_repository` as the main worktree's repository, we can detect\n>> and load the actual repository of the main worktree and then use\n>> that repository's `is_bare` value extracted from correct configs.\n>> However, this approach is a bit riskier and could also affect\n>> performance. Since we had the assignment `worktree->repo =\n>> the_repository` for a long time already, I decided it's safe to\n>> keep it as it is for now; it can be still fixed separately from\n>> this change.\n> \n> I found this paragraph somewhat confusing because it seems to conflate\n> a repository (i.e. the shared object database) with the `struct\n> repository` type, and the configuration which happens to get loaded\n> and stored (as one of *many* members) of the repository structure. I\n> had to read it several times to understand that this was talking about\n> instantiating a separate `struct repository` initialized from the main\n> worktree configuration. I agree that doing so would likely be overkill\n> and could impact performance negatively. I understand that you added\n> this paragraph because SubmittingPatches suggests to do so, but I\n> think it can probably be omitted in this case unless it can be\n> rewritten to be more clear (but even then I doubt it is necessary to\n> keep it).\n\nTrust me, it took me a while to wrap my head around `struct repository` as well.\nI agree if the explanation is too confusing and doesn’t bring any value, it can be omitted.\n\n> \n>> Real life use case:\n>> 1. Have a bare repo\n>> 2. Create a worktree from the bare repo\n>> 3. In the secondary worktree enable sparse-checkout - this enables\n>> extensions.worktreeConfig and keeps core.bare=true setting in\n>> config.worktree of the bare worktree\n>> 4. The secondary worktree or any other non-bare worktree created\n>> won't be able to use branch main (not even once), but it should be\n>> able to.\n> \n> This is mostly repeating what was said earlier, thus probably isn't\n> adding any value to the commit message. I'd probably drop it.\n\nI agree, your improved description captures this scenario perfectly.\n\n> \n>> Signed-off-by: Olga Pilipenco <olga.pilipenco@shopify.com>\n>> ---\n>>    Changes since v1:\n>> \n>>     * no code changes\n>>     * rebased with maint\n>>     * CC added\n> \n> Sorry. I've had your v1 sitting in my ever-increasingly-large backlog\n> of patches to look at, but have been extra busy the last many months\n> and never managed to get to it.\n\nTotally understand. Thanks again for getting to it eventually.\n\n> \n>>    Existing broken functionality forces our project to use hacks on bare\n>>    repo that we'd like to avoid. I would really appreciate reviews of this\n>>    patch to move closer towards fixing the issue. This is my first\n>>    contribution to git/git, I apologize if I got lost in the instructions,\n>>    but I tried my best to follow the rules.\n> \n> Your submission is fine. Unfortunately, the project has a lack of\n> reviewers but no lack of submitters, so sometimes patches get\n> overlooked or simply buried.\n> \n>> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\n>> @@ -410,6 +410,20 @@ test_expect_success 'bare main worktree has HEAD at branch deleted by secondary\n>> +test_expect_success 'secondary worktree can switch to main if common dir is bare worktree' '\n> \n> The use of \"common dir\" is a bit confusing. Also, this patch is fixing\n> the more general problem that secondary worktrees think that the bare\n> main worktree has a branch checked out. So, perhaps a better title\n> would be:\n> \n>    secondary worktrees recognize core.bare=true in main config.worktree\n> \n> or something?\n\nSounds good, will update.\n\n> \n>> +       test_when_finished \"rm -rf bare_repo non_bare_repo secondary_worktree\" &&\n>> +       git init -b main non_bare_repo &&\n>> +       test_commit -C non_bare_repo x &&\n>> +\n>> +       git clone --bare non_bare_repo bare_repo &&\n>> +       git -C bare_repo config extensions.worktreeConfig true &&\n>> +       git -C bare_repo config unset core.bare &&\n>> +       git -C bare_repo config --worktree core.bare true &&\n>> +\n>> +       git -C bare_repo worktree add ../secondary_worktree &&\n>> +       git -C secondary_worktree checkout main\n>> +'\n> \n> Very straightforward and exactly what I expected to see once I\n> understood the problem.\n> \n>> diff --git a/worktree.c b/worktree.c\n>> @@ -65,6 +65,28 @@ static int is_current_worktree(struct worktree *wt)\n>> +static int is_bare_git_dir(const char *git_dir)\n> \n> Nit: I wonder if a name such as is_main_worktree_bare() would clue\n> readers in a bit more?\n\nI was about to explain how I wanted this function to be more generic and handle all sorts of bare and non-bare cases - whether it’s the main worktree or not. However, after seeing your comments and after revisiting the code, I realized that generalization doesn’t really provide much benefit here. It is much clearer if we're explicit that the bare check in this case is only performed on the main worktree. I’ll update it in the next version.\n\n> \n>> +{\n>> +       int bare = 0;\n>> +       struct config_set cs = { { 0 } };\n> \n> This is not your fault since this construct is used elsewhere in this\n> file (from which I presume you copied it), but project consensus is\n> that using the notation `{{0}}` to work around a complaint from the\n> Apple compiler (and only the Apple compiler) should be avoided, and\n> that `{0}` is preferred. So, if you reroll, changing this to `{0}` may\n> make other reviewers happy (or you can leave it as is to be consistent\n> with existing precedence in this file; I don't feel strongly about\n> it).\n\nI’ll fix it, sounds like a good reason.\n\n> \n>> +       char *config_file;\n>> +       char *worktree_config_file;\n>> +\n>> +       config_file = xstrfmt(\"%s/config\", git_dir);\n>> +       worktree_config_file = xstrfmt(\"%s/config.worktree\",  git_dir);\n>> +\n>> +       git_configset_init(&cs);\n>> +       git_configset_add_file(&cs, config_file);\n>> +       git_configset_add_file(&cs, worktree_config_file);\n> \n> Genuine question: I haven't thought too deeply about it, but do we\n> gain anything by loading $commondir/config here -- which is shared by\n> the main worktree and all secondary worktrees -- considering that it\n> was already loaded and consulted by the earlier is-bare check before\n> this function was even called?\n\nThis function determines if a worktree is bare or not. I want this logic to work even when it’s called from a different context and not rely on other is-bare checks (that are a bit confusing tbh).\n\n> \n>> +       git_configset_get_bool(&cs, \"core.bare\", &bare);\n>> +\n>> +       git_configset_clear(&cs);\n>> +       free(config_file);\n>> +       free(worktree_config_file);\n>> +       return bare;\n> \n> Everything gets cleaned up correctly. Good.\n> \n>> @@ -77,18 +99,16 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n>> +       /*\n>> +        * NEEDSWORK: the_repository is not always main worktree's repository\n>> +       */\n>>        worktree->repo = the_repository;\n>>        worktree->path = strbuf_detach(&worktree_path, NULL);\n> \n> I found this new NEEDSWORK comment rather confusing the first several\n> times I read the patch. It wasn't until I finally realized that the\n> reference to `the_repository` here is the same reference to\n> `the_repository` in the commit message -- which confused me, as well\n> -- that I understood what this was trying to say. The actual problem,\n> of course, is that the _configuration_ stored in `the_repository` is\n> the secondary worktree's configuration, not the main worktree's\n> configuration. Considering that this patch addresses that problem, I'd\n> probably just drop this new comment altogether (unless, perhaps, you\n> rewrite it to talk about the _configuration_ stored in\n> `the_repository`).\n\nThis `the_repository` structure is soooo confusing, took me a while to figure out what it is! I would feel guilty not mentioning that under some circumstances `the_repository` assigned here could be not actual configuration of the worktree object. I don’t know if that will ever matter or not, but I find this assignment kinda “stinky” and want everyone to know about it. I don’t want to change this assignment in this patch because it didn’t bring any harm so far. I’ll try again to rephrase this comment, just to give a heads up in case someone experiences “weird” behaviour in this area (same way the previous NEEDSWORK comment gave me ideas why my workflow didn’t work and inspired me to try to fix it).\n\n> \n>> -       /*\n>> -        * NEEDSWORK: If this function is called from a secondary worktree and\n>> -        * config.worktree is present, is_bare_repository_cfg will reflect the\n>> -        * contents of config.worktree, not the contents of the main worktree.\n>> -        * This means that worktree->is_bare may be set to 0 even if the main\n>> -        * worktree is configured to be bare.\n>> -        */\n>> -       worktree->is_bare = (is_bare_repository_cfg == 1) ||\n>> -               is_bare_repository();\n>>        worktree->is_current = is_current_worktree(worktree);\n>> +       worktree->is_bare = (is_bare_repository_cfg == 1) ||\n>> +               is_bare_repository() ||\n>> +               (!worktree->is_current && is_bare_git_dir(repo_get_common_dir(the_repository)));\n> \n> This is performing the expensive check only if the earlier checks left\n> the question unanswered. Good.\n\nThanks for the review. I’ll incorporate the changes in my next version and hopefully it will be good to go :tada:\nI hope I responded to all the comments, it’s a bit nerve-wrecking to contribute for the first time (so many rules and instructions!) :)\n\n"},{"id":"511408","messageId":"CAPig+cSdbjzTmsBOmFnMxzYLGrUzY46=mkW9S+si2KxLhS623Q@mail.gmail.com","threadId":"62494","inReplyTo":"116C27A8-EF7B-42E1-9606-815FDA3CF94C@shopify.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2025-01-29T13:41:54Z","receivedAt":"2025-01-29T13:42:06Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Jan 28, 2025 at 4:45 PM Olga Pilipenco\n<olga.pilipenco@shopify.com> wrote:\n> > On Jan 19, 2025, at 3:30 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> > On Thu, Jan 16, 2025 at 4:35 PM Olga Pilipenco via GitGitGadget\n> > <gitgitgadget@gmail.com> wrote:\n> > I found that I had to dig around a bit to fully understand the problem\n> > expressed by this commit message. Perhaps adding a bit more detail\n> > would help? Here's my attempt at rewriting the above (also in a way\n> > which is more idiomatic to this project):\n> >\n> >  When extensions.worktreeConfig is true and the main worktree is\n> >  bare -- that is, its config.worktree file contains core.bare=true\n> >  -- commands run from secondary worktrees incorrectly see the main\n> >  worktree as not bare. As such, those commands incorrectly think\n> >  that the repository's default branch (typically \"main\" or\n> >  \"master\") is checked out in the bare repository even though it's\n> >  not. This makes it impossible, for instance, to checkout or delete\n> >  the default branch from a secondary worktree, among other\n> >  shortcomings.\n> >\n> >  This problem occurs because, when extensions.worktreeConfig is\n> >  true, commands run in secondary worktrees only consult\n> >  $commondir/config and $commondir/worktrees/<id>/config.worktree,\n> >  thus they never see the main worktree's core.bare=true setting in\n> >  $commondir/config.worktree.\n> >\n> >  Fix this problem by consulting the main worktree's config.worktree\n> >  file when checking whether it is bare. (This extra work is\n> >  performed only when running from a secondary worktree.)\n>\n> Wow, your explanation is so much better than mine.Thank you for\n> “translating\" it for the world :) I’m still trying to get used to\n> the terminology used in this codebase.  I’ll steal your description\n> for sure (if you don’t mind).\n\nYou are more than welcome to use the proposed commit message rewrite.\n\n(If you want to acknowledge assistance rendered, a Helped-by: trailer,\npreceding your Signed-off-by:, is the way to do so. Or not. It's up to\nyou.)\n\n> >> diff --git a/worktree.c b/worktree.c\n> >> @@ -65,6 +65,28 @@ static int is_current_worktree(struct worktree *wt)\n> >> +static int is_bare_git_dir(const char *git_dir)\n> >\n> > Nit: I wonder if a name such as is_main_worktree_bare() would clue\n> > readers in a bit more?\n>\n> I was about to explain how I wanted this function to be more generic\n> and handle all sorts of bare and non-bare cases - whether it’s the\n> main worktree or not. However, after seeing your comments and after\n> revisiting the code, I realized that generalization doesn’t really\n> provide much benefit here. It is much clearer if we're explicit that\n> the bare check in this case is only performed on the main\n> worktree. I’ll update it in the next version.\n\nI see. When reviewing, I was wondering why the git-dir was being\npassed into the function. Your explanation above answers that\nquestion. On that note, in addition to renaming the function as\nsuggested, for clarity, I would probably go a bit further and pass in\na `struct repository *` rather than passing in the git-dir itself,\njust to make it clear that the function is checking main-worktree\nbareness of the repository in question, as opposed to merely checking\nbareness of any arbitrary directory. (At least, I would find the\nintention more clear at-a-glance with that additional change applied.)\n\n> >> +    config_file = xstrfmt(\"%s/config\", git_dir);\n> >> +    worktree_config_file = xstrfmt(\"%s/config.worktree\", git_dir);\n> >> +\n> >> +    git_configset_init(&cs);\n> >> +    git_configset_add_file(&cs, config_file);\n> >> +    git_configset_add_file(&cs, worktree_config_file);\n> >\n> > Genuine question: I haven't thought too deeply about it, but do we\n> > gain anything by loading $commondir/config here -- which is shared by\n> > the main worktree and all secondary worktrees -- considering that it\n> > was already loaded and consulted by the earlier is-bare check before\n> > this function was even called?\n>\n> This function determines if a worktree is bare or not. I want this\n> logic to work even when it’s called from a different context and not\n> rely on other is-bare checks (that are a bit confusing tbh).\n\nAgreed about the is-bare checks -- and indeed the entire Git startup\nsequence -- being difficult to digest, however...\n\nOne reason I asked the question was due to concern that future readers\nof this code may very well wonder (as I did) why $commondir/config is\nbeing loaded when doing so is (apparently) unnecessary in this\nparticular context. The question is especially pertinent given that\nthis is a private helper function with a single caller. A second\nreason was that, over the years, a good deal of effort has been put\ninto optimizing Git's startup to avoid doing unnecessary work, and\nthis appears to be unnecessary since $commondir/config would already\nhave been consulted by earlier checks before this function gets called\n(assuming I'm correctly understanding the code-flow).\n\nAnyhow, we can probably punt on the question for the moment and leave\nthe code as you wrote it if you feel strongly about it or if you think\nit is clearer this way for future readers.\n\n> >> +    /*\n> >> +    * NEEDSWORK: the_repository is not always main worktree's repository\n> >> +    */\n> >>    worktree->repo = the_repository;\n> >>    worktree->path = strbuf_detach(&worktree_path, NULL);\n> >\n> > I found this new NEEDSWORK comment rather confusing the first several\n> > times I read the patch. It wasn't until I finally realized that the\n> > reference to `the_repository` here is the same reference to\n> > `the_repository` in the commit message -- which confused me, as well\n> > -- that I understood what this was trying to say. The actual problem,\n> > of course, is that the _configuration_ stored in `the_repository` is\n> > the secondary worktree's configuration, not the main worktree's\n> > configuration. Considering that this patch addresses that problem, I'd\n> > probably just drop this new comment altogether (unless, perhaps, you\n> > rewrite it to talk about the _configuration_ stored in\n> > `the_repository`).\n>\n> This `the_repository` structure is soooo confusing, took me a while\n> to figure out what it is! I would feel guilty not mentioning that\n> under some circumstances `the_repository` assigned here could be not\n> actual configuration of the worktree object. I don’t know if that\n> will ever matter or not, but I find this assignment kinda “stinky”\n> and want everyone to know about it. I don’t want to change this\n> assignment in this patch because it didn’t bring any harm so far.\n> I’ll try again to rephrase this comment, just to give a heads up in\n> case someone experiences “weird” behaviour in this area (same way\n> the previous NEEDSWORK comment gave me ideas why my workflow didn’t\n> work and inspired me to try to fix it).\n\nLikely, the confusion is an outcome of the natural evolution\n(mutation) software undergoes. The development of linked worktrees and\nthe concept of a `repository` structure did not necessarily occur\nconcurrently. I suppose one could develop one of two views (if not\nmore) of the `repository`: (1) an in-memory representation of the\n\".git\" directory or bare-repository \"object database\", including all\nworktrees hanging off it, or (2) a single worktree's\nview/representation of the repository, meaning paths, configuration,\n\"index\" specific to that worktree.\n\nIn the present state of the code, the second view is the more accurate\none, so the existing `worktree->repo = the_repository` assignment does\nmake sense without any further commentary. My main concern with the\nNEEDSWORK comment is that it implies that there is a problem with the\nassignment, even though there isn't. While it may be true that the\nentire `repository` idea needs to be rethought or clarified or\nexpanded, that's a global issue permeating the entire code-base, not\nspecific to this one spot, which is why it feels inappropriate to have\na NEEDSWORK comment here. So, I'm not, in general, opposed to a\ncomment explaining the the `worktree->repo` assignment for future\nreaders if you think that would be valuable, but I am concerned about\ngiving it a \"NEEDSWORK\" prefix, which feels misleading for this\nparticular piece of code.\n\n> Thanks for the review. I’ll incorporate the changes in my next\n> version and hopefully it will be good to go :tada: I hope I\n> responded to all the comments, it’s a bit nerve-wrecking to\n> contribute for the first time (so many rules and instructions!) :)\n\nUnderstood, and it didn't help the nerve situation when your v1 was\napparently ignored. Rest assured, though, that your submission was\nnicely done and fixes a real problem which ought to be addressed. (In\nfact, I'm surprised it took this long for someone to tackle it. So,\nthanks.)\n"},{"id":"511423","messageId":"xmqq7c6dk0ol.fsf@gitster.g","threadId":"62494","inReplyTo":"CAPig+cSdbjzTmsBOmFnMxzYLGrUzY46=mkW9S+si2KxLhS623Q@mail.gmail.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-01-29T17:08:26Z","receivedAt":"2025-01-29T17:08:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n>> Wow, your explanation is so much better than mine.Thank you for\n>> “translating\" it for the world :) I’m still trying to get used to\n>> the terminology used in this codebase.  I’ll steal your description\n>> for sure (if you don’t mind).\n>\n> You are more than welcome to use the proposed commit message rewrite.\n>\n> (If you want to acknowledge assistance rendered, a Helped-by: trailer,\n> preceding your Signed-off-by:, is the way to do so. Or not. It's up to\n> you.)\n> ...\n\n> Agreed about the is-bare checks -- and indeed the entire Git startup\n> sequence -- being difficult to digest, however...\n> ...\n\nThanks for an easy-to-read review (and thanks OP for working on it,\nof course).\n\n"},{"id":"511490","messageId":"CAPig+cTHfD1fK73+S3fqQ+Oz_VpBzap5=nFFE1bntSeaHLcu8g@mail.gmail.com","threadId":"62494","inReplyTo":"F15C12AB-2238-4553-AFA5-18277B18CE5A@shopify.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2025-01-30T14:32:22Z","receivedAt":"2025-01-30T14:32:34Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2025 at 2:09 AM Olga Pilipenco <olga.pilipenco@shopify.com> wrot\n> On Jan 29, 2025, at 6:41 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> > I see. When reviewing, I was wondering why the git-dir was being\n> > passed into the function. Your explanation above answers that\n> > question. On that note, in addition to renaming the function as\n> > suggested, for clarity, I would probably go a bit further and pass in\n> > a `struct repository *` rather than passing in the git-dir itself,\n> > just to make it clear that the function is checking main-worktree\n> > bareness of the repository in question, as opposed to merely checking\n> > bareness of any arbitrary directory. (At least, I would find the\n> > intention more clear at-a-glance with that additional change applied.)\n>\n> Indeed, no need to pass git-dir anymore.  There is actually no need\n> to pass `the_repository` because it’s global. I like how this\n> simplified things and made code clearer.\n\nThe reason I suggested passing in a `struct repository *` is that the\nproject is slowly moving away from the `the_repository` global, so\nmaking this new function accept a `struct repository *` as its sole\nargument means less work later on.\n\n> > One reason I asked the question was due to concern that future readers\n> > of this code may very well wonder (as I did) why $commondir/config is\n> > being loaded when doing so is (apparently) unnecessary in this\n> > particular context. The question is especially pertinent given that\n> > this is a private helper function with a single caller. A second\n> > reason was that, over the years, a good deal of effort has been put\n> > into optimizing Git's startup to avoid doing unnecessary work, and\n> > this appears to be unnecessary since $commondir/config would already\n> > have been consulted by earlier checks before this function gets called\n> > (assuming I'm correctly understanding the code-flow).\n>\n> I trust your judgement and knowledge of the code and really like the\n> reasons presented.  I’ll change this function to only check for\n> worktree config.  However I’d like to give it a good name where it’s\n> clear we only check worktree config.  It’s a bit challenging to make\n> it short-ish and not to include multiple “worktree” words in the\n> name.  Before I submit a new release, maybe I have time to quickly\n> align on the name.  What do you think about this one:\n>\n> is_main_worktree_bare_in_worktree_config\n>\n> (It will check if bare=true in the main worktree’s worktree.config)\n>\n> Naming is harder than the code itself :)\n\nIt's a historic \"accident\" that when worktree support was designed,\nthe idea of linking worktrees to a bare repository was not considered.\nSupport for using worktrees with a bare repository was added later.\nHowever, by that time, the term \"main worktree\" was already well\nestablished, with the very unfortunate result that even when there is\nno actual \"main worktree\" but only a bare repository with \"linked\nworktrees\" hanging off it, the repository itself is usually referred\nto as the \"bare main worktree\", which is an obvious misnomer; the\nrepository is just a repository (i.e. the object database and other\nmeta-information) and there is no actual main worktree.\n\nGiven the very real potential for confusion when employing the \"bare\nmain worktree\" misnomer, I suspect that we won't be able to come up\nwith a good name which easily conveys the function's purpose. Since\nthis is an internal helper (hence, there is slightly less pressure to\ncome up with a perfect name) rather than public API, this might be one\nof those cases in which it makes more sense to choose a concise name\nand then explain the function's purpose with a short comment block.\nPerhaps something like this would be most helpful to future readers of\nthe code:\n\n    /*\n     * When in a secondary worktree, and when extensions.worktreeConfig\n     * is true, only $commondir/config and $commondir/worktrees/<id>/\n     * config.worktree are consulted, hence any core.bare=true setting in\n     * $commondir/config.worktree gets overlooked. Thus, check it manually\n     * to determine if the repository is bare.\n     */\n    static int is_repo_bare(struct repository *r) {...}\n\n> Thank you for this thorough explanation. I’ll drop the comment\n> completely. Less code to read.  (To be honest I’m not a big fan of\n> comments and definitely don’t want to introduce confusing comments\n> :)\n\nUnderstood. Self-explanatory code is preferred. That said, a comment\nsuch as the one proposed above can really help readers not intimately\nfamiliar with this otherwise nonobvious behavior, thus may be\njustified.\n\n> Fun fact: my email app kept changing “worktree” to “corktree”. Maybe\n> another git feature?\n\nYour mailer obviously suffers from Gitophobia or is perhaps a nemophilist.\n\n(By the way, when replying, please use the normal \">\", \"> >\", \"> > >\"\nmarkers to signify quoted portions of earlier messages in the thread\nrather than using only indentation. The reason I make this request is\nthat when I replied to your message, my mailer stripped away all\nindentation from your message, leaving all earlier quoted portions\nflush with the left margin, which made it very difficult to figure out\nwhich quotes came from which authors from which earlier messages, and\nI ended up having to reinsert the \"> >\" markers manually to restore\nstructure to my reply.)\n"},{"id":"511498","messageId":"CAPig+cRv9Z2mQ964PNgbbgk+ByGvC1EMNc6=-itD_7v6_4Pg+A@mail.gmail.com","threadId":"62494","inReplyTo":"CAPig+cTHfD1fK73+S3fqQ+Oz_VpBzap5=nFFE1bntSeaHLcu8g@mail.gmail.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2025-01-30T14:44:04Z","receivedAt":"2025-01-30T14:44:16Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2025 at 9:32 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n> (By the way, when replying, please use the normal \">\", \"> >\", \"> > >\"\n> markers to signify quoted portions of earlier messages in the thread\n> rather than using only indentation. The reason I make this request is\n> that when I replied to your message, my mailer stripped away all\n> indentation from your message, leaving all earlier quoted portions\n> flush with the left margin, which made it very difficult to figure out\n> which quotes came from which authors from which earlier messages, and\n> I ended up having to reinsert the \"> >\" markers manually to restore\n> structure to my reply.)\n\nExamining more closely, I see that your message was in fact multipart\nMIME with an HTML portion which was using <blockquote> and whatnot,\nwhich my (plain text) mailer stripped out, hence lost all formatting.\nIf you can configure your mailer to send plain text and use the normal\n\">\" markers, that would be generally helpful on this list.\n"},{"id":"511536","messageId":"CAFLeGL5L=Pv4RH7-7Os+khewvtxHDTX+a4hmmORz=b_e-1FLoQ@mail.gmail.com","threadId":"62494","inReplyTo":"CAPig+cRv9Z2mQ964PNgbbgk+ByGvC1EMNc6=-itD_7v6_4Pg+A@mail.gmail.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco","fromEmail":"olga.pilipenco@shopify.com","sentAt":"2025-01-31T07:05:09Z","receivedAt":"2025-01-31T07:05:21Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"On Thu, Jan 30, 2025 at 7:44 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n>\n> On Thu, Jan 30, 2025 at 9:32 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n> > (By the way, when replying, please use the normal \">\", \"> >\", \"> > >\"\n> > markers to signify quoted portions of earlier messages in the thread\n> > rather than using only indentation. The reason I make this request is\n> > that when I replied to your message, my mailer stripped away all\n> > indentation from your message, leaving all earlier quoted portions\n> > flush with the left margin, which made it very difficult to figure out\n> > which quotes came from which authors from which earlier messages, and\n> > I ended up having to reinsert the \"> >\" markers manually to restore\n> > structure to my reply.)\n>\n> Examining more closely, I see that your message was in fact multipart\n> MIME with an HTML portion which was using <blockquote> and whatnot,\n> which my (plain text) mailer stripped out, hence lost all formatting.\n> If you can configure your mailer to send plain text and use the normal\n> \">\" markers, that would be generally helpful on this list.\n\nUfff, my bad, sorry about that (should be plain text now).\nI'm having trouble responding to my previous email and keeping that\nquoting format,\nso I'll not waste time on trying to fix that (please forgive me, everyone).\nJust want to let you know that I agree with your response, I'll adjust my patch.\nEnough talking, let's get the code out!\n"},{"id":"511557","messageId":"CAPig+cQ0J+R5PqXAFOFo_R4a7cZe0zJoBhBGdf-QB20o=cKCjg@mail.gmail.com","threadId":"62494","inReplyTo":"CAFLeGL5L=Pv4RH7-7Os+khewvtxHDTX+a4hmmORz=b_e-1FLoQ@mail.gmail.com","subject":"Re: [PATCH v2] worktree: detect from secondary worktree if main worktree is bare","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2025-01-31T13:28:26Z","receivedAt":"2025-01-31T13:28:39Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Jan 31, 2025 at 2:05 AM Olga Pilipenco\n<olga.pilipenco@shopify.com> wrote:\n> On Thu, Jan 30, 2025 at 7:44 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n> > Examining more closely, I see that your message was in fact multipart\n> > MIME with an HTML portion which was using <blockquote> and whatnot,\n> > which my (plain text) mailer stripped out, hence lost all formatting.\n> > If you can configure your mailer to send plain text and use the normal\n> > \">\" markers, that would be generally helpful on this list.\n>\n> Ufff, my bad, sorry about that (should be plain text now).\n\nYup, looks good.\n\n> I'm having trouble responding to my previous email and keeping that\n> quoting format,\n> so I'll not waste time on trying to fix that (please forgive me, everyone).\n> Just want to let you know that I agree with your response, I'll adjust my patch.\n> Enough talking, let's get the code out!\n\nThanks.\n"},{"id":"511583","messageId":"pull.1829.v3.git.1738346881907.gitgitgadget@gmail.com","threadId":"62494","inReplyTo":"pull.1829.v2.git.1737063335673.gitgitgadget@gmail.com","subject":"[PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-01-31T18:08:01Z","receivedAt":"2025-01-31T18:08:05Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"From: Olga Pilipenco <olga.pilipenco@shopify.com>\n\nWhen extensions.worktreeConfig is true and the main worktree is\nbare -- that is, its config.worktree file contains core.bare=true\n-- commands run from secondary worktrees incorrectly see the main\nworktree as not bare. As such, those commands incorrectly think\nthat the repository's default branch (typically \"main\" or\n\"master\") is checked out in the bare repository even though it's\nnot. This makes it impossible, for instance, to checkout or delete\nthe default branch from a secondary worktree, among other\nshortcomings.\n\nThis problem occurs because, when extensions.worktreeConfig is\ntrue, commands run in secondary worktrees only consult\n$commondir/config and $commondir/worktrees/<id>/config.worktree,\nthus they never see the main worktree's core.bare=true setting in\n$commondir/config.worktree.\n\nFix this problem by consulting the main worktree's config.worktree\nfile when checking whether it is bare. (This extra work is\nperformed only when running from a secondary worktree.)\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Olga Pilipenco <olga.pilipenco@shopify.com>\n---\n    worktree: detect from secondary worktree if main worktree is bare\n    \n    Changes since v2, all results of the amazing review:\n    \n     * updated description & comments;\n     * private function is_bare_git_dir is replaced with\n       is_main_worktree_bare. The new implementation only checks if main\n       worktree's worktree.config contains information if main worktree is\n       bare or not. It's assumed that other configs of main worktree are\n       already checked for bareness prior this call.\n     * notation { { 0 } } is replaced with {0} that is preferred by the\n       project.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1829%2Folga-mcbfe%2Ffix-bare-repo-detection-with-worktree-config-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1829/olga-mcbfe/fix-bare-repo-detection-with-worktree-config-v3\nPull-Request: https://github.com/gitgitgadget/git/pull/1829\n\nRange-diff vs v2:\n\n 1:  17f4b24d1da ! 1:  f9207746b38 worktree: detect from secondary worktree if main worktree is bare\n     @@ Metadata\n       ## Commit message ##\n          worktree: detect from secondary worktree if main worktree is bare\n      \n     -    Setup:\n     -    1. Have a bare repo with core.bare = true in config.worktree\n     -    2. Create a new worktree\n     +    When extensions.worktreeConfig is true and the main worktree is\n     +    bare -- that is, its config.worktree file contains core.bare=true\n     +    -- commands run from secondary worktrees incorrectly see the main\n     +    worktree as not bare. As such, those commands incorrectly think\n     +    that the repository's default branch (typically \"main\" or\n     +    \"master\") is checked out in the bare repository even though it's\n     +    not. This makes it impossible, for instance, to checkout or delete\n     +    the default branch from a secondary worktree, among other\n     +    shortcomings.\n      \n     -    Behavior:\n     -    From the secondary worktree the main worktree appears as non-bare.\n     +    This problem occurs because, when extensions.worktreeConfig is\n     +    true, commands run in secondary worktrees only consult\n     +    $commondir/config and $commondir/worktrees/<id>/config.worktree,\n     +    thus they never see the main worktree's core.bare=true setting in\n     +    $commondir/config.worktree.\n      \n     -    Expected:\n     -    From the secondary worktree the main worktree should appear as bare.\n     -\n     -    Why current behavior is not good?\n     -    If the main worktree is detected as not bare it doesn't allow\n     -    checking out the branch of the main worktree. There are possibly\n     -    other problems associated with that behavior.\n     -\n     -    Why is it happening?\n     -    While we're inside the secondary worktree we don't initialize the main\n     -    worktree's repository with its configuration.\n     -\n     -    How is it fixed?\n     -    Load actual configs of the main worktree. Also, skip the config loading\n     -    step if we're already inside the current worktree because in that case we\n     -    rely on is_bare_repository() to return the correct result.\n     -\n     -    Other solutions considered:\n     -    Alternatively, instead of incorrectly always using\n     -    `the_repository` as the main worktree's repository, we can detect\n     -    and load the actual repository of the main worktree and then use\n     -    that repository's `is_bare` value extracted from correct configs.\n     -    However, this approach is a bit riskier and could also affect\n     -    performance. Since we had the assignment `worktree->repo =\n     -    the_repository` for a long time already, I decided it's safe to\n     -    keep it as it is for now; it can be still fixed separately from\n     -    this change.\n     -\n     -    Real life use case:\n     -    1. Have a bare repo\n     -    2. Create a worktree from the bare repo\n     -    3. In the secondary worktree enable sparse-checkout - this enables\n     -    extensions.worktreeConfig and keeps core.bare=true setting in\n     -    config.worktree of the bare worktree\n     -    4. The secondary worktree or any other non-bare worktree created\n     -    won't be able to use branch main (not even once), but it should be\n     -    able to.\n     +    Fix this problem by consulting the main worktree's config.worktree\n     +    file when checking whether it is bare. (This extra work is\n     +    performed only when running from a secondary worktree.)\n      \n     +    Helped-by: Eric Sunshine <sunshine@sunshineco.com>\n          Signed-off-by: Olga Pilipenco <olga.pilipenco@shopify.com>\n      \n       ## t/t3200-branch.sh ##\n     @@ t/t3200-branch.sh: test_expect_success 'bare main worktree has HEAD at branch de\n       \tgit -C secondary branch -D main\n       '\n       \n     -+test_expect_success 'secondary worktree can switch to main if common dir is bare worktree' '\n     ++test_expect_success 'secondary worktrees recognize core.bare=true in main config.worktree' '\n      +\ttest_when_finished \"rm -rf bare_repo non_bare_repo secondary_worktree\" &&\n      +\tgit init -b main non_bare_repo &&\n      +\ttest_commit -C non_bare_repo x &&\n     @@ worktree.c: static int is_current_worktree(struct worktree *wt)\n       \treturn is_current;\n       }\n       \n     -+static int is_bare_git_dir(const char *git_dir)\n     ++/*\n     ++* When in a secondary worktree, and when extensions.worktreeConfig\n     ++* is true, only $commondir/config and $commondir/worktrees/<id>/\n     ++* config.worktree are consulted, hence any core.bare=true setting in\n     ++* $commondir/config.worktree gets overlooked. Thus, check it manually\n     ++* to determine if the repository is bare.\n     ++*/\n     ++static int is_main_worktree_bare(struct repository *repo)\n      +{\n      +\tint bare = 0;\n     -+\tstruct config_set cs = { { 0 } };\n     -+\tchar *config_file;\n     -+\tchar *worktree_config_file;\n     -+\n     -+\tconfig_file = xstrfmt(\"%s/config\", git_dir);\n     -+\tworktree_config_file = xstrfmt(\"%s/config.worktree\",  git_dir);\n     ++\tstruct config_set cs = {0};\n     ++\tchar *worktree_config = xstrfmt(\"%s/config.worktree\", repo_get_common_dir(repo));\n      +\n      +\tgit_configset_init(&cs);\n     -+\tgit_configset_add_file(&cs, config_file);\n     -+\tgit_configset_add_file(&cs, worktree_config_file);\n     -+\n     ++\tgit_configset_add_file(&cs, worktree_config);\n      +\tgit_configset_get_bool(&cs, \"core.bare\", &bare);\n      +\n      +\tgit_configset_clear(&cs);\n     -+\tfree(config_file);\n     -+\tfree(worktree_config_file);\n     ++\tfree(worktree_config);\n      +\treturn bare;\n      +}\n      +\n     @@ worktree.c: static int is_current_worktree(struct worktree *wt)\n        * get the main worktree\n        */\n      @@ worktree.c: static struct worktree *get_main_worktree(int skip_reading_head)\n     - \tstrbuf_strip_suffix(&worktree_path, \"/.git\");\n     - \n       \tCALLOC_ARRAY(worktree, 1);\n     -+\t/*\n     -+\t * NEEDSWORK: the_repository is not always main worktree's repository\n     -+\t*/\n       \tworktree->repo = the_repository;\n       \tworktree->path = strbuf_detach(&worktree_path, NULL);\n      -\t/*\n     @@ worktree.c: static struct worktree *get_main_worktree(int skip_reading_head)\n       \tworktree->is_current = is_current_worktree(worktree);\n      +\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n      +\t\tis_bare_repository() ||\n     -+\t\t(!worktree->is_current && is_bare_git_dir(repo_get_common_dir(the_repository)));\n     ++\t\t(!worktree->is_current && is_main_worktree_bare(the_repository));\n      +\n       \tif (!skip_reading_head)\n       \t\tadd_head_info(worktree);\n\n\n t/t3200-branch.sh | 14 ++++++++++++++\n worktree.c        | 35 ++++++++++++++++++++++++++---------\n 2 files changed, 40 insertions(+), 9 deletions(-)\n\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex a3a21c54cf6..f3e720dc10d 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -410,6 +410,20 @@ test_expect_success 'bare main worktree has HEAD at branch deleted by secondary\n \tgit -C secondary branch -D main\n '\n \n+test_expect_success 'secondary worktrees recognize core.bare=true in main config.worktree' '\n+\ttest_when_finished \"rm -rf bare_repo non_bare_repo secondary_worktree\" &&\n+\tgit init -b main non_bare_repo &&\n+\ttest_commit -C non_bare_repo x &&\n+\n+\tgit clone --bare non_bare_repo bare_repo &&\n+\tgit -C bare_repo config extensions.worktreeConfig true &&\n+\tgit -C bare_repo config unset core.bare &&\n+\tgit -C bare_repo config --worktree core.bare true &&\n+\n+\tgit -C bare_repo worktree add ../secondary_worktree &&\n+\tgit -C secondary_worktree checkout main\n+'\n+\n test_expect_success 'git branch --list -v with --abbrev' '\n \ttest_when_finished \"git branch -D t\" &&\n \tgit branch t &&\ndiff --git a/worktree.c b/worktree.c\nindex 248bbb39d43..6df4ccf97f7 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -65,6 +65,28 @@ static int is_current_worktree(struct worktree *wt)\n \treturn is_current;\n }\n \n+/*\n+* When in a secondary worktree, and when extensions.worktreeConfig\n+* is true, only $commondir/config and $commondir/worktrees/<id>/\n+* config.worktree are consulted, hence any core.bare=true setting in\n+* $commondir/config.worktree gets overlooked. Thus, check it manually\n+* to determine if the repository is bare.\n+*/\n+static int is_main_worktree_bare(struct repository *repo)\n+{\n+\tint bare = 0;\n+\tstruct config_set cs = {0};\n+\tchar *worktree_config = xstrfmt(\"%s/config.worktree\", repo_get_common_dir(repo));\n+\n+\tgit_configset_init(&cs);\n+\tgit_configset_add_file(&cs, worktree_config);\n+\tgit_configset_get_bool(&cs, \"core.bare\", &bare);\n+\n+\tgit_configset_clear(&cs);\n+\tfree(worktree_config);\n+\treturn bare;\n+}\n+\n /**\n  * get the main worktree\n  */\n@@ -79,16 +101,11 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n \tCALLOC_ARRAY(worktree, 1);\n \tworktree->repo = the_repository;\n \tworktree->path = strbuf_detach(&worktree_path, NULL);\n-\t/*\n-\t * NEEDSWORK: If this function is called from a secondary worktree and\n-\t * config.worktree is present, is_bare_repository_cfg will reflect the\n-\t * contents of config.worktree, not the contents of the main worktree.\n-\t * This means that worktree->is_bare may be set to 0 even if the main\n-\t * worktree is configured to be bare.\n-\t */\n-\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n-\t\tis_bare_repository();\n \tworktree->is_current = is_current_worktree(worktree);\n+\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n+\t\tis_bare_repository() ||\n+\t\t(!worktree->is_current && is_main_worktree_bare(the_repository));\n+\n \tif (!skip_reading_head)\n \t\tadd_head_info(worktree);\n \treturn worktree;\n\nbase-commit: f93ff170b93a1782659637824b25923245ac9dd1\n-- \ngitgitgadget\n"},{"id":"511585","messageId":"xmqqcyg294ft.fsf@gitster.g","threadId":"62494","inReplyTo":"pull.1829.v3.git.1738346881907.gitgitgadget@gmail.com","subject":"Re: [PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-01-31T19:19:34Z","receivedAt":"2025-01-31T19:19:37Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Olga Pilipenco via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> +/*\n> +* When in a secondary worktree, and when extensions.worktreeConfig\n> +* is true, only $commondir/config and $commondir/worktrees/<id>/\n> +* config.worktree are consulted, hence any core.bare=true setting in\n> +* $commondir/config.worktree gets overlooked. Thus, check it manually\n> +* to determine if the repository is bare.\n> +*/\n> +static int is_main_worktree_bare(struct repository *repo)\n> +{\n> +\tint bare = 0;\n> +\tstruct config_set cs = {0};\n> +\tchar *worktree_config = xstrfmt(\"%s/config.worktree\", repo_get_common_dir(repo));\n> +\n> +\tgit_configset_init(&cs);\n> +\tgit_configset_add_file(&cs, worktree_config);\n> +\tgit_configset_get_bool(&cs, \"core.bare\", &bare);\n> +\n> +\tgit_configset_clear(&cs);\n> +\tfree(worktree_config);\n> +\treturn bare;\n> +}\n\nThat is nicely described.\n\n>  /**\n>   * get the main worktree\n>   */\n> @@ -79,16 +101,11 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n>  \tCALLOC_ARRAY(worktree, 1);\n>  \tworktree->repo = the_repository;\n>  \tworktree->path = strbuf_detach(&worktree_path, NULL);\n> -\t/*\n> -\t * NEEDSWORK: If this function is called from a secondary worktree and\n> -\t * config.worktree is present, is_bare_repository_cfg will reflect the\n> -\t * contents of config.worktree, not the contents of the main worktree.\n> -\t * This means that worktree->is_bare may be set to 0 even if the main\n> -\t * worktree is configured to be bare.\n> -\t */\n> -\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n> -\t\tis_bare_repository();\n>  \tworktree->is_current = is_current_worktree(worktree);\n> +\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n> +\t\tis_bare_repository() ||\n> +\t\t(!worktree->is_current && is_main_worktree_bare(the_repository));\n\nIs \"this worktree does not have is_current bit set\" equivalent to\n\"this worktree is the main one, so is_main_worktree_bare() needs to\nbe consulted\"?  That linkage between \"the is_current bit unset\" and\n\"is the main worktree\" is not obvious to me.\n\nThanks.\n"},{"id":"511586","messageId":"xmqq8qqq943u.fsf@gitster.g","threadId":"62494","inReplyTo":"xmqqcyg294ft.fsf@gitster.g","subject":"Re: [PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-01-31T19:26:45Z","receivedAt":"2025-01-31T19:26:48Z","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> \"Olga Pilipenco via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n>> +/*\n>> +* When in a secondary worktree, and when extensions.worktreeConfig\n>> +* is true, only $commondir/config and $commondir/worktrees/<id>/\n>> +* config.worktree are consulted, hence any core.bare=true setting in\n>> +* $commondir/config.worktree gets overlooked. Thus, check it manually\n>> +* to determine if the repository is bare.\n>> +*/\n>> +static int is_main_worktree_bare(struct repository *repo)\n>> +{\n>> +\tint bare = 0;\n>> +\tstruct config_set cs = {0};\n>> +\tchar *worktree_config = xstrfmt(\"%s/config.worktree\", repo_get_common_dir(repo));\n>> +\n>> +\tgit_configset_init(&cs);\n>> +\tgit_configset_add_file(&cs, worktree_config);\n>> +\tgit_configset_get_bool(&cs, \"core.bare\", &bare);\n>> +\n>> +\tgit_configset_clear(&cs);\n>> +\tfree(worktree_config);\n>> +\treturn bare;\n>> +}\n>\n> That is nicely described.\n>\n>>  /**\n>>   * get the main worktree\n>>   */\n>> @@ -79,16 +101,11 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n>>  \tCALLOC_ARRAY(worktree, 1);\n>>  \tworktree->repo = the_repository;\n>>  \tworktree->path = strbuf_detach(&worktree_path, NULL);\n>> -\t/*\n>> -\t * NEEDSWORK: If this function is called from a secondary worktree and\n>> -\t * config.worktree is present, is_bare_repository_cfg will reflect the\n>> -\t * contents of config.worktree, not the contents of the main worktree.\n>> -\t * This means that worktree->is_bare may be set to 0 even if the main\n>> -\t * worktree is configured to be bare.\n>> -\t */\n>> -\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n>> -\t\tis_bare_repository();\n>>  \tworktree->is_current = is_current_worktree(worktree);\n>> +\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n>> +\t\tis_bare_repository() ||\n>> +\t\t(!worktree->is_current && is_main_worktree_bare(the_repository));\n>\n> Is \"this worktree does not have is_current bit set\" equivalent to\n> \"this worktree is the main one, so is_main_worktree_bare() needs to\n> be consulted\"?  That linkage between \"the is_current bit unset\" and\n> \"is the main worktree\" is not obvious to me.\n\nDoes the thinking behind it go like this?\n\n    We grabbed the \"main\" worktree object and stored it in worktree;\n    it is either our current worktree (in which case is_current is\n    true), or it is not (in which case, is_current is false).  We\n    know that the old logic failed when asking the \"is it bare\"\n    question from a secondary worktree.  !worktree->is_current tells\n    us that we _are_ asking the question from a secondary worktree,\n    so we need to make the extra call to check config.worktree file\n    as well in that case.\n\nPerhaps the logic is clear to those who diagnosed the problem, wrote\nthe patch, and reviewed it, in which case there is no reason to\nreroll.  Perhaps it was just me to whom it was not obvious that\nthe purpose of \"is_current\" check was not about \"are we looking at\nthe main worktree\" but was about \"if we are not in the main worktree,\nwe need this extra check\".\n\nThanks.\n"},{"id":"511591","messageId":"CAFLeGL52tKmurpAHymk42Y9DGazbK8nRdtWAoyzW85eMDxJQhQ@mail.gmail.com","threadId":"62494","inReplyTo":"xmqq8qqq943u.fsf@gitster.g","subject":"Re: [PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco","fromEmail":"olga.pilipenco@shopify.com","sentAt":"2025-01-31T20:11:00Z","receivedAt":"2025-01-31T20:11:11Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"On Fri, Jan 31, 2025 at 12:26 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n> > \"Olga Pilipenco via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> >\n> >> +/*\n> >> +* When in a secondary worktree, and when extensions.worktreeConfig\n> >> +* is true, only $commondir/config and $commondir/worktrees/<id>/\n> >> +* config.worktree are consulted, hence any core.bare=true setting in\n> >> +* $commondir/config.worktree gets overlooked. Thus, check it manually\n> >> +* to determine if the repository is bare.\n> >> +*/\n> >> +static int is_main_worktree_bare(struct repository *repo)\n> >> +{\n> >> +    int bare = 0;\n> >> +    struct config_set cs = {0};\n> >> +    char *worktree_config = xstrfmt(\"%s/config.worktree\", repo_get_common_dir(repo));\n> >> +\n> >> +    git_configset_init(&cs);\n> >> +    git_configset_add_file(&cs, worktree_config);\n> >> +    git_configset_get_bool(&cs, \"core.bare\", &bare);\n> >> +\n> >> +    git_configset_clear(&cs);\n> >> +    free(worktree_config);\n> >> +    return bare;\n> >> +}\n> >\n> > That is nicely described.\n> >\n> >>  /**\n> >>   * get the main worktree\n> >>   */\n> >> @@ -79,16 +101,11 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n> >>      CALLOC_ARRAY(worktree, 1);\n> >>      worktree->repo = the_repository;\n> >>      worktree->path = strbuf_detach(&worktree_path, NULL);\n> >> -    /*\n> >> -     * NEEDSWORK: If this function is called from a secondary worktree and\n> >> -     * config.worktree is present, is_bare_repository_cfg will reflect the\n> >> -     * contents of config.worktree, not the contents of the main worktree.\n> >> -     * This means that worktree->is_bare may be set to 0 even if the main\n> >> -     * worktree is configured to be bare.\n> >> -     */\n> >> -    worktree->is_bare = (is_bare_repository_cfg == 1) ||\n> >> -            is_bare_repository();\n> >>      worktree->is_current = is_current_worktree(worktree);\n> >> +    worktree->is_bare = (is_bare_repository_cfg == 1) ||\n> >> +            is_bare_repository() ||\n> >> +            (!worktree->is_current && is_main_worktree_bare(the_repository));\n> >\n> > Is \"this worktree does not have is_current bit set\" equivalent to\n> > \"this worktree is the main one, so is_main_worktree_bare() needs to\n> > be consulted\"?  That linkage between \"the is_current bit unset\" and\n> > \"is the main worktree\" is not obvious to me.\n>\n> Does the thinking behind it go like this?\n>\n>     We grabbed the \"main\" worktree object and stored it in worktree;\n>     it is either our current worktree (in which case is_current is\n>     true), or it is not (in which case, is_current is false).  We\n>     know that the old logic failed when asking the \"is it bare\"\n>     question from a secondary worktree.  !worktree->is_current tells\n>     us that we _are_ asking the question from a secondary worktree,\n>     so we need to make the extra call to check config.worktree file\n>     as well in that case.\n>\n> Perhaps the logic is clear to those who diagnosed the problem, wrote\n> the patch, and reviewed it, in which case there is no reason to\n> reroll.  Perhaps it was just me to whom it was not obvious that\n> the purpose of \"is_current\" check was not about \"are we looking at\n> the main worktree\" but was about \"if we are not in the main worktree,\n> we need this extra check\".\n>\n> Thanks.\n\nYou did a great job figuring it out and I agree it's confusing at\nfirst, but we tried\nour best to make it less confusing.\n`is_current` check is actually not necessary there, but having it there saves\nextra unnecessary calculations, also describes & fixes the exact scenario\nthat didn't work (not being able to see main worktree as bare from a\nsecondary worktree).\n\nThanks for looking into that.\n"},{"id":"511594","messageId":"xmqq8qqq7n26.fsf@gitster.g","threadId":"62494","inReplyTo":"CAFLeGL52tKmurpAHymk42Y9DGazbK8nRdtWAoyzW85eMDxJQhQ@mail.gmail.com","subject":"Re: [PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-01-31T20:20:17Z","receivedAt":"2025-01-31T20:20:20Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Olga Pilipenco <olga.pilipenco@shopify.com> writes:\n\n>> Perhaps the logic is clear to those who diagnosed the problem, wrote\n>> the patch, and reviewed it, in which case there is no reason to\n>> reroll.  Perhaps it was just me to whom it was not obvious that\n>> the purpose of \"is_current\" check was not about \"are we looking at\n>> the main worktree\" but was about \"if we are not in the main worktree,\n>> we need this extra check\".\n>>\n>> Thanks.\n>\n> You did a great job figuring it out and I agree it's confusing at\n> first, but we tried our best to make it less confusing.\n> `is_current` check is actually not necessary there, but having it there saves\n> extra unnecessary calculations, also describes & fixes the exact scenario\n> that didn't work (not being able to see main worktree as bare from a\n> secondary worktree).\n\nIf I had to do a great job there, then the code does deserve to be\nexplained a bit better for later developers who wonder why it is\nwritten in the way it is, perhaps we a single-liner comment?\n\nThanks.\n"},{"id":"511824","messageId":"CAFLeGL4v90zArJjtCOSGUTGQTq6qQJEcNMhi4P=ucDU+9bGRHg@mail.gmail.com","threadId":"62494","inReplyTo":"xmqq8qqq7n26.fsf@gitster.g","subject":"Re: [PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco","fromEmail":"olga.pilipenco@shopify.com","sentAt":"2025-02-04T19:03:46Z","receivedAt":"2025-02-04T19:03:57Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"On Fri, Jan 31, 2025 at 1:20 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Olga Pilipenco <olga.pilipenco@shopify.com> writes:\n>\n> >> Perhaps the logic is clear to those who diagnosed the problem, wrote\n> >> the patch, and reviewed it, in which case there is no reason to\n> >> reroll.  Perhaps it was just me to whom it was not obvious that\n> >> the purpose of \"is_current\" check was not about \"are we looking at\n> >> the main worktree\" but was about \"if we are not in the main worktree,\n> >> we need this extra check\".\n> >>\n> >> Thanks.\n> >\n> > You did a great job figuring it out and I agree it's confusing at\n> > first, but we tried our best to make it less confusing.\n> > `is_current` check is actually not necessary there, but having it there saves\n> > extra unnecessary calculations, also describes & fixes the exact scenario\n> > that didn't work (not being able to see main worktree as bare from a\n> > secondary worktree).\n>\n> If I had to do a great job there, then the code does deserve to be\n> explained a bit better for later developers who wonder why it is\n> written in the way it is, perhaps we a single-liner comment?\n\nI have 2 versions for comment:\n\n1. Since is_main_worktree_bare explains quite well what it does we can have\na shorter explanation of `!worktree->is_current` part, something like:\n\n/* Additional checks are needed if main worktree is not current\n(checking from secondary worktree) */\n(!worktree->is_current && is_main_worktree_bare(the_repository));\n\n2. Or a bit longer inline explanation that partially repeats the\nexplanation of is_main_worktree_bare\n+ adds explanation about efficiency:\n /*\n  * When in a secondary worktree we have to also verify if the main worktree\n  * is bare in $commondir/config.worktree.\n  * This check is unnecessary if we're currently in the main worktree,\n  * as prior checks already consulted all configs of the current worktree.\n */\n(!worktree->is_current && is_main_worktree_bare(the_repository));\n\nLet me know if any of these work. Thanks.\n\n> Thanks.\n"},{"id":"511825","messageId":"xmqqa5b1ts0y.fsf@gitster.g","threadId":"62494","inReplyTo":"CAFLeGL4v90zArJjtCOSGUTGQTq6qQJEcNMhi4P=ucDU+9bGRHg@mail.gmail.com","subject":"Re: [PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-02-04T19:43:25Z","receivedAt":"2025-02-04T19:43:28Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Olga Pilipenco <olga.pilipenco@shopify.com> writes:\n\n> I have 2 versions for comment:\n>\n> 1. Since is_main_worktree_bare explains quite well what it does we can have\n> a shorter explanation of `!worktree->is_current` part, something like:\n>\n> /* Additional checks are needed if main worktree is not current\n> (checking from secondary worktree) */\n> (!worktree->is_current && is_main_worktree_bare(the_repository));\n\nFor somebody who thought about the issue themselves (like me, before\nwriting the message you are responding to), this shorter form would\nsuffice.  I'd rephrase it more like so\n\n    /* When a secondary worktree, an extra check is needed */\n\nfor brevity, though.\n\n\n> 2. Or a bit longer inline explanation that partially repeats the\n> explanation of is_main_worktree_bare\n> + adds explanation about efficiency:\n>  /*\n>   * When in a secondary worktree we have to also verify if the main worktree\n>   * is bare in $commondir/config.worktree.\n>   * This check is unnecessary if we're currently in the main worktree,\n>   * as prior checks already consulted all configs of the current worktree.\n>  */\n> (!worktree->is_current && is_main_worktree_bare(the_repository));\n\nAnd this more extended version would have helped me by not having to\nask\n\n    Is \"this worktree does not have is_current bit set\" equivalent\n    to \"this worktree is the main one, so is_main_worktree_bare()\n    needs to be consulted\"?  That linkage between \"the is_current\n    bit unset\" and \"is the main worktree\" is not obvious to me.\n\nin the first place.\n\nIn short, both should work, and I personally find that the latter\nmay be a bit more helpful to readers.\n\nTHanks.\n"},{"id":"511826","messageId":"CAFLeGL7aewQTECw3sUPSgysaWCcS+U_7vf=uHDuuvKDkQUHfxA@mail.gmail.com","threadId":"62494","inReplyTo":"xmqqa5b1ts0y.fsf@gitster.g","subject":"Re: [PATCH v3] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco","fromEmail":"olga.pilipenco@shopify.com","sentAt":"2025-02-04T20:33:22Z","receivedAt":"2025-02-04T20:33:34Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"On Tue, Feb 4, 2025 at 12:43 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Olga Pilipenco <olga.pilipenco@shopify.com> writes:\n>\n> > I have 2 versions for comment:\n> >\n> > 1. Since is_main_worktree_bare explains quite well what it does we can have\n> > a shorter explanation of `!worktree->is_current` part, something like:\n> >\n> > /* Additional checks are needed if main worktree is not current\n> > (checking from secondary worktree) */\n> > (!worktree->is_current && is_main_worktree_bare(the_repository));\n>\n> For somebody who thought about the issue themselves (like me, before\n> writing the message you are responding to), this shorter form would\n> suffice.  I'd rephrase it more like so\n>\n>     /* When a secondary worktree, an extra check is needed */\n>\n> for brevity, though.\n>\n>\n> > 2. Or a bit longer inline explanation that partially repeats the\n> > explanation of is_main_worktree_bare\n> > + adds explanation about efficiency:\n> >  /*\n> >   * When in a secondary worktree we have to also verify if the main worktree\n> >   * is bare in $commondir/config.worktree.\n> >   * This check is unnecessary if we're currently in the main worktree,\n> >   * as prior checks already consulted all configs of the current worktree.\n> >  */\n> > (!worktree->is_current && is_main_worktree_bare(the_repository));\n>\n> And this more extended version would have helped me by not having to\n> ask\n>\n>     Is \"this worktree does not have is_current bit set\" equivalent\n>     to \"this worktree is the main one, so is_main_worktree_bare()\n>     needs to be consulted\"?  That linkage between \"the is_current\n>     bit unset\" and \"is the main worktree\" is not obvious to me.\n>\n> in the first place.\n>\n> In short, both should work, and I personally find that the latter\n> may be a bit more helpful to readers.\n>\n> THanks.\n\nPerfect, I'll add the latter one then. Thank you!\n"},{"id":"511851","messageId":"pull.1829.v4.git.1738737014194.gitgitgadget@gmail.com","threadId":"62494","inReplyTo":"pull.1829.v3.git.1738346881907.gitgitgadget@gmail.com","subject":"[PATCH v4] worktree: detect from secondary worktree if main worktree is bare","fromName":"Olga Pilipenco via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-02-05T06:30:13Z","receivedAt":"2025-02-05T06:30:18Z","isPatch":true,"sender":{"key":"olga.pilipenco@shopify.com","avatar":"https://avatars.githubusercontent.com/u/70717429?v=4"},"body":"From: Olga Pilipenco <olga.pilipenco@shopify.com>\n\nWhen extensions.worktreeConfig is true and the main worktree is\nbare -- that is, its config.worktree file contains core.bare=true\n-- commands run from secondary worktrees incorrectly see the main\nworktree as not bare. As such, those commands incorrectly think\nthat the repository's default branch (typically \"main\" or\n\"master\") is checked out in the bare repository even though it's\nnot. This makes it impossible, for instance, to checkout or delete\nthe default branch from a secondary worktree, among other\nshortcomings.\n\nThis problem occurs because, when extensions.worktreeConfig is\ntrue, commands run in secondary worktrees only consult\n$commondir/config and $commondir/worktrees/<id>/config.worktree,\nthus they never see the main worktree's core.bare=true setting in\n$commondir/config.worktree.\n\nFix this problem by consulting the main worktree's config.worktree\nfile when checking whether it is bare. (This extra work is\nperformed only when running from a secondary worktree.)\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Olga Pilipenco <olga.pilipenco@shopify.com>\n---\n    worktree: detect from secondary worktree if main worktree is bare\n    \n    Changes since v3:\n    \n     * added a comment to explain why extra check is needed to detect if\n       main worktree bare\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1829%2Folga-mcbfe%2Ffix-bare-repo-detection-with-worktree-config-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1829/olga-mcbfe/fix-bare-repo-detection-with-worktree-config-v4\nPull-Request: https://github.com/gitgitgadget/git/pull/1829\n\nRange-diff vs v3:\n\n 1:  f9207746b38 ! 1:  a34459b5681 worktree: detect from secondary worktree if main worktree is bare\n     @@ worktree.c: static struct worktree *get_main_worktree(int skip_reading_head)\n       \tworktree->is_current = is_current_worktree(worktree);\n      +\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n      +\t\tis_bare_repository() ||\n     ++\t\t/*\n     ++\t\t * When in a secondary worktree we have to also verify if the main\n     ++\t\t * worktree is bare in $commondir/config.worktree.\n     ++\t\t * This check is unnecessary if we're currently in the main worktree,\n     ++\t\t * as prior checks already consulted all configs of the current worktree.\n     ++\t\t */\n      +\t\t(!worktree->is_current && is_main_worktree_bare(the_repository));\n      +\n       \tif (!skip_reading_head)\n\n\n t/t3200-branch.sh | 14 ++++++++++++++\n worktree.c        | 41 ++++++++++++++++++++++++++++++++---------\n 2 files changed, 46 insertions(+), 9 deletions(-)\n\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex a3a21c54cf6..f3e720dc10d 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -410,6 +410,20 @@ test_expect_success 'bare main worktree has HEAD at branch deleted by secondary\n \tgit -C secondary branch -D main\n '\n \n+test_expect_success 'secondary worktrees recognize core.bare=true in main config.worktree' '\n+\ttest_when_finished \"rm -rf bare_repo non_bare_repo secondary_worktree\" &&\n+\tgit init -b main non_bare_repo &&\n+\ttest_commit -C non_bare_repo x &&\n+\n+\tgit clone --bare non_bare_repo bare_repo &&\n+\tgit -C bare_repo config extensions.worktreeConfig true &&\n+\tgit -C bare_repo config unset core.bare &&\n+\tgit -C bare_repo config --worktree core.bare true &&\n+\n+\tgit -C bare_repo worktree add ../secondary_worktree &&\n+\tgit -C secondary_worktree checkout main\n+'\n+\n test_expect_success 'git branch --list -v with --abbrev' '\n \ttest_when_finished \"git branch -D t\" &&\n \tgit branch t &&\ndiff --git a/worktree.c b/worktree.c\nindex 248bbb39d43..d4a68c9c235 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -65,6 +65,28 @@ static int is_current_worktree(struct worktree *wt)\n \treturn is_current;\n }\n \n+/*\n+* When in a secondary worktree, and when extensions.worktreeConfig\n+* is true, only $commondir/config and $commondir/worktrees/<id>/\n+* config.worktree are consulted, hence any core.bare=true setting in\n+* $commondir/config.worktree gets overlooked. Thus, check it manually\n+* to determine if the repository is bare.\n+*/\n+static int is_main_worktree_bare(struct repository *repo)\n+{\n+\tint bare = 0;\n+\tstruct config_set cs = {0};\n+\tchar *worktree_config = xstrfmt(\"%s/config.worktree\", repo_get_common_dir(repo));\n+\n+\tgit_configset_init(&cs);\n+\tgit_configset_add_file(&cs, worktree_config);\n+\tgit_configset_get_bool(&cs, \"core.bare\", &bare);\n+\n+\tgit_configset_clear(&cs);\n+\tfree(worktree_config);\n+\treturn bare;\n+}\n+\n /**\n  * get the main worktree\n  */\n@@ -79,16 +101,17 @@ static struct worktree *get_main_worktree(int skip_reading_head)\n \tCALLOC_ARRAY(worktree, 1);\n \tworktree->repo = the_repository;\n \tworktree->path = strbuf_detach(&worktree_path, NULL);\n-\t/*\n-\t * NEEDSWORK: If this function is called from a secondary worktree and\n-\t * config.worktree is present, is_bare_repository_cfg will reflect the\n-\t * contents of config.worktree, not the contents of the main worktree.\n-\t * This means that worktree->is_bare may be set to 0 even if the main\n-\t * worktree is configured to be bare.\n-\t */\n-\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n-\t\tis_bare_repository();\n \tworktree->is_current = is_current_worktree(worktree);\n+\tworktree->is_bare = (is_bare_repository_cfg == 1) ||\n+\t\tis_bare_repository() ||\n+\t\t/*\n+\t\t * When in a secondary worktree we have to also verify if the main\n+\t\t * worktree is bare in $commondir/config.worktree.\n+\t\t * This check is unnecessary if we're currently in the main worktree,\n+\t\t * as prior checks already consulted all configs of the current worktree.\n+\t\t */\n+\t\t(!worktree->is_current && is_main_worktree_bare(the_repository));\n+\n \tif (!skip_reading_head)\n \t\tadd_head_info(worktree);\n \treturn worktree;\n\nbase-commit: f93ff170b93a1782659637824b25923245ac9dd1\n-- \ngitgitgadget\n"}]}