Browse Source

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'
Stefan Sperling 2 years ago
parent
commit
492b05992b
1 changed files with 1 additions and 1 deletions
  1. 1 1
      dulwich/objects.py

+ 1 - 1
dulwich/objects.py

@@ -135,7 +135,7 @@ def hex_to_filename(path, hex):
     # os.path.join accepts bytes or unicode, but all args must be of the same
     # type. Make sure that hex which is expected to be bytes, is the same type
     # as path.
-    if getattr(path, "encode", None) is not None:
+    if type(path) != type(hex) and getattr(path, "encode", None) is not None:
         hex = hex.decode("ascii")
     dir = hex[:2]
     file = hex[2:]