Skip to content

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

Open
meliezer wants to merge 1 commit intogitpython-developers:mainfrom
meliezer:fix-worktree-git-dir
Open

Fix Repo() autodiscovery in linked worktrees when GIT_DIR is set#2128
meliezer wants to merge 1 commit intogitpython-developers:mainfrom
meliezer:fix-worktree-git-dir

Conversation

@meliezer
Copy link
Copy Markdown

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 from 6ad051a to a98c43b Compare April 20, 2026 10:28
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.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

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

2 participants