[PATCH 5 of 6] cmdutil: extract latest tags closures in templatekw

Patrick Mezard pmezard at gmail.com
Mon Nov 30 16:34:45 CST 2009


# HG changeset patch
# User Patrick Mezard <pmezard at gmail.com>
# Date 1259616498 -3600
# Node ID c892c6cc06cbdab1354e0af2312d74eb7061ab52
# Parent  9651033b608af8b664eba0962a0fc943235f373b
cmdutil: extract latest tags closures in templatekw

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -757,9 +757,6 @@
                                          'manifest': '{rev}:{node|formatnode}',
                                          'filecopy': '{name} ({source})',
                                          'extra': '{key}={value|stringescape}'})
-        # Cache mapping from rev to a tuple with tag date, tag
-        # distance and tag name
-        self._latesttagcache = {-1: (0, 0, 'null')}
 
     def use_template(self, t):
         '''set template string to use'''
@@ -777,30 +774,6 @@
             return []
         return parents
 
-    def _latesttaginfo(self, rev):
-        '''return date, distance and name for the latest tag of rev'''
-        todo = [rev]
-        while todo:
-            rev = todo.pop()
-            if rev in self._latesttagcache:
-                continue
-            ctx = self.repo[rev]
-            tags = [t for t in ctx.tags() if self.repo.tagtype(t) == 'global']
-            if tags:
-                self._latesttagcache[rev] = ctx.date()[0], 0, ':'.join(sorted(tags))
-                continue
-            try:
-                # The tuples are laid out so the right one can be found by comparison.
-                pdate, pdist, ptag = max(
-                    self._latesttagcache[p.rev()] for p in ctx.parents())
-            except KeyError:
-                # Cache miss - recurse
-                todo.append(rev)
-                todo.extend(p.rev() for p in ctx.parents())
-                continue
-            self._latesttagcache[rev] = pdate, pdist + 1, ptag
-        return self._latesttagcache[rev]
-
     def _show(self, ctx, copies, props):
         '''show a single changeset or file revision'''
 
@@ -814,17 +787,10 @@
         def showcopies(repo, ctx, templ, **args):
             c = [{'name': x[0], 'source': x[1]} for x in copies]
             return showlist(templ, 'file_copy', c, plural='file_copies', **args)
-        
-        def showlatesttag(repo, ctx, templ, **args):
-            return self._latesttaginfo(ctx.rev())[2]
-        def showlatesttagdistance(repo, ctx, templ, **args):
-            return self._latesttaginfo(ctx.rev())[1]
 
         defprops = {
             'file_copies': showcopies,            
             'parents': showparents,            
-            'latesttag': showlatesttag,
-            'latesttagdistance': showlatesttagdistance,
             }
         props = props.copy()
         props.update(templatekw.keywords)
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -75,6 +75,39 @@
                                      ctx.node())[:3]
     return cache['files']
 
+def getlatesttags(repo, ctx, cache):
+    '''return date, distance and name for the latest tag of rev'''
+
+    if 'latesttags' not in cache:
+        # Cache mapping from rev to a tuple with tag date, tag
+        # distance and tag name
+        cache['latesttags'] = {-1: (0, 0, 'null')}
+    latesttags = cache['latesttags']
+
+    rev = ctx.rev()
+    todo = [rev]
+    while todo:
+        rev = todo.pop()
+        if rev in latesttags:
+            continue
+        ctx = repo[rev]
+        tags = [t for t in ctx.tags() if repo.tagtype(t) == 'global']
+        if tags:
+            latesttags[rev] = ctx.date()[0], 0, ':'.join(sorted(tags))
+            continue
+        try:
+            # The tuples are laid out so the right one can be found by
+            # comparison.
+            pdate, pdist, ptag = max(
+                latesttags[p.rev()] for p in ctx.parents())
+        except KeyError:
+            # Cache miss - recurse
+            todo.append(rev)
+            todo.extend(p.rev() for p in ctx.parents())
+            continue
+        latesttags[rev] = pdate, pdist + 1, ptag
+    return latesttags[rev]
+
 def showauthor(repo, ctx, templ, **args):
     return ctx.user()
 
@@ -117,6 +150,12 @@
 def showfiles(repo, ctx, templ, **args):
     return showlist(templ, 'file', ctx.files(), **args)
 
+def showlatesttag(repo, ctx, templ, cache, **args):
+    return getlatesttags(repo, ctx, cache)[2]
+
+def showlatesttagdistance(repo, ctx, templ, cache, **args):
+    return getlatesttags(repo, ctx, cache)[1]
+
 def showmanifest(repo, ctx, templ, **args):
     args = args.copy()
     args.update(dict(rev=repo.manifest.rev(ctx.changeset()[0]),
@@ -143,6 +182,8 @@
     'file_dels': showfiledels,
     'file_mods': showfilemods,
     'files': showfiles,
+    'latesttag': showlatesttag,
+    'latesttagdistance': showlatesttagdistance,
     'manifest': showmanifest,
     'node': shownode,
     'rev': showrev,


More information about the Mercurial-devel mailing list