[PATCH 3 of 4 RFC V2] formatter: convert identify command

David Carr davidmc24 at gmail.com
Sun Sep 16 00:44:59 CDT 2012


# HG changeset patch
# User David M. Carr  <david at carrclan.us>
# Date 1347770083 14400
# Node ID 1ae6f841281c9f0c7f8924613c1d0ee285d6a679
# Parent  49bf6af6f2eaa291c343e609d1550ddce1cfe0ec
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
and subsequent review.  After this change, identify should produce identical
output unless a non-default formatter is used.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3492,8 +3492,17 @@
 
     hexfunc = ui.debugflag and hex or short
     default = not (num or id or branch or tags or bookmarks)
-    output = []
     revs = []
+    data = {}
+    fm = ui.formatter('identify', opts)
+    class FormatWriter(object):
+        def __init__(self):
+            self._first = True
+        def writeplain(self, text):
+            fm.plain((not self._first and ' ' or '') + text)
+            self._first = False
+    fw = FormatWriter()
+    fm.startitem()
 
     if source:
         source, branches = hg.parseurl(ui.expandpath(source))
@@ -3511,8 +3520,6 @@
             rev = "tip"
 
         remoterev = peer.lookup(rev)
-        if default or id:
-            output = [hexfunc(remoterev)]
 
         def getbms():
             bms = []
@@ -3524,60 +3531,76 @@
 
             return bms
 
+        bms = getbms()
+        fm.data(node=hex(remoterev), bookmarks=bms)
+
+        if default or id:
+            fw.writeplain(hexfunc(remoterev))
         if bookmarks:
-            output.extend(getbms())
+            fw.writeplain(' '.join(bms))
         elif default and not ui.quiet:
             # multiple bookmarks for a single parent separated by '/'
-            bm = '/'.join(getbms())
-            if bm:
-                output.append(bm)
+            if bms:
+                fw.writeplain('/'.join(bms))
     else:
         if not rev:
             ctx = repo[None]
             parents = ctx.parents()
-            changed = ""
+            changed = False
             if default or id or num:
                 if (util.any(repo.status())
                     or util.any(ctx.sub(s).dirty() for s in ctx.substate)):
-                    changed = '+'
+                    changed = True
+            changedstr = changed and '+' or ''
+            fm.data(changed=changed)
+            if len(parents) > 1:
+                fm.data(nodes=[hex(p.node()) for p in parents],
+                    nums=[str(p.rev()) for p in parents])
+            else:
+                fm.data(node=hex(parents[0].node()),
+                    num=str(parents[0].rev()))
+            ts = ctx.tags() # ensure tag cache is valid before starting output
             if default or id:
-                output = ["%s%s" %
-                  ('+'.join([hexfunc(p.node()) for p in parents]), changed)]
+                fw.writeplain('+'.join([hexfunc(p.node()) for p in parents])
+                    + changedstr)
             if num:
-                output.append("%s%s" %
-                  ('+'.join([str(p.rev()) for p in parents]), changed))
+                fw.writeplain('+'.join([str(p.rev()) for p in parents])
+                    + changedstr)
         else:
             ctx = scmutil.revsingle(repo, rev)
+            fm.data(node=hex(ctx.node()), num=str(ctx.rev()))
+            ts = ctx.tags() # ensure tag cache is valid before starting output
             if default or id:
-                output = [hexfunc(ctx.node())]
+                fw.writeplain(hexfunc(ctx.node()))
             if num:
-                output.append(str(ctx.rev()))
-
+                fw.writeplain(str(ctx.rev()))
+
+        b = ctx.branch()
+        bms = ctx.bookmarks()
+        fm.data(branch=b, tags=ts, bookmarks=bms)
         if default and not ui.quiet:
-            b = ctx.branch()
             if b != 'default':
-                output.append("(%s)" % b)
+                fw.writeplain("(%s)" % b)
 
             # multiple tags for a single parent separated by '/'
-            t = '/'.join(ctx.tags())
-            if t:
-                output.append(t)
+            if ts:
+                fw.writeplain('/'.join(ts))
 
             # multiple bookmarks for a single parent separated by '/'
-            bm = '/'.join(ctx.bookmarks())
-            if bm:
-                output.append(bm)
+            if bms:
+                fw.writeplain('/'.join(bms))
         else:
             if branch:
-                output.append(ctx.branch())
+                fw.writeplain(b)
 
             if tags:
-                output.extend(ctx.tags())
+                fw.writeplain(' '.join(ts))
 
             if bookmarks:
-                output.extend(ctx.bookmarks())
-
-    ui.write("%s\n" % ' '.join(output))
+                fw.writeplain(' '.join(bms))
+
+    fm.plain('\n')
+    fm.end()
 
 @command('import|patch',
     [('p', 'strip', 1,


More information about the Mercurial-devel mailing list