Skip to content

Commit 8ca5750

Browse files
committed
fix: read non-UTF-8 packed refs
1 parent 07e8055 commit 8ca5750

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

git/refs/symbolic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def _iter_packed_refs(cls, repo: "Repo") -> Iterator[Tuple[str, str]]:
149149
The packed refs file will be kept open as long as we iterate.
150150
"""
151151
try:
152-
with open(cls._get_packed_refs_path(repo), "rt", encoding="UTF-8") as fp:
152+
with open(cls._get_packed_refs_path(repo), "rt", encoding="UTF-8", errors="surrogateescape") as fp:
153153
for line in fp:
154154
line = line.strip()
155155
if not line:

test/test_refs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ def test_from_pathlike(self):
7777
# Check remoteness
7878
assert Reference(self.rorepo, PathLikeMock("refs/remotes/origin")).is_remote()
7979

80+
def test_iter_packed_refs_with_non_utf8_name(self):
81+
with tempfile.TemporaryDirectory() as tdir:
82+
with self._repo_with_initial_commit(Path(tdir)) as repo:
83+
packed_refs_path = Path(Reference._get_packed_refs_path(repo))
84+
hexsha = repo.head.commit.hexsha
85+
packed_refs_path.write_bytes(
86+
b"# pack-refs with: peeled\n" + hexsha.encode("ascii") + b" refs/tags/non-utf8-\xe9\n"
87+
)
88+
89+
tag_data = [(tag.commit.hexsha, tag.path) for tag in repo.tags]
90+
91+
self.assertEqual(tag_data, [(hexsha, "refs/tags/non-utf8-\udce9")])
92+
8093
def test_tag_base(self):
8194
tag_object_refs = []
8295
for tag in self.rorepo.tags:

0 commit comments

Comments
 (0)