diff --git a/mercurial/help/templates.txt b/mercurial/help/templates.txt --- a/mercurial/help/templates.txt +++ b/mercurial/help/templates.txt @@ -66,6 +66,8 @@ In addition to filters, there are some b - shortest(node) +- startswith(string, text) + - strip(text[, chars]) - sub(pat, repl, expr) @@ -124,3 +126,7 @@ Some sample command line templates: - Mark the working copy parent with '@':: $ hg log --template "{ifcontains(rev, revset('.'), '@')}\n" + +- Show only commit descriptions that start with "template":: + + $ hg log --template "{startswith(\"template\", firstline(desc))}\n" diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -466,6 +466,17 @@ def sub(context, mapping, args): src = stringify(_evalifliteral(args[2], context, mapping)) yield re.sub(pat, rpl, src) +def startswith(context, mapping, args): + if len(args) != 2: + raise error.ParseError(_("startswith expects two arguments")) + + patn = stringify(args[0][0](context, mapping, args[0][1])) + text = stringify(args[1][0](context, mapping, args[1][1])) + if text.startswith(patn): + return text + return '' + + methods = { "string": lambda e, c: (runstring, e[1]), "rawstring": lambda e, c: (runrawstring, e[1]), @@ -490,6 +501,7 @@ funcs = { "revset": revset, "rstdoc": rstdoc, "shortest": shortest, + "startswith": startswith, "strip": strip, "sub": sub, } 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 @@ -1884,3 +1884,30 @@ Test splitlines | foo other 3 o foo line 1 foo line 2 + +Test startswith + $ hg log -Gv -R a --template "{startswith(desc)}" + hg: parse error: startswith expects two arguments + [255] + + $ hg log -Gv -R a --template "{startswith('line', desc)}" + @ + | + o + | + o + + o + |\ + | o + | | + o | + |/ + o + | + o + | + o + | + o line 1 + line 2