changeset 39877:f8eb71f9e3bd

storageutil: new module for storage primitives (API) There will exist common code between storage backends. It would be nice to have a central place to put that code. This commit attempts to create that place by creating the "storageutil" module. The first thing we move is revlog.hash(), which is the function for computing the SHA-1 hash of revision fulltext and parents. Differential Revision: https://phab.mercurial-scm.org/D4753
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 24 Sep 2018 14:23:54 -0700
parents a269fa55467e
children 3e896b51aa5d
files mercurial/revlog.py mercurial/utils/storageutil.py tests/simplestorerepo.py
diffstat 3 files changed, 49 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revlog.py	Mon Sep 24 13:35:50 2018 -0700
+++ b/mercurial/revlog.py	Mon Sep 24 14:23:54 2018 -0700
@@ -16,7 +16,6 @@
 import collections
 import contextlib
 import errno
-import hashlib
 import os
 import re
 import struct
@@ -74,6 +73,7 @@
 )
 from .utils import (
     interfaceutil,
+    storageutil,
     stringutil,
 )
 
@@ -197,33 +197,6 @@
         raise ValueError('unknown revlog index flags')
     return int(int(offset) << 16 | type)
 
-_nullhash = hashlib.sha1(nullid)
-
-def hash(text, p1, p2):
-    """generate a hash from the given text and its parent hashes
-
-    This hash combines both the current file contents and its history
-    in a manner that makes it easy to distinguish nodes with the same
-    content in the revision graph.
-    """
-    # As of now, if one of the parent node is null, p2 is null
-    if p2 == nullid:
-        # deep copy of a hash is faster than creating one
-        s = _nullhash.copy()
-        s.update(p1)
-    else:
-        # none of the parent nodes are nullid
-        if p1 < p2:
-            a = p1
-            b = p2
-        else:
-            a = p2
-            b = p1
-        s = hashlib.sha1(a)
-        s.update(b)
-    s.update(text)
-    return s.digest()
-
 @attr.s(slots=True, frozen=True)
 class _revisioninfo(object):
     """Information about a revision that allows building its fulltext
@@ -1383,7 +1356,7 @@
         returns True if text is different than what is stored.
         """
         p1, p2 = self.parents(node)
-        return hash(text, p1, p2) != node
+        return storageutil.hashrevisionsha1(text, p1, p2) != node
 
     def _cachesegment(self, offset, data):
         """Add a segment to the revlog cache.
@@ -1672,7 +1645,7 @@
         Available as a function so that subclasses can replace the hash
         as needed.
         """
-        return hash(text, p1, p2)
+        return storageutil.hashrevisionsha1(text, p1, p2)
 
     def _processflags(self, text, flags, operation, raw=False):
         """Inspect revision data flags and applies transforms defined by
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/utils/storageutil.py	Mon Sep 24 14:23:54 2018 -0700
@@ -0,0 +1,41 @@
+# storageutil.py - Storage functionality agnostic of backend implementation.
+#
+# Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+import hashlib
+
+from ..node import (
+    nullid,
+)
+
+_nullhash = hashlib.sha1(nullid)
+
+def hashrevisionsha1(text, p1, p2):
+    """Compute the SHA-1 for revision data and its parents.
+
+    This hash combines both the current file contents and its history
+    in a manner that makes it easy to distinguish nodes with the same
+    content in the revision graph.
+    """
+    # As of now, if one of the parent node is null, p2 is null
+    if p2 == nullid:
+        # deep copy of a hash is faster than creating one
+        s = _nullhash.copy()
+        s.update(p1)
+    else:
+        # none of the parent nodes are nullid
+        if p1 < p2:
+            a = p1
+            b = p2
+        else:
+            a = p2
+            b = p1
+        s = hashlib.sha1(a)
+        s.update(b)
+    s.update(text)
+    return s.digest()
--- a/tests/simplestorerepo.py	Mon Sep 24 13:35:50 2018 -0700
+++ b/tests/simplestorerepo.py	Mon Sep 24 14:23:54 2018 -0700
@@ -40,6 +40,7 @@
 )
 from mercurial.utils import (
     interfaceutil,
+    storageutil,
 )
 
 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
@@ -285,7 +286,7 @@
     def checkhash(self, text, node, p1=None, p2=None, rev=None):
         if p1 is None and p2 is None:
             p1, p2 = self.parents(node)
-        if node != revlog.hash(text, p1, p2):
+        if node != storageutil.hashrevisionsha1(text, p1, p2):
             raise simplestoreerror(_("integrity check failed on %s") %
                 self._path)
 
@@ -342,7 +343,7 @@
 
         p1, p2 = self.parents(node)
 
-        if revlog.hash(t, p1, p2) == node:
+        if storageutil.hashrevisionsha1(t, p1, p2) == node:
             return False
 
         if self.iscensored(self.rev(node)):
@@ -420,11 +421,11 @@
         validatenode(p2)
 
         if flags:
-            node = node or revlog.hash(text, p1, p2)
+            node = node or storageutil.hashrevisionsha1(text, p1, p2)
 
         rawtext, validatehash = self._processflags(text, flags, 'write')
 
-        node = node or revlog.hash(text, p1, p2)
+        node = node or storageutil.hashrevisionsha1(text, p1, p2)
 
         if node in self._indexbynode:
             return node