mercurial/templatefuncs.py
author Raphaël Gomès <rgomes@octobus.net>
Tue, 15 Mar 2022 09:26:26 +0100
changeset 48965 a82f5d2b48fb
parent 48929 278030cc9353
child 49248 63fd0282ad40
permissions -rw-r--r--
branching: merge stable into default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36922
521f6c7e1756 templater: split template functions to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
     1
# templatefuncs.py - common template functions
1909
37b9f80a5fbb add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1906
diff changeset
     2
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 45942
diff changeset
     3
# Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
1909
37b9f80a5fbb add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1906
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8223
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9842
diff changeset
     6
# GNU General Public License version 2 or any later version.
1909
37b9f80a5fbb add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1906
diff changeset
     7
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
     8
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
     9
import re
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    10
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    11
from .i18n import _
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 46819
diff changeset
    12
from .node import bin
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    13
from . import (
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31520
diff changeset
    14
    color,
44592
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
    15
    dagop,
43051
da3329fe01e3 templatefuncs: account for user's diffopts in diff() (BC)
Denis Laxalde <denis@laxalde.org>
parents: 41720
diff changeset
    16
    diffutil,
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
    17
    encoding,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    18
    error,
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    19
    minirst,
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
    20
    obsutil,
40934
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
    21
    pycompat,
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
    22
    registrar,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    23
    revset as revsetmod,
31024
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30625
diff changeset
    24
    revsetlang,
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
    25
    scmutil,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    26
    templatefilters,
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    27
    templatekw,
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    28
    templateutil,
25985
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    29
    util,
7eb357b5f774 templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25815
diff changeset
    30
)
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
    31
