[PATCH] interhg: extension for hyperlink to BTS in changeset and changelog

Hideya 'OhaC' OHASHI ohachige at gmail.com
Tue Jun 19 10:53:49 CDT 2007


2007/6/18, Matt Mackall <mpm at selenic.com>:
> On Mon, Jun 18, 2007 at 12:59:42AM +0900, OHASHI Hideya wrote:
> > --- a/mercurial/hgweb/hgweb_mod.py    Wed Jun 13 13:15:53 2007 -0500
> > +++ b/mercurial/hgweb/hgweb_mod.py    Mon Jun 18 00:57:09 2007 +0900
> > @@ -11,7 +11,7 @@ from mercurial.node import *
> >  from mercurial.node import *
> >  from mercurial.i18n import gettext as _
> >  from mercurial import mdiff, ui, hg, util, archival, streamclone, patch
> > -from mercurial import revlog, templater
> > +from mercurial import revlog, templater, extensions
> >  from common import get_mtime, staticfile, style_map, paritygen
> >
> >  def _up(p):
> > @@ -78,6 +78,11 @@ class hgweb(object):
> >          self.templatepath = self.config("web", "templates",
> >                                          templater.templatepath(),
> >                                          untrusted=False)
> > +        self.interhg = None
> > +        try:
> > +            self.interhg = extensions.find('interhg')
> > +        except KeyError:
> > +            pass
>
> This is backwards. No part of Mercurial should know about a particular
> extension! The extension instead needs to find a way to modify the
> behaviour of Mercurial without Mercurial's help.
>
> For instance, the extension can do something like:
>
> from mercurial.hgweb import hgweb_mod
>
> origfunc = hgweb_mod.hgweb.changelog
>
> def myfilter(self, ctx, shortlog):
>     res = origfunc(self, ctx, shortlog)
>     # change the result
>     ...
>
> hgweb_mod.hgweb.changelog = origfunc
>
> If that doesn't work, you can subclass and replace hgweb_mod.hgweb and
> override the relevant functions.
>
> Or you can wrap the templater and detect when it's processing the
> "changelog" or "changeset" templates.

Thank you for your advice.
I could rewrited this extension independently.

First I simply override "escape".
But it's used from several points, So I used a magic keyword ":InterHg:".

I don't like this keyword solution.
But it works well in changeset view.
Does anyone give me some advices?

# HG changeset patch
# User OHASHI Hideya <ohachige at gmail.com>
# Date 1182267325 -32400
# Node ID 38afbf8bff96b4d0b127b472aba07b5972be40e0
# Parent  3daed3680554787f32bdc4cac7c034ad6129692d
interhg: extension for hyperlink to BTS in changeset.

When commit log has some BTS IDs, Hyperlink will be shown up.
The BTS ID format is configurable.

diff -r 3daed3680554 -r 38afbf8bff96 doc/hgrc.5.txt
--- a/doc/hgrc.5.txt    Wed Jun 13 13:15:53 2007 -0500
+++ b/doc/hgrc.5.txt    Wed Jun 20 00:35:25 2007 +0900
@@ -537,6 +537,17 @@ web::
   templates;;
     Where to find the HTML templates. Default is install path.

+interhg::
+  Settings for extensions that links to BTS.
+  bts;;
+    Base URL to use BTS. Such as Bugzilla, trac, redMine.
+    Example: "http://www.selenic.com/mercurial/bts/issue"
+  regexp;;
+    Regular expression for BTS issue keywords. Default is #(\d+)
+    Example: "issue(\d+)"
+  format;;
+    Print format. Default is #%%s
+    Example: "issue%%s"

 AUTHOR
 ------
diff -r 3daed3680554 -r 38afbf8bff96 hgext/interhg.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/hgext/interhg.py  Wed Jun 20 00:35:25 2007 +0900
@@ -0,0 +1,62 @@
+# interhg.py - interhg
+#
+# Copyright 2007 OHASHI Hideya <ohachige at gmail.com>
+#
+# This software may be used and distributed according to the terms
+# of the GNU General Public License, incorporated herein by reference.
+
+import re
+from mercurial.hgweb import hgweb_mod
+from mercurial import templater
+
+orig_escape = templater.common_filters["escape"]
+
+interhg_table = [ None, None, None ]
+
+def interhg_escape(x):
+    if interhg_table[0] == None:
+        return orig_escape(x)
+    try:
+        header = x.index(":InterHg:")
+        escstr = orig_escape(x[9:])
+        links = ""
+        regexp = re.compile(interhg_table[0])
+        pos = 0
+        while True:
+            match = regexp.search(escstr, pos)
+            if not match:
+                break
+            start = match.start()
+            end = match.end()
+            t_id = match.group(1)
+            t_name = interhg_table[1] % t_id
+            t_str = "<a href=\"" + interhg_table[2] + t_id + "\">" + t_name
+            links = links + escstr[pos:start] + t_str + "</a>"
+            pos = end
+        return links + escstr[pos:]
+    except ValueError:
+        pass
+    return orig_escape(x)
+
+templater.common_filters["escape"] = interhg_escape
+
+orig_refresh = hgweb_mod.hgweb.refresh
+
+def interhg_refresh(self):
+    interhg_table[0] = self.config('interhg', 'regexp', '#(\d+)')
+    interhg_table[1] = self.config('interhg', 'format', '#%s')
+    interhg_table[2] = self.config('interhg', 'bts', 'http://localhost/')
+    return orig_refresh(self)
+
+hgweb_mod.hgweb.refresh = interhg_refresh
+
+orig_changeset = hgweb_mod.hgweb.changeset
+
+def interhg_changeset(self, ctx):
+    desc = ctx.description()
+    def interhg_description():
+        return ":InterHg:" + desc
+    ctx.description = interhg_description
+    return orig_changeset(self, ctx)
+
+hgweb_mod.hgweb.changeset = interhg_changeset


More information about the Mercurial-devel mailing list