[PATCH 1 of 5 remotefilelog-http] remotefilelogserver: tease out a _loadfileblob method for future use

raf at durin42.com raf at durin42.com
Thu Jul 16 10:36:45 CDT 2015


# HG changeset patch
# User Augie Fackler <augie at google.com>
# Date 1435690927 14400
#      Tue Jun 30 15:02:07 2015 -0400
# Node ID 7b0a44b1efa00588e8ca8c6b14ef61b819a956e1
# Parent  56443b2b25f017e97df717360340ebae777f6bb5
remotefilelogserver: tease out a _loadfileblob method for future use

We're about to introduce a new getfiles method, so let's take this
opportunity to split out the file loading code so it'll be used in
only one place.

diff --git a/remotefilelog/remotefilelogserver.py b/remotefilelog/remotefilelogserver.py
--- a/remotefilelog/remotefilelogserver.py
+++ b/remotefilelog/remotefilelogserver.py
@@ -178,6 +178,32 @@ def onetimesetup(ui):
 
     wrapfunction(httpprotocol, 'iscmd', _iscmd)
 
+def _loadfileblob(repo, cachepath, path, node):
+    filecachepath = os.path.join(cachepath, path, hex(node))
+    if not os.path.exists(filecachepath):
+        filectx = repo.filectx(path, fileid=node)
+        if filectx.node() == nullid:
+            repo.changelog = changelog.changelog(repo.svfs)
+            filectx = repo.filectx(path, fileid=node)
+
+        text = createfileblob(filectx)
+        text = lz4.compressHC(text)
+
+        dirname = os.path.dirname(filecachepath)
+        if not os.path.exists(dirname):
+            os.makedirs(dirname)
+        try:
+            with open(filecachepath, "w") as f:
+                f.write(text)
+        except IOError:
+            # Don't abort if the user only has permission to read,
+            # and not write.
+            pass
+    else:
+        with open(filecachepath, "r") as f:
+            text = f.read()
+    return text
+
 def getfiles(repo, proto):
     """A server api for requesting particular versions of particular files.
     """
@@ -209,29 +235,7 @@ def getfiles(repo, proto):
 
                 path = request[40:]
 
-                filecachepath = os.path.join(cachepath, path, hex(node))
-                if not os.path.exists(filecachepath):
-                    filectx = repo.filectx(path, fileid=node)
-                    if filectx.node() == nullid:
-                        repo.changelog = changelog.changelog(repo.svfs)
-                        filectx = repo.filectx(path, fileid=node)
-
-                    text = createfileblob(filectx)
-                    text = lz4.compressHC(text)
-
-                    dirname = os.path.dirname(filecachepath)
-                    if not os.path.exists(dirname):
-                        os.makedirs(dirname)
-                    try:
-                        with open(filecachepath, "w") as f:
-                            f.write(text)
-                    except IOError:
-                        # Don't abort if the user only has permission to read,
-                        # and not write.
-                        pass
-                else:
-                    with open(filecachepath, "r") as f:
-                        text = f.read()
+                text = _loadfileblob(repo, cachepath, path, node)
 
                 yield '%d\n%s' % (len(text), text)
 


More information about the Mercurial-devel mailing list