Skip to content

Repo instantiated from a bare-repository worktree incorrectly sets bare = True #2223

Description

@isomogyi

Title

Repo instantiated from a bare-repository worktree incorrectly sets bare = True

Description

Since 3.1.58+, instantiating a git.Repo targeting a working tree folder that was created from a bare repository (via git worktree add) inherits the bare = True setting from the main repository's .git location.

This causes repo.working_tree_dir to resolve to None, which causes operations like index.add() / _entries_for_paths to fail with:
InvalidGitRepositoryError: Cannot rewrite paths without a working tree

Minimal Reproduction Example

from git import Repo
import subprocess, tempfile, os

with tempfile.TemporaryDirectory() as tmpdir:
    bare_path = os.path.join(tmpdir, "bare.git")
    worktree_path = os.path.join(tmpdir, "wt")
    
    # 1. Init bare repo and add a commit
    repo_bare = Repo.init(bare_path, bare=True)
    
    # 2. Add a worktree
    repo_bare.git.worktree("add", "-b", "main", worktree_path)
    
    # 3. Open worktree repo
    wt_repo = Repo(worktree_path)
    
    # Expected: wt_repo.bare is False, wt_repo.working_tree_dir is worktree_path
    # Actual: wt_repo.bare is True, wt_repo.working_tree_dir is None
    print("Is bare:", wt_repo.bare)
    print("Working tree dir:", wt_repo.working_tree_dir)

Is bare: True
Working tree dir: None

while using 3.1.57 the same code produces the correct results
Is bare: False
Working tree dir: /tmp/tmpgz7gz3y6/wt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions