Mercurial > hg
changeset 22668:13e3f07d74a3
templater: add count template filter, plus tests
Previously there was no way of telling how much children or bookmarks or tags a
certain changeset has in a template. It was possible to tell if a changeset has
either 0 or not 0 bookmarks, but not 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.
(The previous version of this patch had an incorrect email subject and was
apparently lost - patchwork says the patch has been accepted, but it's not so.
The changes between that and this patch are minimal: now the filter does not
disturb the alphabetical order of function definitions and dict keys.)
author | Anton Shestakov <engored@ya.ru> |
---|---|
date | Tue, 09 Sep 2014 22:14:13 +0900 |
parents | 3acc3f95548c |
children | 00c8abe64cf3 |
files | mercurial/templatefilters.py tests/test-command-template.t |
diffstat | 2 files changed, 37 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/templatefilters.py Sun Sep 28 17:35:33 2014 -0700 +++ b/mercurial/templatefilters.py Tue Sep 09 22:14:13 2014 +0900 @@ -66,6 +66,10 @@ """ return os.path.basename(path) +def count(i): + """:count: List or text. Returns the length as an integer.""" + return len(i) + def datefilter(text): """:date: Date. Returns a date in a Unix date format, including the timezone: "Mon Sep 04 15:13:13 2006 0700". @@ -366,6 +370,7 @@ "addbreaks": addbreaks, "age": age, "basename": basename, + "count": count, "date": datefilter, "domain": domain, "email": email,
--- a/tests/test-command-template.t Sun Sep 28 17:35:33 2014 -0700 +++ b/tests/test-command-template.t Tue Sep 09 22:14:13 2014 +0900 @@ -1762,6 +1762,38 @@ $ 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