[PATCH RFC] formatter: convert identify command

David Carr davidmc24 at gmail.com
Tue Sep 11 20:26:54 CDT 2012


# HG changeset patch
# User David M. Carr  <david at carrclan.us>
# Date 1347413070 14400
# Node ID f16835267ff0075424d56db9aa210ef60fd578f8
# Parent  ba61e84ff4ce474366294df7cd3a1d6b8b5132d5
formatter: convert identify command

This is an attempt at converting the identify command to use the formatter,
towards "generic templating".  It was based on mpm's example in 68007f0557de.
After this change, identify should produce identical output unless a non-default
formatter is used.

diff -r ba61e84ff4ce -r f16835267ff0 mercurial/commands.py
--- a/mercurial/commands.py	Tue Sep 11 20:16:11 2012 -0400
+++ b/mercurial/commands.py	Tue Sep 11 21:24:30 2012 -0400
@@ -3492,7 +3492,9 @@
 
     hexfunc = ui.debugflag and hex or short
     default = not (num or id or branch or tags or bookmarks)
-    output = []
+    fm = ui.formatter('identify', opts)
+    fields = []
+    fieldvalues = []
     revs = []
 
     if source:
@@ -3512,7 +3514,8 @@
 
         remoterev = peer.lookup(rev)
         if default or id:
-            output = [hexfunc(remoterev)]
+            fields.append('node')
+            fieldvalues.append(hexfunc(remoterev))
 
         def getbms():
             bms = []
@@ -3525,12 +3528,14 @@
             return bms
 
         if bookmarks:
-            output.extend(getbms())
+            fields.append('bookmarks')
+            fieldvalues.append(' '.join(getbms()))
         elif default and not ui.quiet:
             # multiple bookmarks for a single parent separated by '/'
             bm = '/'.join(getbms())
             if bm:
-                output.append(bm)
+                fields.append('bookmarks')
+                fieldvalues.append(bm)
     else:
         if not rev:
             ctx = repo[None]
@@ -3541,43 +3546,56 @@
                     or util.any(ctx.sub(s).dirty() for s in ctx.substate)):
                     changed = '+'
             if default or id:
-                output = ["%s%s" %
-                  ('+'.join([hexfunc(p.node()) for p in parents]), changed)]
+                fields.append('node')
+                fieldvalues.append("%s%s" %
+                  ('+'.join([hexfunc(p.node()) for p in parents]), changed))
             if num:
-                output.append("%s%s" %
+                fields.append('num')
+                fieldvalues.append("%s%s" %
                   ('+'.join([str(p.rev()) for p in parents]), changed))
         else:
             ctx = scmutil.revsingle(repo, rev)
             if default or id:
-                output = [hexfunc(ctx.node())]
+                fields.append('node')
+                fieldvalues.append(hexfunc(ctx.node()))
             if num:
-                output.append(str(ctx.rev()))
+                fields.append('num')
+                fieldvalues.append(str(ctx.rev()))
 
         if default and not ui.quiet:
             b = ctx.branch()
             if b != 'default':
-                output.append("(%s)" % b)
+                fields.append('branch')
+                fieldvalues.append("(%s)" % b)
 
             # multiple tags for a single parent separated by '/'
             t = '/'.join(ctx.tags())
             if t:
-                output.append(t)
+                fields.append('tags')
+                fieldvalues.append(t)
 
             # multiple bookmarks for a single parent separated by '/'
             bm = '/'.join(ctx.bookmarks())
             if bm:
-                output.append(bm)
+                fields.append('bookmarks')
+                fieldvalues.append(bm)
         else:
             if branch:
-                output.append(ctx.branch())
+                fields.append('branch')
+                fieldvalues.append(ctx.branch())
 
             if tags:
-                output.extend(ctx.tags())
+                fields.append('tags')
+                fieldvalues.append(' '.join(ctx.tags()))
 
             if bookmarks:
-                output.extend(ctx.bookmarks())
-
-    ui.write("%s\n" % ' '.join(output))
+                fields.append('bookmarks')
+                fieldvalues.append(' '.join(ctx.bookmarks()))
+
+    fm.startitem()
+    format = ' '.join(['%s'] * len(fieldvalues))+'\n'
+    fm.write(' '.join(fields), format, *fieldvalues)
+    fm.end()
 
 @command('import|patch',
     [('p', 'strip', 1,


More information about the Mercurial-devel mailing list