Re: [PATCH v2 07/11] git-gui: try harder to find worktree from gitdir
- From
- Shroom Moo <egg_mushroomcow@foxmail.com>
- Date
- May 22, 2026, 15:09 UTC
- Message-ID
- <tencent_9FA5CF10A1D7CAC770366E84AD5CA9A2D107@qq.com>
- In-Reply-To
- <780a52d9-7119-4870-990a-16280005503d@gmail.com>
On 5/22/26 1:45 AM, Mark Levedahl wrote:
Show 20 quoted lines
> >
> > 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.
>
> MarkThe 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