copystore: use progress helper
authorMartin von Zweigbergk <martinvonz@google.com>
Sun, 17 Jun 2018 22:57:34 -0700
changeset 38380 63e6f5ae84bc
parent 38379 3a7c33a2cc5e
child 38381 1a2ff11e8a88
copystore: use progress helper Differential Revision: https://phab.mercurial-scm.org/D3781
mercurial/hg.py
mercurial/util.py
--- a/mercurial/hg.py	Sun Jun 17 22:09:15 2018 -0700
+++ b/mercurial/hg.py	Sun Jun 17 22:57:34 2018 -0700
@@ -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)
--- a/mercurial/util.py	Sun Jun 17 22:09:15 2018 -0700
+++ b/mercurial/util.py	Sun Jun 17 22:57:34 2018 -0700
@@ -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