{"thread":{"id":"54324","subject":"GIT_DIR output from git worktree list","startedAt":"2020-09-29T17:30:54Z","lastAt":"2020-12-13T06:08:07Z","messageCount":9,"participants":["Gabriel Nützi","Eric Sunshine","Ganriel Nützi","Duy Nguyen"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"406635","messageId":"CAA9rTudStQOK7kRa6jYJHE3D3grnFF3idQe65h9t6oJB30xa1A@mail.gmail.com","threadId":"54324","inReplyTo":null,"subject":"GIT_DIR output from git worktree list","fromName":"Gabriel Nützi","fromEmail":"gnuetzi@gmail.com","sentAt":"2020-09-29T17:30:40Z","receivedAt":"2020-09-29T17:30:54Z","isPatch":false,"sender":{"key":"gnuetzi@gmail.com","avatar":"https://gravatar.com/avatar/eda99c30173930819ae1c26c3dff249edd4b3c735c3886d268c0d6a71c6307c9?d=mp&s=160"},"body":"Hello,\n\nWhen you do move the .git folder somewhere else:\n\ngit init Test && cd Test && mv .git .git-b\n\nand then do\n\ncd Test &&\ngit --git-dir=.git-b --work-tree . worktree list\n\nthe output is :\n..../Test/.git-b  0000000 [master]\n\nWhy is the output a .git Dir and not a worktree. I expected `.../Test`.\n\nThanks for the help.\nBR Gabriel\n"},{"id":"406658","messageId":"CAPig+cTwNwt+_f4FYDqy5xVsDVU3pqfKXtK6GKtWLLqbU6Y8Vg@mail.gmail.com","threadId":"54324","inReplyTo":"CAA9rTudStQOK7kRa6jYJHE3D3grnFF3idQe65h9t6oJB30xa1A@mail.gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2020-09-30T05:33:11Z","receivedAt":"2020-09-30T05:33:26Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Sep 29, 2020 at 1:31 PM Gabriel Nützi <gnuetzi@gmail.com> wrote:\n> When you do move the .git folder somewhere else:\n>\n> git init Test && cd Test && mv .git .git-b\n> git --git-dir=.git-b --work-tree . worktree list\n>\n> the output is :\n> ..../Test/.git-b  0000000 [master]\n>\n> Why is the output a .git Dir and not a worktree. I expected `.../Test`.\n\nBecause the implementation doesn't really look for or know where the\nmain worktree is. Instead, it's taking the path of the repository and\nstripping off \"/.git\" if present, and reporting that as the main\nworktree. Under normal circumstances this works just fine because\neither (a) the repository _is_ the .git directory within the main\nworktree, or (b) it's a bare repository and there is no main worktree,\nthus it reports the repository path instead of the non-existent main\nworktree.\n\nI suppose one way to fix this would be to specially check if\n--work-tree or GIT_WORK_TREE is specified and use that value as the\npath of the main worktree. (This special case would only be used when\ncomputing the main worktree path; it would not be used when computing\nlinked worktree paths.)\n"},{"id":"406660","messageId":"335b6e6a-9527-5444-da80-08faf43db606@gmail.com","threadId":"54324","inReplyTo":"CAPig+cTwNwt+_f4FYDqy5xVsDVU3pqfKXtK6GKtWLLqbU6Y8Vg@mail.gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Gabriel Nützi","fromEmail":"gnuetzi@gmail.com","sentAt":"2020-09-30T07:34:42Z","receivedAt":"2020-09-30T07:34:48Z","isPatch":false,"sender":{"key":"gnuetzi@gmail.com","avatar":"https://gravatar.com/avatar/eda99c30173930819ae1c26c3dff249edd4b3c735c3886d268c0d6a71c6307c9?d=mp&s=160"},"body":"Am 30.09.2020 um 07:33 schrieb Eric Sunshine:\n\n> On Tue, Sep 29, 2020 at 1:31 PM Gabriel Nützi <gnuetzi@gmail.com> wrote:\n>> When you do move the .git folder somewhere else:\n>>\n>> git init Test && cd Test && mv .git .git-b\n>> git --git-dir=.git-b --work-tree . worktree list\n>>\n>> the output is :\n>> ..../Test/.git-b  0000000 [master]\n>>\n>> Why is the output a .git Dir and not a worktree. I expected `.../Test`.\n> Because the implementation doesn't really look for or know where the\n> main worktree is. Instead, it's taking the path of the repository and\n> stripping off \"/.git\" if present, and reporting that as the main\n> worktree. Under normal circumstances this works just fine because\n> either (a) the repository _is_ the .git directory within the main\n> worktree, or (b) it's a bare repository and there is no main worktree,\n> thus it reports the repository path instead of the non-existent main\n> worktree.\n\nOk, it's kind of sad that as far as I understood, there is probably always only this heuristic about stripping \".git\"\nbecause the main worktree path is not registered inside the .git dir or somewhere else.\nI mean you can technically name the git directory what ever you want and in that case how is that stripping than going to work?\nBut of course when you do so, you probably need to set either GIT_DIR, or or use --git-dir\n\nAlso what I realized is, isn't the doc a bit inexact about GIT_DIR:\n\n> GIT_DIR is the location of the .git folder.\n\nShouldn't it read: GIT_DIR is the path of the .git folder. Location could mean its the parent folder? Hm...\n\n> I suppose one way to fix this would be to specially check if\n> --work-tree or GIT_WORK_TREE is specified and use that value as the\n> path of the main worktree. (This special case would only be used when\n> computing the main worktree path; it would not be used when computing\n> linked worktree paths.)\n\nProbably, --work-tree, GIT_WORK_TREE or core.worktree.\n\nThanks Eric!\nBR\n\n"},{"id":"406706","messageId":"CAPig+cSzVVdiH62Gc-ww56OFfPh4-2+3_paWEJf085TgC=nNvg@mail.gmail.com","threadId":"54324","inReplyTo":"335b6e6a-9527-5444-da80-08faf43db606@gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2020-09-30T20:35:31Z","receivedAt":"2020-09-30T20:36:04Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Sep 30, 2020 at 3:34 AM Gabriel Nützi <gnuetzi@gmail.com> wrote:\n> Am 30.09.2020 um 07:33 schrieb Eric Sunshine:\n> > Because the implementation doesn't really look for or know where the\n> > main worktree is. Instead, it's taking the path of the repository and\n> > stripping off \"/.git\" if present, and reporting that as the main\n> > worktree. [...]\n>\n> Ok, it's kind of sad that as far as I understood, there is probably\n> always only this heuristic about stripping \".git\" because the main\n> worktree path is not registered inside the .git dir or somewhere\n> else.\n\nThis is, of course, an artifact of Git's history. While Git itself is\nreasonably old at this point, linked worktrees are a relatively recent\ninvention, and there had never been a reason to record the path of the\nmain worktree anywhere. Also, not recording the path allows the\nproject directory to be relocated easily if needed.\n\n> I mean you can technically name the git directory what ever you want\n> and in that case how is that stripping than going to work?  But of\n> course when you do so, you probably need to set either GIT_DIR, or\n> or use --git-dir\n\nI don't think it's accurate to say that \"you can technically name the\ngit directory what ever you want\". The only _blessed_ name for that\ndirectory is `.git/` if you expect any sort of automatic behavior\nregarding repository discovery and determination of the top level of a\nworktree.\n\nCertainly, you're free to locate the repository elsewhere -- or name\nit something else -- but when you do so, you knowingly give up the\nconvenience of the automatic behaviors, and instead have to rely upon\n--git-dir, GIT_DIR and --worktree, GIT_WORK_TREE, and core.worktree,\nwhich is why those exist.\n\n(Rather than --git-dir, --git-work-tree and cousins, you could also\nplace a \"gitfile\" named `.git` in the main worktree referencing the\nactual repository path, and by doing so regain all the automatic\nbehaviors. Alternately, `git init --separate-git-dir` could set this\nup for you.)\n\nAt any rate, the above comments aside, as mentioned previously, a way\nforward might be to teach git-worktree to consult --work-tree,\nGIT_WORK_TREE, core.worktree when computing the path of the main\nworktree. The fact that it doesn't yet do so is merely an oversight, I\nthink; one of the several such oversights during initial development,\nsome of which have already been corrected.\n\n> Also what I realized is, isn't the doc a bit inexact about GIT_DIR:\n>\n> > GIT_DIR is the location of the .git folder.\n>\n> Shouldn't it read: GIT_DIR is the path of the .git folder. Location\n> could mean its the parent folder? Hm...\n\nThat might indeed be an improvement. Perhaps submit a patch making\nthis change?\n"},{"id":"411584","messageId":"CAPig+cTsEx-puHn1N2=fBVAgdvc7cutCDTC7vBJuLm5utObfJg@mail.gmail.com","threadId":"54324","inReplyTo":"CAPig+cTwNwt+_f4FYDqy5xVsDVU3pqfKXtK6GKtWLLqbU6Y8Vg@mail.gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2020-12-07T04:34:15Z","receivedAt":"2020-12-07T04:35:27Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Sep 30, 2020 at 1:33 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Tue, Sep 29, 2020 at 1:31 PM Gabriel Nützi <gnuetzi@gmail.com> wrote:\n> > When you do move the .git folder somewhere else:\n> > git init Test && cd Test && mv .git .git-b\n> > git --git-dir=.git-b --work-tree . worktree list\n> > the output is :\n> > ..../Test/.git-b  0000000 [master]\n> > Why is the output a .git Dir and not a worktree. I expected `.../Test`.\n>\n> I suppose one way to fix this would be to specially check if\n> --work-tree or GIT_WORK_TREE is specified and use that value as the\n> path of the main worktree. (This special case would only be used when\n> computing the main worktree path; it would not be used when computing\n> linked worktree paths.)\n\nFixing this is more complex than it seems at first glance. In fact,\nI'm not sure there is a good fix at present without somehow recording\nthe location of the main worktree somewhere.\n\nThe problem is that determining the location of the main worktree by\nconsulting --work-tree or GIT_WORK_TREE when listing worktrees would\nonly give the desired result when `git worktree list` is run from\nwithin the main worktree. If it is run within a secondary worktree,\nthen neither --work-tree nor GIT_WORK_TREE would be referencing the\nmain worktree (at best, they'd be referencing the secondary worktree),\nwhich means that they would not help in determining the location of\nthe main worktree, thus `git worktree list` in a secondary worktree\nwould give different output.\n\nConsulting core.worktree _may_ be doable, but it's iffy and would be\nextra complicated because that configuration value is treated\nspecially. In particular, the value of core.worktree of the main\nworktree is intentionally hidden from secondary worktrees. So, while\nit _may_ be possible to write special-purpose code to go and seek out\nthe value of core.worktree for the main worktree by manually\nspelunking various configuration files, it would be complicated. In\nfact, it would be doubly complicated because it would require two\ndistinct implementations: one for when extensions.worktreeConfig is\nenabled and one for when it is not.\n\nThe fact that the output of `git worktree list` would differ depending\nupon whether it is run in the main worktree or a secondary worktree\n(and whether core.worktree is configured or --work-tree or\nGIT_WORK_TREE is used) makes me quite hesitant about these approaches.\nI worry that such inconsistency would be perceived as instability, as\nwell as making it difficult to script `git worktree` reliably.\n\nSo, at present, I think any solution which can produce reliable,\nconsistent output may need to record the path of the main worktree\nsomewhere, but I haven't thought too deeply yet about how to do that\ncleanly (while also taking other Git implementations into account).\n"},{"id":"411586","messageId":"A462326B-505D-4A92-B789-21BB8FE6AA16@gmail.com","threadId":"54324","inReplyTo":"CAPig+cTsEx-puHn1N2=fBVAgdvc7cutCDTC7vBJuLm5utObfJg@mail.gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Ganriel Nützi","fromEmail":"gnuetzi@gmail.com","sentAt":"2020-12-07T07:28:28Z","receivedAt":"2020-12-07T07:29:16Z","isPatch":false,"sender":{"key":"gnuetzi@gmail.com","avatar":"https://gravatar.com/avatar/eda99c30173930819ae1c26c3dff249edd4b3c735c3886d268c0d6a71c6307c9?d=mp&s=160"},"body":"As of your rational, wouldn‘t it be good anyway to have a file „worktree“ inside the .git dir (of the worktree) containing the path to the main worktree? So to speak any worktree always has a .git dir with a back link to its main worktree (the existence of the git dir might pose other problems?) \n\n\n\nVon meinem iPhone gesendet\n\n> Am 07.12.2020 um 05:34 schrieb Eric Sunshine <sunshine@sunshineco.com>:\n> \n>> On Wed, Sep 30, 2020 at 1:33 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n>>> On Tue, Sep 29, 2020 at 1:31 PM Gabriel Nützi <gnuetzi@gmail.com> wrote:\n>>> When you do move the .git folder somewhere else:\n>>> git init Test && cd Test && mv .git .git-b\n>>> git --git-dir=.git-b --work-tree . worktree list\n>>> the output is :\n>>> ..../Test/.git-b  0000000 [master]\n>>> Why is the output a .git Dir and not a worktree. I expected `.../Test`.\n>> \n>> I suppose one way to fix this would be to specially check if\n>> --work-tree or GIT_WORK_TREE is specified and use that value as the\n>> path of the main worktree. (This special case would only be used when\n>> computing the main worktree path; it would not be used when computing\n>> linked worktree paths.)\n> \n> Fixing this is more complex than it seems at first glance. In fact,\n> I'm not sure there is a good fix at present without somehow recording\n> the location of the main worktree somewhere.\n> \n> The problem is that determining the location of the main worktree by\n> consulting --work-tree or GIT_WORK_TREE when listing worktrees would\n> only give the desired result when `git worktree list` is run from\n> within the main worktree. If it is run within a secondary worktree,\n> then neither --work-tree nor GIT_WORK_TREE would be referencing the\n> main worktree (at best, they'd be referencing the secondary worktree),\n> which means that they would not help in determining the location of\n> the main worktree, thus `git worktree list` in a secondary worktree\n> would give different output.\n> \n> Consulting core.worktree _may_ be doable, but it's iffy and would be\n> extra complicated because that configuration value is treated\n> specially. In particular, the value of core.worktree of the main\n> worktree is intentionally hidden from secondary worktrees. So, while\n> it _may_ be possible to write special-purpose code to go and seek out\n> the value of core.worktree for the main worktree by manually\n> spelunking various configuration files, it would be complicated. In\n> fact, it would be doubly complicated because it would require two\n> distinct implementations: one for when extensions.worktreeConfig is\n> enabled and one for when it is not.\n> \n> The fact that the output of `git worktree list` would differ depending\n> upon whether it is run in the main worktree or a secondary worktree\n> (and whether core.worktree is configured or --work-tree or\n> GIT_WORK_TREE is used) makes me quite hesitant about these approaches.\n> I worry that such inconsistency would be perceived as instability, as\n> well as making it difficult to script `git worktree` reliably.\n> \n> So, at present, I think any solution which can produce reliable,\n> consistent output may need to record the path of the main worktree\n> somewhere, but I haven't thought too deeply yet about how to do that\n> cleanly (while also taking other Git implementations into account).\n"},{"id":"411736","messageId":"CAPig+cQad_yyVgwQQ-NZyy7SergM-fUMeMAm9RsV4zEqNt9TDg@mail.gmail.com","threadId":"54324","inReplyTo":"A462326B-505D-4A92-B789-21BB8FE6AA16@gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2020-12-08T19:27:34Z","receivedAt":"2020-12-08T20:42:24Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Mon, Dec 7, 2020 at 2:28 AM Ganriel Nützi <gnuetzi@gmail.com> wrote:\n> As of your rational, wouldn‘t it be good anyway to have a file\n> „worktree“ inside the .git dir (of the worktree) containing the path\n> to the main worktree? So to speak any worktree always has a .git dir\n> with a back link to its main worktree (the existence of the git dir\n> might pose other problems?)\n\nI'm having trouble understanding your proposal. Secondary worktrees\ndon't have a .git/ directory; instead they have a \"gitfile\" named .git\nwhich is just a pointer to the worktree's administrative directory\nwithin the repository (specifically, <repo>/worktrees/<id>/, where\n<repo> might be a bare repository or the .git/ directory of the main\nworktree).\n\nSo a secondary worktree's back-link is a pointer into the repository;\nit's not necessarily pointing at the main worktree, as you discovered\nand reported in the email which started this thread.\n\nWhat I was suggesting was that it might be the case that the only way\nto solve this would be to store the location of the main worktree\nsomewhere within <repo> in some file, and then `git worktree list`\nwould consult that file to learn the location of the main worktree.\nThis is complicated by the fact that that file would have to be\nupdated automatically if the main worktree directory is ever moved. It\nalso needs to be done in such a way that it is easy for other Git\nimplementations to understand and not trip over.\n\nSo, it's certainly possible, but it's something which requires more\nthought, and there might be alternate and better solutions I'm\noverlooking.\n"},{"id":"411832","messageId":"CACsJy8Dq9Lz1YkkqvR0Fa1PLUBJhNJTmLtnRssi=DDpWAQTWww@mail.gmail.com","threadId":"54324","inReplyTo":"CAPig+cQad_yyVgwQQ-NZyy7SergM-fUMeMAm9RsV4zEqNt9TDg@mail.gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2020-12-09T09:30:58Z","receivedAt":"2020-12-09T09:32:07Z","isPatch":false,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Dec 9, 2020 at 2:27 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n> What I was suggesting was that it might be the case that the only way\n> to solve this would be to store the location of the main worktree\n> somewhere within <repo> in some file, and then `git worktree list`\n> would consult that file to learn the location of the main worktree.\n> This is complicated by the fact that that file would have to be\n> updated automatically if the main worktree directory is ever moved. It\n> also needs to be done in such a way that it is easy for other Git\n> implementations to understand and not trip over.\n\nNot that it matters because I don't do Git anymore. But when I was\ndoing this I imagined unify main and secondary worktrees. When you\nswitch from single to multiple worktrees, there is no main worktree\nanymore. The place that is main worktree still has your files, but it\nhas the same status and treatment as any other worktree. And the repo\nis tucked away somewhere safe.\n-- \nDuy\n"},{"id":"412130","messageId":"CAPig+cRZ-018Krj2fGq0hx5qNptW8qUiwhkws8ixvk242Z_b0g@mail.gmail.com","threadId":"54324","inReplyTo":"CACsJy8Dq9Lz1YkkqvR0Fa1PLUBJhNJTmLtnRssi=DDpWAQTWww@mail.gmail.com","subject":"Re: GIT_DIR output from git worktree list","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2020-12-13T06:07:08Z","receivedAt":"2020-12-13T06:08:07Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Dec 9, 2020 at 4:31 AM Duy Nguyen <pclouds@gmail.com> wrote:\n> On Wed, Dec 9, 2020 at 2:27 AM Eric Sunshine <sunshine@sunshineco.com> wrote:\n> > What I was suggesting was that it might be the case that the only way\n> > to solve this would be to store the location of the main worktree\n> > somewhere within <repo> in some file, and then `git worktree list`\n> > would consult that file to learn the location of the main worktree.\n>\n> Not that it matters because I don't do Git anymore. But when I was\n> doing this I imagined unify main and secondary worktrees. When you\n> switch from single to multiple worktrees, there is no main worktree\n> anymore. The place that is main worktree still has your files, but it\n> has the same status and treatment as any other worktree. And the repo\n> is tucked away somewhere safe.\n\nI had some vague thoughts along those lines, as well, but such a\nchange is outside the scope of my limited time these days.\n\nSuperficially, the idea I expressed above, of storing the path of the\nmain worktree in some file within the repository (let's say\n`.git/mainworktreepath`) doesn't seem like it should be too difficult\n(though I haven't really thought it through). And, if the main\nworktree does get moved, the file could be fixed up or kept up-to-date\nby the `git worktree repair` command.\n\n(By the way, thanks for responding. It's nice to hear from you again\nafter your long absence.)\n"}]}