Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork937
Description
Thegit.Repo()
constructor in GitPython fails to correctly initialize a repository when called without a path from within a Gitworktree 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:
mkdir main_repocd main_repogit inittouch initial_file.txtgit add initial_file.txtgit commit -m"Initial commit"
Create a Git worktree:
git worktree add worktree_acd worktree_a
Create a simple Python script (e.g.,
test_repo.py
) to initialize a Repo object:
importosfromgitimportRepo,InvalidGitRepositoryErrorprint(f"Current working directory:{os.getcwd()}")print(f"GIT_DIR environment variable:{os.getenv('GIT_DIR')}")try:repo=Repo()print(f"Repo initialized successfully:{repo.working_dir}")exceptInvalidGitRepositoryErrorase:print(f"Error initializing Repo():{e}")try:repo_with_cwd=Repo(os.getcwd())print(f"Repo initialized with cwd successfully:{repo_with_cwd.working_dir}")print(f"Repo(os.getcwd()).git_dir:{Repo(os.getcwd()).git_dir}")exceptInvalidGitRepositoryErrorase:print(f"Error initializing Repo(os.getcwd()):{e}")
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:
git test-repo
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 anInvalidGitRepositoryError
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:
- GitPython Version: 3.1.44
- Python Version: 3.10.14
- Operating System: Linux
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 passingos.getcwd()
to theRepo()
constructor or unsetting theGIT_DIR
environment variable before callingRepo()
seems to work.