Skip to content

Fix Repo.active_branch resolution for reftable-backed repositories#2114

Merged
Byron merged 1 commit intomainfrom
copilot/fix-issue-with-repository-fetching
Mar 21, 2026
Merged

Fix Repo.active_branch resolution for reftable-backed repositories#2114
Byron merged 1 commit intomainfrom
copilot/fix-issue-with-repository-fetching

Conversation

Copy link

Copilot AI commented Mar 20, 2026

GitPython currently infers the active branch from ref files directly, which breaks on repositories using Git’s reftable ref storage and can surface refs/heads/.invalid as the active branch. This PR switches active-branch resolution to Git’s symbolic-ref plumbing so behavior stays correct across ref backends.

  • Active branch resolution

    • Replaced Repo.active_branch’s direct HEAD reference dereference path with git symbolic-ref -q HEAD.
    • Constructs Head from the resolved symbolic ref path returned by Git.
    • Preserves detached-HEAD semantics by translating symbolic-ref’s non-symbolic result into the existing TypeError behavior.
  • Regression coverage

    • Added a reftable-specific test for empty repositories that validates:
      • repo.active_branch.path matches git symbolic-ref HEAD
      • repo.active_branch.name matches git symbolic-ref --short HEAD
    • Added a detached-HEAD test asserting Repo.active_branch raises TypeError with the expected detached-reference message.
@property
def active_branch(self) -> Head:
    try:
        return Head(self, self.git.symbolic_ref("-q", "HEAD").strip())
    except GitCommandError as err:
        if err.status == 1:
            raise TypeError("HEAD is a detached symbolic reference as it points to a commit") from err
        raise

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix issue with repository fetching in GitPython Fix Repo.active_branch resolution for reftable-backed repositories Mar 20, 2026
Copilot AI requested a review from Byron March 20, 2026 22:20
@Byron Byron force-pushed the copilot/fix-issue-with-repository-fetching branch from 057fc47 to 0a27f73 Compare March 21, 2026 00:06
@Byron Byron marked this pull request as ready for review March 21, 2026 00:06
Copilot AI review requested due to automatic review settings March 21, 2026 00:06
Copy link
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

This PR aims to make Repo.active_branch work correctly on repositories using Git’s reftable ref backend (where filesystem-based ref parsing can misidentify the active branch).

Changes:

  • Added a guard in Repo.active_branch that raises ValueError when the resolved branch name is .invalid.
  • Added tests asserting Repo.active_branch raises ValueError when HEAD points at refs/heads/.invalid (including a reftable init case).
  • Added a detached-HEAD regression test asserting Repo.active_branch raises TypeError.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
git/repo/base.py Adds .invalid detection and raises ValueError instead of resolving via git plumbing.
test/test_repo.py Adds tests for .invalid HEAD and detached HEAD behavior (currently expecting exceptions).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Byron Byron force-pushed the copilot/fix-issue-with-repository-fetching branch from 0a27f73 to bbfa27c Compare March 21, 2026 02:02
@Byron Byron linked an issue Mar 21, 2026 that may be closed by this pull request
reviewed-by: Sebastian Thiel <sebastian.thiel@icloud.com>
@Byron Byron force-pushed the copilot/fix-issue-with-repository-fetching branch from bbfa27c to 77b1135 Compare March 21, 2026 02:05
@Byron Byron merged commit ddca0b3 into main Mar 21, 2026
59 checks passed
@Byron Byron deleted the copilot/fix-issue-with-repository-fetching branch March 21, 2026 02:26
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.

active_branch is detected incorrectly when using reftable

4 participants