Patchwork [03,of,11] subrepo: replace "_calcfilehash" invocation by "vfs.tryread"

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Nov. 19, 2014, 9:41 a.m.
Message ID <1afed68c6dfb8f6609ad.1416390108@juju>
Download mbox | patch
Permalink /patch/6791/
State Accepted
Commit 2ff394bbfa7446780c998576141d334702e6ae84
Headers show

Comments

Katsunori FUJIWARA - Nov. 19, 2014, 9:41 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1416389714 -32400
#      Wed Nov 19 18:35:14 2014 +0900
# Node ID 1afed68c6dfb8f6609adc40c5abd78047e21f4cb
# Parent  a19ee70858778a179d46d1985e7260d3f1b9deed
subrepo: replace "_calcfilehash" invocation by "vfs.tryread"

"_calcfilehash" can be completely replaced by simple "vfs.tryread"
invocation.

    def _calcfilehash(filename):
        data = ''
        if os.path.exists(filename):
            fd = open(filename, 'rb')
            data = fd.read()
            fd.close()
        return util.sha1(data).hexdigest()

Building absolute path "absname" up by "self._repo.join" for files in
"filelist" is avoided, because "vfs.tryread" does so internally.

Patch

diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -566,9 +566,10 @@  class hgsubrepo(abstractsubrepo):
         # sort the files that will be hashed in increasing (likely) file size
         filelist = ('bookmarks', 'store/phaseroots', 'store/00changelog.i')
         yield '# %s\n' % _expandedabspath(remotepath)
+        vfs = self._repo.vfs
         for relname in filelist:
-            absname = os.path.normpath(self._repo.join(relname))
-            yield '%s = %s\n' % (relname, _calcfilehash(absname))
+            filehash = util.sha1(vfs.tryread(relname)).hexdigest()
+            yield '%s = %s\n' % (relname, filehash)
 
     def _getstorehashcachepath(self, remotepath):
         '''get a unique path for the store hash cache'''