Patchwork [3,of,6] branches: port to generic templater

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 2, 2014, 2:59 p.m.
Message ID <5676c0addf27c674be4a.1412261945@mimosa>
Download mbox | patch
Permalink /patch/6085/
State Superseded
Commit bd6e95cb82b40b7c768ecfe3338719ca874b3e45
Headers show

Comments

Yuya Nishihara - Oct. 2, 2014, 2:59 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1412258396 -32400
#      Thu Oct 02 22:59:56 2014 +0900
# Node ID 5676c0addf27c674be4a399e3c6b86edca5fa32f
# Parent  04e13b88d3b047406ca954fbb79de228874817eb
branches: port to generic templater
Mads Kiilerich - Oct. 2, 2014, 3:27 p.m.
On 10/02/2014 04:59 PM, Yuya Nishihara wrote:
> @@ -1093,7 +1094,11 @@ def branches(ui, repo, active=False, clo
>       Returns 0.
>       """
>   
> -    hexfunc = ui.debugflag and hex or short
> +    fm = ui.formatter('branches', opts)
> +    if fm or ui.debugflag:
> +        hexfunc = hex
> +    else:
> +        hexfunc = short

I don't remember if I mentioned it before, but I think we reuse this 
pattern so often that we should introduce something like 
ui.hexfunc(alwaysfull=fm).

/Mads
Yuya Nishihara - Oct. 2, 2014, 4 p.m.
On Thu, 02 Oct 2014 17:27:19 +0200, Mads Kiilerich wrote:
> On 10/02/2014 04:59 PM, Yuya Nishihara wrote:
> > @@ -1093,7 +1094,11 @@ def branches(ui, repo, active=False, clo
> >       Returns 0.
> >       """
> >   
> > -    hexfunc = ui.debugflag and hex or short
> > +    fm = ui.formatter('branches', opts)
> > +    if fm or ui.debugflag:
> > +        hexfunc = hex
> > +    else:
> > +        hexfunc = short
> 
> I don't remember if I mentioned it before, but I think we reuse this 
> pattern so often that we should introduce something like 
> ui.hexfunc(alwaysfull=fm).

Yeah, I have another one in "bookmarks -T" patch.

Since formatter knows ui, maybe we can do fm.hexfunc() -> hexfunc or
fm.hex -> hexfunc ?

Regards,

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1076,9 +1076,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
@@ -1093,7 +1094,11 @@  def branches(ui, repo, active=False, clo
     Returns 0.
     """
 
-    hexfunc = ui.debugflag and hex or short
+    fm = ui.formatter('branches', opts)
+    if fm or ui.debugflag:
+        hexfunc = hex
+    else:
+        hexfunc = short
 
     allheads = set(repo.heads())
     branches = []
@@ -1119,16 +1124,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()
         fmt = ' ' * max(31 - len(str(rev)) - encoding.colwidth(tag), 0)
         fmt += '%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