[PATCH 2 of 2 RFC V2] debugobsolete: add formatter support (issue5134)

liscju piotr.listkiewicz at gmail.com
Fri Aug 26 03:56:52 EDT 2016


# HG changeset patch
# User liscju <piotr.listkiewicz at gmail.com>
# Date 1467710115 -7200
#      Tue Jul 05 11:15:15 2016 +0200
# Node ID 81d8a553992a8e9e8ebd349654e79716e7dc8c6d
# Parent  be0a41929f01e4fc9e439a3c39986b94d878a251
debugobsolete: add formatter support (issue5134)

This solution keeps the same output debugobsolete had
before formatter support, but there is issue
I didnt manage to resolve: as Yuya pointed out - "printing
index=None is wrong. It needs a real index value plus a
condition whether or not index is printed in plain output.".
I don't how to get "real index value" and about "condition
whether or not index is printed in plain output" - I have
no idea what is wrong with current behaviour.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1611,25 +1611,44 @@ def show_changeset(ui, repo, opts, buffe
 
     return changeset_templater(ui, repo, matchfn, opts, tmpl, mapfile, buffered)
 
-def showmarker(ui, marker, index=None):
+def showmarker(fm, marker, index=None):
     """utility function to display obsolescence marker in a readable way
 
     To be used by debug function."""
+    fm.condwrite(index is not None, 'index', '%i', index)
     if index is not None:
-        ui.write("%i " % index)
-    ui.write(hex(marker.precnode()))
-    for repl in marker.succnodes():
-        ui.write(' ')
-        ui.write(hex(repl))
-    ui.write(' %X ' % marker.flags())
-    parents = marker.parentnodes()
-    if parents is not None:
-        ui.write('{%s} ' % ', '.join(hex(p) for p in parents))
-    ui.write('(%s) ' % util.datestr(marker.date()))
-    ui.write('{%s}' % (', '.join('%r: %r' % t for t in
-                                 sorted(marker.metadata().items())
-                                 if t[0] != 'date')))
-    ui.write('\n')
+        fm.plain(' ')
+    fm.write('precnode', '%s', hex(marker.precnode()))
+    if marker.succnodes():
+        fm.plain(' ')
+    hexnodes = [hex(repl) for repl in marker.succnodes()]
+    fm.condwrite(marker.succnodes(),
+                 'succnodes',
+                 '%s',
+                 fm.formatlist(hexnodes, 'node', fmt='%s'))
+    fm.plain(' ')
+    fm.write('flags', '%X', marker.flags())
+    fm.plain(' ')
+    parents = marker.parentnodes() or []
+    if parents:
+        fm.plain('{')
+    hexparents = [hex(p) for p in parents]
+    fm.condwrite(parents,
+             'parents',
+             '%s',
+             fm.formatlist(hexparents, 'parent', fmt='%s', sep=','))
+    if parents:
+        fm.plain('} ')
+    fm.plain('(')
+    fm.write('date', '%s', fm.formatdate(marker.date()))
+    fm.plain(') {')
+    # Always only one of fm.plain and fm.data will print metadata,
+    # it is done to keep backward compatibility
+    fm.plain((', '.join('%r: %r' % t for t in
+                        sorted(marker.metadata().items())
+                        if t[0] != 'date')))
+    fm.data(metadata=marker.metadata())
+    fm.plain('}\n')
 
 def finddate(ui, repo, date):
     """Find the tipmost changeset that matches the given date spec"""
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3044,7 +3044,7 @@ def debuglocks(ui, repo, **opts):
          ('r', 'rev', [], _('display markers relevant to REV')),
          ('', 'index', False, _('display index of the marker')),
          ('', 'delete', [], _('delete markers specified by indices')),
-        ] + commitopts2,
+        ] + commitopts2 + formatteropts,
          _('[OBSOLETED [REPLACEMENT ...]]'))
 def debugobsolete(ui, repo, precursor=None, *successors, **opts):
     """create arbitrary obsolete marker
@@ -3132,6 +3132,7 @@ def debugobsolete(ui, repo, precursor=No
             markerset = set(markers)
             isrelevant = lambda m: m in markerset
 
+        fm = ui.formatter('debugobsolete', opts)
         for i, m in enumerate(markerstoiter):
             if not isrelevant(m):
                 # marker can be irrelevant when we're iterating over a set
@@ -3143,7 +3144,9 @@ def debugobsolete(ui, repo, precursor=No
                 # are relevant to --rev value
                 continue
             ind = i if opts.get('index') else None
-            cmdutil.showmarker(ui, m, index=ind)
+            fm.startitem()
+            cmdutil.showmarker(fm, m, index=ind)
+        fm.end()
 
 @command('debugpathcomplete',
          [('f', 'full', None, _('complete an entire path')),
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -261,7 +261,7 @@ Show all commands + options
   debuglocks: force-lock, force-wlock
   debugmergestate: 
   debugnamecomplete: 
-  debugobsolete: flags, record-parents, rev, index, delete, date, user
+  debugobsolete: flags, record-parents, rev, index, delete, date, user, template
   debugpathcomplete: full, normal, added, removed
   debugpushkey: 
   debugpvec: 
diff --git a/tests/test-obsolete.t b/tests/test-obsolete.t
--- a/tests/test-obsolete.t
+++ b/tests/test-obsolete.t
@@ -645,6 +645,82 @@ List of both
   cda648ca50f50482b7055c0b0c4c117bba6733d9 3de5eca88c00aa039da7399a220f4a5221faa585 0 (*) {'user': 'test'} (glob)
   cdbce2fbb16313928851e97e0d85413f3f7eb77f ca819180edb99ed25ceafb3e9584ac287e240b00 0 (Thu Jan 01 00:22:17 1970 +0000) {'user': 'test'}
 
+  $ hg debugobsolete --hidden --rev 3::6 -T json
+  [
+   {
+    "date": [1339.0, 0],
+    "flags": 0,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": [],
+    "precnode": "1337133713371337133713371337133713371337",
+    "succnodes": ["5601fb93a350734d935195fee37f4054c529ff39"]
+   },
+   {
+    "date": [1339.0, 0],
+    "flags": 0,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": [],
+    "precnode": "1339133913391339133913391339133913391339",
+    "succnodes": ["ca819180edb99ed25ceafb3e9584ac287e240b00"]
+   },
+   {
+    "date": [121.0, 120],
+    "flags": 12,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": [],
+    "precnode": "245bde4270cd1072a27757984f9cda8ba26f08ca",
+    "succnodes": ["cdbce2fbb16313928851e97e0d85413f3f7eb77f"]
+   },
+   {
+    "date": [1338.0, 0],
+    "flags": 1,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": [],
+    "precnode": "5601fb93a350734d935195fee37f4054c529ff39",
+    "succnodes": ["6f96419950729f3671185b847352890f074f7557"]
+   },
+   {
+    "date": [0.0, 0],
+    "flags": 0,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": ["6f96419950729f3671185b847352890f074f7557"],
+    "precnode": "94b33453f93bdb8d457ef9b770851a618bf413e1",
+    "succnodes": []
+   },
+   {
+    "date": [1338.0, 0],
+    "flags": 0,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": [],
+    "precnode": "ca819180edb99ed25ceafb3e9584ac287e240b00",
+    "succnodes": ["1337133713371337133713371337133713371337"]
+   },
+   {
+    "date": [*, 0], (glob)
+    "flags": 0,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": [],
+    "precnode": "cda648ca50f50482b7055c0b0c4c117bba6733d9",
+    "succnodes": ["3de5eca88c00aa039da7399a220f4a5221faa585"]
+   },
+   {
+    "date": [1337.0, 0],
+    "flags": 0,
+    "index": null,
+    "metadata": {"user": "test"},
+    "parents": [],
+    "precnode": "cdbce2fbb16313928851e97e0d85413f3f7eb77f",
+    "succnodes": ["ca819180edb99ed25ceafb3e9584ac287e240b00"]
+   }
+  ]
+
 #if serve
 
 Test the debug output for exchange


More information about the Mercurial-devel mailing list