Ver Fonte

Handle more errors when trying to read a ref

Some files aren't just not there, but actually invalid. Different
operating systems handle that differently, but rather than getting
cute and specific, treat any error when reading a ref as indicating
that it's not there.

(As an example, double quotes raise one type of error on Windows;
non-UTF-8 paths another on macOS, and e.g. ZFS on Linux triggers a
third if UTF-8 is active.)

See: https://foss.heptapod.net/mercurial/hg-git/-/issues/397
Dan Villiom Podlaski Christiansen há 2 anos atrás
pai
commit
2339d6a0a8
1 ficheiros alterados com 10 adições e 3 exclusões
  1. 10 3
      dulwich/refs.py

+ 10 - 3
dulwich/refs.py

@@ -748,7 +748,10 @@ class DiskRefsContainer(RefsContainer):
                 else:
                     # Read only the first 40 bytes
                     return header + f.read(40 - len(SYMREF))
-        except (FileNotFoundError, IsADirectoryError, NotADirectoryError):
+        except (OSError, UnicodeError):
+            # don't assume anything specific about the error; in
+            # particular, invalid or forbidden paths can raise weird
+            # errors depending on the specific operating system
             return None
 
     def _remove_packed_ref(self, name):
@@ -965,9 +968,13 @@ class DiskRefsContainer(RefsContainer):
 
             # remove the reference file itself
             try:
+                found = os.path.lexists(filename)
+            except OSError:
+                # may only be packed, or otherwise unstorable
+                found = False
+
+            if found:
                 os.remove(filename)
-            except FileNotFoundError:
-                pass  # may only be packed
 
             self._remove_packed_ref(name)
             self._log(