Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Repo() fails in Git Worktree whenGIT_DIR environment variable is set #2022

Open
@Leokap

Description

@Leokap

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:

  1. Set up a main Git repository:

    mkdir main_repocd main_repogit inittouch initial_file.txtgit add initial_file.txtgit commit -m"Initial commit"
  2. Create a Git worktree:

    git worktree add worktree_acd worktree_a
  3. 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}")
  1. 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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp