hex_to_filename: don't attempt to convert types if they already match
An error in hex_to_filename() could be triggered via the objects-pack command.
if arguments path and hex arrive as type 'str' then the previous logic would
attempt to convert 'hex' into a string, expecting it to be a binary string
regardless of what its type really is.
With this final fix in place, dulwich pack-objects succeeds again.
$ git rev-list main | dulwich pack-objects foo
Traceback (most recent call last):
File "/home/stsp/dulwich/venv/bin/dulwich", line 33, in <module>
sys.exit(load_entry_point('dulwich==0.20.50', 'console_scripts', 'dulwich')())
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/cli.py", line 783, in main
return cmd_kls().run(argv[1:])
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/cli.py", line 541, in run
porcelain.pack_objects(".", object_ids, packf, idxf)
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/porcelain.py", line 1754, in pack_objects
entries, data_sum = write_pack_objects(
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/pack.py", line 1713, in write_pack_objects
return write_pack_data(
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/pack.py", line 1803, in write_pack_data
for chunk in chunk_generator:
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/pack.py", line 1754, in _pack_data_chunks
for i, (type_num, object_id, delta_base, raw) in enumerate(records):
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/pack.py", line 1675, in <genexpr>
(
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/object_store.py", line 1114, in __iter__
yield self.store[sha], path
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/object_store.py", line 145, in __getitem__
type_num, uncomp = self.get_raw(sha)
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/object_store.py", line 582, in get_raw
ret = self._get_loose_object(hexsha)
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/object_store.py", line 744, in _get_loose_object
path = self._get_shafile_path(sha)
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/object_store.py", line 731, in _get_shafile_path
return hex_to_filename(self.path, sha)
File "/home/stsp/dulwich/venv/lib/python3.9/site-packages/dulwich/objects.py", line 139, in hex_to_filename
hex = hex.decode("ascii")
AttributeError: 'str' object has no attribute 'decode'