[PATCH 1 of 4] stringutil: promote smartset.prettyformat() to utility function

Yuya Nishihara yuya at tcha.org
Mon Jun 11 13:07:06 UTC 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1528599009 -32400
#      Sun Jun 10 11:50:09 2018 +0900
# Node ID 40a20b62d7cc17999c8e879d62b4d95f1c3bb4cd
# Parent  15a1e37f80bd5af4b8eedd55b0c347c41a974839
stringutil: promote smartset.prettyformat() to utility function

It will be used by debugwalk.

diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -70,7 +70,6 @@ from . import (
     scmutil,
     setdiscovery,
     simplemerge,
-    smartset,
     sshpeer,
     sslutil,
     streamclone,
@@ -2236,8 +2235,8 @@ def debugrevspec(ui, repo, expr, **opts)
         arevs = revset.makematcher(treebystage['analyzed'])(repo)
         brevs = revset.makematcher(treebystage['optimized'])(repo)
         if opts['show_set'] or (opts['show_set'] is None and ui.verbose):
-            ui.write(("* analyzed set:\n"), smartset.prettyformat(arevs), "\n")
-            ui.write(("* optimized set:\n"), smartset.prettyformat(brevs), "\n")
+            ui.write(("* analyzed set:\n"), stringutil.prettyrepr(arevs), "\n")
+            ui.write(("* optimized set:\n"), stringutil.prettyrepr(brevs), "\n")
         arevs = list(arevs)
         brevs = list(brevs)
         if arevs == brevs:
@@ -2260,7 +2259,7 @@ def debugrevspec(ui, repo, expr, **opts)
     func = revset.makematcher(tree)
     revs = func(repo)
     if opts['show_set'] or (opts['show_set'] is None and ui.verbose):
-        ui.write(("* set:\n"), smartset.prettyformat(revs), "\n")
+        ui.write(("* set:\n"), stringutil.prettyrepr(revs), "\n")
     if not opts['show_revs']:
         return
     for c in revs:
diff --git a/mercurial/smartset.py b/mercurial/smartset.py
--- a/mercurial/smartset.py
+++ b/mercurial/smartset.py
@@ -1129,17 +1129,3 @@ class fullreposet(_spanset):
 
         other.sort(reverse=self.isdescending())
         return other
-
-def prettyformat(revs):
-    lines = []
-    rs = pycompat.byterepr(revs)
-    p = 0
-    while p < len(rs):
-        q = rs.find('<', p + 1)
-        if q < 0:
-            q = len(rs)
-        l = rs.count('<', 0, p) - rs.count('>', 0, p)
-        assert l >= 0
-        lines.append((l, rs[p:q].rstrip()))
-        p = q
-    return '\n'.join('  ' * l + s for l, s in lines)
diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py
+++ b/mercurial/utils/stringutil.py
@@ -45,6 +45,21 @@ def pprint(o, bprefix=False):
     else:
         return pycompat.byterepr(o)
 
+def prettyrepr(o):
+    """Pretty print a representation of a possibly-nested object"""
+    lines = []
+    rs = pycompat.byterepr(o)
+    p = 0
+    while p < len(rs):
+        q = rs.find('<', p + 1)
+        if q < 0:
+            q = len(rs)
+        l = rs.count('<', 0, p) - rs.count('>', 0, p)
+        assert l >= 0
+        lines.append((l, rs[p:q].rstrip()))
+        p = q
+    return '\n'.join('  ' * l + s for l, s in lines)
+
 def binary(s):
     """return true if a string is binary data"""
     return bool(s and '\0' in s)
diff --git a/tests/test-glog.t b/tests/test-glog.t
--- a/tests/test-glog.t
+++ b/tests/test-glog.t
@@ -91,6 +91,7 @@ o  (0) root
   >   revsetlang,
   >   smartset,
   > )
+  > from mercurial.utils import stringutil
   > 
   > def logrevset(repo, pats, opts):
   >     revs = logcmdutil._initialrevs(repo, opts)
@@ -112,7 +113,7 @@ o  (0) root
   >             ui = repo.ui
   >             ui.write(b'%r\n' % (opts.get(b'rev', []),))
   >             ui.write(revsetlang.prettyformat(tree) + b'\n')
-  >             ui.write(smartset.prettyformat(revs) + b'\n')
+  >             ui.write(stringutil.prettyrepr(revs) + b'\n')
   >             revs = smartset.baseset()  # display no revisions
   >         return revs, filematcher
   >     extensions.wrapfunction(logcmdutil, 'getrevs', printrevset)
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -42,8 +42,8 @@ these predicates use '\0' as a separator
   >     registrar,
   >     revset,
   >     revsetlang,
-  >     smartset,
   > )
+  > from mercurial.utils import stringutil
   > cmdtable = {}
   > command = registrar.command(cmdtable)
   > @command(b'debugrevlistspec',
@@ -63,7 +63,7 @@ these predicates use '\0' as a separator
   >     func = revset.match(ui, expr, lookup=revset.lookupfn(repo))
   >     revs = func(repo)
   >     if ui.verbose:
-  >         ui.note(b"* set:\n", smartset.prettyformat(revs), b"\n")
+  >         ui.note(b"* set:\n", stringutil.prettyrepr(revs), b"\n")
   >     for c in revs:
   >         ui.write(b"%d\n" % c)
   > EOF


More information about the Mercurial-devel mailing list