@@ -122,14 +122,21 @@
revcache['files'] = repo.status(ctx.p1(), ctx)[:3]
return revcache['files']
-def getlatesttags(repo, ctx, cache):
+def getlatesttags(repo, ctx, cache, pattern=None):
'''return date, distance and name for the latest tag of rev'''
- if 'latesttags' not in cache:
+ cachename = 'latesttags'
+ if pattern is not None:
+ cachename += '-' + pattern
+ match = util.stringmatcher(pattern)[2]
+ else:
+ match = util.always
+
+ if cachename 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']
+ cache[cachename] = {-1: (0, 0, ['null'])}
+ latesttags = cache[cachename]
rev = ctx.rev()
todo = [rev]
@@ -139,7 +146,8 @@
continue
ctx = repo[rev]
tags = [t for t in ctx.tags()
- if (repo.tagtype(t) and repo.tagtype(t) != 'local')]
+ if (repo.tagtype(t) and repo.tagtype(t) != 'local'
+ and match(t))]
if tags:
latesttags[rev] = ctx.date()[0], 0, [t for t in sorted(tags)]
continue
@@ -551,6 +551,42 @@
return minirst.format(text, style=style, keep=['verbose'])
+def latesttag(context, mapping, args):
+ """:latesttag(pattern): The global tags matching the given pattern on the
+ most recent globally tagged ancestor of this changeset."""
+ if len(args) != 1:
+ # i18n: "latesttag" is a keyword
+ raise error.ParseError(_("latesttag expects one argument"))
+
+ pattern = stringify(args[0][0](context, mapping, args[0][1]))
+
+ repo, ctx = mapping['repo'], mapping['ctx']
+ cache = mapping['cache']
+ latesttags = templatekw.getlatesttags(repo, ctx, cache, pattern)
+
+ def changes(tag):
+ offset = 0
+ revs = [ctx.rev()]
+
+ # The only() revset doesn't currently support wdir()
+ if ctx.rev() is None:
+ offset = 1
+ revs = [p.rev() for p in ctx.parents()]
+
+ return len(repo.revs('only(%ld, %s)', revs, tag)) + offset
+
+ makemap = lambda v: {
+ 'changes': changes(v[2][0]),
+ 'distance': v[1],
+ # Not the date the tag was applied, but not much we can do about that
+ #'tagdate': util.makedate(v[0]),
+ 'tags': v[2]#templatekw.showlist('tag', v[2], separator=':', **mapping),
+ }
+
+ latesttags = [latesttags]
+ f = templatekw._showlist('latesttag', latesttags, **mapping)
+ return templatekw._hybrid(f, latesttags, makemap, lambda x: x['latesttag'])
+
def shortest(context, mapping, args):
""":shortest(node, minlength=4): Obtain the shortest representation of
a node."""
@@ -693,6 +729,7 @@
"indent": indent,
"join": join,
"label": label,
+ "latesttag": latesttag,
"pad": pad,
"revset": revset,
"rstdoc": rstdoc,
@@ -2681,6 +2681,19 @@
1: t1+0
0: null+1
+ $ hg log --template "{rev}: {latesttag('re:^t[13]$') % '{tags}, C: {changes}, D: {distance}'}\n"
+ 10: t3, C: 8, D: 7
+ 9: t3, C: 7, D: 6
+ 8: t3, C: 6, D: 5
+ 7: t3, C: 5, D: 4
+ 6: t3, C: 4, D: 3
+ 5: t3, C: 3, D: 2
+ 4: t3, C: 1, D: 1
+ 3: t3, C: 0, D: 0
+ 2: t1, C: 1, D: 1
+ 1: t1, C: 0, D: 0
+ 0: null, C: 1, D: 1
+
$ cd ..