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