hgbook

annotate en/ch11-template.xml @ 559:b90b024729f1

WIP DocBook snapshot that all compiles. Mirabile dictu!
author Bryan O'Sullivan <bos@serpentine.com>
date Wed Feb 18 00:22:09 2009 -0800 (2009-02-18)
parents en/ch11-template.tex@5cd47f721686
children 21c62e09b99f
rev   line source
bos@559 1 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->
bos@559 2
bos@559 3 <chapter id="chap:template">
bos@559 4 <title>Customising the output of Mercurial</title>
bos@559 5
bos@559 6 <para>Mercurial provides a powerful mechanism to let you control how
bos@559 7 it displays information. The mechanism is based on templates.
bos@559 8 You can use templates to generate specific output for a single
bos@559 9 command, or to customise the entire appearance of the built-in web
bos@559 10 interface.</para>
bos@559 11
bos@559 12 <sect1 id="sec:style">
bos@559 13 <title>Using precanned output styles</title>
bos@559 14
bos@559 15 <para>Packaged with Mercurial are some output styles that you can
bos@559 16 use immediately. A style is simply a precanned template that
bos@559 17 someone wrote and installed somewhere that Mercurial can
bos@559 18 find.</para>
bos@559 19
bos@559 20 <para>Before we take a look at Mercurial's bundled styles, let's
bos@559 21 review its normal output.</para>
bos@559 22
bos@559 23 <para><!-- &interaction.template.simple.normal; --></para>
bos@559 24
bos@559 25 <para>This is somewhat informative, but it takes up a lot of
bos@559 26 space&emdash;five lines of output per changeset. The
bos@559 27 <literal>compact</literal> style reduces this to three lines,
bos@559 28 presented in a sparse manner.</para>
bos@559 29
bos@559 30 <para><!-- &interaction.template.simple.compact; --></para>
bos@559 31
bos@559 32 <para>The <literal>changelog</literal> style hints at the
bos@559 33 expressive power of Mercurial's templating engine. This style
bos@559 34 attempts to follow the GNU Project's changelog
bos@559 35 guidelines<citation>web:changelog</citation>.</para>
bos@559 36
bos@559 37 <para><!-- &interaction.template.simple.changelog; --></para>
bos@559 38
bos@559 39 <para>You will not be shocked to learn that Mercurial's default
bos@559 40 output style is named <literal>default</literal>.</para>
bos@559 41
bos@559 42 <sect2>
bos@559 43 <title>Setting a default style</title>
bos@559 44
bos@559 45 <para>You can modify the output style that Mercurial will use
bos@559 46 for every command by editing your <filename role="special">
bos@559 47 /.hgrc</filename>\ file, naming the style you would prefer
bos@559 48 to use.</para>
bos@559 49
bos@559 50 <programlisting>[ui] style = compact</programlisting>
bos@559 51
bos@559 52 <para>If you write a style of your own, you can use it by either
bos@559 53 providing the path to your style file, or copying your style
bos@559 54 file into a location where Mercurial can find it (typically
bos@559 55 the <literal>templates</literal> subdirectory of your
bos@559 56 Mercurial install directory).</para>
bos@559 57
bos@559 58 </sect2>
bos@559 59 </sect1>
bos@559 60 <sect1>
bos@559 61 <title>Commands that support styles and templates</title>
bos@559 62
bos@559 63 <para>All of Mercurial's
bos@559 64 <quote><literal>log</literal>-like</quote> commands let you use
bos@559 65 styles and templates: <command role="hg-cmd">hg
bos@559 66 incoming</command>, <command role="hg-cmd">hg log</command>,
bos@559 67 <command role="hg-cmd">hg outgoing</command>, and <command
bos@559 68 role="hg-cmd">hg tip</command>.</para>
bos@559 69
bos@559 70 <para>As I write this manual, these are so far the only commands
bos@559 71 that support styles and templates. Since these are the most
bos@559 72 important commands that need customisable output, there has been
bos@559 73 little pressure from the Mercurial user community to add style
bos@559 74 and template support to other commands.</para>
bos@559 75
bos@559 76 </sect1>
bos@559 77 <sect1>
bos@559 78 <title>The basics of templating</title>
bos@559 79
bos@559 80 <para>At its simplest, a Mercurial template is a piece of text.
bos@559 81 Some of the text never changes, while other parts are
bos@559 82 <emphasis>expanded</emphasis>, or replaced with new text, when
bos@559 83 necessary.</para>
bos@559 84
bos@559 85 <para>Before we continue, let's look again at a simple example of
bos@559 86 Mercurial's normal output.</para>
bos@559 87
bos@559 88 <para><!-- &interaction.template.simple.normal; --></para>
bos@559 89
bos@559 90 <para>Now, let's run the same command, but using a template to
bos@559 91 change its output.</para>
bos@559 92
bos@559 93 <para><!-- &interaction.template.simple.simplest; --></para>
bos@559 94
bos@559 95 <para>The example above illustrates the simplest possible
bos@559 96 template; it's just a piece of static text, printed once for
bos@559 97 each changeset. The <option
bos@559 98 role="hg-opt-log">--template</option> option to the <command
bos@559 99 role="hg-cmd">hg log</command> command tells Mercurial to use
bos@559 100 the given text as the template when printing each
bos@559 101 changeset.</para>
bos@559 102
bos@559 103 <para>Notice that the template string above ends with the text
bos@559 104 <quote><literal>\n</literal></quote>. This is an
bos@559 105 <emphasis>escape sequence</emphasis>, telling Mercurial to print
bos@559 106 a newline at the end of each template item. If you omit this
bos@559 107 newline, Mercurial will run each piece of output together. See
bos@559 108 section <xref linkend="sec:template:escape"/> for more details
bos@559 109 of escape sequences.</para>
bos@559 110
bos@559 111 <para>A template that prints a fixed string of text all the time
bos@559 112 isn't very useful; let's try something a bit more
bos@559 113 complex.</para>
bos@559 114
bos@559 115 <para><!-- &interaction.template.simple.simplesub; --></para>
bos@559 116
bos@559 117 <para>As you can see, the string
bos@559 118 <quote><literal>{desc}</literal></quote> in the template has
bos@559 119 been replaced in the output with the description of each
bos@559 120 changeset. Every time Mercurial finds text enclosed in curly
bos@559 121 braces (<quote><literal>{</literal></quote> and
bos@559 122 <quote>\texttt{}}</quote>), it will try to replace the braces
bos@559 123 and text with the expansion of whatever is inside. To print a
bos@559 124 literal curly brace, you must escape it, as described in section
bos@559 125 <xref
bos@559 126 linkend="sec:template:escape"/>.</para>
bos@559 127
bos@559 128 </sect1>
bos@559 129 <sect1 id="sec:template:keyword">
bos@559 130 <title>Common template keywords</title>
bos@559 131
bos@559 132 <para>You can start writing simple templates immediately using the
bos@559 133 keywords below.</para>
bos@559 134
bos@559 135 <itemizedlist>
bos@559 136 <listitem><para><literal
bos@559 137 role="template-keyword">author</literal>: String. The
bos@559 138 unmodified author of the changeset.</para>
bos@559 139 </listitem>
bos@559 140 <listitem><para><literal
bos@559 141 role="template-keyword">branches</literal>: String. The
bos@559 142 name of the branch on which the changeset was committed.
bos@559 143 Will be empty if the branch name was
bos@559 144 <literal>default</literal>.</para>
bos@559 145 </listitem>
bos@559 146 <listitem><para><literal role="template-keyword">date</literal>:
bos@559 147 Date information. The date when the changeset was
bos@559 148 committed. This is <emphasis>not</emphasis> human-readable;
bos@559 149 you must pass it through a filter that will render it
bos@559 150 appropriately. See section <xref
bos@559 151 linkend="sec:template:filter"/> for more information
bos@559 152 on filters. The date is expressed as a pair of numbers. The
bos@559 153 first number is a Unix UTC timestamp (seconds since January
bos@559 154 1, 1970); the second is the offset of the committer's
bos@559 155 timezone from UTC, in seconds.</para>
bos@559 156 </listitem>
bos@559 157 <listitem><para><literal role="template-keyword">desc</literal>:
bos@559 158 String. The text of the changeset description.</para>
bos@559 159 </listitem>
bos@559 160 <listitem><para><literal
bos@559 161 role="template-keyword">files</literal>: List of strings.
bos@559 162 All files modified, added, or removed by this
bos@559 163 changeset.</para>
bos@559 164 </listitem>
bos@559 165 <listitem><para><literal
bos@559 166 role="template-keyword">file_adds</literal>: List of
bos@559 167 strings. Files added by this changeset.</para>
bos@559 168 </listitem>
bos@559 169 <listitem><para><literal
bos@559 170 role="template-keyword">file_dels</literal>: List of
bos@559 171 strings. Files removed by this changeset.</para>
bos@559 172 </listitem>
bos@559 173 <listitem><para><literal role="template-keyword">node</literal>:
bos@559 174 String. The changeset identification hash, as a
bos@559 175 40-character hexadecimal string.</para>
bos@559 176 </listitem>
bos@559 177 <listitem><para><literal
bos@559 178 role="template-keyword">parents</literal>: List of
bos@559 179 strings. The parents of the changeset.</para>
bos@559 180 </listitem>
bos@559 181 <listitem><para><literal role="template-keyword">rev</literal>:
bos@559 182 Integer. The repository-local changeset revision
bos@559 183 number.</para>
bos@559 184 </listitem>
bos@559 185 <listitem><para><literal role="template-keyword">tags</literal>:
bos@559 186 List of strings. Any tags associated with the
bos@559 187 changeset.</para>
bos@559 188 </listitem></itemizedlist>
bos@559 189
bos@559 190 <para>A few simple experiments will show us what to expect when we
bos@559 191 use these keywords; you can see the results below.</para>
bos@559 192
bos@559 193 <!-- &interaction.template.simple.keywords; -->
bos@559 194
bos@559 195 <para>As we noted above, the date keyword does not produce
bos@559 196 human-readable output, so we must treat it specially. This
bos@559 197 involves using a <emphasis>filter</emphasis>, about which more
bos@559 198 in section <xref
bos@559 199 linkend="sec:template:filter"/>.</para>
bos@559 200
bos@559 201 <para><!-- &interaction.template.simple.datekeyword; --></para>
bos@559 202
bos@559 203 </sect1>
bos@559 204 <sect1 id="sec:template:escape">
bos@559 205 <title>Escape sequences</title>
bos@559 206
bos@559 207 <para>Mercurial's templating engine recognises the most commonly
bos@559 208 used escape sequences in strings. When it sees a backslash
bos@559 209 (<quote><literal>\</literal></quote>) character, it looks at the
bos@559 210 following character and substitutes the two characters with a
bos@559 211 single replacement, as described below.</para>
bos@559 212
bos@559 213 <itemizedlist>
bos@559 214 <listitem><para><literal>\textbackslash\textbackslash</literal>:
bos@559 215 Backslash, <quote><literal>\</literal></quote>, ASCII
bos@559 216 134.</para>
bos@559 217 </listitem>
bos@559 218 <listitem><para><literal>\textbackslash n</literal>: Newline,
bos@559 219 ASCII 12.</para>
bos@559 220 </listitem>
bos@559 221 <listitem><para><literal>\textbackslash r</literal>: Carriage
bos@559 222 return, ASCII 15.</para>
bos@559 223 </listitem>
bos@559 224 <listitem><para><literal>\textbackslash t</literal>: Tab, ASCII
bos@559 225 11.</para>
bos@559 226 </listitem>
bos@559 227 <listitem><para><literal>\textbackslash v</literal>: Vertical
bos@559 228 tab, ASCII 13.</para>
bos@559 229 </listitem>
bos@559 230 <listitem><para><literal>\textbackslash {</literal>: Open curly
bos@559 231 brace, <quote><literal>{</literal></quote>, ASCII
bos@559 232 173.</para>
bos@559 233 </listitem>
bos@559 234 <listitem><para><literal>\textbackslash }</literal>: Close curly
bos@559 235 brace, <quote><literal>}</literal></quote>, ASCII
bos@559 236 175.</para>
bos@559 237 </listitem></itemizedlist>
bos@559 238
bos@559 239 <para>As indicated above, if you want the expansion of a template
bos@559 240 to contain a literal <quote><literal>\</literal></quote>,
bos@559 241 <quote><literal>{</literal></quote>, or
bos@559 242 <quote><literal>{</literal></quote> character, you must escape
bos@559 243 it.</para>
bos@559 244
bos@559 245 </sect1>
bos@559 246 <sect1 id="sec:template:filter">
bos@559 247 <title>Filtering keywords to change their results</title>
bos@559 248
bos@559 249 <para>Some of the results of template expansion are not
bos@559 250 immediately easy to use. Mercurial lets you specify an optional
bos@559 251 chain of <emphasis>filters</emphasis> to modify the result of
bos@559 252 expanding a keyword. You have already seen a common filter,
bos@559 253 <literal role="template-kw-filt-date">isodate</literal>, in
bos@559 254 action above, to make a date readable.</para>
bos@559 255
bos@559 256 <para>Below is a list of the most commonly used filters that
bos@559 257 Mercurial supports. While some filters can be applied to any
bos@559 258 text, others can only be used in specific circumstances. The
bos@559 259 name of each filter is followed first by an indication of where
bos@559 260 it can be used, then a description of its effect.</para>
bos@559 261
bos@559 262 <itemizedlist>
bos@559 263 <listitem><para><literal
bos@559 264 role="template-filter">addbreaks</literal>: Any text. Add
bos@559 265 an XHTML <quote><literal>&lt;br/&gt;</literal></quote> tag
bos@559 266 before the end of every line except the last. For example,
bos@559 267 <quote><literal>foo\nbar</literal></quote> becomes
bos@559 268 <quote><literal>foo&lt;br/&gt;\nbar</literal></quote>.</para>
bos@559 269 </listitem>
bos@559 270 <listitem><para><literal
bos@559 271 role="template-kw-filt-date">age</literal>: <literal
bos@559 272 role="template-keyword">date</literal> keyword. Render
bos@559 273 the age of the date, relative to the current time. Yields a
bos@559 274 string like <quote><literal>10
bos@559 275 minutes</literal></quote>.</para>
bos@559 276 </listitem>
bos@559 277 <listitem><para><literal
bos@559 278 role="template-filter">basename</literal>: Any text, but
bos@559 279 most useful for the <literal
bos@559 280 role="template-keyword">files</literal> keyword and its
bos@559 281 relatives. Treat the text as a path, and return the
bos@559 282 basename. For example,
bos@559 283 <quote><literal>foo/bar/baz</literal></quote> becomes
bos@559 284 <quote><literal>baz</literal></quote>.</para>
bos@559 285 </listitem>
bos@559 286 <listitem><para><literal
bos@559 287 role="template-kw-filt-date">date</literal>: <literal
bos@559 288 role="template-keyword">date</literal> keyword. Render a
bos@559 289 date in a similar format to the Unix <literal
bos@559 290 role="template-keyword">date</literal> command, but with
bos@559 291 timezone included. Yields a string like <quote><literal>Mon
bos@559 292 Sep 04 15:13:13 2006 -0700</literal></quote>.</para>
bos@559 293 </listitem>
bos@559 294 <listitem><para><literal
bos@559 295 role="template-kw-filt-author">domain</literal>: Any text,
bos@559 296 but most useful for the <literal
bos@559 297 role="template-keyword">author</literal> keyword. Finds
bos@559 298 the first string that looks like an email address, and
bos@559 299 extract just the domain component. For example,
bos@559 300 <quote><literal>Bryan O'Sullivan
bos@559 301 &lt;bos@serpentine.com&gt;</literal></quote> becomes
bos@559 302 <quote><literal>serpentine.com</literal></quote>.</para>
bos@559 303 </listitem>
bos@559 304 <listitem><para><literal
bos@559 305 role="template-kw-filt-author">email</literal>: Any text,
bos@559 306 but most useful for the <literal
bos@559 307 role="template-keyword">author</literal> keyword. Extract
bos@559 308 the first string that looks like an email address. For
bos@559 309 example, <quote><literal>Bryan O'Sullivan
bos@559 310 &lt;bos@serpentine.com&gt;</literal></quote> becomes
bos@559 311 <quote><literal>bos@serpentine.com</literal></quote>.</para>
bos@559 312 </listitem>
bos@559 313 <listitem><para><literal
bos@559 314 role="template-filter">escape</literal>: Any text.
bos@559 315 Replace the special XML/XHTML characters
bos@559 316 <quote><literal>&amp;</literal></quote>,
bos@559 317 <quote><literal>&lt;</literal></quote> and
bos@559 318 <quote><literal>&gt;</literal></quote> with XML
bos@559 319 entities.</para>
bos@559 320 </listitem>
bos@559 321 <listitem><para><literal
bos@559 322 role="template-filter">fill68</literal>: Any text. Wrap
bos@559 323 the text to fit in 68 columns. This is useful before you
bos@559 324 pass text through the <literal
bos@559 325 role="template-filter">tabindent</literal> filter, and
bos@559 326 still want it to fit in an 80-column fixed-font
bos@559 327 window.</para>
bos@559 328 </listitem>
bos@559 329 <listitem><para><literal
bos@559 330 role="template-filter">fill76</literal>: Any text. Wrap
bos@559 331 the text to fit in 76 columns.</para>
bos@559 332 </listitem>
bos@559 333 <listitem><para><literal
bos@559 334 role="template-filter">firstline</literal>: Any text.
bos@559 335 Yield the first line of text, without any trailing
bos@559 336 newlines.</para>
bos@559 337 </listitem>
bos@559 338 <listitem><para><literal
bos@559 339 role="template-kw-filt-date">hgdate</literal>: <literal
bos@559 340 role="template-keyword">date</literal> keyword. Render
bos@559 341 the date as a pair of readable numbers. Yields a string
bos@559 342 like <quote><literal>1157407993
bos@559 343 25200</literal></quote>.</para>
bos@559 344 </listitem>
bos@559 345 <listitem><para><literal
bos@559 346 role="template-kw-filt-date">isodate</literal>: <literal
bos@559 347 role="template-keyword">date</literal> keyword. Render
bos@559 348 the date as a text string in ISO 8601 format. Yields a
bos@559 349 string like <quote><literal>2006-09-04 15:13:13
bos@559 350 -0700</literal></quote>.</para>
bos@559 351 </listitem>
bos@559 352 <listitem><para><literal
bos@559 353 role="template-filter">obfuscate</literal>: Any text, but
bos@559 354 most useful for the <literal
bos@559 355 role="template-keyword">author</literal> keyword. Yield
bos@559 356 the input text rendered as a sequence of XML entities. This
bos@559 357 helps to defeat some particularly stupid screen-scraping
bos@559 358 email harvesting spambots.</para>
bos@559 359 </listitem>
bos@559 360 <listitem><para><literal
bos@559 361 role="template-kw-filt-author">person</literal>: Any text,
bos@559 362 but most useful for the <literal
bos@559 363 role="template-keyword">author</literal> keyword. Yield
bos@559 364 the text before an email address. For example,
bos@559 365 <quote><literal>Bryan O'Sullivan
bos@559 366 &lt;bos@serpentine.com&gt;</literal></quote> becomes
bos@559 367 <quote><literal>Bryan O'Sullivan</literal></quote>.</para>
bos@559 368 </listitem>
bos@559 369 <listitem><para><literal
bos@559 370 role="template-kw-filt-date">rfc822date</literal>:
bos@559 371 <literal role="template-keyword">date</literal> keyword.
bos@559 372 Render a date using the same format used in email headers.
bos@559 373 Yields a string like <quote><literal>Mon, 04 Sep 2006
bos@559 374 15:13:13 -0700</literal></quote>.</para>
bos@559 375 </listitem>
bos@559 376 <listitem><para><literal
bos@559 377 role="template-kw-filt-node">short</literal>: Changeset
bos@559 378 hash. Yield the short form of a changeset hash, i.e. a
bos@559 379 12-character hexadecimal string.</para>
bos@559 380 </listitem>
bos@559 381 <listitem><para><literal
bos@559 382 role="template-kw-filt-date">shortdate</literal>: <literal
bos@559 383 role="template-keyword">date</literal> keyword. Render
bos@559 384 the year, month, and day of the date. Yields a string like
bos@559 385 <quote><literal>2006-09-04</literal></quote>.</para>
bos@559 386 </listitem>
bos@559 387 <listitem><para><literal role="template-filter">strip</literal>:
bos@559 388 Any text. Strip all leading and trailing whitespace from
bos@559 389 the string.</para>
bos@559 390 </listitem>
bos@559 391 <listitem><para><literal
bos@559 392 role="template-filter">tabindent</literal>: Any text.
bos@559 393 Yield the text, with every line except the first starting
bos@559 394 with a tab character.</para>
bos@559 395 </listitem>
bos@559 396 <listitem><para><literal
bos@559 397 role="template-filter">urlescape</literal>: Any text.
bos@559 398 Escape all characters that are considered
bos@559 399 <quote>special</quote> by URL parsers. For example,
bos@559 400 <literal>foo bar</literal> becomes
bos@559 401 <literal>foo%20bar</literal>.</para>
bos@559 402 </listitem>
bos@559 403 <listitem><para><literal
bos@559 404 role="template-kw-filt-author">user</literal>: Any text,
bos@559 405 but most useful for the <literal
bos@559 406 role="template-keyword">author</literal> keyword. Return
bos@559 407 the <quote>user</quote> portion of an email address. For
bos@559 408 example, <quote><literal>Bryan O'Sullivan
bos@559 409 &lt;bos@serpentine.com&gt;</literal></quote> becomes
bos@559 410 <quote><literal>bos</literal></quote>.</para>
bos@559 411 </listitem></itemizedlist>
bos@559 412
bos@559 413 <!-- &interaction.template.simple.manyfilters; -->
bos@559 414
bos@559 415 <note>
bos@559 416 <para> If you try to apply a filter to a piece of data that it
bos@559 417 cannot process, Mercurial will fail and print a Python
bos@559 418 exception. For example, trying to run the output of the
bos@559 419 <literal role="template-keyword">desc</literal> keyword into
bos@559 420 the <literal role="template-kw-filt-date">isodate</literal>
bos@559 421 filter is not a good idea.</para>
bos@559 422 </note>
bos@559 423
bos@559 424 <sect2>
bos@559 425 <title>Combining filters</title>
bos@559 426
bos@559 427 <para>It is easy to combine filters to yield output in the form
bos@559 428 you would like. The following chain of filters tidies up a
bos@559 429 description, then makes sure that it fits cleanly into 68
bos@559 430 columns, then indents it by a further 8 characters (at least
bos@559 431 on Unix-like systems, where a tab is conventionally 8
bos@559 432 characters wide).</para>
bos@559 433
bos@559 434 <para><!-- &interaction.template.simple.combine; --></para>
bos@559 435
bos@559 436 <para>Note the use of <quote><literal>\t</literal></quote> (a
bos@559 437 tab character) in the template to force the first line to be
bos@559 438 indented; this is necessary since <literal
bos@559 439 role="template-keyword">tabindent</literal> indents all
bos@559 440 lines <emphasis>except</emphasis> the first.</para>
bos@559 441
bos@559 442 <para>Keep in mind that the order of filters in a chain is
bos@559 443 significant. The first filter is applied to the result of the
bos@559 444 keyword; the second to the result of the first filter; and so
bos@559 445 on. For example, using <literal>fill68|tabindent</literal>
bos@559 446 gives very different results from
bos@559 447 <literal>tabindent|fill68</literal>.</para>
bos@559 448
bos@559 449
bos@559 450 </sect2>
bos@559 451 </sect1>
bos@559 452 <sect1>
bos@559 453 <title>From templates to styles</title>
bos@559 454
bos@559 455 <para>A command line template provides a quick and simple way to
bos@559 456 format some output. Templates can become verbose, though, and
bos@559 457 it's useful to be able to give a template a name. A style file
bos@559 458 is a template with a name, stored in a file.</para>
bos@559 459
bos@559 460 <para>More than that, using a style file unlocks the power of
bos@559 461 Mercurial's templating engine in ways that are not possible
bos@559 462 using the command line <option
bos@559 463 role="hg-opt-log">--template</option> option.</para>
bos@559 464
bos@559 465 <sect2>
bos@559 466 <title>The simplest of style files</title>
bos@559 467
bos@559 468 <para>Our simple style file contains just one line:</para>
bos@559 469
bos@559 470 <para><!-- &interaction.template.simple.rev; --></para>
bos@559 471
bos@559 472 <para>This tells Mercurial, <quote>if you're printing a
bos@559 473 changeset, use the text on the right as the
bos@559 474 template</quote>.</para>
bos@559 475
bos@559 476 </sect2>
bos@559 477 <sect2>
bos@559 478 <title>Style file syntax</title>
bos@559 479
bos@559 480 <para>The syntax rules for a style file are simple.</para>
bos@559 481
bos@559 482 <itemizedlist>
bos@559 483 <listitem><para>The file is processed one line at a
bos@559 484 time.</para>
bos@559 485 </listitem>
bos@559 486 <listitem><para>Leading and trailing white space are
bos@559 487 ignored.</para>
bos@559 488 </listitem>
bos@559 489 <listitem><para>Empty lines are skipped.</para>
bos@559 490 </listitem>
bos@559 491 <listitem><para>If a line starts with either of the characters
bos@559 492 <quote><literal>#</literal></quote> or
bos@559 493 <quote><literal>;</literal></quote>, the entire line is
bos@559 494 treated as a comment, and skipped as if empty.</para>
bos@559 495 </listitem>
bos@559 496 <listitem><para>A line starts with a keyword. This must start
bos@559 497 with an alphabetic character or underscore, and can
bos@559 498 subsequently contain any alphanumeric character or
bos@559 499 underscore. (In regexp notation, a keyword must match
bos@559 500 <literal>[A-Za-z_][A-Za-z0-9_]*</literal>.)</para>
bos@559 501 </listitem>
bos@559 502 <listitem><para>The next element must be an
bos@559 503 <quote><literal>=</literal></quote> character, which can
bos@559 504 be preceded or followed by an arbitrary amount of white
bos@559 505 space.</para>
bos@559 506 </listitem>
bos@559 507 <listitem><para>If the rest of the line starts and ends with
bos@559 508 matching quote characters (either single or double quote),
bos@559 509 it is treated as a template body.</para>
bos@559 510 </listitem>
bos@559 511 <listitem><para>If the rest of the line <emphasis>does
bos@559 512 not</emphasis> start with a quote character, it is
bos@559 513 treated as the name of a file; the contents of this file
bos@559 514 will be read and used as a template body.</para>
bos@559 515 </listitem></itemizedlist>
bos@559 516
bos@559 517 </sect2>
bos@559 518 </sect1>
bos@559 519 <sect1>
bos@559 520 <title>Style files by example</title>
bos@559 521
bos@559 522 <para>To illustrate how to write a style file, we will construct a
bos@559 523 few by example. Rather than provide a complete style file and
bos@559 524 walk through it, we'll mirror the usual process of developing a
bos@559 525 style file by starting with something very simple, and walking
bos@559 526 through a series of successively more complete examples.</para>
bos@559 527
bos@559 528 <sect2>
bos@559 529 <title>Identifying mistakes in style files</title>
bos@559 530
bos@559 531 <para>If Mercurial encounters a problem in a style file you are
bos@559 532 working on, it prints a terse error message that, once you
bos@559 533 figure out what it means, is actually quite useful.</para>
bos@559 534
bos@559 535 <!-- &interaction.template.svnstyle.syntax.input; -->
bos@559 536
bos@559 537 <para>Notice that <filename>broken.style</filename> attempts to
bos@559 538 define a <literal>changeset</literal> keyword, but forgets to
bos@559 539 give any content for it. When instructed to use this style
bos@559 540 file, Mercurial promptly complains.</para>
bos@559 541
bos@559 542 <para><!-- &interaction.template.svnstyle.syntax.error;
bos@559 543 --></para>
bos@559 544
bos@559 545 <para>This error message looks intimidating, but it is not too
bos@559 546 hard to follow.</para>
bos@559 547
bos@559 548 <itemizedlist>
bos@559 549 <listitem><para>The first component is simply Mercurial's way
bos@559 550 of saying <quote>I am giving up</quote>.</para>
bos@559 551 <programlisting>___abort___: broken.style:1: parse
bos@559 552 error</programlisting>
bos@559 553 </listitem>
bos@559 554 <listitem><para>Next comes the name of the style file that
bos@559 555 contains the error.</para>
bos@559 556 <programlisting>
bos@559 557 abort: ___broken.style___:1: parse error
bos@559 558 </programlisting>
bos@559 559 </listitem>
bos@559 560 <listitem><para>Following the file name is the line number
bos@559 561 where the error was encountered.</para>
bos@559 562 <programlisting>abort: broken.style:___1___: parse
bos@559 563 error</programlisting>
bos@559 564 </listitem>
bos@559 565 <listitem><para>Finally, a description of what went
bos@559 566 wrong.</para>
bos@559 567 <programlisting>abort: broken.style:1: ___parse
bos@559 568 error___</programlisting>
bos@559 569 </listitem>
bos@559 570 <listitem><para>The description of the problem is not always
bos@559 571 clear (as in this case), but even when it is cryptic, it
bos@559 572 is almost always trivial to visually inspect the offending
bos@559 573 line in the style file and see what is wrong.</para>
bos@559 574 </listitem></itemizedlist>
bos@559 575
bos@559 576 </sect2>
bos@559 577 <sect2>
bos@559 578 <title>Uniquely identifying a repository</title>
bos@559 579
bos@559 580 <para>If you would like to be able to identify a Mercurial
bos@559 581 repository <quote>fairly uniquely</quote> using a short string
bos@559 582 as an identifier, you can use the first revision in the
bos@559 583 repository. <!-- &interaction.template.svnstyle.id; --> This
bos@559 584 is not guaranteed to be unique, but it is nevertheless useful
bos@559 585 in many cases.</para>
bos@559 586 <itemizedlist>
bos@559 587 <listitem><para>It will not work in a completely empty
bos@559 588 repository, because such a repository does not have a
bos@559 589 revision zero.</para>
bos@559 590 </listitem>
bos@559 591 <listitem><para>Neither will it work in the (extremely rare)
bos@559 592 case where a repository is a merge of two or more formerly
bos@559 593 independent repositories, and you still have those
bos@559 594 repositories around.</para>
bos@559 595 </listitem></itemizedlist>
bos@559 596 <para>Here are some uses to which you could put this
bos@559 597 identifier:</para>
bos@559 598 <itemizedlist>
bos@559 599 <listitem><para>As a key into a table for a database that
bos@559 600 manages repositories on a server.</para>
bos@559 601 </listitem>
bos@559 602 <listitem><para>As half of a {<emphasis>repository
bos@559 603 ID</emphasis>, <emphasis>revision ID</emphasis>} tuple.
bos@559 604 Save this information away when you run an automated build
bos@559 605 or other activity, so that you can <quote>replay</quote>
bos@559 606 the build later if necessary.</para>
bos@559 607 </listitem></itemizedlist>
bos@559 608
bos@559 609 </sect2>
bos@559 610 <sect2>
bos@559 611 <title>Mimicking Subversion's output</title>
bos@559 612
bos@559 613 <para>Let's try to emulate the default output format used by
bos@559 614 another revision control tool, Subversion. <!--
bos@559 615 &interaction.template.svnstyle.short; --></para>
bos@559 616
bos@559 617 <para>Since Subversion's output style is fairly simple, it is
bos@559 618 easy to copy-and-paste a hunk of its output into a file, and
bos@559 619 replace the text produced above by Subversion with the
bos@559 620 template values we'd like to see expanded. <!--
bos@559 621 &interaction.template.svnstyle.template; --></para>
bos@559 622
bos@559 623 <para>There are a few small ways in which this template deviates
bos@559 624 from the output produced by Subversion.</para>
bos@559 625 <itemizedlist>
bos@559 626 <listitem><para>Subversion prints a <quote>readable</quote>
bos@559 627 date (the <quote>\texttt{Wed, 27 Sep 2006}</quote> in the
bos@559 628 example output above) in parentheses. Mercurial's
bos@559 629 templating engine does not provide a way to display a date
bos@559 630 in this format without also printing the time and time
bos@559 631 zone.</para>
bos@559 632 </listitem>
bos@559 633 <listitem><para>We emulate Subversion's printing of
bos@559 634 <quote>separator</quote> lines full of
bos@559 635 <quote><literal>-</literal></quote> characters by ending
bos@559 636 the template with such a line. We use the templating
bos@559 637 engine's <literal role="template-keyword">header</literal>
bos@559 638 keyword to print a separator line as the first line of
bos@559 639 output (see below), thus achieving similar output to
bos@559 640 Subversion.</para>
bos@559 641 </listitem>
bos@559 642 <listitem><para>Subversion's output includes a count in the
bos@559 643 header of the number of lines in the commit message. We
bos@559 644 cannot replicate this in Mercurial; the templating engine
bos@559 645 does not currently provide a filter that counts the number
bos@559 646 of lines the template generates.</para>
bos@559 647 </listitem></itemizedlist>
bos@559 648 <para>It took me no more than a minute or two of work to replace
bos@559 649 literal text from an example of Subversion's output with some
bos@559 650 keywords and filters to give the template above. The style
bos@559 651 file simply refers to the template. <!--
bos@559 652 &interaction.template.svnstyle.style; --></para>
bos@559 653
bos@559 654 <para>We could have included the text of the template file
bos@559 655 directly in the style file by enclosing it in quotes and
bos@559 656 replacing the newlines with
bos@559 657 <quote><literal>\n</literal></quote> sequences, but it would
bos@559 658 have made the style file too difficult to read. Readability
bos@559 659 is a good guide when you're trying to decide whether some text
bos@559 660 belongs in a style file, or in a template file that the style
bos@559 661 file points to. If the style file will look too big or
bos@559 662 cluttered if you insert a literal piece of text, drop it into
bos@559 663 a template instead.</para>
bos@559 664
bos@559 665 </sect2>
bos@559 666 </sect1>
bos@559 667 </chapter>
bos@559 668
bos@559 669 <!--
bos@559 670 local variables:
bos@559 671 sgml-parent-document: ("00book.xml" "book" "chapter")
bos@559 672 end:
bos@559 673 -->