Fix Repo() autodiscovery in linked worktrees when GIT_DIR is set#2128
Conversation
a98c43b to
b0354d5
Compare
April 20, 2026 10:44
There was a problem hiding this comment.
Pull request overview
Fixes git.Repo() autodiscovery when invoked from within a linked worktree and GIT_DIR is set to that worktree’s internal git directory (e.g. .git/worktrees/<id>), preventing erroneous InvalidGitRepositoryError.
Changes:
- Added a regression test covering
Repo()autodiscovery in a linked worktree withGIT_DIRset. - Updated repository discovery to recognize linked-worktree git directories by their metadata files.
- Derived the correct working tree directory from linked worktree metadata during discovery.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
test/test_repo.py |
Adds regression coverage ensuring Repo() works when GIT_DIR is set inside a linked worktree. |
git/repo/base.py |
Extends autodiscovery logic to detect linked-worktree git dirs and compute the correct working tree directory. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
Byron
left a comment
There was a problem hiding this comment.
Thanks.
In all honesty, I'm unable to tell if this makes any sense and if this is what Git does. But all we have is tests and as it doesn't break anything probably it's good enough.
Something I'd appreciate is the error message of the test when the patch here is not applied.
Besides that, I think a couple of changes are necessary.
Sorry, something went wrong.
b0354d5 to
3a94f7c
Compare
April 21, 2026 15:57
|
Addressed, thanks — I gave precedence to GIT_WORK_TREE, adjusted the comment, and replaced Path.read_text(). The linked worktree gitdir file uses a different format, so I handled it locally. |
Sorry, something went wrong.
3a94f7c to
b5eb249
Compare
April 21, 2026 16:07
|
Thanks, just one piece missing. |
Sorry, something went wrong.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
Handle linked worktree git directories when GIT_DIR points to .git/worktrees/<name>. Previously Repo() could fail with InvalidGitRepositoryError in this scenario, while Repo(os.getcwd()) worked correctly. Add regression test to cover autodiscovery in linked worktrees.
b5eb249 to
4c6ec60
Compare
April 22, 2026 12:52
|
Thanks — I can’t re-run the failed jobs from my side since I don’t have write access to the upstream repository. The remaining failures are on the Windows 3.11/3.12 jobs, and the one visible failure is Could you please re-run the failed Windows jobs once? If they still fail consistently, I can investigate further. |
Sorry, something went wrong.
|
Thanks — after the retry, only one Windows job is still failing, in test/test_index.py::TestIndex::test_index_mutation with a git-checkout-index symlink warning. That looks unrelated to this change, since this PR only touches git/repo/base.py and test/test_repo.py. The linked-worktree tests added here are passing. If you’d like, I can investigate that Windows failure separately, but it seems outside the scope of this PR. |
Sorry, something went wrong.
|
I could simply push the following code in this PR. There are already many other XFAIL. @Byron, do you agree? |
Sorry, something went wrong.
|
It's strange that it works on one runner, but on on the other. Or maybe related to the python version. |
Sorry, something went wrong.
1085a7c
into
gitpython-developers:main
May 6, 2026
Fixes #2022
This PR fixes repository autodiscovery in linked worktrees when GIT_DIR is set.
Summary
Fix
Repo()autodiscovery whenGIT_DIRpoints to a linked worktree git directory.Previously, calling
Repo()inside a linked worktree withGIT_DIR=$(git rev-parse --git-dir)could fail withInvalidGitRepositoryError, whileRepo(os.getcwd())resolved the repository correctly.Changes
GIT_DIRis setReproduction
Inside a linked worktree:
Before: raises
InvalidGitRepositoryErrorAfter: resolves repository correctly