Patchwork [1,of,5,V2] formatter: add general way to switch hex/short functions

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 3, 2014, 2:02 p.m.
Message ID <7437e043012b8fe79b8e.1412344925@mimosa>
Download mbox | patch
Permalink /patch/6090/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 3, 2014, 2:02 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1412342402 -32400
#      Fri Oct 03 22:20:02 2014 +0900
# Node ID 7437e043012b8fe79b8e08db290de4a28d9ddc57
# Parent  78c916f24dd99a56e4c29153a5df3bd7d1c40edd
formatter: add general way to switch hex/short functions

This seems a bit awkward, but it can avoid duplicates in annotate, tags,
branches and bookmarks.

I guess fm.hexfunc can eventually be removed (or redesigned) when it gets
template backend.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -276,10 +276,7 @@  def annotate(ui, repo, *pats, **opts):
 
     fm = ui.formatter('annotate', opts)
     datefunc = ui.quiet and util.shortdate or util.datestr
-    if fm or ui.debugflag:
-        hexfn = hex
-    else:
-        hexfn = short
+    hexfn = fm.hexfunc
 
     opmap = [('user', ' ', lambda x: x[0].user(), ui.shortuser),
              ('number', ' ', lambda x: x[0].rev(), str),
@@ -6085,10 +6082,7 @@  def tags(ui, repo, **opts):
     """
 
     fm = ui.formatter('tags', opts)
-    if fm or ui.debugflag:
-        hexfunc = hex
-    else:
-        hexfunc = short
+    hexfunc = fm.hexfunc
     tagtype = ""
 
     for t, n in reversed(repo.tagslist()):
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -6,6 +6,7 @@ 
 # GNU General Public License version 2 or any later version.
 
 import cPickle
+from node import hex, short
 from i18n import _
 import encoding, util
 
@@ -16,6 +17,8 @@  class baseformatter(object):
         self._style = opts.get("style")
         self._template = opts.get("template")
         self._item = None
+        # function to convert node to string suitable for this output
+        self.hexfunc = hex
     def __nonzero__(self):
         '''return False if we're not doing real templating so we can
         skip extra work'''
@@ -51,6 +54,10 @@  class plainformatter(baseformatter):
     '''the default text output scheme'''
     def __init__(self, ui, topic, opts):
         baseformatter.__init__(self, ui, topic, opts)
+        if ui.debugflag:
+            self.hexfunc = hex
+        else:
+            self.hexfunc = short
     def __nonzero__(self):
         return False
     def startitem(self):