Re: [PATCH v2 07/11] git-gui: try harder to find worktree from gitdir
- From
Mark Levedahl <mlevedahl@gmail.com>
- Date
- May 22, 2026, 16:57 UTC
- Message-ID
- <8d03455a-82dc-4d32-aa57-af3d92714e31@gmail.com>
- In-Reply-To
- <tencent_9FA5CF10A1D7CAC770366E84AD5CA9A2D107@qq.com>
On 5/22/26 11:09 AM, Shroom Moo wrote:
Show 74 quoted lines
> On 5/22/26 1:45 AM, Mark Levedahl wrote:
>>> Additionally, [file exists {gitdir}] checks for the gitdir file in
>>> the current working directory. Since the function has not yet
>>> switched to $_gitdir when this check runs, it is almost impossible
>>> to find the file. Consequently, this logic never triggers, preventing
>>> linked worktrees from being recognized.
>>>
>>> Maybe the identification of linked worktree should not directly look
>>> for the gitdir file, but should check whether there is a.git file and
>>> its content points to... /.git/worktrees/... ? Anyways, using the
>>> literal {gitdir} to search in the current directory lead to risks.
>>>
>>> Shroom
>>>
>> We cannot get to this code if not inside the gitdir, and if the user set GIT_DIR and/or
>> GIT_WORK_TREE to do something clever, that either worked or the code already threw an
>> error. git, without GIT_WORK_TREE set, uses the current directory as the worktree, or the
>> parent directory containing .git. So, we must be inside the gitdir if this code path gets hit.
>>
>> Mark
> The relative path issue with {gitdir} is indeed difficult to trigger
> in practice. To trigger this problem, the following conditions must
> be met simultaneously:
> - git rev-parse --absolute-git-dir succeeds (the repository is valid).
> - git rev-parse --show-toplevel fails (the working tree is
> undetectable).
> - The current directory is not $_gitdir.
> - A gitdir file exists under $_gitdir (i.e., the gitdir of a linked
> worktree).
>
> Unluckily, this occurs in:
>
> ```MINGW64 Shell
> # Firstly, enter a test folder
> mkdir test-main
> cd test-main
>
> git init
>
> echo main > file.txt
> git add file.txt
> git commit -m "initial"
>
> # In feature branch
> git branch feature
>
> # Add worktree
> git worktree add ../test-feature feature
>
> cd ../
> cd test-feature
> cat .git
>
> cd ../
>
> mkdir outside
> cd outside
>
> export GIT_DIR="/(PREVIOUSPATH)/test-main/.git/worktrees/feature"
> unset GIT_WORK_TREE
> git gui blame HEAD file.txt --trace
> ```
>
> ```Wish
> puts $_gitworktree
> ```
>
> The current directory is "outside", not the expected linked worktree.
> [file exists {gitdir}] in find_worktree_from_gitdir searched in the
> current directory but failed, resulting in the inability to identify
> linked worktree. The blame mode can still work without worktree.
>
> Shroom
>You exported GIT_DIR pointing to a valid gitdir, and as documented git takes your current directory ("outside") to be the worktree. This is working as expected. Had finding a worktree failed, git-gui would issue an error message and stop because you defined GIT_DIR.
Mark