Skip to content

Commit 4dfadf0

Browse files
zzl0facebook-github-bot
authored andcommitted
templater: add built-in filterby() function
Summary: This diff adds a built-in filterby() function to list the files that matches the given pattern. Reviewed By: muirdm Differential Revision: D81588900 fbshipit-source-id: 29315872b5b1e85135fb744c9e175c7338818b37
1 parent ebc7bc8 commit 4dfadf0

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

eden/scm/sapling/helptext.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4311,6 +4311,10 @@
43114311
43124312
$ @prog@ log -r 0 --template "pythonfiles: {join(files('**.py'), ', ')}\n"
43134313
4314+
- Count the number of changed files ending with ".py"::
4315+
4316+
$ @prog@ log -r 0 -T "{filterby('**.py', files) | count}\n"
4317+
43144318
- Separate non-empty arguments by a " "::
43154319
43164320
$ @prog@ log -r 0 --template "{separate(' ', node, bookmarks, tags}\n"

eden/scm/sapling/templater.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,21 @@ def startswith(context, mapping, args):
13451345
return ""
13461346

13471347

1348+
@templatefunc("filterby(pattern, files)")
1349+
def filterby(context, mapping, args):
1350+
"""Returns the files that match the given "pattern". See :prog:`help patterns`."""
1351+
if len(args) != 2:
1352+
raise error.ParseError(_("filterby expects two arguments"))
1353+
1354+
patn = evalstring(context, mapping, args[0])
1355+
texts = evalfuncarg(context, mapping, args[1])
1356+
1357+
ctx = mapping["ctx"]
1358+
m = ctx.match([patn])
1359+
texts = [t for t in texts if m.matchfn(t)]
1360+
return templatekw.showlist("text", texts, mapping)
1361+
1362+
13481363
@templatefunc("truncate(text, maxlines, [suffix])")
13491364
def truncate(context, mapping, args):
13501365
"""Truncate text to no more than "maxlines" in length. If "suffix" is

eden/scm/tests/test-command-template.t

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,6 +3630,20 @@
36303630
36313631
0
36323632
3633+
# Test filterby function
3634+
$ hg log -T "{rev}\n{join(files, '\n')}\n"
3635+
2
3636+
aa
3637+
b
3638+
1
3639+
3640+
0
3641+
a
3642+
$ hg log -r . -T "{filterby('path:aa', files)}\n"
3643+
aa
3644+
$ hg log -r . -T "{filterby('path:aa', files) | count}\n"
3645+
1
3646+
36333647
# Test relpath function
36343648
36353649
$ hg log -r0 -T '{files % "{file|relpath}\n"}'

0 commit comments

Comments
 (0)