Patchwork [03,of,11,sparse,V3] sparse: clean up config signature code

login
register
mail settings
Submitter Gregory Szorc
Date July 7, 2017, 1:18 a.m.
Message ID <1d856c7fb9c08bd71ed9.1499390304@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/22075/
State Accepted
Headers show

Comments

Gregory Szorc - July 7, 2017, 1:18 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1499382096 25200
#      Thu Jul 06 16:01:36 2017 -0700
# Node ID 1d856c7fb9c08bd71ed9bb802917d45f07fb8714
# Parent  01f89a68e9269eec249207478e1e8344e90b7496
sparse: clean up config signature code

Before, 0 was being used as the default signature value and we cast
the int to a string. We also handled I/O exceptions manually.

The new code uses cfs.tryread() so we always feed data into the
hasher. The empty string does hash and and should be suitable
for input into a cache key.

The changes made the code simple enough that the separate checksum
function could be inlined.

Patch

diff --git a/mercurial/sparse.py b/mercurial/sparse.py
--- a/mercurial/sparse.py
+++ b/mercurial/sparse.py
@@ -132,10 +132,6 @@  def activeprofiles(repo):
 def invalidatesignaturecache(repo):
     repo._sparsesignaturecache.clear()
 
-def _checksum(self, path):
-    data = self.vfs.read(path)
-    return hashlib.sha1(data).hexdigest()
-
 def configsignature(repo, includetemp=True):
     """Obtain the signature string for the current sparse configuration.
 
@@ -148,25 +144,18 @@  def configsignature(repo, includetemp=Tr
     if includetemp:
         tempsignature = cache.get('tempsignature')
     else:
-        tempsignature = 0
+        tempsignature = '0'
 
     if signature is None or (includetemp and tempsignature is None):
-        signature = 0
-        try:
-            signature = _checksum('sparse')
-        except (OSError, IOError):
-            pass
+        signature = hashlib.sha1(repo.vfs.tryread('sparse')).hexdigest()
         cache['signature'] = signature
 
-        tempsignature = 0
         if includetemp:
-            try:
-                tempsignature = _checksum('tempsparse')
-            except (OSError, IOError):
-                pass
+            raw = repo.vfs.tryread('tempsparse')
+            tempsignature = hashlib.sha1(raw).hexdigest()
             cache['tempsignature'] = tempsignature
 
-    return '%s %s' % (str(signature), str(tempsignature))
+    return '%s %s' % (signature, tempsignature)
 
 def writeconfig(repo, includes, excludes, profiles):
     """Write the sparse config file given a sparse configuration."""