Source

redmine-mq-issue4455 / hgext-helper.diff

# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1269771309 -32400
# Node ID 053b5fdb500a6d640ec3762f4c032e883e46b32f
# Parent  f059ef361c4656151b29f45c2ba24552125b5cb9
Add Mercurial helper extention

- 'rhsummary' and 'rhmanifest' for reducing the number of hg command calls.
- 'rhdiff' for compatibility with Mercurial<1.1.

Also renamed TEMPLATES_DIR to HELPERS_DIR because the directory now contains
templates and a helper extention.

Original version was written by Alessio Franceschelli,
downloaded from http://www.redmine.org/attachments/3395/overhaul.py

Signed-off-by: Yuya Nishihara <yuya@tcha.org>

diff --git a/lib/redmine/scm/adapters/mercurial/redminehelper.py b/lib/redmine/scm/adapters/mercurial/redminehelper.py
new file mode 100644
--- /dev/null
+++ b/lib/redmine/scm/adapters/mercurial/redminehelper.py
@@ -0,0 +1,167 @@
+# redminehelper: Redmine helper extension for Mercurial
+#
+# Copyright 2010 Alessio Franceschelli (alefranz.net)
+# Copyright 2010-2011 Yuya Nishihara <yuya@tcha.org>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+"""helper commands for Redmine to reduce the number of hg calls
+
+To test this extension, please try::
+
+    $ hg --config extensions.redminehelper=redminehelper.py rhsummary
+
+I/O encoding:
+
+:file path: urlencoded, raw string
+:tag name: utf-8
+:branch name: utf-8
+:node: 12-digits (short) hex string
+
+Output example of rhsummary::
+
+    <?xml version="1.0"?>
+    <rhsummary>
+      <repository root="/foo/bar">
+        <tip revision="1234" node="abcdef0123..."/>
+        <tag revision="123" node="34567abc..." name="1.1.1"/>
+        <branch .../>
+        ...
+      </repository>
+    </rhsummary>
+
+Output example of rhmanifest::
+
+    <?xml version="1.0"?>
+    <rhmanifest>
+      <repository root="/foo/bar">
+        <manifest revision="1234" path="lib">
+          <file name="diff.rb" revision="123" node="34567abc..." time="12345"
+                 size="100"/>
+          ...
+          <dir name="redmine"/>
+          ...
+        </manifest>
+      </repository>
+    </rhmanifest>
+"""
+import re, time, cgi, urllib
+from mercurial import cmdutil, commands, node, error
+
+_x = cgi.escape
+_u = lambda s: cgi.escape(urllib.quote(s))
+
+def _tip(ui, repo):
+    # see mercurial/commands.py:tip
+    def tiprev():
+        try:
+            return len(repo) - 1
+        except TypeError:  # Mercurial < 1.1
+            return repo.changelog.count() - 1
+    tipctx = repo.changectx(tiprev())
+    ui.write('<tip revision="%d" node="%s"/>\n'
+             % (tipctx.rev(), _x(node.short(tipctx.node()))))
+
+_SPECIAL_TAGS = ('tip',)
+
+def _tags(ui, repo):
+    # see mercurial/commands.py:tags
+    for t, n in reversed(repo.tagslist()):
+        if t in _SPECIAL_TAGS:
+            continue
+        try:
+            r = repo.changelog.rev(n)
+        except error.LookupError:
+            continue
+        ui.write('<tag revision="%d" node="%s" name="%s"/>\n'
+                 % (r, _x(node.short(n)), _x(t)))
+
+def _branches(ui, repo):
+    # see mercurial/commands.py:branches
+    def iterbranches():
+        for t, n in repo.branchtags().iteritems():
+            yield t, n, repo.changelog.rev(n)
+    def branchheads(branch):
+        try:
+            return repo.branchheads(branch, closed=False)
+        except TypeError:  # Mercurial < 1.2
+            return repo.branchheads(branch)
+    for t, n, r in sorted(iterbranches(), key=lambda e: e[2], reverse=True):
+        if repo.lookup(r) in branchheads(t):
+            ui.write('<branch revision="%d" node="%s" name="%s"/>\n'
+                     % (r, _x(node.short(n)), _x(t)))
+
+def _manifest(ui, repo, path, rev):
+    ctx = repo.changectx(rev)
+    ui.write('<manifest revision="%d" path="%s">\n'
+             % (ctx.rev(), _u(path)))
+
+    known = set()
+    pathprefix = (path.rstrip('/') + '/').lstrip('/')
+    for f, n in sorted(ctx.manifest().iteritems(), key=lambda e: e[0]):
+        if not f.startswith(pathprefix):
+            continue
+        name = re.sub(r'/.*', '/', f[len(pathprefix):])
+        if name in known:
+            continue
+        known.add(name)
+
+        if name.endswith('/'):
+            ui.write('<dir name="%s"/>\n'
+                     % _x(urllib.quote(name[:-1])))
+        else:
+            fctx = repo.filectx(f, fileid=n)
+            tm, tzoffset = fctx.date()
+            ui.write('<file name="%s" revision="%d" node="%s" '
+                     'time="%d" size="%d"/>\n'
+                     % (_u(name), fctx.rev(), _x(node.short(fctx.node())),
+                        tm, fctx.size(), ))
+
+    ui.write('</manifest>\n')
+
+def rhdiff(ui, repo, *pats, **opts):
+    """diff repository (or selected files)"""
+    change = opts.pop('change', None)
+    if change:  # add -c option for Mercurial<1.1
+        base = repo.changectx(change).parents()[0].rev()
+        opts['rev'] = [str(base), change]
+    opts['nodates'] = True
+    return commands.diff(ui, repo, *map(urllib.unquote, pats), **opts)
+
+def rhmanifest(ui, repo, path='', **opts):
+    """output the sub-manifest of the specified directory"""
+    ui.write('<?xml version="1.0"?>\n')
+    ui.write('<rhmanifest>\n')
+    ui.write('<repository root="%s">\n' % _u(repo.root))
+    try:
+        _manifest(ui, repo, urllib.unquote(path), opts.get('rev'))
+    finally:
+        ui.write('</repository>\n')
+        ui.write('</rhmanifest>\n')
+
+def rhsummary(ui, repo, **opts):
+    """output the summary of the repository"""
+    ui.write('<?xml version="1.0"?>\n')
+    ui.write('<rhsummary>\n')
+    ui.write('<repository root="%s">\n' % _u(repo.root))
+    try:
+        _tip(ui, repo)
+        _tags(ui, repo)
+        _branches(ui, repo)
+        # TODO: bookmarks in core (Mercurial>=1.8)
+    finally:
+        ui.write('</repository>\n')
+        ui.write('</rhsummary>\n')
+
+# This extension should be compatible with Mercurial 0.9.5.
+# Note that Mercurial 0.9.5 doesn't have extensions.wrapfunction().
+cmdtable = {
+    'rhdiff': (rhdiff,
+               [('r', 'rev', [], 'revision'),
+                ('c', 'change', '', 'change made by revision')],
+               'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...'),
+    'rhmanifest': (rhmanifest,
+                   [('r', 'rev', '', 'show the specified revision')],
+                   'hg rhmanifest [-r REV] [PATH]'),
+    'rhsummary': (rhsummary, [], 'hg rhsummary'),
+}
diff --git a/lib/redmine/scm/adapters/mercurial_adapter.rb b/lib/redmine/scm/adapters/mercurial_adapter.rb
--- a/lib/redmine/scm/adapters/mercurial_adapter.rb
+++ b/lib/redmine/scm/adapters/mercurial_adapter.rb
@@ -25,7 +25,8 @@ module Redmine
 
         # Mercurial executable name
         HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
-        TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
+        HELPERS_DIR = File.dirname(__FILE__) + "/mercurial"
+        HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py"
         TEMPLATE_NAME = "hg-template"
         TEMPLATE_EXTENSION = "tmpl"
 
@@ -73,7 +74,7 @@ module Redmine
             else
               ver = "0.9.5"
             end
-            "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
+            "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
           end
         end
 
@@ -216,6 +217,7 @@ module Redmine
         # Runs 'hg' command with the given args
         def hg(*args, &block)
           full_args = [HG_BIN, '--cwd', url, '--encoding', 'utf-8']
+          full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
           full_args += args
           ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
           if $? && $?.exitstatus != 0
diff --git a/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb b/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb
--- a/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb
+++ b/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb
@@ -4,7 +4,7 @@ begin
 
   class MercurialAdapterTest < ActiveSupport::TestCase
 
-    TEMPLATES_DIR = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATES_DIR
+    HELPERS_DIR = Redmine::Scm::Adapters::MercurialAdapter::HELPERS_DIR
     TEMPLATE_NAME = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_NAME
     TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION
 
@@ -149,7 +149,7 @@ begin
       end
 
       def test_template_path_for(version, template)
-        assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
+        assert_equal "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
                      @adapter.class.template_path_for(version)
         assert File.exist?(@adapter.class.template_path_for(version))
       end