D7849: core: migrate uses of hashlib.sha1 to hashutil.sha1

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Mon Jan 13 17:52:07 EST 2020


durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7849

AFFECTED FILES
  mercurial/chgserver.py
  mercurial/exchange.py
  mercurial/hg.py
  mercurial/localrepo.py
  mercurial/merge.py
  mercurial/obsolete.py
  mercurial/patch.py
  mercurial/repair.py
  mercurial/revlogutils/sidedata.py
  mercurial/scmutil.py
  mercurial/sparse.py
  mercurial/store.py
  mercurial/subrepo.py
  mercurial/util.py
  mercurial/utils/storageutil.py
  mercurial/wireprotov1peer.py
  mercurial/wireprotov2server.py

CHANGE DETAILS

diff --git a/mercurial/wireprotov2server.py b/mercurial/wireprotov2server.py
--- a/mercurial/wireprotov2server.py
+++ b/mercurial/wireprotov2server.py
@@ -8,7 +8,6 @@
 
 import collections
 import contextlib
-import hashlib
 
 from .i18n import _
 from .node import (
@@ -31,6 +30,7 @@
 from .interfaces import util as interfaceutil
 from .utils import (
     cborutil,
+    hashutil,
     stringutil,
 )
 
@@ -858,7 +858,7 @@
 
         cacher.adjustcachekeystate(state)
 
-        hasher = hashlib.sha1()
+        hasher = hashutil.sha1()
         for chunk in cborutil.streamencode(state):
             hasher.update(chunk)
 
diff --git a/mercurial/wireprotov1peer.py b/mercurial/wireprotov1peer.py
--- a/mercurial/wireprotov1peer.py
+++ b/mercurial/wireprotov1peer.py
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import sys
 import weakref
 
@@ -31,6 +30,7 @@
     repository,
     util as interfaceutil,
 )
+from .utils import hashutil
 
 urlreq = util.urlreq
 
@@ -489,7 +489,7 @@
 
         if heads != [b'force'] and self.capable(b'unbundlehash'):
             heads = wireprototypes.encodelist(
-                [b'hashed', hashlib.sha1(b''.join(sorted(heads))).digest()]
+                [b'hashed', hashutil.sha1(b''.join(sorted(heads))).digest()]
             )
         else:
             heads = wireprototypes.encodelist(heads)
diff --git a/mercurial/utils/storageutil.py b/mercurial/utils/storageutil.py
--- a/mercurial/utils/storageutil.py
+++ b/mercurial/utils/storageutil.py
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import re
 import struct
 
@@ -24,8 +23,9 @@
     pycompat,
 )
 from ..interfaces import repository
+from ..utils import hashutil
 
-_nullhash = hashlib.sha1(nullid)
+_nullhash = hashutil.sha1(nullid)
 
 
 def hashrevisionsha1(text, p1, p2):
@@ -48,7 +48,7 @@
         else:
             a = p2
             b = p1
-        s = hashlib.sha1(a)
+        s = hashutil.sha1(a)
         s.update(b)
     s.update(text)
     return s.digest()
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -53,6 +53,7 @@
 )
 from .utils import (
     compression,
+    hashutil,
     procutil,
     stringutil,
 )
@@ -197,7 +198,7 @@
 
 DIGESTS = {
     b'md5': hashlib.md5,
-    b'sha1': hashlib.sha1,
+    b'sha1': hashutil.sha1,
     b'sha512': hashlib.sha512,
 }
 # List of digest types from strongest to weakest
diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -9,7 +9,6 @@
 
 import copy
 import errno
-import hashlib
 import os
 import re
 import stat
@@ -37,6 +36,7 @@
 )
 from .utils import (
     dateutil,
+    hashutil,
     procutil,
     stringutil,
 )
@@ -61,7 +61,7 @@
 
 def _getstorehashcachename(remotepath):
     '''get a unique filename for the store hash cache of a remote repository'''
-    return node.hex(hashlib.sha1(_expandedabspath(remotepath)).digest())[0:12]
+    return node.hex(hashutil.sha1(_expandedabspath(remotepath)).digest())[0:12]
 
 
 class SubrepoAbort(error.Abort):
@@ -514,7 +514,7 @@
         yield b'# %s\n' % _expandedabspath(remotepath)
         vfs = self._repo.vfs
         for relname in filelist:
-            filehash = node.hex(hashlib.sha1(vfs.tryread(relname)).digest())
+            filehash = node.hex(hashutil.sha1(vfs.tryread(relname)).digest())
             yield b'%s = %s\n' % (relname, filehash)
 
     @propertycache
