Patchwork [2,of,5,RFC] largefiles: refactor basestore, extract _gethash method

login
register
mail settings
Submitter Mads Kiilerich
Date Oct. 10, 2013, 2:35 a.m.
Message ID <287245cf5fa1264f1966.1381372550@mk-desktop>
Download mbox | patch
Permalink /patch/2740/
State Accepted
Commit ae65192fd6b4abef0ab0c5fa169540cc2344a71f
Headers show

Comments

Mads Kiilerich - Oct. 10, 2013, 2:35 a.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1381372124 -7200
#      Thu Oct 10 04:28:44 2013 +0200
# Node ID 287245cf5fa1264f19664704994dbfd9c38cfdff
# Parent  0b23c2624ea5134c2b1b6586e539bb92210fc484
largefiles: refactor basestore, extract _gethash method

Patch

diff --git a/hgext/largefiles/basestore.py b/hgext/largefiles/basestore.py
--- a/hgext/largefiles/basestore.py
+++ b/hgext/largefiles/basestore.py
@@ -59,8 +59,6 @@ 
         missing = []
         ui = self.ui
 
-        util.makedirs(lfutil.storepath(self.repo, ''))
-
         at = 0
         available = self.exists(set(hash for (_filename, hash) in files))
         for filename, hash in files:
@@ -75,32 +73,44 @@ 
                 missing.append(filename)
                 continue
 
-            storefilename = lfutil.storepath(self.repo, hash)
-            tmpfile = util.atomictempfile(storefilename + '.tmp',
-                                          createmode=self.repo.store.createmode)
-
-            try:
-                hhash = self._getfile(tmpfile, filename, hash)
-            except StoreError, err:
-                ui.warn(err.longmessage())
-                hhash = ""
-            tmpfile.close()
-
-            if hhash != hash:
-                if hhash != "":
-                    ui.warn(_('%s: data corruption (expected %s, got %s)\n')
-                            % (filename, hash, hhash))
-                util.unlink(storefilename + '.tmp')
+            if self._gethash(filename, hash):
+                success.append((filename, hash))
+            else:
                 missing.append(filename)
-                continue
-
-            util.rename(storefilename + '.tmp', storefilename)
-            lfutil.linktousercache(self.repo, hash)
-            success.append((filename, hhash))
 
         ui.progress(_('getting largefiles'), None)
         return (success, missing)
 
+    def _gethash(self, filename, hash):
+        """Get file with the provided hash and store it in the local repo's
+        store and in the usercache.
+        filename is for informational messages only.
+        """
+        util.makedirs(lfutil.storepath(self.repo, ''))
+        storefilename = lfutil.storepath(self.repo, hash)
+
+        tmpname = storefilename + '.tmp'
+        tmpfile = util.atomictempfile(tmpname,
+                                      createmode=self.repo.store.createmode)
+
+        try:
+            gothash = self._getfile(tmpfile, filename, hash)
+        except StoreError, err:
+            self.ui.warn(err.longmessage())
+            gothash = ""
+        tmpfile.close()
+
+        if gothash != hash:
+            if gothash != "":
+                self.ui.warn(_('%s: data corruption (expected %s, got %s)\n')
+                             % (filename, hash, gothash))
+            util.unlink(tmpname)
+            return False
+
+        util.rename(tmpname, storefilename)
+        lfutil.linktousercache(self.repo, hash)
+        return True
+
     def verify(self, revs, contents=False):
         '''Verify the existence (and, optionally, contents) of every big
         file revision referenced by every changeset in revs.