Fix UnicodeDecodeError when reading packed-refs with non-UTF8 characters #2091
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2064
The
packed-refsfile can contain ref names that are not valid UTF-8 (e.g., Latin-1 encoded tag names created by older Git versions or systems with different locale settings). Previously, GitPython would fail withUnicodeDecodeErrorwhen reading such files.Reproduction
As described in #2064:
Before fix:
After fix: Successfully reads all 101 tags.
Changes
errors='surrogateescape'to theopen()call in_iter_packed_refs()Technical Details
The
surrogateescapeerror handler is Python's standard approach for handling potentially non-UTF8 data in filesystem operations. It:\uDC80-\uDCFF)This is the same approach used by Python's
os.fsdecode()and is recommended for filesystem operations where encoding may be unknown or mixed.