diff --git a/mercurial/store.py b/mercurial/store.py
--- a/mercurial/store.py
+++ b/mercurial/store.py
@@ -9,7 +9,6 @@
 
 import errno
 import functools
-import hashlib
 import os
 import stat
 
@@ -25,6 +24,7 @@
     util,
     vfs as vfsmod,
 )
+from .utils import hashutil
 
 parsers = policy.importmod('parsers')
 # how much bytes should be read from fncache in one read
@@ -273,7 +273,7 @@
 
 
 def _hashencode(path, dotencode):
-    digest = node.hex(hashlib.sha1(path).digest())
+    digest = node.hex(hashutil.sha1(path).digest())
     le = lowerencode(path[5:]).split(b'/')  # skips prefix 'data/' or 'meta/'
     parts = _auxencode(le, dotencode)
     basename = parts[-1]
diff --git a/mercurial/sparse.py b/mercurial/sparse.py
--- a/mercurial/sparse.py
+++ b/mercurial/sparse.py
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import os
 
 from .i18n import _
@@ -24,6 +23,7 @@
     scmutil,
     util,
 )
+from .utils import hashutil
 
 # Whether sparse features are enabled. This variable is intended to be
 # temporary to facilitate porting sparse to core. It should eventually be
@@ -205,12 +205,12 @@
         tempsignature = b'0'
 
     if signature is None or (includetemp and tempsignature is None):
-        signature = hex(hashlib.sha1(repo.vfs.tryread(b'sparse')).digest())
+        signature = hex(hashutil.sha1(repo.vfs.tryread(b'sparse')).digest())
         cache[b'signature'] = signature
 
         if includetemp:
             raw = repo.vfs.tryread(b'tempsparse')
-            tempsignature = hex(hashlib.sha1(raw).digest())
+            tempsignature = hex(hashutil.sha1(raw).digest())
             cache[b'tempsignature'] = tempsignature
 
     return b'%s %s' % (signature, tempsignature)
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -9,7 +9,6 @@
 
 import errno
 import glob
-import hashlib
 import os
 import posixpath
 import re
@@ -48,6 +47,7 @@
 )
 
 from .utils import (
+    hashutil,
     procutil,
     stringutil,
 )
@@ -366,7 +366,7 @@
     key = None
     revs = sorted(r for r in cl.filteredrevs if r <= maxrev)
     if revs:
-        s = hashlib.sha1()
+        s = hashutil.sha1()
         for rev in revs:
             s.update(b'%d;' % rev)
         key = s.digest()
diff --git a/mercurial/revlogutils/sidedata.py b/mercurial/revlogutils/sidedata.py
--- a/mercurial/revlogutils/sidedata.py
+++ b/mercurial/revlogutils/sidedata.py
@@ -33,10 +33,10 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import struct
 
 from .. import error
+from ..utils import hashutil
 
 ## sidedata type constant
 # reserve a block for testing purposes.
@@ -64,7 +64,7 @@
     sidedata.sort()
     rawtext = [SIDEDATA_HEADER.pack(len(sidedata))]
     for key, value in sidedata:
-        digest = hashlib.sha1(value).digest()
+        digest = hashutil.sha1(value).digest()
         rawtext.append(SIDEDATA_ENTRY.pack(key, len(value), digest))
     for key, value in sidedata:
         rawtext.append(value)
@@ -85,7 +85,7 @@
         # read the data associated with that entry
         nextdataoffset = dataoffset + size
         entrytext = text[dataoffset:nextdataoffset]
-        readdigest = hashlib.sha1(entrytext).digest()
+        readdigest = hashutil.sha1(entrytext).digest()
         if storeddigest != readdigest:
             raise error.SidedataHashError(key, storeddigest, readdigest)
         sidedata[key] = entrytext
diff --git a/mercurial/repair.py b/mercurial/repair.py
--- a/mercurial/repair.py
+++ b/mercurial/repair.py
@@ -9,7 +9,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 
 from .i18n import _
 from .node import (
@@ -29,7 +28,10 @@
     pycompat,
     util,
 )
