changeset 684:8687c5aa4f35

Add svnrev, svnpath and svnuuid keyword. Based on a patch by Wagner Bruna posted here: http://groups.google.com/group/hgsubversion/browse_thread/thread/b3913337e021ab18
author Andi Albrecht <albrecht.andi@gmail.com>
date Thu, 02 Sep 2010 21:30:17 +0200
parents 4589d48c9e1b
children d4781cff2adc
files hgsubversion/__init__.py tests/run.py tests/test_template_keywords.py
diffstat 3 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/__init__.py
+++ b/hgsubversion/__init__.py
@@ -23,6 +23,7 @@ from mercurial import commands
 from mercurial import extensions
 from mercurial import help
 from mercurial import hg
+from mercurial import templatekw
 from mercurial import util as hgutil
 from mercurial import demandimport
 demandimport.ignore.extend([
@@ -132,6 +133,32 @@ def uisetup(ui):
     else:
         help.helptable = help.helptable + entries
 
+
+def _get_svnmeta(ctx):
+    """Returns a dictionary with parsed convert_revision or None."""
+    unparsed = ctx.extra().get('convert_revision', '')
+    if unparsed.startswith('svn:'):
+        unparsed = unparsed[4:]  # remove "svn:"
+        return {'svnuuid': unparsed[:36],
+                'svnpath': unparsed[36:].rsplit('@', 1)[0],
+                'svnrev': unparsed[36:].rsplit('@', 1)[-1]}
+    return None
+
+
+def _show_tpl_kw(ctx, kw):
+    convinfo = _get_svnmeta(ctx)
+    if convinfo is None:
+        return ''
+    return convinfo.get(kw, '')
+
+
+templatekw.keywords.update({
+    'svnrev': lambda repo, ctx, templ, **a: _show_tpl_kw(ctx, 'svnrev'),
+    'svnpath': lambda repo, ctx, templ, **a: _show_tpl_kw(ctx, 'svnpath'),
+    'svnuuid': lambda repo, ctx, templ, **a: _show_tpl_kw(ctx, 'svnuuid'),
+    })
+
+
 def reposetup(ui, repo):
     if repo.local():
        svnrepo.generate_repo_class(ui, repo)
--- a/tests/run.py
+++ b/tests/run.py
@@ -25,6 +25,7 @@ def tests():
     import test_startrev
     import test_svnwrap
     import test_tags
+    import test_template_keywords
     import test_utility_commands
     import test_urls
 
new file mode 100644
--- /dev/null
+++ b/tests/test_template_keywords.py
@@ -0,0 +1,39 @@
+import test_util
+
+import unittest
+
+from mercurial import commands
+from mercurial import ui
+
+
+class CapturingUI(ui.ui):
+
+    def __init__(self, *args, **kwds):
+        super(CapturingUI, self).__init__(*args, **kwds)
+        self._output = ""
+
+    def write(self, msg, *args, **kwds):
+        self._output += msg
+
+
+class TestLogKeywords(test_util.TestBase):
+
+    def test_svn_keywords(self):
+        defaults = {'date': None, 'rev': None, 'user': None}
+        repo = self._load_fixture_and_fetch('two_revs.svndump')
+        ui = CapturingUI()
+        commands.log(ui, repo, template='{rev}:{svnrev} ', **defaults)
+        self.assertEqual(ui._output, '0:2 1:3 ')
+        ui = CapturingUI()
+        commands.log(ui, repo, template='{rev}:{svnpath} ', **defaults)
+        self.assertEqual(ui._output, '0:/trunk 1:/trunk ')
+        ui = CapturingUI()
+        commands.log(ui, repo, template='{rev}:{svnuuid} ', **defaults)
+        self.assertEqual(ui._output,
+                         ('0:df2126f7-00ab-4d49-b42c-7e981dde0bcf '
+                          '1:df2126f7-00ab-4d49-b42c-7e981dde0bcf '))
+
+
+def suite():
+    all = [unittest.TestLoader().loadTestsFromTestCase(TestLogKeywords),]
+    return unittest.TestSuite(all)