Patchwork [6,of,7] annotate: split functions to get data without applying text formatting

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 17, 2014, 3:11 p.m.
Message ID <9ef71571dd3004f9c39d.1410966687@mimosa>
Download mbox | patch
Permalink /patch/5849/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 17, 2014, 3:11 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1410878424 -32400
#      Tue Sep 16 23:40:24 2014 +0900
# Node ID 9ef71571dd3004f9c39de3db5dd701303be54111
# Parent  7ef5ea99b22359027cd33651e87569a4c0aff623
annotate: split functions to get data without applying text formatting

This prepares for porting to generic templater API, where raw data should
be passed to the formatter.

makefunc() is necessary to build closure in list comprehension.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -275,15 +275,14 @@  def annotate(ui, repo, *pats, **opts):
         opts['file'] = True
 
     datefunc = ui.quiet and util.shortdate or util.datestr
-    getdate = util.cachefunc(lambda x: datefunc(x[0].date()))
     hexfn = ui.debugflag and hex or short
 
-    opmap = [('user', ' ', lambda x: ui.shortuser(x[0].user())),
-             ('number', ' ', lambda x: str(x[0].rev())),
-             ('changeset', ' ', lambda x: hexfn(x[0].node())),
-             ('date', ' ', getdate),
-             ('file', ' ', lambda x: x[0].path()),
-             ('line_number', ':', lambda x: str(x[1])),
+    opmap = [('user', ' ', lambda x: x[0].user(), ui.shortuser),
+             ('number', ' ', lambda x: x[0].rev(), str),
+             ('changeset', ' ', lambda x: hexfn(x[0].node()), str),
+             ('date', ' ', lambda x: x[0].date(), util.cachefunc(datefunc)),
+             ('file', ' ', lambda x: x[0].path(), str),
+             ('line_number', ':', lambda x: x[1], str),
             ]
 
     if (not opts.get('user') and not opts.get('changeset')
@@ -294,7 +293,10 @@  def annotate(ui, repo, *pats, **opts):
     if linenumber and (not opts.get('changeset')) and (not opts.get('number')):
         raise util.Abort(_('at least one of -n/-c is required for -l'))
 
-    funcmap = [(func, sep) for op, sep, func in opmap if opts.get(op)]
+    def makefunc(get, fmt):
+        return lambda x: fmt(get(x))
+    funcmap = [(makefunc(get, fmt), sep) for op, sep, get, fmt in opmap
+               if opts.get(op)]
     funcmap[0] = (funcmap[0][0], '') # no separator in front of first column
 
     def bad(x, y):