Repo()
fails in Git Worktree when GIT_DIR
environment variable is set
#2022
Labels
Repo()
fails in Git Worktree when GIT_DIR
environment variable is set
#2022
The
git.Repo()
constructor in GitPython fails to correctly initialize a repository when called without a path from within a Git worktree and theGIT_DIR
environment variable is set to that worktree. This variable is often automatically set by Git when executing an alias to point to the current repo or worktree git_dir. WhileRepo(os.getcwd())
finds the worktree correctly, callingRepo()
withGIT_DIR
set toRepo(os.getcwd()).git_dir
raises anInvalidGitRepositoryError
.Minimal Reproducing Example:
Set up a main Git repository:
Create a Git worktree:
git worktree add worktree_a cd worktree_a
Create a simple Python script (e.g.,
test_repo.py
) to initialize a Repo object:Create a Git alias to run the Python script (e.g., in
.git/config
or usinggit config --local alias.test-repo '!python /path/to/test_repo.py'
):Replace
/path/to/test_repo.py
with the actual path to your script.Run the Git alias from within the worktree:
Expected Behavior:
Repo()
should successfully initialize the repository, recognizing the worktree's context and linking back to the main repository's.git
directory.Observed Behavior:
Raises an
InvalidGitRepositoryError
whenRepo()
is called without a path, whileRepo(os.getcwd())
succeeds. The output will also show that theGIT_DIR
environment variable is set within the worktree's context.Additional Information:
Maybe one should also check if such happens for submodules since they also have the repo dir in a different place than the normal .git dir.
Workaround:
Explicitly passing
os.getcwd()
to theRepo()
constructor or unsetting theGIT_DIR
environment variable before callingRepo()
seems to work.The text was updated successfully, but these errors were encountered: