sparse: refactor sparsechecksum()
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 01 Jul 2017 11:56:39 -0700
changeset 33295 c72e9c61d2b1
parent 33294 a5921ad2eb99
child 33296 ee616196227c
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.
hgext/sparse.py
--- a/hgext/sparse.py	Thu Jul 06 10:57:26 2017 -0700
+++ b/hgext/sparse.py	Sat Jul 01 11:56:39 2017 -0700
@@ -493,9 +493,9 @@
             # resolve and can be slow.
             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
@@ -511,8 +511,7 @@
             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
@@ -520,8 +519,7 @@
                 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