[PATCH 07 of 13 sparse] sparse: refactor sparsechecksum()

Gregory Szorc gregory.szorc at gmail.com
Sat Jul 1 21:55:24 EDT 2017


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1498935399 25200
#      Sat Jul 01 11:56:39 2017 -0700
# Node ID fc1292b23044d76d25f8a88ff819abf5ac0ed6cf
# Parent  2789e039d0ad86b5a2b505bf2b881025db85d23c
sparse: refactor sparsechecksum()

This was relying on garbage collection to close the opened
file, which is a bug. Both callers simply called into self.vfs
to resolve the path. So refactor to use the vfs layer.

While we're here, rename the method to reflect it is internal
and to break anyone relying on the old behavior.

diff --git a/hgext/sparse.py b/hgext/sparse.py
--- a/hgext/sparse.py
+++ b/hgext/sparse.py
@@ -491,9 +491,9 @@ def _wraprepo(ui, repo):
         def getrawprofile(self, profile, changeid):
             return self.filectx(profile, changeid=changeid).data()
 
-        def sparsechecksum(self, filepath):
-            fh = open(filepath)
-            return hashlib.sha1(fh.read()).hexdigest()
+        def _sparsechecksum(self, path):
+            data = self.vfs.read(path)
+            return hashlib.sha1(data).hexdigest()
 
         def _sparsesignature(self, includetemp=True):
             """Returns the signature string representing the contents of the
@@ -509,8 +509,7 @@ def _wraprepo(ui, repo):
             if signature is None or (includetemp and tempsignature is None):
                 signature = 0
                 try:
-                    sparsepath = self.vfs.join('sparse')
-                    signature = self.sparsechecksum(sparsepath)
+                    signature = self._sparsechecksum('sparse')
                 except (OSError, IOError):
                     pass
                 signaturecache['signature'] = signature
@@ -518,8 +517,7 @@ def _wraprepo(ui, repo):
                 tempsignature = 0
                 if includetemp:
                     try:
-                        tempsparsepath = self.vfs.join('tempsparse')
-                        tempsignature = self.sparsechecksum(tempsparsepath)
+                        tempsignature = self._sparsechecksum('tempsparse')
                     except (OSError, IOError):
                         pass
                     signaturecache['tempsignature'] = tempsignature


More information about the Mercurial-devel mailing list