[PATCH 2 of 3] progress: move the singleton logic to the ui module

Pierre-Yves David pierre-yves.david at ens-lyon.org
Tue Jun 9 12:58:28 CDT 2015


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1433723194 25200
#      Sun Jun 07 17:26:34 2015 -0700
# Node ID 083b0e44c3cd57e342cd1799de05ec7387952835
# Parent  3172c583ed1f9b21f2792ca4382dbab2ca2ff102
progress: move the singleton logic to the ui module

The use of a singleton for all of progress handling is debatable (because
config may vary). However this is how the extention have been doing it so far.
We move that code in the ui module because this is where is should belong on
progress is moved into core.

diff --git a/hgext/progress.py b/hgext/progress.py
--- a/hgext/progress.py
+++ b/hgext/progress.py
@@ -34,15 +34,13 @@ the item, but this can be changed by add
 would take the last num characters, or ``+<num>`` for the first num
 characters.
 """
 
 from mercurial import progress
-
-_singleton = None
+from mercurial import ui as uimod
 
 def uisetup(ui):
-    global _singleton
     class progressui(ui.__class__):
         _progbar = None
 
         def _quiet(self):
             return self.debugflag or self.quiet
@@ -71,11 +69,9 @@ def uisetup(ui):
         if getattr(ui, '_progbar', dval) is dval:
             ui.__class__ = progressui
             # we instantiate one globally shared progress bar to avoid
             # competing progress bars when multiple UI objects get created
             if not progressui._progbar:
-                if _singleton is None:
-                    _singleton = progress.progbar(ui)
-                progressui._progbar = _singleton
+                progressui._progbar = uimod.getprogbar(ui)
 
 def reposetup(ui, repo):
     uisetup(repo.ui)
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -5,11 +5,11 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
 from i18n import _
 import errno, getpass, os, socket, sys, tempfile, traceback
-import config, scmutil, util, error, formatter
+import config, scmutil, util, error, formatter, progress
 from node import hex
 
 samplehgrcs = {
     'user':
 """# example user config (see "hg help config" for more info)
@@ -981,5 +981,17 @@ class path(object):
         ``rawloc`` is the raw location, as defined in the config.
         """
         self.name = name
         # We'll do more intelligent things with rawloc in the future.
         self.loc = rawloc
+
+# we instantiate one globally shared progress bar to avoid
+# competing progress bars when multiple UI objects get created
+_progresssingleton = None
+
+def getprogbar(ui):
+    global _progresssingleton
+    if _progresssingleton is None:
+        # passing 'ui' object to the singleton is fishy,
+        # this is how the extension used to work but feel free to rework it.
+        _progresssingleton = progress.progbar(ui)
+    return _progresssingleton
diff --git a/tests/test-module-imports.t b/tests/test-module-imports.t
--- a/tests/test-module-imports.t
+++ b/tests/test-module-imports.t
@@ -29,9 +29,9 @@ these may expose other cycles.
   mercurial/templater.py mixed imports
      stdlib:    parser
      relative:  config, error, templatefilters, templatekw, util
   mercurial/ui.py mixed imports
      stdlib:    formatter
-     relative:  config, error, scmutil, util
+     relative:  config, error, progress, scmutil, util
   Import cycle: mercurial.cmdutil -> mercurial.context -> mercurial.subrepo -> mercurial.cmdutil
   Import cycle: hgext.largefiles.basestore -> hgext.largefiles.localstore -> hgext.largefiles.basestore
   Import cycle: mercurial.commands -> mercurial.commandserver -> mercurial.dispatch -> mercurial.commands


More information about the Mercurial-devel mailing list