diff mercurial/utils/storageutil.py @ 40005:1d97a332c6d9

storageutil: extract copy metadata retrieval out of filelog As part of implementing an alternate storage backend, I found myself reinventing this wheel. Let's create a utility function for doing the work. Differential Revision: https://phab.mercurial-scm.org/D4800
author Gregory Szorc <gregory.szorc@gmail.com>
date Fri, 28 Sep 2018 11:37:49 -0700
parents fa3dc85a747e
children 422beffd71ba
line wrap: on
line diff
--- a/mercurial/utils/storageutil.py	Fri Sep 28 11:29:05 2018 -0700
+++ b/mercurial/utils/storageutil.py	Fri Sep 28 11:37:49 2018 -0700
@@ -89,6 +89,25 @@
     offset = text.index(b'\x01\n', 2)
     return text[offset + 2:]
 
+def filerevisioncopied(store, node):
+    """Resolve file revision copy metadata.
+
+    Returns ``False`` if the file has no copy metadata. Otherwise a
+    2-tuple of the source filename and node.
+    """
+    if store.parents(node)[0] != nullid:
+        return False
+
+    meta = parsemeta(store.revision(node))[0]
+
+    # copy and copyrev occur in pairs. In rare cases due to old bugs,
+    # one can occur without the other. So ensure both are present to flag
+    # as a copy.
+    if meta and b'copy' in meta and b'copyrev' in meta:
+        return meta[b'copy'], bin(meta[b'copyrev'])
+
+    return False
+
 def iterrevs(storelen, start=0, stop=None):
     """Iterate over revision numbers in a store."""
     step = 1