[PATCH 2 of 2] largefiles: replace invocation of os.path module by vfs in lfutil.py

liscju piotr.listkiewicz at gmail.com
Mon Mar 14 16:31:08 EDT 2016


# HG changeset patch
# User liscju <piotr.listkiewicz at gmail.com>
# Date 1457983222 -3600
#      Mon Mar 14 20:20:22 2016 +0100
# Node ID 7d99ed52e3d5908a34e92d99094e613c3c30f050
# Parent  a4715f03a6c7bdcaec1ef5a4a9c530c5911dfd02
largefiles: replace invocation of os.path module by vfs in lfutil.py

Replaces invocations os.path functions to methods in vfs. Unfortunately
(in my view) this makes code less readable, because instead of using
clear variable names with path it needs to replace them with vfs(..).
I need guidance how to make such transition look more readable.

For example in this patch there is example with few places with
wvfs.join(standindir), standindir before this patch was absolute
path, in this it is changed to relative because it is used also
in expression wvfs.join(standindir, pat).

diff -r a4715f03a6c7 -r 7d99ed52e3d5 hgext/largefiles/lfutil.py
--- a/hgext/largefiles/lfutil.py	Sun Mar 13 23:16:44 2016 +0100
+++ b/hgext/largefiles/lfutil.py	Mon Mar 14 20:20:22 2016 +0100
@@ -120,20 +120,21 @@ def openlfdirstate(ui, repo, create=True
     Return a dirstate object that tracks largefiles: i.e. its root is
     the repo root, but it is saved in .hg/largefiles/dirstate.
     '''
-    lfstoredir = repo.join(longname)
-    opener = scmutil.opener(lfstoredir)
+    vfs = repo.vfs
+    lfstoredir = longname
+    opener = scmutil.opener(vfs.join(lfstoredir))
     lfdirstate = largefilesdirstate(opener, ui, repo.root,
                                      repo.dirstate._validate)
 
     # If the largefiles dirstate does not exist, populate and create
     # it. This ensures that we create it on the first meaningful
     # largefiles operation in a new clone.
-    if create and not os.path.exists(os.path.join(lfstoredir, 'dirstate')):
+    if create and not vfs.exists(vfs.join(lfstoredir, 'dirstate')):
         matcher = getstandinmatcher(repo)
         standins = repo.dirstate.walk(matcher, [], False, False)
 
         if len(standins) > 0:
-            util.makedirs(lfstoredir)
+            vfs.makedirs(lfstoredir)
 
         for standin in standins:
             lfile = splitstandin(standin)
@@ -200,30 +201,30 @@ def copyfromcache(repo, hash, filename):
     file was not found in either cache (which should not happened:
     this is meant to be called only after ensuring that the needed
     largefile exists in the cache).'''
+    wvfs = repo.wvfs
     path = findfile(repo, hash)
     if path is None:
         return False
-    util.makedirs(os.path.dirname(repo.wjoin(filename)))
+    wvfs.makedirs(wvfs.dirname(wvfs.join(filename)))
     # The write may fail before the file is fully written, but we
     # don't use atomic writes in the working copy.
-    dest = repo.wjoin(filename)
     with open(path, 'rb') as srcfd:
-        with open(dest, 'wb') as destfd:
+        with wvfs(filename, 'wb') as destfd:
             gothash = copyandhash(srcfd, destfd)
     if gothash != hash:
         repo.ui.warn(_('%s: data corruption in %s with hash %s\n')
                      % (filename, path, gothash))
-        util.unlink(dest)
+        wvfs.unlink(filename)
         return False
     return True
 
 def copytostore(repo, rev, file, uploaded=False):
+    wvfs = repo.wvfs
     hash = readstandin(repo, file, rev)
     if instore(repo, hash):
         return
-    absfile = repo.wjoin(file)
-    if os.path.exists(absfile):
-        copytostoreabsolute(repo, absfile, hash)
+    if wvfs.exists(file):
+        copytostoreabsolute(repo, wvfs.join(file), hash)
     else:
         repo.ui.warn(_("%s: largefile %s not available from local store\n") %
                      (file, hash))
@@ -257,21 +258,22 @@ def linktousercache(repo, hash):
 
 def getstandinmatcher(repo, rmatcher=None):
     '''Return a match object that applies rmatcher to the standin directory'''
-    standindir = repo.wjoin(shortname)
+    wvfs = repo.wvfs
+    standindir = shortname
 
     # no warnings about missing files or directories
     badfn = lambda f, msg: None
 
     if rmatcher and not rmatcher.always():
-        pats = [os.path.join(standindir, pat) for pat in rmatcher.files()]
+        pats = [wvfs.join(standindir, pat) for pat in rmatcher.files()]
         if not pats:
-            pats = [standindir]
+            pats = [wvfs.join(standindir)]
         match = scmutil.match(repo[None], pats, badfn=badfn)
         # if pats is empty, it would incorrectly always match, so clear _always
         match._always = False
     else:
         # no patterns: relative to repo root
-        match = scmutil.match(repo[None], [standindir], badfn=badfn)
+        match = scmutil.match(repo[None], [wvfs.join(standindir)], badfn=badfn)
     return match
 
 def composestandinmatcher(repo, rmatcher):
@@ -315,7 +317,7 @@ def splitstandin(filename):
 
 def updatestandin(repo, standin):
     file = repo.wjoin(splitstandin(standin))
-    if os.path.exists(file):
+    if repo.wvfs.exists(splitstandin(standin)):
         hash = hashfile(file)
         executable = getexecutable(file)
         writestandin(repo, standin, hash, executable)
@@ -419,7 +421,7 @@ def synclfdirstate(repo, lfdirstate, lfi
         state, mtime = '?', -1
     if state == 'n':
         if (normallookup or mtime < 0 or
-            not os.path.exists(repo.wjoin(lfile))):
+            not repo.wvfs.exists(lfile)):
             # state 'n' doesn't ensure 'clean' in this case
             lfdirstate.normallookup(lfile)
         else:
@@ -525,12 +527,11 @@ def updatestandinsbymatch(repo, match):
         # removed/renamed)
         for lfile in lfiles:
             if lfile in modifiedfiles:
-                if os.path.exists(
-                        repo.wjoin(standin(lfile))):
+                if repo.wvfs.exists(standin(lfile)):
                     # this handles the case where a rebase is being
                     # performed and the working copy is not updated
                     # yet.
-                    if os.path.exists(repo.wjoin(lfile)):
+                    if repo.wvfs.exists(lfile):
                         updatestandin(repo,
                             standin(lfile))
 


More information about the Mercurial-devel mailing list