Patchwork D3781: copystore: use progress helper

login
register
mail settings
Submitter phabricator
Date June 18, 2018, 7:05 a.m.
Message ID <differential-rev-PHID-DREV-ectp2wq4d77bjkpuacib-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/32244/
State Superseded
Headers show

Comments

phabricator - June 18, 2018, 7:05 a.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3781

AFFECTED FILES
  mercurial/hg.py
  mercurial/util.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1631,31 +1631,30 @@ 
         except shutil.Error as inst:
             raise error.Abort(str(inst))
 
-def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
+def copyfiles(src, dst, hardlink=None, progress=None):
     """Copy a directory tree using hardlinks if possible."""
     num = 0
 
-    gettopic = lambda: hardlink and _('linking') or _('copying')
+    def settopic():
+        if progress:
+            progress.topic = _('linking') if hardlink else _('copying')
 
     if os.path.isdir(src):
         if hardlink is None:
             hardlink = (os.stat(src).st_dev ==
                         os.stat(os.path.dirname(dst)).st_dev)
-        topic = gettopic()
+        settopic()
         os.mkdir(dst)
         for name, kind in listdir(src):
             srcname = os.path.join(src, name)
             dstname = os.path.join(dst, name)
-            def nprog(t, pos):
-                if pos is not None:
-                    return progress(t, pos + num)
-            hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
+            hardlink, n = copyfiles(srcname, dstname, hardlink, progress)
             num += n
     else:
         if hardlink is None:
             hardlink = (os.stat(os.path.dirname(src)).st_dev ==
                         os.stat(os.path.dirname(dst)).st_dev)
-        topic = gettopic()
+        settopic()
 
         if hardlink:
             try:
@@ -1666,8 +1665,8 @@ 
         else:
             shutil.copy(src, dst)
         num += 1
-        progress(topic, num)
-    progress(topic, None)
+        if progress:
+            progress.increment()
 
     return hardlink, num
 
diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -372,13 +372,9 @@ 
     destlock = None
     try:
         hardlink = None
+        topic = _('linking') if hardlink else _('copying')
+        progress = ui.makeprogress(topic)
         num = 0
-        closetopic = [None]
-        def prog(topic, pos):
-            if pos is None:
-                closetopic[0] = topic
-            else:
-                ui.progress(topic, pos + num)
         srcpublishing = srcrepo.publishing()
         srcvfs = vfsmod.vfs(srcrepo.sharedpath)
         dstvfs = vfsmod.vfs(destpath)
@@ -395,16 +391,13 @@ 
                     # lock to avoid premature writing to the target
                     destlock = lock.lock(dstvfs, lockfile)
                 hardlink, n = util.copyfiles(srcvfs.join(f), dstvfs.join(f),
-                                             hardlink, progress=prog)
+                                             hardlink, progress)
                 num += n
         if hardlink:
             ui.debug("linked %d files\n" % num)
-            if closetopic[0]:
-                ui.progress(closetopic[0], None)
         else:
             ui.debug("copied %d files\n" % num)
-            if closetopic[0]:
-                ui.progress(closetopic[0], None)
+        progress.complete()
         return destlock
     except: # re-raises
         release(destlock)