Patchwork [V2] templates: add built-in files() function

login
register
mail settings
Submitter Hannes Oldenburg
Date Sept. 23, 2016, 2:35 p.m.
Message ID <5be94ae732b1391c0d0a.1474641332@localhost.localdomain>
Download mbox | patch
Permalink /patch/16769/
State Superseded
Headers show

Comments

Hannes Oldenburg - Sept. 23, 2016, 2:35 p.m.
# HG changeset patch
# User Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
# Date 1474618505 0
#      Fri Sep 23 08:15:05 2016 +0000
# Node ID 5be94ae732b1391c0d0a06fe898d3c4eae69cb38
# Parent  5271ae66615207f39cc41d78f4541bc6f8ca6ff6
templates: add built-in files() function

We already support multiple primitive for listing files, which were
affected by the current changeset.
This patch adds files() which returns files of the current changeset
matching a given pattern or fileset query via the "set:" prefix.
Yuya Nishihara - Sept. 24, 2016, 12:51 a.m.
On Fri, 23 Sep 2016 14:35:32 +0000, Hannes Oldenburg wrote:
> # HG changeset patch
> # User Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
> # Date 1474618505 0
> #      Fri Sep 23 08:15:05 2016 +0000
> # Node ID 5be94ae732b1391c0d0a06fe898d3c4eae69cb38
> # Parent  5271ae66615207f39cc41d78f4541bc6f8ca6ff6
> templates: add built-in files() function

> --- a/mercurial/templater.py	Wed Sep 21 17:05:27 2016 -0400
> +++ b/mercurial/templater.py	Fri Sep 23 08:15:05 2016 +0000
> @@ -699,6 +699,20 @@
>          tzoffset = util.makedate()[1]
>      return (date[0], tzoffset)
>  
> +@templatefunc('files(pattern)')
> +def files(context, mapping, args):
> +    """All files of the current changeset matching the pattern. See
> +    :hg:`help patterns`."""

Nit: template functions are sorted alphabetically.

> +    if not len(args) == 1:
> +        # i18n: "revset" is a keyword

s/revset/files/

> +        raise error.ParseError(_("files expects one argument"))
> +
> +    raw = evalstring(context, mapping, args[0])
> +    ctx = mapping['ctx']
> +    m = ctx.match(raw)

match() expects a list of patterns. It should be [raw].

> --- a/tests/test-command-template.t	Wed Sep 21 17:05:27 2016 -0400
> +++ b/tests/test-command-template.t	Fri Sep 23 08:15:05 2016 +0000
> @@ -3501,6 +3501,18 @@
>    5:13207e5a10d9fd28ec424934298e176197f2c67f,
>    4:bbe44766e73d5f11ed2177f1838de10c53ef3e74
>  
> +Test files function
> +
> +  $ hg log -T "{rev}\n{join(files('*'), '\n')}\n"
> +  2
> +  a
> +  aa
> +  b
> +  1
> +  a
> +  0
> +  a

Can you add tests to catch the bug in this patch?

Patch

diff -r 5271ae666152 -r 5be94ae732b1 mercurial/help/templates.txt
--- a/mercurial/help/templates.txt	Wed Sep 21 17:05:27 2016 -0400
+++ b/mercurial/help/templates.txt	Fri Sep 23 08:15:05 2016 +0000
@@ -95,6 +95,10 @@ 
 
    $ hg log -r 0 --template "files: {join(files, ', ')}\n"
 
+- Join the list of files ending with ".py" with a ", "::
+
+   $ hg log -r 0 --template "pythonfiles: {join(files('**.py'), ', ')}\n"
+
 - Separate non-empty arguments by a " "::
 
    $ hg log -r 0 --template "{separate(' ', node, bookmarks, tags}\n"
diff -r 5271ae666152 -r 5be94ae732b1 mercurial/templater.py
--- a/mercurial/templater.py	Wed Sep 21 17:05:27 2016 -0400
+++ b/mercurial/templater.py	Fri Sep 23 08:15:05 2016 +0000
@@ -699,6 +699,20 @@ 
         tzoffset = util.makedate()[1]
     return (date[0], tzoffset)
 
+@templatefunc('files(pattern)')
+def files(context, mapping, args):
+    """All files of the current changeset matching the pattern. See
+    :hg:`help patterns`."""
+    if not len(args) == 1:
+        # i18n: "revset" is a keyword
+        raise error.ParseError(_("files expects one argument"))
+
+    raw = evalstring(context, mapping, args[0])
+    ctx = mapping['ctx']
+    m = ctx.match(raw)
+    files = list(ctx.matches(m))
+    return templatekw.showlist("file", files, **mapping)
+
 @templatefunc('revset(query[, formatargs...])')
 def revset(context, mapping, args):
     """Execute a revision set query. See
diff -r 5271ae666152 -r 5be94ae732b1 tests/test-command-template.t
--- a/tests/test-command-template.t	Wed Sep 21 17:05:27 2016 -0400
+++ b/tests/test-command-template.t	Fri Sep 23 08:15:05 2016 +0000
@@ -3501,6 +3501,18 @@ 
   5:13207e5a10d9fd28ec424934298e176197f2c67f,
   4:bbe44766e73d5f11ed2177f1838de10c53ef3e74
 
+Test files function
+
+  $ hg log -T "{rev}\n{join(files('*'), '\n')}\n"
+  2
+  a
+  aa
+  b
+  1
+  a
+  0
+  a
+
 Test active bookmark templating
 
   $ hg book foo
diff -r 5271ae666152 -r 5be94ae732b1 tests/test-help.t
--- a/tests/test-help.t	Wed Sep 21 17:05:27 2016 -0400
+++ b/tests/test-help.t	Fri Sep 23 08:15:05 2016 +0000
@@ -1551,6 +1551,9 @@ 
   $ hg help template.files
       files         List of strings. All files modified, added, or removed by
                     this changeset.
+      files(pattern)
+                    All files of the current changeset matching the pattern. See
+                    'hg help patterns'.
 
 Test section lookup by translated message