[PATCH] template: add rev*(revision) functions for use with revsets

timeless at mozdev.org timeless at mozdev.org
Wed Sep 2 04:30:12 UTC 2015


# HG changeset patch
# User timeless at mozdev.org
# Date 1441163415 14400
#      Tue Sep 01 23:10:15 2015 -0400
# Node ID ce481a6fe8d07685374baca22066dcdc4ab72c80
# Parent  f77a3f27cea533232dc107a72773e90e2de9549d
template: add rev*(revision) functions for use with revsets

revauthor
revbisect
revbookmarks
revbranches
revdate
revdesc
revdiffstat
revfiles
revset
revtags

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -15,8 +15,10 @@
 from . import (
     config,
     error,
+    hbisect,
     minirst,
     parser,
+    patch,
     revset as revsetmod,
     templatefilters,
     templatekw,
@@ -544,6 +546,80 @@
         tzoffset = util.makedate()[1]
     return (date[0], tzoffset)
 
+def ctxrepo(func, context, mapping, args):
+    if not len(args) == 1:
+        raise error.ParseError(_("%s expects one argument" % func))
+    raw = stringify(args[0][0](context, mapping, args[0][1]))
+    rev = raw
+    ctx = mapping['ctx']
+    repo = ctx.repo()
+    ctx = repo[rev]
+    return ctx, repo
+
+def revdesc(context, mapping, args):
+    """:revdesc(rev): Show description.
+    See :hg:`help templates.desc`."""
+    ctx, repo = ctxrepo('revdesc', context, mapping, args)
+    return ctx.description().strip()
+
+def revfiles(context, mapping, args):
+    """:revfiles(rev): Show files.
+    See :hg:`help templates.files`."""
+    ctx, repo = ctxrepo('revfiles', context, mapping, args)
+    modified, added, removed = repo.status(ctx.p1(), ctx)[:3]
+    files = modified + added + removed
+    return templatekw.showlist("file", files, **mapping)
+
+def revtags(context, mapping, args):
+    """:revtags(rev): Show tags.
+    See :hg:`help templates.tags`."""
+    ctx, repo = ctxrepo('revtags', context, mapping, args)
+    namespace = 'tags'
+    ns = repo.names[namespace]
+    names = ns.names(repo, ctx.node())
+    return names
+
+def revauthor(context, mapping, args):
+    """:revauthor(rev): Show author.
+    See :hg:`help templates.author`."""
+    ctx, repo = ctxrepo('revauthor', context, mapping, args)
+    return ctx.user()
+
+def revbisect(context, mapping, args):
+    """:revbisect(rev): Show bisect.
+    See :hg:`help templates.bisect`."""
+    ctx, repo = ctxrepo('revbisect', context, mapping, args)
+    return hbisect.label(repo, ctx.node())
+
+def revbranches(context, mapping, args):
+    """:revbranches(rev): Show branches.
+    See :hg:`help templates.branch`."""
+    ctx, repo = ctxrepo('revbranches', context, mapping, args)
+    return ctx.branch()
+
+def revbookmarks(context, mapping, args):
+    """:revbookmarks(rev): Show bookmarks
+    See :hg:`help templates.bookmarks`."""
+    ctx, repo = ctxrepo('revbookmarks', context, mapping, args)
+    bookmarks = ctx.bookmarks()
+    return bookmarks
+
+def revdate(context, mapping, args):
+    """:revdate(rev): Show date.
+    See :hg:`help templates.date`."""
+    ctx, repo = ctxrepo('revdate', context, mapping, args)
+    return ctx.date()
+
+def revdiffstat(context, mapping, args):
+    """:revdiffstat(rev): Show diffstat.
+    See :hg:`help templates.diffstat`."""
+    ctx, repo = ctxrepo('revdiffstat', context, mapping, args)
+    stats = patch.diffstatdata(util.iterlines(ctx.diff()))
+    count = len(stats)
+    added = sum([a[1] for a in stats])
+    removed = sum([a[2] for a in stats])
+    return "%s: +%s/-%s" % (count, added, removed)
+
 def revset(context, mapping, args):
     """:revset(query[, formatargs...]): Execute a revision set query. See
     :hg:`help revset`."""
@@ -730,7 +806,16 @@
     "label": label,
     "localdate": localdate,
     "pad": pad,
+    "revauthor": revauthor,
+    "revbisect": revbisect,
+    "revbookmarks": revbookmarks,
+    "revbranches": revbranches,
+    "revdate": revdate,
+    "revdesc": revdesc,
+    "revdiffstat": revdiffstat,
+    "revfiles": revfiles,
     "revset": revset,
+    "revtags": revtags,
     "rstdoc": rstdoc,
     "shortest": shortest,
     "startswith": startswith,
diff --git a/tests/test-log.t b/tests/test-log.t
--- a/tests/test-log.t
+++ b/tests/test-log.t
@@ -2094,4 +2094,47 @@
      date:        Thu Jan 01 00:00:00 1970 +0000
      summary:     A1B1C1
   
+Verify rev*() template functions
+
+  $ hg bisect -b 0
+  $ hg bisect -g 2 > ignored
+  $ hg bookmark mark -r 1
+  $ hg log -r 0 --template '{revset("all()") % "changeset:\t{revision}\nbranch:\t{revision|revbranches}\ntag:\t\t{revision|revtags}\nbookmarks:\t{revision|revbookmarks}\nbisect:\t{revision|revbisect}\nuser:\t{revision|revauthor}\ndate:\t{revision|revdate|date}\nsummary:\t{revision|revdesc|firstline}\ndiffstat:\t{revision|revdiffstat}\nfiles:\t{revision|revfiles}\n\n"}\n'
+  changeset:	0
+  branch:	default
+  tag:		
+  bookmarks:	
+  bisect:	bad
+  user:	test
+  date:	Thu Jan 01 00:00:00 1970 +0000
+  summary:	A1B1C1
+  diffstat:	21: +21/-0
+  files:	A B C D E F G H I J K L M N O P Q R S T U
+  
+  changeset:	1
+  branch:	default
+  tag:		
+  bookmarks:	mark
+  bisect:	
+  user:	test
+  date:	Thu Jan 01 00:00:00 1970 +0000
+  summary:	A2B2C2
+  diffstat:	3: +3/-3
+  files:	A B C
+  
+  changeset:	2
+  branch:	default
+  tag:		tip
+  bookmarks:	
+  bisect:	good
+  user:	test
+  date:	Thu Jan 01 00:00:00 1970 +0000
+  summary:	A3B2C2
+  diffstat:	3: +3/-3
+  files:	A B C
+  
+  
+  $ hg log -r . --template '{revtags()}'
+  hg: parse error: revtags expects one argument
+  [255]
   $ cd ..


More information about the Mercurial-devel mailing list