from .utils import (
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
    32
    dateutil,
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
    33
    stringutil,
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
    34
)
13176
895f54a79c6e templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents: 13175
diff changeset
    35
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    36
evalrawexp = templateutil.evalrawexp
38227
1c8098cf560a templater: always join() over a wrapped object (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38225
diff changeset
    37
evalwrapped = templateutil.evalwrapped
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    38
evalfuncarg = templateutil.evalfuncarg
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    39
evalboolean = templateutil.evalboolean
37224
67efce231633 templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents: 37210
diff changeset
    40
evaldate = templateutil.evaldate
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    41
evalinteger = templateutil.evalinteger
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    42
evalstring = templateutil.evalstring
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    43
evalstringliteral = templateutil.evalstringliteral
36443
8dbd97aef915 templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents: 36245
diff changeset
    44
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
    45
# dict of template built-in functions
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
    46
funcs = {}
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
    47
templatefunc = registrar.templatefunc(funcs)
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
    48
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    49
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    50
@templatefunc(b'date(date[, fmt])')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
    51
def date(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
    52
    """Format a date. See :hg:`help dates` for formatting
26106
c568c4db036f templatefilters: remove redundant 'date' and 'strip' filters
Yuya Nishihara <yuya@tcha.org>
parents: 26105
diff changeset
    53
    strings. The default is a Unix date format, including the timezone:
c568c4db036f templatefilters: remove redundant 'date' and 'strip' filters
Yuya Nishihara <yuya@tcha.org>
parents: 26105
diff changeset
    54
    "Mon Sep 04 15:13:13 2006 0700"."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
    55
    if not (1 <= len(args) <= 2):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
    56
        # i18n: "date" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    57
        raise error.ParseError(_(b"date expects one or two arguments"))
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
    58
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    59
    date = evaldate(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    60
        context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    61
        mapping,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    62
        args[0],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    63
        # i18n: "date" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    64
        _(b"date expects a date information"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    65
    )
24903
09124cce913f templater: fix crash by passing invalid object to date() function
Yuya Nishihara <yuya@tcha.org>
parents: 24886
diff changeset
    66
    fmt = None
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
    67
    if len(args) == 2:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
    68
        fmt = evalstring(context, mapping, args[1])
37225
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
    69
    if fmt is None:
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
    70
        return dateutil.datestr(date)
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
    71
    else:
e70a90a72b80 templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37224
diff changeset
    72
        return dateutil.datestr(date, fmt)
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
    73
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    74
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    75
@templatefunc(b'dict([[key=]value...])', argspec=b'*args **kwargs')
31926
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
    76
def dict_(context, mapping, args):
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
    77
    """Construct a dict from key-value pairs. A key may be omitted if
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
    78
    a value expression can provide an unambiguous name."""
31926
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
    79
    data = util.sortdict()
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
    80
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    81
    for v in args[b'args']:
36913
da2977e674a3 templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36912
diff changeset
    82
        k = templateutil.findsymbolicname(v)
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
    83
        if not k:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    84
            raise error.ParseError(_(b'dict key cannot be inferred'))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    85
        if k in data or k in args[b'kwargs']:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    86
            raise error.ParseError(_(b"duplicated dict key '%s' inferred") % k)
31928
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
    87
        data[k] = evalfuncarg(context, mapping, v)
277b3e2d711b templater: add shorthand for building a dict like {"key": key}
Yuya Nishihara <yuya@tcha.org>
parents: 31927
diff changeset
    88
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    89
    data.update(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    90
        (k, evalfuncarg(context, mapping, v))
48929
278030cc9353 templatefuncs: remove pycompat.iteritems()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48913
diff changeset
    91
        for k, v in args[b'kwargs'].items()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    92
    )
36921
32f9b7e3f056 templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents: 36920
diff changeset
    93
    return templateutil.hybriddict(data)
31926
932241b8c644 templater: add dict() constructor
Yuya Nishihara <yuya@tcha.org>
parents: 31922
diff changeset
    94
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    95
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    96
@templatefunc(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    97
    b'diff([includepattern [, excludepattern]])', requires={b'ctx', b'ui'}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
    98
)
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
    99
def diff(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   100
    """Show a diff, optionally
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   101
    specifying files to include or exclude."""
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   102
    if len(args) > 2:
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   103
        # i18n: "diff" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   104
        raise error.ParseError(_(b"diff expects zero, one, or two arguments"))
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   105
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   106
    def getpatterns(i):
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   107
        if i < len(args):
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   108
            s = evalstring(context, mapping, args[i]).strip()
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   109
            if s:
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   110
                return [s]
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   111
        return []
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   112
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   113
    ctx = context.resource(mapping, b'ctx')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   114
    ui = context.resource(mapping, b'ui')
43051
da3329fe01e3 templatefuncs: account for user's diffopts in diff() (BC)
Denis Laxalde <denis@laxalde.org>
parents: 41720
diff changeset
   115
    diffopts = diffutil.diffallopts(ui)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   116
    chunks = ctx.diff(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   117
        match=ctx.match([], getpatterns(0), getpatterns(1)), opts=diffopts
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   118
    )
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   119
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   120
    return b''.join(chunks)
22434
40ce05b50148 templater: add "diff" template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22304
diff changeset
   121
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   122
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   123
@templatefunc(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   124
    b'extdata(source)', argspec=b'source', requires={b'ctx', b'cache'}
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   125
)
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   126
def extdata(context, mapping, args):
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   127
    """Show a text read from the specified extdata source. (EXPERIMENTAL)"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   128
    if b'source' not in args:
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   129
        # i18n: "extdata" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   130
        raise error.ParseError(_(b'extdata expects one argument'))
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   131
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   132
    source = evalstring(context, mapping, args[b'source'])
37931
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
   133
    if not source:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   134
        sym = templateutil.findsymbolicname(args[b'source'])
37931
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
   135
        if sym:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   136
            raise error.ParseError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   137
                _(b'empty data source specified'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   138
                hint=_(b"did you mean extdata('%s')?") % sym,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   139
            )
37931
faa41fd282d1 templatefuncs: show hint if extdata source is evaluated to empty (issue5843)
Yuya Nishihara <yuya@tcha.org>
parents: 37865
diff changeset
   140
        else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   141
            raise error.ParseError(_(b'empty data source specified'))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   142
    cache = context.resource(mapping, b'cache').setdefault(b'extdata', {})
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   143
    ctx = context.resource(mapping, b'ctx')
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   144
    if source in cache:
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   145
        data = cache[source]
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   146
    else:
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   147
        data = cache[source] = scmutil.extdatasource(ctx.repo(), source)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   148
    return data.get(ctx.rev(), b'')
34458
a1b89c8ad32d templater: add experimental support for extdata
Yuya Nishihara <yuya@tcha.org>
parents: 34346
diff changeset
   149
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   150
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   151
@templatefunc(b'files(pattern)', requires={b'ctx'})
30008
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   152
def files(context, mapping, args):
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   153
    """All files of the current changeset matching the pattern. See
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   154
    :hg:`help patterns`."""
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   155
    if not len(args) == 1:
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   156
        # i18n: "files" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   157
        raise error.ParseError(_(b"files expects one argument"))
30008
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   158
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   159
    raw = evalstring(context, mapping, args[0])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   160
    ctx = context.resource(mapping, b'ctx')
30008
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   161
    m = ctx.match([raw])
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   162
    files = list(ctx.matches(m))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   163
    return templateutil.compatfileslist(context, mapping, b"file", files)
30008
e83f89d3b1f7 templates: add built-in files() function
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29848
diff changeset
   164
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   165
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   166
@templatefunc(b'fill(text[, width[, initialident[, hangindent]]])')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   167
def fill(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   168
    """Fill many
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   169
    paragraphs with optional indentation. See the "fill" filter."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   170
    if not (1 <= len(args) <= 4):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
   171
        # i18n: "fill" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   172
        raise error.ParseError(_(b"fill expects one to four arguments"))
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   173
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   174
    text = evalstring(context, mapping, args[0])
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   175
    width = 76
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   176
    initindent = b''
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   177
    hangindent = b''
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   178
    if 2 <= len(args) <= 4:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   179
        width = evalinteger(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   180
            context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   181
            mapping,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   182
            args[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   183
            # i18n: "fill" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   184
            _(b"fill expects an integer width"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   185
        )
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   186
        try:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   187
            initindent = evalstring(context, mapping, args[2])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   188
            hangindent = evalstring(context, mapping, args[3])
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   189
        except IndexError:
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   190
            pass
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   191
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   192
    return templatefilters.fill(text, width, initindent, hangindent)
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   193
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   194
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   195
@templatefunc(b'filter(iterable[, expr])')
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
   196
def filter_(context, mapping, args):
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   197
    """Remove empty elements from a list or a dict. If expr specified, it's
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   198
    applied to each element to test emptiness."""
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   199
    if not (1 <= len(args) <= 2):
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
   200
        # i18n: "filter" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   201
        raise error.ParseError(_(b"filter expects one or two arguments"))
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
   202
    iterable = evalwrapped(context, mapping, args[0])
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   203
    if len(args) == 1:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   204
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   205
        def select(w):
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   206
            return w.tobool(context, mapping)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   207
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   208
    else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   209
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   210
        def select(w):
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   211
            if not isinstance(w, templateutil.mappable):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   212
                raise error.ParseError(_(b"not filterable by expression"))
38449
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   213
            lm = context.overlaymap(mapping, w.tomap(context))
bc8d925342f0 templater: extend filter() to accept template expression for emptiness test
Yuya Nishihara <yuya@tcha.org>
parents: 38448
diff changeset
   214
            return evalboolean(context, lm, args[1])
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   215
38448
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
   216
    return iterable.filter(context, mapping, select)
dae829b4de78 templater: introduce filter() function to remove empty items from list
Yuya Nishihara <yuya@tcha.org>
parents: 38428
diff changeset
   217
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   218
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   219
@templatefunc(b'formatnode(node)', requires={b'ui'})
31169
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   220
def formatnode(context, mapping, args):
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   221
    """Obtain the preferred form of a changeset hash. (DEPRECATED)"""
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   222
    if len(args) != 1:
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   223
        # i18n: "formatnode" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   224
        raise error.ParseError(_(b"formatnode expects one argument"))
31169
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   225
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   226
    ui = context.resource(mapping, b'ui')
31169
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   227
    node = evalstring(context, mapping, args[0])
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   228
    if ui.debugflag:
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   229
        return node
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   230
    return templatefilters.short(node)
48a8b2e5fe31 templater: port formatnode filter from changeset_templater
Yuya Nishihara <yuya@tcha.org>
parents: 31024
diff changeset
   231
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   232
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   233
@templatefunc(b'mailmap(author)', requires={b'repo', b'cache'})
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   234
def mailmap(context, mapping, args):
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   235
    """Return the author, updated according to the value
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   236
    set in the .mailmap file"""
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   237
    if len(args) != 1:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   238
        raise error.ParseError(_(b"mailmap expects one argument"))
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   239
37260
8e57c3b0dce4 templatefuncs: do not crash because of invalid value fed to mailmap()
Yuya Nishihara <yuya@tcha.org>
parents: 37244
diff changeset
   240
    author = evalstring(context, mapping, args[0])
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   241
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   242
    cache = context.resource(mapping, b'cache')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   243
    repo = context.resource(mapping, b'repo')
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   244
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   245
    if b'mailmap' not in cache:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   246
        data = repo.wvfs.tryread(b'.mailmap')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   247
        cache[b'mailmap'] = stringutil.parsemailmap(data)
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   248
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   249
    return stringutil.mapname(cache[b'mailmap'], author)
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents: 37163
diff changeset
   250
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   251
40189
9458dbfa7f33 templatefuncs: add truncate parameter to pad
Mark Thomas <mbthomas@fb.com>
parents: 39367
diff changeset
   252
@templatefunc(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   253
    b'pad(text, width[, fillchar=\' \'[, left=False[, truncate=False]]])',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   254
    argspec=b'text width fillchar left truncate',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   255
)
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
   256
def pad(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   257
    """Pad text with a
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   258
    fill character."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   259
    if b'text' not in args or b'width' not in args:
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
   260
        # i18n: "pad" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   261
        raise error.ParseError(_(b"pad() expects two to four arguments"))
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
   262
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   263
    width = evalinteger(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   264
        context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   265
        mapping,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   266
        args[b'width'],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   267
        # i18n: "pad" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   268
        _(b"pad() expects an integer width"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   269
    )
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
   270
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   271
    text = evalstring(context, mapping, args[b'text'])
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
   272
40189
9458dbfa7f33 templatefuncs: add truncate parameter to pad
Mark Thomas <mbthomas@fb.com>
parents: 39367
diff changeset
   273
    truncate = False
29818
407879b0893b templater: rename "right" argument of pad() function
Yuya Nishihara <yuya@tcha.org>
parents: 29817
diff changeset
   274
    left = False
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   275
    fillchar = b' '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   276
    if b'fillchar' in args:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   277
        fillchar = evalstring(context, mapping, args[b'fillchar'])
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31520
diff changeset
   278
        if len(color.stripeffects(fillchar)) != 1:
31519
3725986b151a templater: reject bad fillchar argument passed to pad()
Yuya Nishihara <yuya@tcha.org>
parents: 31169
diff changeset
   279
            # i18n: "pad" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   280
            raise error.ParseError(_(b"pad() expects a single fill character"))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   281
    if b'left' in args:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   282
        left = evalboolean(context, mapping, args[b'left'])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   283
    if b'truncate' in args:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   284
        truncate = evalboolean(context, mapping, args[b'truncate'])
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
   285
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31520
diff changeset
   286
    fillwidth = width - encoding.colwidth(color.stripeffects(text))
40189
9458dbfa7f33 templatefuncs: add truncate parameter to pad
Mark Thomas <mbthomas@fb.com>
parents: 39367
diff changeset
   287
    if fillwidth < 0 and truncate:
9458dbfa7f33 templatefuncs: add truncate parameter to pad
Mark Thomas <mbthomas@fb.com>
parents: 39367
diff changeset
   288
        return encoding.trim(color.stripeffects(text), width, leftside=left)
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
   289
    if fillwidth <= 0:
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
   290
        return text
29818
407879b0893b templater: rename "right" argument of pad() function
Yuya Nishihara <yuya@tcha.org>
parents: 29817
diff changeset
   291
    if left:
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
   292
        return fillchar * fillwidth + text
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
   293
    else:
31520
6f150bb19317 templater: make pad() compute actual width
Yuya Nishihara <yuya@tcha.org>
parents: 31519
diff changeset
   294
        return text + fillchar * fillwidth
20370
aa51392da507 template: add pad function for padding output
Durham Goode <durham@fb.com>
parents: 20369
diff changeset
   295
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   296
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   297
@templatefunc(b'indent(text, indentchars[, firstline])')
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   298
def indent(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   299
    """Indents all non-empty lines
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   300
    with the characters given in the indentchars string. An optional
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   301
    third parameter will override the indent for the first line only
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   302
    if present."""
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   303
    if not (2 <= len(args) <= 3):
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   304
        # i18n: "indent" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   305
        raise error.ParseError(_(b"indent() expects two or three arguments"))
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   306
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   307
    text = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   308
    indent = evalstring(context, mapping, args[1])
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   309
43695
fa246ada356b templates: make {indent("", " ")} be empty
Martin von Zweigbergk <martinvonz@google.com>
parents: 43106
diff changeset
   310
    firstline = indent
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   311
    if len(args) == 3:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   312
        firstline = evalstring(context, mapping, args[2])
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   313
43695
fa246ada356b templates: make {indent("", " ")} be empty
Martin von Zweigbergk <martinvonz@google.com>
parents: 43106
diff changeset
   314
    return templatefilters.indent(text, indent, firstline=firstline)
25489
ef8956aa8755 templater: introduce indent function
Ryan McElroy <rmcelroy@fb.com>
parents: 25096
diff changeset
   315
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   316
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   317
@templatefunc(b'get(dict, key)')
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
   318
def get(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   319
    """Get an attribute/key from an object. Some keywords
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   320
    are complex types. This function allows you to obtain the value of an
26197
fb6c08a9b40a templater: fix get English
timeless@mozdev.org
parents: 26188
diff changeset
   321
    attribute on these types."""
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
   322
    if len(args) != 2:
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
   323
        # i18n: "get" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   324
        raise error.ParseError(_(b"get() expects two arguments"))
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
   325
38240
c2456a7726c1 templater: do dict lookup over a wrapped object
Yuya Nishihara <yuya@tcha.org>
parents: 38239
diff changeset
   326
    dictarg = evalwrapped(context, mapping, args[0])
38244
688fbb758ba9 templater: resolve type of dict key in getmember()
Yuya Nishihara <yuya@tcha.org>
parents: 38243
diff changeset
   327
    key = evalrawexp(context, mapping, args[1])
38243
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
   328
    try:
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
   329
        return dictarg.getmember(context, mapping, key)
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
   330
    except error.ParseError as err:
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
   331
        # i18n: "get" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   332
        hint = _(b"get() expects a dict as first argument")
38243
06d11cd90516 templater: promote getmember() to an interface of wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38242
diff changeset
   333
        raise error.ParseError(bytes(err), hint=hint)
18582
ef78450c8df6 templater: add get() function to access dict element (e.g. extra)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 18289
diff changeset
   334
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   335
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   336
@templatefunc(b'config(section, name[, default])', requires={b'ui'})
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   337
def config(context, mapping, args):
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   338
    """Returns the requested hgrc config option as a string."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   339
    fn = context.resource(mapping, b'ui').config
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   340
    return _config(context, mapping, args, fn, evalstring)
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   341
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   342
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   343
@templatefunc(b'configbool(section, name[, default])', requires={b'ui'})
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   344
def configbool(context, mapping, args):
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   345
    """Returns the requested hgrc config option as a boolean."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   346
    fn = context.resource(mapping, b'ui').configbool
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   347
    return _config(context, mapping, args, fn, evalboolean)
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   348
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   349
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   350
@templatefunc(b'configint(section, name[, default])', requires={b'ui'})
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   351
def configint(context, mapping, args):
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   352
    """Returns the requested hgrc config option as an integer."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   353
    fn = context.resource(mapping, b'ui').configint
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   354
    return _config(context, mapping, args, fn, evalinteger)
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   355
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   356
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   357
def _config(context, mapping, args, configfn, defaultfn):
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   358
    if not (2 <= len(args) <= 3):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   359
        raise error.ParseError(_(b"config expects two or three arguments"))
41720
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   360
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   361
    # The config option can come from any section, though we specifically
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   362
    # reserve the [templateconfig] section for dynamically defining options
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   363
    # for this function without also requiring an extension.
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   364
    section = evalstringliteral(context, mapping, args[0])
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   365
    name = evalstringliteral(context, mapping, args[1])
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   366
    if len(args) == 3:
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   367
        default = defaultfn(context, mapping, args[2])
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   368
        return configfn(section, name, default)
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   369
    else:
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   370
        return configfn(section, name)
6704696141b8 templates: adding a config() function for template customization
rdamazio@google.com
parents: 40935
diff changeset
   371
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   372
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   373
@templatefunc(b'if(expr, then[, else])')
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   374
def if_(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   375
    """Conditionally execute based on the result of
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   376
    an expression."""
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   377
    if not (2 <= len(args) <= 3):
17890
ca6850b9dd9e i18n: add "i18n" comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17729
diff changeset
   378
        # i18n: "if" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   379
        raise error.ParseError(_(b"if expects two or three arguments"))
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   380
29816
034412ca28c3 templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents: 29815
diff changeset
   381
    test = evalboolean(context, mapping, args[0])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   382
    if test:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
   383
        return evalrawexp(context, mapping, args[1])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   384
    elif len(args) == 3:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
   385
        return evalrawexp(context, mapping, args[2])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   386
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   387
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   388
@templatefunc(b'ifcontains(needle, haystack, then[, else])')
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
   389
def ifcontains(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   390
    """Conditionally execute based
30049
f18cc848b48e templater: use "needle" and "haystack" as (meta-)variables for ifcontains()
Anton Shestakov <av6@dwimlabs.net>
parents: 30008
diff changeset
   391
    on whether the item "needle" is in "haystack"."""
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
   392
    if not (3 <= len(args) <= 4):
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
   393
        # i18n: "ifcontains" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   394
        raise error.ParseError(_(b"ifcontains expects three or four arguments"))
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
   395
38267
fb874fc1d9b4 templater: abstract ifcontains() over wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38265
diff changeset
   396
    haystack = evalwrapped(context, mapping, args[1])
34659
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
   397
    try:
37163
0fb28899e81a templater: factor out unwrapastype() from evalastype()
Yuya Nishihara <yuya@tcha.org>
parents: 37015
diff changeset
   398
        needle = evalrawexp(context, mapping, args[0])
38267
fb874fc1d9b4 templater: abstract ifcontains() over wrapped types
Yuya Nishihara <yuya@tcha.org>
parents: 38265
diff changeset
   399
        found = haystack.contains(context, mapping, needle)
34659
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
   400
    except error.ParseError:
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
   401
        found = False
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
   402
34659
3edfd472f3cb templater: fix ifcontains() to handle type mismatch gracefully
Yuya Nishihara <yuya@tcha.org>
parents: 34581
diff changeset
   403
    if found:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
   404
        return evalrawexp(context, mapping, args[2])
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
   405
    elif len(args) == 4:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
   406
        return evalrawexp(context, mapping, args[3])
20518
1e43f15a647f template: add ifcontains template function
Durham Goode <durham@fb.com>
parents: 20371
diff changeset
   407
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   408
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   409
@templatefunc(b'ifeq(expr1, expr2, then[, else])')
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   410
def ifeq(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   411
    """Conditionally execute based on
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   412
    whether 2 items are equivalent."""
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   413
    if not (3 <= len(args) <= 4):
17890
ca6850b9dd9e i18n: add "i18n" comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17729
diff changeset
   414
        # i18n: "ifeq" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   415
        raise error.ParseError(_(b"ifeq expects three or four arguments"))
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   416
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   417
    test = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   418
    match = evalstring(context, mapping, args[1])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   419
    if test == match:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
   420
        return evalrawexp(context, mapping, args[2])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   421
    elif len(args) == 4:
37015
a318bb154d42 templatefuncs: do not stringify result of if*() expression
Yuya Nishihara <yuya@tcha.org>
parents: 36922
diff changeset
   422
        return evalrawexp(context, mapping, args[3])
17636
ce18dbcd91c8 templater: add if/ifeq conditionals
Matt Mackall <mpm@selenic.com>
parents: 17635
diff changeset
   423
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   424
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   425
@templatefunc(b'join(list, sep)')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   426
def join(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   427
    """Join items in a list with a delimiter."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   428
    if not (1 <= len(args) <= 2):
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   429
        # i18n: "join" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   430
        raise error.ParseError(_(b"join expects one or two arguments"))
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   431
38227
1c8098cf560a templater: always join() over a wrapped object (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38225
diff changeset
   432
    joinset = evalwrapped(context, mapping, args[0])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   433
    joiner = b" "
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   434
    if len(args) > 1:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   435
        joiner = evalstring(context, mapping, args[1])
38227
1c8098cf560a templater: always join() over a wrapped object (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38225
diff changeset
   436
    return joinset.join(context, mapping, joiner)
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   437
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   438
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   439
@templatefunc(b'label(label, expr)', requires={b'ui'})
18289
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
   440
def label(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   441
    """Apply a label to generated content. Content with
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   442
    a label applied can result in additional post-processing, such as
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   443
    automatic colorization."""
18289
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
   444
    if len(args) != 2:
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
   445
        # i18n: "label" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   446
        raise error.ParseError(_(b"label expects two arguments"))
18289
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
   447
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   448
    ui = context.resource(mapping, b'ui')
28374
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
   449
    thing = evalstring(context, mapping, args[1])
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
   450
    # preserve unknown symbol as literal so effects like 'red', 'bold',
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
   451
    # etc. don't need to be quoted
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
   452
    label = evalstringliteral(context, mapping, args[0])
af3bd9d1dbc1 templater: move label() function from color extension
Yuya Nishihara <yuya@tcha.org>
parents: 28373
diff changeset
   453
28384
3356bf61fa25 formatter: make labels work with templated output
Kostia Balytskyi <ikostia@fb.com>
parents: 28374
diff changeset
   454
    return ui.label(thing, label)
18289
9bfb53106328 templater: add no-op template function 'label'
Sean Farley <sean.michael.farley@gmail.com>
parents: 17991
diff changeset
   455
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   456
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   457
@templatefunc(b'latesttag([pattern])')
26485
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
   458
def latesttag(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   459
    """The global tags matching the given pattern on the
31850
f0d719e513fc templatekw: clarify the result of {latesttag} when no tag exists
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
   460
    most recent globally tagged ancestor of this changeset.
f0d719e513fc templatekw: clarify the result of {latesttag} when no tag exists
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
   461
    If no such tags exist, the "{tag}" template resolves to
38144
bd7a3fa71a72 help: mention pattern syntax of latesttag() template function
Yuya Nishihara <yuya@tcha.org>
parents: 37931
diff changeset
   462
    the string "null". See :hg:`help revisions.patterns` for the pattern
bd7a3fa71a72 help: mention pattern syntax of latesttag() template function
Yuya Nishihara <yuya@tcha.org>
parents: 37931
diff changeset
   463
    syntax.
bd7a3fa71a72 help: mention pattern syntax of latesttag() template function
Yuya Nishihara <yuya@tcha.org>
parents: 37931
diff changeset
   464
    """
26485
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
   465
    if len(args) > 1:
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
   466
        # i18n: "latesttag" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   467
        raise error.ParseError(_(b"latesttag expects at most one argument"))
26485
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
   468
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
   469
    pattern = None
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
   470
    if len(args) == 1:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   471
        pattern = evalstring(context, mapping, args[0])
36596
b5d39a09656a templatekw: switch latesttags template keywords to new API
Yuya Nishihara <yuya@tcha.org>
parents: 36595
diff changeset
   472
    return templatekw.showlatesttags(context, mapping, pattern)
26485
43bf9471fae9 templater: introduce {latesttag()} function to match a pattern (issue4184)
Matt Harbison <matt_harbison@yahoo.com>
parents: 26334
diff changeset
   473
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   474
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   475
@templatefunc(b'localdate(date[, tz])')
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
   476
def localdate(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   477
    """Converts a date to the specified timezone.
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   478
    The default is local date."""
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   479
    if not (1 <= len(args) <= 2):
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
   480
        # i18n: "localdate" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   481
        raise error.ParseError(_(b"localdate expects one or two arguments"))
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
   482
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   483
    date = evaldate(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   484
        context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   485
        mapping,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   486
        args[0],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   487
        # i18n: "localdate" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   488
        _(b"localdate expects a date information"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   489
    )
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   490
    if len(args) >= 2:
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   491
        tzoffset = None
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   492
        tz = evalfuncarg(context, mapping, args[1])
36550
7f6be7121b28 py3: replace type 'str' by 'bytes' in templater.py
Yuya Nishihara <yuya@tcha.org>
parents: 36546
diff changeset
   493
        if isinstance(tz, bytes):
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36596
diff changeset
   494
            tzoffset, remainder = dateutil.parsetimezone(tz)
29636
84ef4517de03 date: refactor timezone parsing
Matt Mackall <mpm@selenic.com>
parents: 29085
diff changeset
   495
            if remainder:
84ef4517de03 date: refactor timezone parsing
Matt Mackall <mpm@selenic.com>
parents: 29085
diff changeset
   496
                tzoffset = None
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   497
        if tzoffset is None:
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   498
            try:
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   499
                tzoffset = int(tz)
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   500
            except (TypeError, ValueError):
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   501
                # i18n: "localdate" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   502
                raise error.ParseError(_(b"localdate expects a timezone"))
26128
51f6940d3b4f templater: add optional timezone argument to localdate()
Yuya Nishihara <yuya@tcha.org>
parents: 26127
diff changeset
   503
    else:
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36596
diff changeset
   504
        tzoffset = dateutil.makedate()[1]
38285
8d6109b49b31 templater: introduce a wrapper for date tuple (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38267
diff changeset
   505
    return templateutil.date((date[0], tzoffset))
26127
7012be5ab5bd templater: port localdate filter to a function
Yuya Nishihara <yuya@tcha.org>
parents: 26125
diff changeset
   506
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   507
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   508
@templatefunc(b'max(iterable)')
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   509
def max_(context, mapping, args, **kwargs):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   510
    """Return the max of an iterable"""
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   511
    if len(args) != 1:
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   512
        # i18n: "max" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   513
        raise error.ParseError(_(b"max expects one argument"))
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   514
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   515
    iterable = evalwrapped(context, mapping, args[0])
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   516
    try:
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   517
        return iterable.getmax(context, mapping)
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   518
    except error.ParseError as err:
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   519
        # i18n: "max" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   520
        hint = _(b"max first argument should be an iterable")
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   521
        raise error.ParseError(bytes(err), hint=hint)
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   522
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   523
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   524
@templatefunc(b'min(iterable)')
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   525
def min_(context, mapping, args, **kwargs):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   526
    """Return the min of an iterable"""
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   527
    if len(args) != 1:
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   528
        # i18n: "min" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   529
        raise error.ParseError(_(b"min expects one argument"))
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   530
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   531
    iterable = evalwrapped(context, mapping, args[0])
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   532
    try:
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   533
        return iterable.getmin(context, mapping)
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   534
    except error.ParseError as err:
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   535
        # i18n: "min" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   536
        hint = _(b"min first argument should be an iterable")
38265
41ae9b3cbfb9 templater: abstract min/max away
Yuya Nishihara <yuya@tcha.org>
parents: 38244
diff changeset
   537
        raise error.ParseError(bytes(err), hint=hint)
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   538
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   539
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   540
@templatefunc(b'mod(a, b)')
30115
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
   541
def mod(context, mapping, args):
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
   542
    """Calculate a mod b such that a / b + a mod b == a"""
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
   543
    if not len(args) == 2:
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
   544
        # i18n: "mod" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   545
        raise error.ParseError(_(b"mod expects two arguments"))
30115
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
   546
30116
1c01fa29630f templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents: 30115
diff changeset
   547
    func = lambda a, b: a % b
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   548
    return templateutil.runarithmetic(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   549
        context, mapping, (func, args[0], args[1])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   550
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   551
30115
8e42dfde93d1 templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents: 30083
diff changeset
   552
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   553
@templatefunc(b'obsfateoperations(markers)')
34287
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   554
def obsfateoperations(context, mapping, args):
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   555
    """Compute obsfate related information based on markers (EXPERIMENTAL)"""
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   556
    if len(args) != 1:
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   557
        # i18n: "obsfateoperations" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   558
        raise error.ParseError(_(b"obsfateoperations expects one argument"))
34287
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   559
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   560
    markers = evalfuncarg(context, mapping, args[0])
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   561
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   562
    try:
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   563
        data = obsutil.markersoperations(markers)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   564
        return templateutil.hybridlist(data, name=b'operation')
34287
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   565
    except (TypeError, KeyError):
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   566
        # i18n: "obsfateoperations" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   567
        errmsg = _(b"obsfateoperations first argument should be an iterable")
34287
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   568
        raise error.ParseError(errmsg)
7cdc8c5a481a templates: introduce a obsfateoperation() function
Martin von Zweigbergk <martinvonz@google.com>
parents: 34250
diff changeset
   569
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   570
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   571
@templatefunc(b'obsfatedate(markers)')
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   572
def obsfatedate(context, mapping, args):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   573
    """Compute obsfate related information based on markers (EXPERIMENTAL)"""
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   574
    if len(args) != 1:
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   575
        # i18n: "obsfatedate" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   576
        raise error.ParseError(_(b"obsfatedate expects one argument"))
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   577
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   578
    markers = evalfuncarg(context, mapping, args[0])
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   579
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   580
    try:
38285
8d6109b49b31 templater: introduce a wrapper for date tuple (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 38267
diff changeset
   581
        # TODO: maybe this has to be a wrapped list of date wrappers?
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   582
        data = obsutil.markersdates(markers)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   583
        return templateutil.hybridlist(data, name=b'date', fmt=b'%d %d')
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   584
    except (TypeError, KeyError):
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   585
        # i18n: "obsfatedate" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   586
        errmsg = _(b"obsfatedate first argument should be an iterable")
33995
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   587
        raise error.ParseError(errmsg)
c35c0f54f420 template: compute dates in obsfatedate
Boris Feld <boris.feld@octobus.net>
parents: 33994
diff changeset
   588
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   589
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   590
@templatefunc(b'obsfateusers(markers)')
33994
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   591
def obsfateusers(context, mapping, args):
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   592
    """Compute obsfate related information based on markers (EXPERIMENTAL)"""
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   593
    if len(args) != 1:
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   594
        # i18n: "obsfateusers" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   595
        raise error.ParseError(_(b"obsfateusers expects one argument"))
33994
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   596
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   597
    markers = evalfuncarg(context, mapping, args[0])
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   598
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   599
    try:
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   600
        data = obsutil.markersusers(markers)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   601
        return templateutil.hybridlist(data, name=b'user')
33994
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   602
    except (TypeError, KeyError, ValueError):
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   603
        # i18n: "obsfateusers" is a keyword
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   604
        msg = _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   605
            b"obsfateusers first argument should be an iterable of "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   606
            b"obsmakers"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   607
        )
33994
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   608
        raise error.ParseError(msg)
38f08eaba6b0 template: compute user in obsfateusers
Boris Feld <boris.feld@octobus.net>
parents: 33993
diff changeset
   609
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   610
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   611
@templatefunc(b'obsfateverb(successors, markers)')
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   612
def obsfateverb(context, mapping, args):
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   613
    """Compute obsfate related information based on successors (EXPERIMENTAL)"""
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
   614
    if len(args) != 2:
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   615
        # i18n: "obsfateverb" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   616
        raise error.ParseError(_(b"obsfateverb expects two arguments"))
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   617
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   618
    successors = evalfuncarg(context, mapping, args[0])
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
   619
    markers = evalfuncarg(context, mapping, args[1])
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   620
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   621
    try:
35010
b81ad5b78a81 obsfate: makes successorsetverb takes the markers as argument
Boris Feld <boris.feld@octobus.net>
parents: 34838
diff changeset
   622
        return obsutil.obsfateverb(successors, markers)
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   623
    except TypeError:
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   624
        # i18n: "obsfateverb" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   625
        errmsg = _(b"obsfateverb first argument should be countable")
33993
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   626
        raise error.ParseError(errmsg)
3d0f8918351b template: compute verb in obsfateverb
Boris Feld <boris.feld@octobus.net>
parents: 33554
diff changeset
   627
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   628
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   629
@templatefunc(b'relpath(path)', requires={b'repo'})
30083
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   630
def relpath(context, mapping, args):
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   631
    """Convert a repository-absolute path into a filesystem path relative to
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   632
    the current working directory."""
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   633
    if len(args) != 1:
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   634
        # i18n: "relpath" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   635
        raise error.ParseError(_(b"relpath expects one argument"))
30083
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   636
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   637
    repo = context.resource(mapping, b'repo')
30083
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   638
    path = evalstring(context, mapping, args[0])
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   639
    return repo.pathto(path)
bd1f043d1ea3 templater: add relpath() to convert repo path to relative path (issue5394)
Yuya Nishihara <yuya@tcha.org>
parents: 30049
diff changeset
   640
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   641
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   642
@templatefunc(b'revset(query[, formatargs...])', requires={b'repo', b'cache'})
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   643
def revset(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   644
    """Execute a revision set query. See
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   645
    :hg:`help revset`."""
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   646
    if not len(args) > 0:
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   647
        # i18n: "revset" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   648
        raise error.ParseError(_(b"revset expects one or more arguments"))
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   649
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   650
    raw = evalstring(context, mapping, args[0])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   651
    repo = context.resource(mapping, b'repo')
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   652
22304
5678b0e3608f templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21960
diff changeset
   653
    def query(expr):
37674
f83cb91b052e revset: pass in lookup function instead of repo (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37327
diff changeset
   654
        m = revsetmod.match(repo.ui, expr, lookup=revsetmod.lookupfn(repo))
24114
fafd9a1284cf revset: make match function initiate query from full set by default
Yuya Nishihara <yuya@tcha.org>
parents: 23167
diff changeset
   655
        return m(repo)
22304
5678b0e3608f templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21960
diff changeset
   656
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   657
    if len(args) > 1:
44591
1f81f680912f templater: remember cache key of evaluated revset
Yuya Nishihara <yuya@tcha.org>
parents: 44589
diff changeset
   658
        key = None  # dynamically-created revs shouldn't be cached
28333
41373244f4e5 templater: fix revset() to evaluate format arguments eagerly
Yuya Nishihara <yuya@tcha.org>
parents: 28332
diff changeset
   659
        formatargs = [evalfuncarg(context, mapping, a) for a in args[1:]]
31024
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30625
diff changeset
   660
        revs = query(revsetlang.formatspec(raw, *formatargs))
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   661
    else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   662
        cache = context.resource(mapping, b'cache')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   663
        revsetcache = cache.setdefault(b"revsetcache", {})
44591
1f81f680912f templater: remember cache key of evaluated revset
Yuya Nishihara <yuya@tcha.org>
parents: 44589
diff changeset
   664
        key = raw
1f81f680912f templater: remember cache key of evaluated revset
Yuya Nishihara <yuya@tcha.org>
parents: 44589
diff changeset
   665
        if key in revsetcache:
1f81f680912f templater: remember cache key of evaluated revset
Yuya Nishihara <yuya@tcha.org>
parents: 44589
diff changeset
   666
            revs = revsetcache[key]
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   667
        else:
22304
5678b0e3608f templater: enable alias predicates to be used in "revset()" function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21960
diff changeset
   668
            revs = query(raw)
44591
1f81f680912f templater: remember cache key of evaluated revset
Yuya Nishihara <yuya@tcha.org>
parents: 44589
diff changeset
   669
            revsetcache[key] = revs
1f81f680912f templater: remember cache key of evaluated revset
Yuya Nishihara <yuya@tcha.org>
parents: 44589
diff changeset
   670
    return templateutil.revslist(repo, revs, name=b'revision', cachekey=key)
20519
cda9d2b6beab template: add revset() template function
Durham Goode <durham@fb.com>
parents: 20518
diff changeset
   671
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   672
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   673
@templatefunc(b'rstdoc(text, style)')
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
   674
def rstdoc(context, mapping, args):
30332
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 30232
diff changeset
   675
    """Format reStructuredText."""
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
   676
    if len(args) != 2:
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
   677
        # i18n: "rstdoc" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   678
        raise error.ParseError(_(b"rstdoc expects two arguments"))
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
   679
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   680
    text = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   681
    style = evalstring(context, mapping, args[1])
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
   682
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   683
    return minirst.format(text, style=style, keep=[b'verbose'])
18747
f5db3092790f hgweb: generate HTML documentation
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18582
diff changeset
   684
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   685
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   686
@templatefunc(b'search(pattern, text)')
40934
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   687
def search(context, mapping, args):
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   688
    """Look for the first text matching the regular expression pattern.
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   689
    Groups are accessible as ``{1}``, ``{2}``, ... in %-mapped template."""
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   690
    if len(args) != 2:
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   691
        # i18n: "search" is a keyword
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   692
        raise error.ParseError(_(b'search expects two arguments'))
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   693
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   694
    pat = evalstring(context, mapping, args[0])
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   695
    src = evalstring(context, mapping, args[1])
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   696
    try:
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   697
        patre = re.compile(pat)
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   698
    except re.error:
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   699
        # i18n: "search" is a keyword
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   700
        raise error.ParseError(_(b'search got an invalid pattern: %s') % pat)
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   701
    # named groups shouldn't shadow *reserved* resource keywords
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   702
    badgroups = context.knownresourcekeys() & set(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   703
        pycompat.byteskwargs(patre.groupindex)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   704
    )
40934
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   705
    if badgroups:
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   706
        raise error.ParseError(
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   707
            # i18n: "search" is a keyword
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   708
            _(b'invalid group %(group)s in search pattern: %(pat)s')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   709
            % {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   710
                b'group': b', '.join(b"'%s'" % g for g in sorted(badgroups)),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   711
                b'pat': pat,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   712
            }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   713
        )
40934
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   714
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   715
    match = patre.search(src)
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   716
    if not match:
40935
4591c9791a82 templatefuncs: specialize "no match" value of search() to allow % operation
Yuya Nishihara <yuya@tcha.org>
parents: 40934
diff changeset
   717
        return templateutil.mappingnone()
40934
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   718
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   719
    lm = {b'0': match.group(0)}
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   720
    lm.update((b'%d' % i, v) for i, v in enumerate(match.groups(), 1))
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   721
    lm.update(pycompat.byteskwargs(match.groupdict()))
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   722
    return templateutil.mappingdict(lm, tmpl=b'{0}')
d3e688b9ef2e templatefuncs: add regexp search() function that extracts substring
Yuya Nishihara <yuya@tcha.org>
parents: 40518
diff changeset
   723
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   724
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   725
@templatefunc(b'separate(sep, args...)', argspec=b'sep *args')
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   726
def separate(context, mapping, args):
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   727
    """Add a separator between non-empty arguments."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   728
    if b'sep' not in args:
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   729
        # i18n: "separate" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   730
        raise error.ParseError(_(b"separate expects at least one argument"))
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   731
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   732
    sep = evalstring(context, mapping, args[b'sep'])
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   733
    first = True
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   734
    for arg in args[b'args']:
29085
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   735
        argstr = evalstring(context, mapping, arg)
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   736
        if not argstr:
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   737
            continue
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   738
        if first:
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   739
            first = False
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   740
        else:
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   741
            yield sep
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   742
        yield argstr
df838803c1d4 templater: add separate() template function
Martin von Zweigbergk <martinvonz@google.com>
parents: 28957
diff changeset
   743
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   744
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   745
@templatefunc(b'shortest(node, minlength=4)', requires={b'repo', b'cache'})
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   746
def shortest(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   747
    """Obtain the shortest representation of
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   748
    a node."""
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   749
    if not (1 <= len(args) <= 2):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
   750
        # i18n: "shortest" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   751
        raise error.ParseError(_(b"shortest() expects one or two arguments"))
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   752
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   753
    hexnode = evalstring(context, mapping, args[0])
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   754
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   755
    minlength = 4
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   756
    if len(args) > 1:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   757
        minlength = evalinteger(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   758
            context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   759
            mapping,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   760
            args[1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   761
            # i18n: "shortest" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   762
            _(b"shortest() expects an integer minlength"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   763
        )
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   764
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   765
    repo = context.resource(mapping, b'repo')
47041
a407fe56d6e8 core: don't hard-code hex node lengths
Joerg Sonnenberger <joerg@bec.de>
parents: 47012
diff changeset
   766
    hexnodelen = 2 * repo.nodeconstants.nodelen
a407fe56d6e8 core: don't hard-code hex node lengths
Joerg Sonnenberger <joerg@bec.de>
parents: 47012
diff changeset
   767
    if len(hexnode) > hexnodelen:
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   768
        return hexnode
47041
a407fe56d6e8 core: don't hard-code hex node lengths
Joerg Sonnenberger <joerg@bec.de>
parents: 47012
diff changeset
   769
    elif len(hexnode) == hexnodelen:
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   770
        try:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   771
            node = bin(hexnode)
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   772
        except TypeError:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   773
            return hexnode
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   774
    else:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   775
        try:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   776
            node = scmutil.resolvehexnodeidprefix(repo, hexnode)
37859
66dc9db6ed2c shortest: make {shortest("fffffffff")} work again
Martin von Zweigbergk <martinvonz@google.com>
parents: 37709
diff changeset
   777
        except error.WdirUnsupported:
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 46819
diff changeset
   778
            node = repo.nodeconstants.wdirid
37859
66dc9db6ed2c shortest: make {shortest("fffffffff")} work again
Martin von Zweigbergk <martinvonz@google.com>
parents: 37709
diff changeset
   779
        except error.LookupError:
37709
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   780
            return hexnode
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   781
        if not node:
7b2955624777 scmutil: make shortesthexnodeidprefix() take a full binary nodeid
Martin von Zweigbergk <martinvonz@google.com>
parents: 37708
diff changeset
   782
            return hexnode
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   783
    cache = context.resource(mapping, b'cache')
37865
da083d9fafab shortest: don't keep checking for longer prefix if node doesn't exist (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 37859
diff changeset
   784
    try:
38853
3588e41f796d shortest: cache disambiguation revset
Martin von Zweigbergk <martinvonz@google.com>
parents: 38449
diff changeset
   785
        return scmutil.shortesthexnodeidprefix(repo, node, minlength, cache)
37865
da083d9fafab shortest: don't keep checking for longer prefix if node doesn't exist (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 37859
diff changeset
   786
    except error.RepoLookupError:
da083d9fafab shortest: don't keep checking for longer prefix if node doesn't exist (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 37859
diff changeset
   787
        return hexnode
20369
9c6b86dd2ed2 template: add shortest(node) template function
Durham Goode <durham@fb.com>
parents: 20312
diff changeset
   788
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   789
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   790
@templatefunc(b'strip(text[, chars])')
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
   791
def strip(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   792
    """Strip characters from a string. By default,
26106
c568c4db036f templatefilters: remove redundant 'date' and 'strip' filters
Yuya Nishihara <yuya@tcha.org>
parents: 26105
diff changeset
   793
    strips all leading and trailing whitespace."""
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
   794
    if not (1 <= len(args) <= 2):
23112
3226ed457928 i18n: add i18n comment to error messages of template functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22844
diff changeset
   795
        # i18n: "strip" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   796
        raise error.ParseError(_(b"strip expects one or two arguments"))
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
   797
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   798
    text = evalstring(context, mapping, args[0])
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
   799
    if len(args) == 2:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   800
        chars = evalstring(context, mapping, args[1])
19330
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
   801
        return text.strip(chars)
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
   802
    return text.strip()
867b9957d895 templater: add strip function with chars as an extra argument
Alexander Plavin <me@aplavin.ru>
parents: 19228
diff changeset
   803
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   804
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   805
@templatefunc(b'sub(pattern, replacement, expression)')
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   806
def sub(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   807
    """Perform text substitution
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   808
    using regular expressions."""
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   809
    if len(args) != 3:
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   810
        # i18n: "sub" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   811
        raise error.ParseError(_(b"sub expects three arguments"))
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   812
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   813
    pat = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   814
    rpl = evalstring(context, mapping, args[1])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   815
    src = evalstring(context, mapping, args[2])
26188
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   816
    try:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   817
        patre = re.compile(pat)
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   818
    except re.error:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   819
        # i18n: "sub" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   820
        raise error.ParseError(_(b"sub got an invalid pattern: %s") % pat)
26188
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   821
    try:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   822
        yield patre.sub(rpl, src)
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   823
    except re.error:
662ea52d5dca templater: catch regexp error at sub() function
Yuya Nishihara <yuya@tcha.org>
parents: 26128
diff changeset
   824
        # i18n: "sub" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   825
        raise error.ParseError(_(b"sub got an invalid replacement: %s") % rpl)
19390
3af3a165db18 templater: sort functions alphabetically, as filters are
Alexander Plavin <me@aplavin.ru>
parents: 19330
diff changeset
   826
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   827
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   828
@templatefunc(b'startswith(pattern, text)')
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
   829
def startswith(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   830
    """Returns the value from the "text" argument
24586
90e3f5d22dad templater: add consistent docstrings to functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24337
diff changeset
   831
    if it begins with the content from the "pattern" argument."""
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
   832
    if len(args) != 2:
21960
2896d450fec4 templater: add i18n comments to error messages of newly added functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21846
diff changeset
   833
        # i18n: "startswith" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   834
        raise error.ParseError(_(b"startswith expects two arguments"))
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
   835
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   836
    patn = evalstring(context, mapping, args[0])
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   837
    text = evalstring(context, mapping, args[1])
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
   838
    if text.startswith(patn):
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
   839
        return text
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   840
    return b''
21821
4a445dc5abff templater: introduce startswith function
Ryan McElroy <rmcelroy@fb.com>
parents: 21798
diff changeset
   841
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   842
44592
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   843
@templatefunc(
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   844
    b'subsetparents(rev, revset)',
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   845
    argspec=b'rev revset',
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   846
    requires={b'repo', b'cache'},
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   847
)
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   848
def subsetparents(context, mapping, args):
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   849
    """Look up parents of the rev in the sub graph given by the revset."""
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   850
    if b'rev' not in args or b'revset' not in args:
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   851
        # i18n: "subsetparents" is a keyword
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   852
        raise error.ParseError(_(b"subsetparents expects two arguments"))
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   853
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   854
    repo = context.resource(mapping, b'repo')
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   855
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   856
    rev = templateutil.evalinteger(context, mapping, args[b'rev'])
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   857
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   858
    # TODO: maybe subsetparents(rev) should be allowed. the default revset
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   859
    # will be the revisions specified by -rREV argument.
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   860
    q = templateutil.evalwrapped(context, mapping, args[b'revset'])
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   861
    if not isinstance(q, templateutil.revslist):
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   862
        # i18n: "subsetparents" is a keyword
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   863
        raise error.ParseError(_(b"subsetparents expects a queried revset"))
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   864
    subset = q.tovalue(context, mapping)
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   865
    key = q.cachekey
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   866
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   867
    if key:
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   868
        # cache only if revset query isn't dynamic
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   869
        cache = context.resource(mapping, b'cache')
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   870
        walkercache = cache.setdefault(b'subsetparentswalker', {})
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   871
        if key in walkercache:
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   872
            walker = walkercache[key]
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   873
        else:
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   874
            walker = dagop.subsetparentswalker(repo, subset)
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   875
            walkercache[key] = walker
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   876
    else:
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   877
        # for one-shot use, specify startrev to limit the search space
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   878
        walker = dagop.subsetparentswalker(repo, subset, startrev=rev)
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   879
    return templateutil.revslist(repo, walker.parentsset(rev))
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   880
7cd5c0968139 templater: add subsetparents(rev, revset) function
Yuya Nishihara <yuya@tcha.org>
parents: 44591
diff changeset
   881
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   882
@templatefunc(b'word(number, text[, separator])')
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   883
def word(context, mapping, args):
28696
efa192203623 templater: use templatefunc to mark a function as template function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28695
diff changeset
   884
    """Return the nth word from a string."""
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   885
    if not (2 <= len(args) <= 3):
21960
2896d450fec4 templater: add i18n comments to error messages of newly added functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21846
diff changeset
   886
        # i18n: "word" is a keyword
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   887
        raise error.ParseError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   888
            _(b"word expects two or three arguments, got %d") % len(args)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   889
        )
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   890
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   891
    num = evalinteger(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   892
        context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   893
        mapping,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   894
        args[0],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   895
        # i18n: "word" is a keyword
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   896
        _(b"word expects an integer index"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   897
    )
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   898
    text = evalstring(context, mapping, args[1])
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   899
    if len(args) == 3:
28348
ccedb17a5657 templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents: 28346
diff changeset
   900
        splitter = evalstring(context, mapping, args[2])
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   901
    else:
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   902
        splitter = None
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   903
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   904
    tokens = text.split(splitter)
26502
4ca98a389152 templater: protect word() from crashing on out of range negative value
Matt Harbison <matt_harbison@yahoo.com>
parents: 25815
diff changeset
   905
    if num >= len(tokens) or num < -len(tokens):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   906
        return b''
21846
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   907
    else:
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   908
        return tokens[num]
8f23f8096606 templater: introduce word function
Ryan McElroy <rmcelroy@fb.com>
parents: 21822
diff changeset
   909
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   910
28695
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
   911
def loadfunction(ui, extname, registrarobj):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44592
diff changeset
   912
    """Load template function from specified registrarobj"""
48913
f254fc73d956 global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
   913
    for name, func in registrarobj._table.items():
28695
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
   914
        funcs[name] = func
cc103bd0dbf9 registrar: add templatefunc to mark a function as template function (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28687
diff changeset
   915
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43051
diff changeset
   916
24601
d80819f67d59 templater: tell hggettext to collect help of template functions
Yuya Nishihara <yuya@tcha.org>
parents: 24586
diff changeset
   917
# tell hggettext to extract docstrings from these functions:
d80819f67d59 templater: tell hggettext to collect help of template functions
Yuya Nishihara <yuya@tcha.org>
parents: 24586
diff changeset
   918
i18nfunctions = funcs.values()