◐ Shell
reader mode source ↗
Skip to content

Fix Repo() autodiscovery in linked worktrees when GIT_DIR is set#2128

Merged
Byron merged 1 commit into
gitpython-developers:mainfrom
meliezer:fix-worktree-git-dir
May 6, 2026
Merged

Fix Repo() autodiscovery in linked worktrees when GIT_DIR is set#2128
Byron merged 1 commit into
gitpython-developers:mainfrom
meliezer:fix-worktree-git-dir

Conversation

@meliezer

Copy link
Copy Markdown
Contributor

Fixes #2022

This PR fixes repository autodiscovery in linked worktrees when GIT_DIR is set.

Summary

Fix Repo() autodiscovery when GIT_DIR points to a linked worktree git directory.

Previously, calling Repo() inside a linked worktree with GIT_DIR=$(git rev-parse --git-dir) could fail with InvalidGitRepositoryError, while Repo(os.getcwd()) resolved the repository correctly.

Changes

  • add regression coverage for autodiscovery in linked worktrees when GIT_DIR is set
  • detect linked worktree git directories during repository discovery
  • derive the working tree directory correctly from the linked worktree metadata

Reproduction

Inside a linked worktree:

export GIT_DIR=$(git rev-parse --git-dir)
python -c "from git import Repo; Repo()"

Before: raises InvalidGitRepositoryError
After: resolves repository correctly

@meliezer meliezer force-pushed the fix-worktree-git-dir branch 2 times, most recently from a98c43b to b0354d5 Compare April 20, 2026 10:44
@Byron Byron requested a review from Copilot April 20, 2026 13:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide 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 with GIT_DIR set.
  • 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.

@Byron Byron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide 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.

@meliezer meliezer force-pushed the fix-worktree-git-dir branch from b0354d5 to 3a94f7c Compare April 21, 2026 15:57
@meliezer

Copy link
Copy Markdown
Contributor Author

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.

@Byron

Byron commented Apr 22, 2026

Copy link
Copy Markdown
Member

Thanks, just one piece missing.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide 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.

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.
@meliezer meliezer force-pushed the fix-worktree-git-dir branch from b5eb249 to 4c6ec60 Compare April 22, 2026 12:52
@meliezer

Copy link
Copy Markdown
Contributor Author

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 test/test_index.py::TestIndex::test_index_mutation (git-checkout-index / symlink warning), which looks unrelated to this PR since this change only touches git/repo/base.py and test/test_repo.py.

Could you please re-run the failed Windows jobs once? If they still fail consistently, I can investigate further.

@meliezer

Copy link
Copy Markdown
Contributor Author

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.

@meliezer

Copy link
Copy Markdown
Contributor Author

I could simply push the following code in this PR. There are already many other XFAIL. @Byron, do you agree?
@pytest.mark.xfail(sys.platform == "win32", reason="Git checkout-index fallisce sui symlink in Windows CI") def test_index_mutation(self, rw_repo):

@Byron

Byron commented Apr 25, 2026

Copy link
Copy Markdown
Member

It's strange that it works on one runner, but on on the other. Or maybe related to the python version.
But… it seems to be a new failure that everyone is seeing right now, so let's just neuter it and get this merged :).

@Byron Byron mentioned this pull request Apr 25, 2026
Hide details View details @Byron Byron merged commit 1085a7c into gitpython-developers:main May 6, 2026
117 of 122 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Repo() fails in Git Worktree when GIT_DIR environment variable is set

3 participants