-from .utils import stringutil
+from .utils import (
+    hashutil,
+    stringutil,
+)
 
 
 def backupbundle(
@@ -45,7 +47,7 @@
     # Include a hash of all the nodes in the filename for uniqueness
     allcommits = repo.set(b'%ln::%ln', bases, heads)
     allhashes = sorted(c.hex() for c in allcommits)
-    totalhash = hashlib.sha1(b''.join(allhashes)).digest()
+    totalhash = hashutil.sha1(b''.join(allhashes)).digest()
     name = b"%s/%s-%s-%s.hg" % (
         backupdir,
         short(node),
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -12,7 +12,6 @@
 import contextlib
 import copy
 import errno
-import hashlib
 import os
 import re
 import shutil
@@ -41,6 +40,7 @@
 )
 from .utils import (
     dateutil,
+    hashutil,
     procutil,
     stringutil,
 )
@@ -2943,7 +2943,7 @@
         if not text:
             text = b""
         l = len(text)
-        s = hashlib.sha1(b'blob %d\0' % l)
+        s = hashutil.sha1(b'blob %d\0' % l)
         s.update(text)
         return hex(s.digest())
 
diff --git a/mercurial/obsolete.py b/mercurial/obsolete.py
--- a/mercurial/obsolete.py
+++ b/mercurial/obsolete.py
@@ -70,7 +70,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import struct
 
 from .i18n import _
@@ -85,7 +84,10 @@
     pycompat,
     util,
 )
-from .utils import dateutil
+from .utils import (
+    dateutil,
+    hashutil,
+)
 
 parsers = policy.importmod('parsers')
 
@@ -1028,7 +1030,7 @@
 
 def makefoldid(relation, user):
 
-    folddigest = hashlib.sha1(user)
+    folddigest = hashutil.sha1(user)
     for p in relation[0] + relation[1]:
         folddigest.update(b'%d' % p.rev())
         folddigest.update(p.node())
diff --git a/mercurial/merge.py b/mercurial/merge.py
--- a/mercurial/merge.py
+++ b/mercurial/merge.py
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import shutil
 import stat
 import struct
@@ -39,6 +38,7 @@
     util,
     worker,
 )
+from .utils import hashutil
 
 _pack = struct.pack
 _unpack = struct.unpack
@@ -512,7 +512,7 @@
         """hash the path of a local file context for storage in the .hg/merge
         directory."""
 
-        return hex(hashlib.sha1(path).digest())
+        return hex(hashutil.sha1(path).digest())
 
     def add(self, fcl, fco, fca, fd):
         """add a new (potentially?) conflicting file the merge state
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import os
 import random
 import sys
@@ -74,6 +73,7 @@
 )
 
 from .utils import (
+    hashutil,
     procutil,
     stringutil,
 )
@@ -2007,7 +2007,7 @@
             )
 
         idbase = b"%.40f#%f" % (random.random(), time.time())
-        ha = hex(hashlib.sha1(idbase).digest())
+        ha = hex(hashutil.sha1(idbase).digest())
         txnid = b'TXN:' + ha
         self.hook(b'pretxnopen', throw=True, txnname=desc, txnid=txnid)
 
diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -9,7 +9,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import os
 import shutil
 import stat
@@ -48,7 +47,7 @@
     verify as verifymod,
     vfs as vfsmod,
 )
-
+from .utils import hashutil
 from .interfaces import repository as repositorymod
 
 release = lock.release
@@ -738,7 +737,7 @@
                 )
         elif sharenamemode == b'remote':
             sharepath = os.path.join(
-                sharepool, node.hex(hashlib.sha1(source).digest())
+                sharepool, node.hex(hashutil.sha1(source).digest())
             )
         else:
             raise error.Abort(
diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import collections
-import hashlib
 
 from .i18n import _
 from .node import (
@@ -40,7 +39,10 @@
     wireprototypes,
 )
 from .interfaces import repository
-from .utils import stringutil
+from .utils import (
+    hashutil,
+    stringutil,
+)
 
 urlerr = util.urlerr
 urlreq = util.urlreq
@@ -2705,7 +2707,7 @@
     Used by peer for unbundling.
     """
     heads = repo.heads()
-    heads_hash = hashlib.sha1(b''.join(sorted(heads))).digest()
+    heads_hash = hashutil.sha1(b''.join(sorted(heads))).digest()
     if not (
         their_heads == [b'force']
         or their_heads == heads
diff --git a/mercurial/chgserver.py b/mercurial/chgserver.py
--- a/mercurial/chgserver.py
+++ b/mercurial/chgserver.py
@@ -41,7 +41,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import inspect
 import os
 import re
@@ -67,6 +66,7 @@
 )
 
 from .utils import (
+    hashutil,
     procutil,
     stringutil,
 )
@@ -74,7 +74,7 @@
 
 def _hashlist(items):
     """return sha1 hexdigest for a list"""
-    return node.hex(hashlib.sha1(stringutil.pprint(items)).digest())
+    return node.hex(hashutil.sha1(stringutil.pprint(items)).digest())
 
 
 # sensitive config sections affecting confighash



To: durin42, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list