Skip to content

Handle non-UTF-8 names in packed refs#2190

Closed
pick7 wants to merge 1 commit into
gitpython-developers:mainfrom
pick7:codex/packed-refs-surrogateescape
Closed

Handle non-UTF-8 names in packed refs#2190
pick7 wants to merge 1 commit into
gitpython-developers:mainfrom
pick7:codex/packed-refs-surrogateescape

Conversation

@pick7

@pick7 pick7 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Read packed-refs with Python surrogateescape handling.
  • Preserve undecodable ref-name bytes instead of failing during decoding.
  • Add a repo.tags regression test using a non-UTF-8 tag name.

Fixes #2064.

Validation

  • GIT_PYTHON_TEST_GIT_REPO_BASE=/tmp/gitpython-test-fixture.i7gtuO .venv/bin/python -m pytest -o addopts= test/test_refs.py
  • .venv/bin/python -m ruff check git/refs/symbolic.py test/test_refs.py
  • .venv/bin/python -m ruff format --check git/refs/symbolic.py test/test_refs.py
  • .venv/bin/python -m mypy git/refs/symbolic.py

AI agent disclosure

This pull request was prepared and submitted by OpenAI Codex acting as an AI agent through the contributor account.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves GitPython’s ability to read packed-refs entries whose ref names contain non‑UTF‑8 bytes, matching Git’s behavior more closely and preventing UnicodeDecodeError when enumerating tags/refs.

Changes:

  • Read packed-refs using UTF‑8 with errors="surrogateescape" to preserve undecodable bytes.
  • Add a regression test that writes a packed-refs file containing a non‑UTF‑8 tag name and asserts it is surfaced via surrogate escapes.

Reviewed changes

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

File Description
git/refs/symbolic.py Updates packed-refs reading to use surrogateescape so non‑UTF‑8 ref names can be iterated without decode failures.
test/test_refs.py Adds a regression test ensuring non‑UTF‑8 packed tag names are preserved via surrogateescape rather than raising.

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

Comment thread test/test_refs.py
Comment thread git/refs/symbolic.py
"""
try:
with open(cls._get_packed_refs_path(repo), "rt", encoding="UTF-8") as fp:
with open(cls._get_packed_refs_path(repo), "rt", encoding="UTF-8", errors="surrogateescape") as fp:
@Byron

Byron commented Jul 26, 2026

Copy link
Copy Markdown
Member

I think the only way this can be solved properly is if no encoding is performed at all. This is a breaking change, which GitPython currently can't do.

Adding surrogates can lead to all kinds of issues downstream, so failing hard is the way to go for now.

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.

Encoding issue with tags in packed-refs file

3 participants