[PATCH] templates: add {lasttag} and {lasttagdistance} keywords

Patrick Mézard pmezard at gmail.com
Wed Jul 15 01:53:23 CDT 2009


Dirkjan Ochtman a écrit :
> On Tue, Jul 14, 2009 at 18:45, Gilles Moris<gilles.moris at free.fr> wrote:
>>  mercurial/cmdutil.py |  40 ++++++++++++++++++++++++++++++++++++++++
>>  mercurial/help.py    |   3 +++
>>  tests/test-log       |  44 ++++++++++++++++++++++++++++++++++++++++++++
>>  tests/test-log.out   |  50 ++++++++++++++++++++++++++++++++++++++++++++++++++
>>  4 files changed, 137 insertions(+), 0 deletions(-)
> 
> So I've been thinking about the templatefilter mess in cmdutil's
> templater functions, and I'd like to propose a way out:
> 
> - all template filter functions accept a new **defaults dict in
> addition to their current argument(s)
> - we always pass 'repo' into the templater defaults

And 'ctx' ?

Cannot we give them a cleaner prototype instead, like: filter(repo, ctx, **args)?

I have rebased a patch allowing third-party code to register new keywords. Perhaps it can help deciding what should be done. Anyway, having a common interface for all filter would kill the first for loop generating the closures.

# HG changeset patch
# User Patrick Mezard <pmezard at gmail.com>
# Date 1226072359 -3600
# Node ID 4cacb27a74aee4589f5b00fd1cffdfb70d39a9d2
# Parent  31177742f54a1b6ad142277e8471a105b2b130ee
localrepo: support custom template keywords

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -877,6 +877,13 @@
                 removes += i[2]
             return '%s: +%s/-%s' % (files, adds, removes)
 
+        # Register extension specific properties
+        allprops = {}
+        for name, propfn in self.repo.extprops.iteritems():
+            def propfunc(propfn=propfn, **args):
+                return propfn(self.repo, ctx)
+            allprops[name] = propfunc
+
         defprops = {
             'author': ctx.user(),
             'branches': showbranches,
@@ -895,8 +902,8 @@
             'extras': showextras,
             'diffstat': showdiffstat,
             }
-        props = props.copy()
-        props.update(defprops)
+        allprops.update(props)
+        allprops.update(defprops)
 
         # find correct templates for current mode
 
@@ -918,7 +925,7 @@
 
             # write header
             if types['header']:
-                h = templater.stringify(self.t(types['header'], **props))
+                h = templater.stringify(self.t(types['header'], **allprops))
                 if self.buffered:
                     self.header[ctx.rev()] = h
                 else:
@@ -926,7 +933,7 @@
 
             # write changeset metadata, then patch if requested
             key = types['changeset']
-            self.ui.write(templater.stringify(self.t(key, **props)))
+            self.ui.write(templater.stringify(self.t(key, **allprops)))
             self.showpatch(ctx.node())
 
         except KeyError, inst:
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -97,6 +97,7 @@
         self.nodetagscache = None
         self.filterpats = {}
         self._datafilters = {}
+        self.extprops = {} # extensions specific properties
         self._transref = self._lockref = self._wlockref = None
 
     @propertycache
@@ -586,6 +587,16 @@
     def adddatafilter(self, name, filter):
         self._datafilters[name] = filter
 
+    def settemplatekey(self, name, propfn):
+        """Register a changeset template keyword.
+
+        Registered functions will be called like: 
+        propfn(repo, ctx) where ctx is the revision changectx
+        object. It is supposed to return requested property as a
+        string.
+        """
+        self.extprops[name] = propfn
+
     def wread(self, filename):
         if self._link(filename):
             data = os.readlink(self.wjoin(filename))

--
Patrick Mézard







More information about the Mercurial-devel mailing list