Patchwork template: add rev*(revision) functions for use with revsets

login
register
mail settings
Submitter timeless@mozdev.org
Date Sept. 2, 2015, 4:30 a.m.
Message ID <ce481a6fe8d07685374b.1441168212@waste.org>
Download mbox | patch
Permalink /patch/10365/
State Rejected
Headers show

Comments

timeless@mozdev.org - Sept. 2, 2015, 4:30 a.m.
# HG changeset patch
# User timeless@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
Yuya Nishihara - Sept. 2, 2015, 2:20 p.m.
On Tue, 01 Sep 2015 23:30:12 -0500, timeless@mozdev.org wrote:
> # HG changeset patch
> # User timeless@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()

My idea is to add a function or operator that switches mapping['ctx'].
For example, "{desc[rev]}" will copy mapping, replace ctx by repo[rev],
then call showdescription().

https://selenic.com/pipermail/mercurial-devel/2015-May/070092.html
Pierre-Yves David - Sept. 8, 2015, 10:48 p.m.
On 09/01/2015 09:30 PM, timeless@mozdev.org wrote:
> # HG changeset patch
> # User timeless@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

 From this description, it is unclear to me what his changesets is 
about. Can you clarify this?

>
> 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 ..
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel
>
timeless - Sept. 11, 2015, 3:42 a.m.
Pierre-Yves David wrote:
> On 09/01/2015 09:30 PM, timeless@mozdev.org wrote:
>>
>> # HG changeset patch
>> # User timeless@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
>
> From this description, it is unclear to me what his changesets is about. Can
> you clarify this?

split into 11 changesets (8 of which made it through pushgate):
http://patchwork.serpentine.com/patch/10478/

is that better?

Patch

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 ..