{"thread":{"id":"57820","subject":"Able to checkout same branch in multiple worktrees when using symbolic-refs","startedAt":"2022-04-29T19:06:13Z","lastAt":"2022-05-02T18:30:45Z","messageCount":4,"participants":["Austin Morton","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"454633","messageId":"CAAir=1MbwGtONW7yWRWoXKzAiwqwhOAqdhfWYMYLxt1vANuUOA@mail.gmail.com","threadId":"57820","inReplyTo":null,"subject":"Able to checkout same branch in multiple worktrees when using symbolic-refs","fromName":"Austin Morton","fromEmail":"austin.morton@aquatic.com","sentAt":"2022-04-29T19:05:56Z","receivedAt":"2022-04-29T19:06:13Z","isPatch":false,"sender":{"key":"austin.morton@aquatic.com","avatar":null},"body":"Hello,\n\nWhen using a symbolic-ref I am able to inadvertently checkout the same\nbranch in multiple worktrees when using the symbolic-ref name, despite\nbeing prevented from doing so if I use the target branch name.\n\nBelow is a minimal reproducer:\n\n$ git --version\ngit version 2.36.0\n$ git init .\n$ git status\n$ git commit --allow-empty -m \"Initial commit\"\n$ git symbolic-ref refs/heads/main refs/heads/master\n$ git worktree add ../worktree2\n$ git worktree list\n/home/amorton/test/worktree1 cd8312d [master]\n/home/amorton/test/worktree2 cd8312d [worktree2]\n$ cd ../worktree2\n$ git checkout master\nfatal: 'master' is already checked out at '/home/amorton/test/worktree1'\n$ git checkout main\nSwitched to branch 'main'\n$ git worktree list\n/home/amorton/test/worktree1 cd8312d [master]\n/home/amorton/test/worktree2 cd8312d [master]\n\n\n-- \nAustin Morton\naustin.morton@aquatic.com\n"},{"id":"454638","messageId":"xmqqsfpvabib.fsf@gitster.g","threadId":"57820","inReplyTo":"CAAir=1MbwGtONW7yWRWoXKzAiwqwhOAqdhfWYMYLxt1vANuUOA@mail.gmail.com","subject":"Re: Able to checkout same branch in multiple worktrees when using symbolic-refs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-04-29T20:19:08Z","receivedAt":"2022-04-29T20:19:16Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Austin Morton <austin.morton@aquatic.com> writes:\n\n> When using a symbolic-ref I am able to inadvertently checkout the same\n> branch in multiple worktrees when using the symbolic-ref name, despite\n> being prevented from doing so if I use the target branch name.\n\nDon't do that if it hurts ;-)\n\nIf you checked out 'main' in the secondary worktree and made a\ncommit there, the symbolic-ref makes 'master' to be updated,\nconfusing the primary worktree whose index and the working tree\nfiles record work relative to the old commit before the secondary\nworktree updated it via the symbolic-ref, which is exactly the same\nsituation that the \"do not check out the same branch in two\ndifferent worktrees\" protection tries to save the user from, but I\ndo not think we currently do so.\n\nBecause this \"do not check a branch out twice and let it be futzed\nwith from the two places\" is primarily a mechanism to help\ninexperienced users from getting confused (and there is an escape\nhatch mechanism to allow it to those who know what they are doing),\nand use of symbolic-ref to make 'main' and 'master' synonyms with\neach other is not something inexperienced users who have no clue on\nwhat they are doing would be doing anyway, it may not be a huge\ndeal.\n\nI also suspect there is no way, other than scanning _all_ the local\nbranches, to see if some other branch aliases the branch we are\nabout to check out.  It may potentially be somewhat expensive.\n\nBut it would be nice if it can get fixed in inexpensively.\n\nThanks for a report.\n\n\n> Below is a minimal reproducer:\n>\n> $ git --version\n> git version 2.36.0\n> $ git init .\n> $ git status\n> $ git commit --allow-empty -m \"Initial commit\"\n> $ git symbolic-ref refs/heads/main refs/heads/master\n> $ git worktree add ../worktree2\n> $ git worktree list\n> /home/amorton/test/worktree1 cd8312d [master]\n> /home/amorton/test/worktree2 cd8312d [worktree2]\n> $ cd ../worktree2\n> $ git checkout master\n> fatal: 'master' is already checked out at '/home/amorton/test/worktree1'\n> $ git checkout main\n> Switched to branch 'main'\n> $ git worktree list\n> /home/amorton/test/worktree1 cd8312d [master]\n> /home/amorton/test/worktree2 cd8312d [master]\n"},{"id":"454720","messageId":"CAAir=1NY=98Z_cTrEyUn6tcPFR3UGNUmXs=2hg27LMGijGZpUw@mail.gmail.com","threadId":"57820","inReplyTo":"xmqqsfpvabib.fsf@gitster.g","subject":"Re: Able to checkout same branch in multiple worktrees when using symbolic-refs","fromName":"Austin Morton","fromEmail":"austin.morton@aquatic.com","sentAt":"2022-05-02T14:54:33Z","receivedAt":"2022-05-02T14:54:49Z","isPatch":false,"sender":{"key":"austin.morton@aquatic.com","avatar":null},"body":"By your own definition I am not an inexperienced user (since I am\nusing worktrees and symbolic-refs), but this has bitten me on enough\noccasions to beat me into submission and submit a bug report ;-)\n\nI don't know why you would need to scan all the local branches, unless\nthat is what \"git worktree list\" ends up doing under the hood?\n\n\"HEAD\" in a worktree that was checked out using the symbolic-ref still\ncontains the symbolic ref name, while the worktree list resolves it.\n\n$ cat .git/worktrees/worktree2/HEAD\nref: refs/heads/main\n$ git worktree list\n/home/amorton/test/worktree1 cd8312d [master]\n/home/amorton/test/worktree2 cd8312d [master]\n\nSeems like you would \"just\" need to resolve the symbolic-ref in the\nsame way that the worktree code does when checking against existing\nworktrees in find_shared_symref during checkout.\n\nCheers,\nAustin\n\nOn Fri, Apr 29, 2022 at 8:19 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Austin Morton <austin.morton@aquatic.com> writes:\n>\n> > When using a symbolic-ref I am able to inadvertently checkout the same\n> > branch in multiple worktrees when using the symbolic-ref name, despite\n> > being prevented from doing so if I use the target branch name.\n>\n> Don't do that if it hurts ;-)\n>\n> If you checked out 'main' in the secondary worktree and made a\n> commit there, the symbolic-ref makes 'master' to be updated,\n> confusing the primary worktree whose index and the working tree\n> files record work relative to the old commit before the secondary\n> worktree updated it via the symbolic-ref, which is exactly the same\n> situation that the \"do not check out the same branch in two\n> different worktrees\" protection tries to save the user from, but I\n> do not think we currently do so.\n>\n> Because this \"do not check a branch out twice and let it be futzed\n> with from the two places\" is primarily a mechanism to help\n> inexperienced users from getting confused (and there is an escape\n> hatch mechanism to allow it to those who know what they are doing),\n> and use of symbolic-ref to make 'main' and 'master' synonyms with\n> each other is not something inexperienced users who have no clue on\n> what they are doing would be doing anyway, it may not be a huge\n> deal.\n>\n> I also suspect there is no way, other than scanning _all_ the local\n> branches, to see if some other branch aliases the branch we are\n> about to check out.  It may potentially be somewhat expensive.\n>\n> But it would be nice if it can get fixed in inexpensively.\n>\n> Thanks for a report.\n>\n>\n> > Below is a minimal reproducer:\n> >\n> > $ git --version\n> > git version 2.36.0\n> > $ git init .\n> > $ git status\n> > $ git commit --allow-empty -m \"Initial commit\"\n> > $ git symbolic-ref refs/heads/main refs/heads/master\n> > $ git worktree add ../worktree2\n> > $ git worktree list\n> > /home/amorton/test/worktree1 cd8312d [master]\n> > /home/amorton/test/worktree2 cd8312d [worktree2]\n> > $ cd ../worktree2\n> > $ git checkout master\n> > fatal: 'master' is already checked out at '/home/amorton/test/worktree1'\n> > $ git checkout main\n> > Switched to branch 'main'\n> > $ git worktree list\n> > /home/amorton/test/worktree1 cd8312d [master]\n> > /home/amorton/test/worktree2 cd8312d [master]\n"},{"id":"454737","messageId":"xmqq7d73zt0x.fsf@gitster.g","threadId":"57820","inReplyTo":"CAAir=1NY=98Z_cTrEyUn6tcPFR3UGNUmXs=2hg27LMGijGZpUw@mail.gmail.com","subject":"Re: Able to checkout same branch in multiple worktrees when using symbolic-refs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-05-02T18:30:38Z","receivedAt":"2022-05-02T18:30:45Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Austin Morton <austin.morton@aquatic.com> writes:\n\n[jc: swapped the blocks to chronological order, and trimmed parts\nthat are not relevant to this reply].\n\n> On Fri, Apr 29, 2022 at 8:19 PM Junio C Hamano <gitster@pobox.com> wrote:\n>>\n>> Austin Morton <austin.morton@aquatic.com> writes:\n>>\n>> I also suspect there is no way, other than scanning _all_ the local\n>> branches, to see if some other branch aliases the branch we are\n>> about to check out.  It may potentially be somewhat expensive.\n>\n> I don't know why you would need to scan all the local branches, unless\n> that is what \"git worktree list\" ends up doing under the hood?\n> ...\n> Seems like you would \"just\" need to resolve the symbolic-ref in the\n> same way that the worktree code does when checking against existing\n> worktrees in find_shared_symref during checkout.\n\nYeah, you're right.\n\nYou would scan all the worktrees instead of asking all the\nlocal branches \"Are you a symlink that points at a branch I am about\nto check out?  If so, yell loudly to stop me.\"\n\n>> But it would be nice if it can get fixed in inexpensively.\n>>\n>> Thanks for a report.\n\nSo, yeah, it may be doable to fix it inexpensively.\n\nPatches welcome ;-)\n\nThanks.\n"}]}