Patchwork [2,of,2,V2] templater: add count template filter, plus tests

login
register
mail settings
Submitter Anton Shestakov
Date Sept. 9, 2014, 1:25 p.m.
Message ID <3195371410269125@web7m.yandex.ru>
Download mbox | patch
Permalink /patch/5736/
State Superseded
Commit 13e3f07d74a343a352a8adafb37d32087a1d823f
Headers show

Comments

Anton Shestakov - Sept. 9, 2014, 1:25 p.m.
# HG changeset patch
# User Anton Shestakov <engored@ya.ru>
# Date 1410268453 -32400
#      Tue Sep 09 22:14:13 2014 +0900
# Node ID 698e992e50c7a11a398acb4a4b2e4ec1e6097177
# Parent  9bb0ab35e90ac9ebf6adbd6e503f3412d9f2adb7
templater: add count template filter, plus tests

Currently there is no way of telling how much children or bookmarks or tags a
certain changeset has in a template. It is possible to tell if a changeset have
either 0 or not 0 bookmarks, but no way to tell if it has 1 or 2 of them, for
example.

This filter, simply named count, makes it possible to count the number of items
in a list or the length of a string (or, anything that python's len can count).
E.g.: {children|count}, {bookmarks|count}, {file_adds|count}.

Testing the filter on node hash and shortened node hash is chosen because they
both have defined length.

As for lists of strings, children, tags and file_adds are used, because they
provide some variety and also prove that what's counted is the number of string
items in the list, and not the list stringified (they are lists of non-empty,
multi-character strings).

Additionally, revset template function is used for testing the filter, since
the combination is very flexible and will possibly be used together a lot.
Siddharth Agarwal - Sept. 20, 2014, 1:36 a.m.
On 09/09/2014 06:25 AM, Anton Shestakov wrote:
> # HG changeset patch
> # User Anton Shestakov <engored@ya.ru>
> # Date 1410268453 -32400
> #      Tue Sep 09 22:14:13 2014 +0900
> # Node ID 698e992e50c7a11a398acb4a4b2e4ec1e6097177
> # Parent  9bb0ab35e90ac9ebf6adbd6e503f3412d9f2adb7
> templater: add count template filter, plus tests

Any updates for this patch?

>
> Currently there is no way of telling how much children or bookmarks or tags a
> certain changeset has in a template. It is possible to tell if a changeset have
> either 0 or not 0 bookmarks, but no way to tell if it has 1 or 2 of them, for
> example.
>
> This filter, simply named count, makes it possible to count the number of items
> in a list or the length of a string (or, anything that python's len can count).
> E.g.: {children|count}, {bookmarks|count}, {file_adds|count}.
>
> Testing the filter on node hash and shortened node hash is chosen because they
> both have defined length.
>
> As for lists of strings, children, tags and file_adds are used, because they
> provide some variety and also prove that what's counted is the number of string
> items in the list, and not the list stringified (they are lists of non-empty,
> multi-character strings).
>
> Additionally, revset template function is used for testing the filter, since
> the combination is very flexible and will possibly be used together a lot.
>
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -224,6 +224,10 @@ def jsonescape(s):
>           s = s.replace(k, v)
>       return ''.join(_uescape(c) for c in s)
>   
> +def count(i):
> +    """:count: List or text. Returns the length as an integer."""
> +    return len(i)
> +
>   def localdate(text):
>       """:localdate: Date. Converts a date to local date."""
>       return (util.parsedate(text)[0], util.makedate()[1])
> @@ -379,6 +383,7 @@ filters = {
>       "isodatesec": isodatesec,
>       "json": json,
>       "jsonescape": jsonescape,
> +    "count": count,
>       "localdate": localdate,
>       "nonempty": nonempty,
>       "obfuscate": obfuscate,
> diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> --- a/tests/test-command-template.t
> +++ b/tests/test-command-template.t
> @@ -1413,6 +1413,38 @@ Age filter:
>     $ hg log -l1 --template '{date|age}\n'
>     7 years from now
>   
> +Count filter:
> +
> +  $ hg log -l1 --template '{node|count} {node|short|count}\n'
> +  40 12
> +
> +  $ hg log -l1 --template '{revset("null^")|count} {revset(".")|count} {revset("0::3")|count}\n'
> +  0 1 4
> +
> +  $ hg log -G --template '{rev}: children: {children|count}, \
> +  > tags: {tags|count}, file_adds: {file_adds|count}, \
> +  > ancestors: {revset("ancestors(%s)", rev)|count}'
> +  @  9: children: 0, tags: 1, file_adds: 1, ancestors: 3
> +  |
> +  o  8: children: 1, tags: 0, file_adds: 2, ancestors: 2
> +  |
> +  o  7: children: 1, tags: 0, file_adds: 1, ancestors: 1
> +
> +  o    6: children: 0, tags: 0, file_adds: 0, ancestors: 7
> +  |\
> +  | o  5: children: 1, tags: 0, file_adds: 1, ancestors: 5
> +  | |
> +  o |  4: children: 1, tags: 0, file_adds: 0, ancestors: 5
> +  |/
> +  o  3: children: 2, tags: 0, file_adds: 0, ancestors: 4
> +  |
> +  o  2: children: 1, tags: 0, file_adds: 1, ancestors: 3
> +  |
> +  o  1: children: 1, tags: 0, file_adds: 1, ancestors: 2
> +  |
> +  o  0: children: 1, tags: 0, file_adds: 1, ancestors: 1
> +
> +
>   Error on syntax:
>   
>     $ echo 'x = "f' >> t
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
Anton Shestakov - Sept. 20, 2014, 3:46 a.m.
20.09.2014, 10:36, "Siddharth Agarwal" <sid@less-broken.com>:
> On 09/09/2014 06:25 AM, Anton Shestakov wrote:
>>  # HG changeset patch
>>  # User Anton Shestakov <engored@ya.ru>
>>  # Date 1410268453 -32400
>>  #      Tue Sep 09 22:14:13 2014 +0900
>>  # Node ID 698e992e50c7a11a398acb4a4b2e4ec1e6097177
>>  # Parent  9bb0ab35e90ac9ebf6adbd6e503f3412d9f2adb7
>>  templater: add count template filter, plus tests
>
> Any updates for this patch?

I'm not sure who you're asking, but I got nothing new. I could though resubmit
this with a corrected email subject line and rebased against current tip,
but that's trivial. Should I do that? I'm new here, sorry.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -224,6 +224,10 @@  def jsonescape(s):
         s = s.replace(k, v)
     return ''.join(_uescape(c) for c in s)
 
+def count(i):
+    """:count: List or text. Returns the length as an integer."""
+    return len(i)
+
 def localdate(text):
     """:localdate: Date. Converts a date to local date."""
     return (util.parsedate(text)[0], util.makedate()[1])
@@ -379,6 +383,7 @@  filters = {
     "isodatesec": isodatesec,
     "json": json,
     "jsonescape": jsonescape,
+    "count": count,
     "localdate": localdate,
     "nonempty": nonempty,
     "obfuscate": obfuscate,
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -1413,6 +1413,38 @@  Age filter:
   $ hg log -l1 --template '{date|age}\n'
   7 years from now
 
+Count filter:
+
+  $ hg log -l1 --template '{node|count} {node|short|count}\n'
+  40 12
+
+  $ hg log -l1 --template '{revset("null^")|count} {revset(".")|count} {revset("0::3")|count}\n'
+  0 1 4
+
+  $ hg log -G --template '{rev}: children: {children|count}, \
+  > tags: {tags|count}, file_adds: {file_adds|count}, \
+  > ancestors: {revset("ancestors(%s)", rev)|count}'
+  @  9: children: 0, tags: 1, file_adds: 1, ancestors: 3
+  |
+  o  8: children: 1, tags: 0, file_adds: 2, ancestors: 2
+  |
+  o  7: children: 1, tags: 0, file_adds: 1, ancestors: 1
+  
+  o    6: children: 0, tags: 0, file_adds: 0, ancestors: 7
+  |\
+  | o  5: children: 1, tags: 0, file_adds: 1, ancestors: 5
+  | |
+  o |  4: children: 1, tags: 0, file_adds: 0, ancestors: 5
+  |/
+  o  3: children: 2, tags: 0, file_adds: 0, ancestors: 4
+  |
+  o  2: children: 1, tags: 0, file_adds: 1, ancestors: 3
+  |
+  o  1: children: 1, tags: 0, file_adds: 1, ancestors: 2
+  |
+  o  0: children: 1, tags: 0, file_adds: 1, ancestors: 1
+  
+
 Error on syntax:
 
   $ echo 'x = "f' >> t