[PATCH 1 of 3] localrepo: support custom template properties

Patrick Mezard pmezard at gmail.com
Thu Sep 18 06:53:02 CDT 2008


# HG changeset patch
# User Patrick Mezard <pmezard at gmail.com>
# Date 1221737490 -7200
# Node ID 252f622570a453cadb84d174aa8070a61fdc57d0
# Parent  277c91fe8384d4cd29e0f8a123068bcce0248d9e
localrepo: support custom template properties

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -826,6 +826,13 @@
                              node=hex(changes[0])))
             return self.t('manifest', **args)
 
+        # Register extension specific properties
+        allprops = {}
+        for name, propfn in self.repo.extprops.iteritems():
+            def propfunc(propfn=propfn, **args):
+                return propfn(self.repo, rev, changes[5])
+            allprops[name] = propfunc
+
         defprops = {
             'author': changes[1],
             'branches': showbranches,
@@ -843,8 +850,8 @@
             'tags': showtags,
             'extras': showextras,
             }
-        props = props.copy()
-        props.update(defprops)
+        allprops.update(props)
+        allprops.update(defprops)
 
         try:
             if self.ui.debugflag and 'header_debug' in self.t:
@@ -858,7 +865,7 @@
             else:
                 key = ''
             if key:
-                h = templater.stringify(self.t(key, **props))
+                h = templater.stringify(self.t(key, **allprops))
                 if self.buffered:
                     self.header[rev] = h
                 else:
@@ -871,7 +878,7 @@
                 key = 'changeset_verbose'
             else:
                 key = 'changeset'
-            self.ui.write(templater.stringify(self.t(key, **props)))
+            self.ui.write(templater.stringify(self.t(key, **allprops)))
             self.showpatch(changenode)
         except KeyError, inst:
             raise util.Abort(_("%s: no key named '%s'") % (self.t.mapfile,
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -81,6 +81,7 @@
         self.nodetagscache = None
         self.filterpats = {}
         self._datafilters = {}
+        self.extprops = {} # extensions specific properties
         self._transref = self._lockref = self._wlockref = None
 
     def __getattr__(self, name):
@@ -528,6 +529,16 @@
 
     def adddatafilter(self, name, filter):
         self._datafilters[name] = filter
+
+    def addextprop(self, name, propfn):
+        """Register a changeset template property.
+
+        Registered function will be called like:
+        propfn(repo, rev, extra) where rev is the changeset node
+        identifier and extra the extra dictionary, and is expected
+        to return requested property as a string.
+        """
+        self.extprops[name] = propfn
 
     def wread(self, filename):
         if self._link(filename):


More information about the Mercurial-devel mailing list