branches: port to generic templater
authorYuya Nishihara <yuya@tcha.org>
Thu, 02 Oct 2014 22:59:56 +0900
changeset 22703 bd6e95cb82b4
parent 22702 08a4e70ed183
child 22704 386339ffe421
branches: port to generic templater
mercurial/commands.py
tests/test-branches.t
tests/test-completion.t
--- a/mercurial/commands.py	Thu Oct 02 22:15:39 2014 +0900
+++ b/mercurial/commands.py	Thu Oct 02 22:59:56 2014 +0900
@@ -1073,9 +1073,10 @@
 
 @command('branches',
     [('a', 'active', False, _('show only branches that have unmerged heads')),
-    ('c', 'closed', False, _('show normal and closed branches'))],
+     ('c', 'closed', False, _('show normal and closed branches')),
+    ] + formatteropts,
     _('[-ac]'))
-def branches(ui, repo, active=False, closed=False):
+def branches(ui, repo, active=False, closed=False, **opts):
     """list repository named branches
 
     List the repository's named branches, indicating which ones are
@@ -1090,7 +1091,8 @@
     Returns 0.
     """
 
-    hexfunc = ui.debugflag and hex or short
+    fm = ui.formatter('branches', opts)
+    hexfunc = fm.hexfunc
 
     allheads = set(repo.heads())
     branches = []
@@ -1116,16 +1118,20 @@
             notice = _(' (inactive)')
         if tag == repo.dirstate.branch():
             label = 'branches.current'
+
+        fm.startitem()
+        fm.write('branch', '%s', tag, label=label)
         rev = ctx.rev()
         padsize = max(31 - len(str(rev)) - encoding.colwidth(tag), 0)
         fmt = ' ' * padsize + '%d:%s'
-        rev = ui.label(fmt % (rev, hexfunc(ctx.node())),
-                       'log.changeset changeset.%s' % ctx.phasestr())
-        labeledtag = ui.label(tag, label)
-        if ui.quiet:
-            ui.write("%s\n" % labeledtag)
-        else:
-            ui.write("%s %s%s\n" % (labeledtag, rev, notice))
+        if not ui.quiet:
+            fm.plain(' ')
+        fm.condwrite(not ui.quiet, 'rev node', fmt, rev, hexfunc(ctx.node()),
+                     label='log.changeset changeset.%s' % ctx.phasestr())
+        if not ui.quiet:
+            fm.plain(notice)
+        fm.plain('\n')
+    fm.end()
 
 @command('bundle',
     [('f', 'force', None, _('run even when the destination is unrelated')),
--- a/tests/test-branches.t	Thu Oct 02 22:15:39 2014 +0900
+++ b/tests/test-branches.t	Thu Oct 02 22:59:56 2014 +0900
@@ -470,4 +470,35 @@
   \x1b[0;35ma\x1b[0m \x1b[0;36m                             5:d8cbc61dbaa6\x1b[0m (inactive) (esc)
   \x1b[0;35mdefault\x1b[0m \x1b[0;36m                       0:19709c5a4e75\x1b[0m (inactive) (esc)
 
+template output:
+
+  $ hg branches -Tjson --closed
+  [
+   {
+    "branch": "b",
+    "node": "e23b5505d1ad24aab6f84fd8c7cb8cd8e5e93be0",
+    "rev": 13
+   },
+   {
+    "branch": "a branch name much longer than the default justification used by branches",
+    "node": "10ff5895aa5793bd378da574af8cec8ea408d831",
+    "rev": 7
+   },
+   {
+    "branch": "c",
+    "node": "f894c25619d3f1484639d81be950e0a07bc6f1f6",
+    "rev": 14
+   },
+   {
+    "branch": "a",
+    "node": "d8cbc61dbaa6dc817175d1e301eecb863f280832",
+    "rev": 5
+   },
+   {
+    "branch": "default",
+    "node": "19709c5a4e75bf938f8e349aff97438539bb729e",
+    "rev": 0
+   }
+  ]
+
   $ cd ..
--- a/tests/test-completion.t	Thu Oct 02 22:15:39 2014 +0900
+++ b/tests/test-completion.t	Thu Oct 02 22:59:56 2014 +0900
@@ -221,7 +221,7 @@
   bisect: reset, good, bad, skip, extend, command, noupdate
   bookmarks: force, rev, delete, rename, inactive
   branch: force, clean
-  branches: active, closed
+  branches: active, closed, template
   bundle: force, rev, branch, base, all, type, ssh, remotecmd, insecure
   cat: output, rev, decode, include, exclude
   config: untrusted, edit, local, global