changeset 29:659346a98de7 0.12

Compatibility fix for hg 2.9 (add `localrepo.branchtags()` if missing)
author Christian Boos <cboos@edgewall.org>
date Thu, 06 Feb 2014 22:34:16 +0100
parents 5b6010df7f43
children 21bb3cc19977
files tracext/hg/backend.py
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/tracext/hg/backend.py	Thu Feb 06 19:27:21 2014 +0100
+++ b/tracext/hg/backend.py	Thu Feb 06 22:34:16 2014 +0100
@@ -20,6 +20,7 @@
 import posixpath
 import re
 import sys
+import types
 
 import pkg_resources
 
@@ -144,6 +145,26 @@
         return s
 
 
+# Note: localrepository.branchtags was removed in mercurial-2.9
+#       see http://selenic.com/hg/rev/4274eda143cb
+def get_branchtags(repo):
+    """return a dict where branch names map to the tipmost head of
+    the branch, open heads come before closed_branches
+    """
+    def branchtip(heads):
+        '''return the tipmost branch head in heads'''
+        tip = heads[-1]
+        for h in reversed(heads):
+            if not repo[h].closesbranch():
+              tip = h
+              break
+        return tip
+
+    bt = {}
+    for bn, heads in repo.branchmap().iteritems():
+        bt[bn] = branchtip(heads)
+    return bt
+
 class trac_ui(ui):
     # Note: will be dropped in 0.13, see MercurialConnector._setup_ui
     def __init__(self, *args, **kwargs):
@@ -528,6 +549,9 @@
                               " repository (Mercurial %(version)s says "
                               "%(error)s)",
                               path=path, version=version, error=error))
+        # restore branchtags() if needed (see StackOverflow:972)
+        if not getattr(self.repo, 'branchtags', None):
+            self.repo.branchtags = types.MethodType(get_branchtags, self.repo)
         Repository.__init__(self, 'hg:%s' % path, params, log)
 
     def from_hg_time(self, timeinfo):