[PATCH 3 of 5 V2] branches: port to generic templater

Yuya Nishihara yuya at tcha.org
Fri Oct 3 09:02:07 CDT 2014


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1412258396 -32400
#      Thu Oct 02 22:59:56 2014 +0900
# Node ID 5989401f7749a7b2bcfb900c07e3c0a897f9f155
# Parent  f143a558f682d5426d7ece84b42cf6dcc217309d
branches: port to generic templater

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1073,9 +1073,10 @@ def branch(ui, repo, label=None, **opts)
 
 @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 @@ def branches(ui, repo, active=False, clo
     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 @@ def branches(ui, repo, active=False, clo
             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')),
diff --git a/tests/test-branches.t b/tests/test-branches.t
--- a/tests/test-branches.t
+++ b/tests/test-branches.t
@@ -470,4 +470,35 @@ custom closed branch color:
   \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 ..
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -221,7 +221,7 @@ Show all commands + options
   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


More information about the Mercurial-devel mailing list