##// END OF EJS Templates
help: apply the section headings from revsets to templates...
Matt Harbison -
r30730:107014f4 default
parent child Browse files
Show More
@@ -1,174 +1,192 b''
1 Mercurial allows you to customize output of commands through
1 Mercurial allows you to customize output of commands through
2 templates. You can either pass in a template or select an existing
2 templates. You can either pass in a template or select an existing
3 template-style from the command line, via the --template option.
3 template-style from the command line, via the --template option.
4
4
5 You can customize output for any "log-like" command: log,
5 You can customize output for any "log-like" command: log,
6 outgoing, incoming, tip, parents, and heads.
6 outgoing, incoming, tip, parents, and heads.
7
7
8 Some built-in styles are packaged with Mercurial. These can be listed
8 Some built-in styles are packaged with Mercurial. These can be listed
9 with :hg:`log --template list`. Example usage::
9 with :hg:`log --template list`. Example usage::
10
10
11 $ hg log -r1.0::1.1 --template changelog
11 $ hg log -r1.0::1.1 --template changelog
12
12
13 A template is a piece of text, with markup to invoke variable
13 A template is a piece of text, with markup to invoke variable
14 expansion::
14 expansion::
15
15
16 $ hg log -r1 --template "{node}\n"
16 $ hg log -r1 --template "{node}\n"
17 b56ce7b07c52de7d5fd79fb89701ea538af65746
17 b56ce7b07c52de7d5fd79fb89701ea538af65746
18
18
19 Keywords
20 ========
21
19 Strings in curly braces are called keywords. The availability of
22 Strings in curly braces are called keywords. The availability of
20 keywords depends on the exact context of the templater. These
23 keywords depends on the exact context of the templater. These
21 keywords are usually available for templating a log-like command:
24 keywords are usually available for templating a log-like command:
22
25
23 .. keywordsmarker
26 .. keywordsmarker
24
27
25 The "date" keyword does not produce human-readable output. If you
28 The "date" keyword does not produce human-readable output. If you
26 want to use a date in your output, you can use a filter to process
29 want to use a date in your output, you can use a filter to process
27 it. Filters are functions which return a string based on the input
30 it. Filters are functions which return a string based on the input
28 variable. Be sure to use the stringify filter first when you're
31 variable. Be sure to use the stringify filter first when you're
29 applying a string-input filter to a list-like input variable.
32 applying a string-input filter to a list-like input variable.
30 You can also use a chain of filters to get the desired output::
33 You can also use a chain of filters to get the desired output::
31
34
32 $ hg tip --template "{date|isodate}\n"
35 $ hg tip --template "{date|isodate}\n"
33 2008-08-21 18:22 +0000
36 2008-08-21 18:22 +0000
34
37
38 Filters
39 =======
40
35 List of filters:
41 List of filters:
36
42
37 .. filtersmarker
43 .. filtersmarker
38
44
39 Note that a filter is nothing more than a function call, i.e.
45 Note that a filter is nothing more than a function call, i.e.
40 ``expr|filter`` is equivalent to ``filter(expr)``.
46 ``expr|filter`` is equivalent to ``filter(expr)``.
41
47
48 Functions
49 =========
50
42 In addition to filters, there are some basic built-in functions:
51 In addition to filters, there are some basic built-in functions:
43
52
44 .. functionsmarker
53 .. functionsmarker
45
54
55 Infix
56 =====
57
46 We provide a limited set of infix arithmetic operations on integers::
58 We provide a limited set of infix arithmetic operations on integers::
47
59
48 + for addition
60 + for addition
49 - for subtraction
61 - for subtraction
50 * for multiplication
62 * for multiplication
51 / for floor division (division rounded to integer nearest -infinity)
63 / for floor division (division rounded to integer nearest -infinity)
52
64
53 Division fulfils the law x = x / y + mod(x, y).
65 Division fulfils the law x = x / y + mod(x, y).
54
66
55 Also, for any expression that returns a list, there is a list operator::
67 Also, for any expression that returns a list, there is a list operator::
56
68
57 expr % "{template}"
69 expr % "{template}"
58
70
59 As seen in the above example, ``{template}`` is interpreted as a template.
71 As seen in the above example, ``{template}`` is interpreted as a template.
60 To prevent it from being interpreted, you can use an escape character ``\{``
72 To prevent it from being interpreted, you can use an escape character ``\{``
61 or a raw string prefix, ``r'...'``.
73 or a raw string prefix, ``r'...'``.
62
74
75 Aliases
76 =======
77
63 New keywords and functions can be defined in the ``templatealias`` section of
78 New keywords and functions can be defined in the ``templatealias`` section of
64 a Mercurial configuration file::
79 a Mercurial configuration file::
65
80
66 <alias> = <definition>
81 <alias> = <definition>
67
82
68 Arguments of the form `a1`, `a2`, etc. are substituted from the alias into
83 Arguments of the form `a1`, `a2`, etc. are substituted from the alias into
69 the definition.
84 the definition.
70
85
71 For example,
86 For example,
72
87
73 ::
88 ::
74
89
75 [templatealias]
90 [templatealias]
76 r = rev
91 r = rev
77 rn = "{r}:{node|short}"
92 rn = "{r}:{node|short}"
78 leftpad(s, w) = pad(s, w, ' ', True)
93 leftpad(s, w) = pad(s, w, ' ', True)
79
94
80 defines two symbol aliases, ``r`` and ``rn``, and a function alias
95 defines two symbol aliases, ``r`` and ``rn``, and a function alias
81 ``leftpad()``.
96 ``leftpad()``.
82
97
83 It's also possible to specify complete template strings, using the
98 It's also possible to specify complete template strings, using the
84 ``templates`` section. The syntax used is the general template string syntax.
99 ``templates`` section. The syntax used is the general template string syntax.
85
100
86 For example,
101 For example,
87
102
88 ::
103 ::
89
104
90 [templates]
105 [templates]
91 nodedate = "{node|short}: {date(date, "%Y-%m-%d")}\n"
106 nodedate = "{node|short}: {date(date, "%Y-%m-%d")}\n"
92
107
93 defines a template, ``nodedate``, which can be called like::
108 defines a template, ``nodedate``, which can be called like::
94
109
95 $ hg log -r . -Tnodedate
110 $ hg log -r . -Tnodedate
96
111
112 Examples
113 ========
114
97 Some sample command line templates:
115 Some sample command line templates:
98
116
99 - Format lists, e.g. files::
117 - Format lists, e.g. files::
100
118
101 $ hg log -r 0 --template "files:\n{files % ' {file}\n'}"
119 $ hg log -r 0 --template "files:\n{files % ' {file}\n'}"
102
120
103 - Join the list of files with a ", "::
121 - Join the list of files with a ", "::
104
122
105 $ hg log -r 0 --template "files: {join(files, ', ')}\n"
123 $ hg log -r 0 --template "files: {join(files, ', ')}\n"
106
124
107 - Join the list of files ending with ".py" with a ", "::
125 - Join the list of files ending with ".py" with a ", "::
108
126
109 $ hg log -r 0 --template "pythonfiles: {join(files('**.py'), ', ')}\n"
127 $ hg log -r 0 --template "pythonfiles: {join(files('**.py'), ', ')}\n"
110
128
111 - Separate non-empty arguments by a " "::
129 - Separate non-empty arguments by a " "::
112
130
113 $ hg log -r 0 --template "{separate(' ', node, bookmarks, tags}\n"
131 $ hg log -r 0 --template "{separate(' ', node, bookmarks, tags}\n"
114
132
115 - Modify each line of a commit description::
133 - Modify each line of a commit description::
116
134
117 $ hg log --template "{splitlines(desc) % '**** {line}\n'}"
135 $ hg log --template "{splitlines(desc) % '**** {line}\n'}"
118
136
119 - Format date::
137 - Format date::
120
138
121 $ hg log -r 0 --template "{date(date, '%Y')}\n"
139 $ hg log -r 0 --template "{date(date, '%Y')}\n"
122
140
123 - Display date in UTC::
141 - Display date in UTC::
124
142
125 $ hg log -r 0 --template "{localdate(date, 'UTC')|date}\n"
143 $ hg log -r 0 --template "{localdate(date, 'UTC')|date}\n"
126
144
127 - Output the description set to a fill-width of 30::
145 - Output the description set to a fill-width of 30::
128
146
129 $ hg log -r 0 --template "{fill(desc, 30)}"
147 $ hg log -r 0 --template "{fill(desc, 30)}"
130
148
131 - Use a conditional to test for the default branch::
149 - Use a conditional to test for the default branch::
132
150
133 $ hg log -r 0 --template "{ifeq(branch, 'default', 'on the main branch',
151 $ hg log -r 0 --template "{ifeq(branch, 'default', 'on the main branch',
134 'on branch {branch}')}\n"
152 'on branch {branch}')}\n"
135
153
136 - Append a newline if not empty::
154 - Append a newline if not empty::
137
155
138 $ hg tip --template "{if(author, '{author}\n')}"
156 $ hg tip --template "{if(author, '{author}\n')}"
139
157
140 - Label the output for use with the color extension::
158 - Label the output for use with the color extension::
141
159
142 $ hg log -r 0 --template "{label('changeset.{phase}', node|short)}\n"
160 $ hg log -r 0 --template "{label('changeset.{phase}', node|short)}\n"
143
161
144 - Invert the firstline filter, i.e. everything but the first line::
162 - Invert the firstline filter, i.e. everything but the first line::
145
163
146 $ hg log -r 0 --template "{sub(r'^.*\n?\n?', '', desc)}\n"
164 $ hg log -r 0 --template "{sub(r'^.*\n?\n?', '', desc)}\n"
147
165
148 - Display the contents of the 'extra' field, one per line::
166 - Display the contents of the 'extra' field, one per line::
149
167
150 $ hg log -r 0 --template "{join(extras, '\n')}\n"
168 $ hg log -r 0 --template "{join(extras, '\n')}\n"
151
169
152 - Mark the active bookmark with '*'::
170 - Mark the active bookmark with '*'::
153
171
154 $ hg log --template "{bookmarks % '{bookmark}{ifeq(bookmark, active, '*')} '}\n"
172 $ hg log --template "{bookmarks % '{bookmark}{ifeq(bookmark, active, '*')} '}\n"
155
173
156 - Find the previous release candidate tag, the distance and changes since the tag::
174 - Find the previous release candidate tag, the distance and changes since the tag::
157
175
158 $ hg log -r . --template "{latesttag('re:^.*-rc$') % '{tag}, {changes}, {distance}'}\n"
176 $ hg log -r . --template "{latesttag('re:^.*-rc$') % '{tag}, {changes}, {distance}'}\n"
159
177
160 - Mark the working copy parent with '@'::
178 - Mark the working copy parent with '@'::
161
179
162 $ hg log --template "{ifcontains(rev, revset('.'), '@')}\n"
180 $ hg log --template "{ifcontains(rev, revset('.'), '@')}\n"
163
181
164 - Show details of parent revisions::
182 - Show details of parent revisions::
165
183
166 $ hg log --template "{revset('parents(%d)', rev) % '{desc|firstline}\n'}"
184 $ hg log --template "{revset('parents(%d)', rev) % '{desc|firstline}\n'}"
167
185
168 - Show only commit descriptions that start with "template"::
186 - Show only commit descriptions that start with "template"::
169
187
170 $ hg log --template "{startswith('template', firstline(desc))}\n"
188 $ hg log --template "{startswith('template', firstline(desc))}\n"
171
189
172 - Print the first word of each line of a commit message::
190 - Print the first word of each line of a commit message::
173
191
174 $ hg log --template "{word(0, desc)}\n"
192 $ hg log --template "{word(0, desc)}\n"
General Comments 0
You need to be logged in to leave comments. Login now