bos@80: \chapter{Customising the output of Mercurial}
bos@80: \label{chap:template}
bos@80:
bos@80: Mercurial provides a powerful mechanism to let you control how it
bos@80: displays information. The mechanism is based on templates. You can
bos@80: use templates to generate specific output for a single command, or to
bos@80: customise the entire appearance of the built-in web interface.
bos@80:
bos@80: \section{Using precanned output styles}
bos@80: \label{sec:style}
bos@80:
bos@80: Packaged with Mercurial are some output styles that you can use
bos@80: immediately. A style is simply a precanned template that someone
bos@83: wrote and installed somewhere that Mercurial can find.
bos@80:
bos@80: Before we take a look at Mercurial's bundled styles, let's review its
bos@80: normal output.
bos@80:
bos@80: \interaction{template.simple.normal}
bos@80:
bos@80: This is somewhat informative, but it takes up a lot of space---five
bos@80: lines of output per changeset. The \texttt{compact} style reduces
bos@80: this to three lines, presented in a sparse manner.
bos@80:
bos@80: \interaction{template.simple.compact}
bos@80:
bos@80: The \texttt{changelog} style hints at the expressive power of
bos@80: Mercurial's templating engine. This style attempts to follow the GNU
bos@80: Project's changelog guidelines\cite{web:changelog}.
bos@80:
bos@80: \interaction{template.simple.changelog}
bos@80:
bos@80: You will not be shocked to learn that Mercurial's default output style
bos@80: is named \texttt{default}.
bos@80:
bos@83: \subsection{Setting a default style}
bos@80:
bos@80: You can modify the output style that Mercurial will use for every
bos@80: command by editing your \hgrc\ file, naming the style you would
bos@80: prefer to use.
bos@80:
bos@80: \begin{codesample2}
bos@80: [ui]
bos@80: style = compact
bos@80: \end{codesample2}
bos@80:
bos@80: If you write a style of your own, you can use it by either providing
bos@80: the path to your style file, or copying your style file into a
bos@80: location where Mercurial can find it (typically the \texttt{templates}
bos@80: subdirectory of your Mercurial install directory).
bos@80:
bos@80: \section{Commands that support styles and templates}
bos@80:
bos@80: All of Mercurial's ``\texttt{log}-like'' commands let you use styles
bos@80: and templates: \hgcmd{incoming}, \hgcmd{log}, \hgcmd{outgoing}, and
bos@80: \hgcmd{tip}.
bos@80:
bos@80: As I write this manual, these are so far the only commands that
bos@80: support styles and templates. Since these are the most important
bos@80: commands that need customisable output, there has been little pressure
bos@80: from the Mercurial user community to add style and template support to
bos@80: other commands.
bos@80:
bos@80: \section{The basics of templating}
bos@80:
bos@80: At its simplest, a Mercurial template is a piece of text. Some of the
bos@80: text never changes, while other parts are \emph{expanded}, or replaced
bos@80: with new text, when necessary.
bos@80:
bos@80: Before we continue, let's look again at a simple example of
bos@80: Mercurial's normal output.
bos@80:
bos@80: \interaction{template.simple.normal}
bos@80:
bos@80: Now, let's run the same command, but using a template to change its
bos@80: output.
bos@80:
bos@80: \interaction{template.simple.simplest}
bos@80:
bos@80: The example above illustrates the simplest possible template; it's
bos@80: just a piece of static text, printed once for each changeset. The
bos@80: \hgopt{log}{--template} option to the \hgcmd{log} command tells
bos@80: Mercurial to use the given text as the template when printing each
bos@80: changeset.
bos@80:
bos@80: Notice that the template string above ends with the text
bos@80: ``\Verb+\n+''. This is an \emph{escape sequence}, telling Mercurial
bos@80: to print a newline at the end of each template item. If you omit this
bos@80: newline, Mercurial will run each piece of output together. See
bos@80: section~\ref{sec:template:escape} for more details of escape sequences.
bos@80:
bos@80: A template that prints a fixed string of text all the time isn't very
bos@80: useful; let's try something a bit more complex.
bos@80:
bos@80: \interaction{template.simple.simplesub}
bos@80:
bos@80: As you can see, the string ``\Verb+{desc}+'' in the template has been
bos@80: replaced in the output with the description of each changeset. Every
bos@80: time Mercurial finds text enclosed in curly braces (``\texttt{\{}''
bos@80: and ``\texttt{\}}''), it will try to replace the braces and text with
bos@80: the expansion of whatever is inside. To print a literal curly brace,
bos@80: you must escape it, as described in section~\ref{sec:template:escape}.
bos@80:
bos@83: \section{Common template keywords}
bos@80: \label{sec:template:keyword}
bos@80:
bos@80: You can start writing simple templates immediately using the keywords
bos@80: below.
bos@80:
bos@80: \begin{itemize}
bos@80: \item[\tplkword{author}] String. The unmodified author of the changeset.
bos@208: \item[\tplkword{branches}] String. The name of the branch on which
bos@208: the changeset was committed. Will be empty if the branch name was
bos@208: \texttt{default}.
bos@80: \item[\tplkword{date}] Date information. The date when the changeset
bos@80: was committed. This is \emph{not} human-readable; you must pass it
bos@80: through a filter that will render it appropriately. See
bos@80: section~\ref{sec:template:filter} for more information on filters.
bos@80: The date is expressed as a pair of numbers. The first number is a
bos@80: Unix UTC timestamp (seconds since January 1, 1970); the second is
bos@80: the offset of the committer's timezone from UTC, in seconds.
bos@80: \item[\tplkword{desc}] String. The text of the changeset description.
bos@80: \item[\tplkword{files}] List of strings. All files modified, added, or
bos@80: removed by this changeset.
bos@80: \item[\tplkword{file\_adds}] List of strings. Files added by this
bos@80: changeset.
bos@80: \item[\tplkword{file\_dels}] List of strings. Files removed by this
bos@80: changeset.
bos@80: \item[\tplkword{node}] String. The changeset identification hash, as a
bos@80: 40-character hexadecimal string.
bos@80: \item[\tplkword{parents}] List of strings. The parents of the
bos@80: changeset.
bos@80: \item[\tplkword{rev}] Integer. The repository-local changeset revision
bos@80: number.
bos@80: \item[\tplkword{tags}] List of strings. Any tags associated with the
bos@80: changeset.
bos@80: \end{itemize}
bos@80:
bos@80: A few simple experiments will show us what to expect when we use these
bos@80: keywords; you can see the results in
bos@80: figure~\ref{fig:template:keywords}.
bos@80:
bos@80: \begin{figure}
bos@80: \interaction{template.simple.keywords}
bos@80: \caption{Template keywords in use}
bos@80: \label{fig:template:keywords}
bos@80: \end{figure}
bos@80:
bos@80: As we noted above, the date keyword does not produce human-readable
bos@80: output, so we must treat it specially. This involves using a
bos@80: \emph{filter}, about which more in section~\ref{sec:template:filter}.
bos@80:
bos@80: \interaction{template.simple.datekeyword}
bos@80:
bos@80: \section{Escape sequences}
bos@80: \label{sec:template:escape}
bos@80:
bos@80: Mercurial's templating engine recognises the most commonly used escape
bos@80: sequences in strings. When it sees a backslash (``\Verb+\+'')
bos@80: character, it looks at the following character and substitutes the two
bos@80: characters with a single replacement, as described below.
bos@80:
bos@80: \begin{itemize}
bos@80: \item[\Verb+\textbackslash\textbackslash+] Backslash, ``\Verb+\+'',
bos@80: ASCII~134.
bos@80: \item[\Verb+\textbackslash n+] Newline, ASCII~12.
bos@80: \item[\Verb+\textbackslash r+] Carriage return, ASCII~15.
bos@80: \item[\Verb+\textbackslash t+] Tab, ASCII~11.
bos@80: \item[\Verb+\textbackslash v+] Vertical tab, ASCII~13.
bos@80: \item[\Verb+\textbackslash \{+] Open curly brace, ``\Verb+{+'', ASCII~173.
bos@80: \item[\Verb+\textbackslash \}+] Close curly brace, ``\Verb+}+'', ASCII~175.
bos@80: \end{itemize}
bos@80:
bos@80: As indicated above, if you want the expansion of a template to contain
bos@80: a literal ``\Verb+\+'', ``\Verb+{+'', or ``\Verb+{+'' character, you
bos@80: must escape it.
bos@80:
bos@83: \section{Filtering keywords to change their results}
bos@80: \label{sec:template:filter}
bos@80:
bos@83: Some of the results of template expansion are not immediately easy to
bos@80: use. Mercurial lets you specify an optional chain of \emph{filters}
bos@80: to modify the result of expanding a keyword. You have already seen a
bos@80: common filter, \tplkwfilt{date}{isodate}, in action above, to make a
bos@80: date readable.
bos@80:
bos@83: Below is a list of the most commonly used filters that Mercurial
bos@83: supports. While some filters can be applied to any text, others can
bos@83: only be used in specific circumstances. The name of each filter is
bos@83: followed first by an indication of where it can be used, then a
bos@83: description of its effect.
bos@83:
bos@80: \begin{itemize}
bos@80: \item[\tplfilter{addbreaks}] Any text. Add an XHTML ``\Verb+
+''
bos@80: tag before the end of every line except the last. For example,
bos@80: ``\Verb+foo\nbar+'' becomes ``\Verb+foo
\nbar+''.
bos@80: \item[\tplkwfilt{date}{age}] \tplkword{date} keyword. Render the
bos@80: age of the date, relative to the current time. Yields a string like
bos@80: ``\Verb+10 minutes+''.
bos@80: \item[\tplfilter{basename}] Any text, but most useful for the
bos@80: \tplkword{files} keyword and its relatives. Treat the text as a
bos@80: path, and return the basename. For example, ``\Verb+foo/bar/baz+''
bos@80: becomes ``\Verb+baz+''.
bos@80: \item[\tplkwfilt{date}{date}] \tplkword{date} keyword. Render a date
bos@80: in a similar format to the Unix \tplkword{date} command, but with
bos@80: timezone included. Yields a string like
bos@80: ``\Verb+Mon Sep 04 15:13:13 2006 -0700+''.
bos@80: \item[\tplkwfilt{author}{domain}] Any text, but most useful for the
bos@80: \tplkword{author} keyword. Finds the first string that looks like
bos@80: an email address, and extract just the domain component. For
bos@80: example, ``\Verb+Bryan O'Sullivan +'' becomes
bos@80: ``\Verb+serpentine.com+''.
bos@80: \item[\tplkwfilt{author}{email}] Any text, but most useful for the
bos@80: \tplkword{author} keyword. Extract the first string that looks like
bos@80: an email address. For example,
bos@80: ``\Verb+Bryan O'Sullivan +'' becomes
bos@80: ``\Verb+bos@serpentine.com+''.
bos@80: \item[\tplfilter{escape}] Any text. Replace the special XML/XHTML
bos@80: characters ``\Verb+&+'', ``\Verb+<+'' and ``\Verb+>+'' with
bos@80: XML entities.
bos@80: \item[\tplfilter{fill68}] Any text. Wrap the text to fit in 68
bos@80: columns. This is useful before you pass text through the
bos@80: \tplfilter{tabindent} filter, and still want it to fit in an
bos@80: 80-column fixed-font window.
bos@80: \item[\tplfilter{fill76}] Any text. Wrap the text to fit in 76
bos@80: columns.
bos@80: \item[\tplfilter{firstline}] Any text. Yield the first line of text,
bos@80: without any trailing newlines.
bos@80: \item[\tplkwfilt{date}{hgdate}] \tplkword{date} keyword. Render the
bos@80: date as a pair of readable numbers. Yields a string like
bos@80: ``\Verb+1157407993 25200+''.
bos@80: \item[\tplkwfilt{date}{isodate}] \tplkword{date} keyword. Render the
bos@80: date as a text string in ISO~8601 format. Yields a string like
bos@80: ``\Verb+2006-09-04 15:13:13 -0700+''.
bos@80: \item[\tplfilter{obfuscate}] Any text, but most useful for the
bos@80: \tplkword{author} keyword. Yield the input text rendered as a
bos@80: sequence of XML entities. This helps to defeat some particularly
bos@80: stupid screen-scraping email harvesting spambots.
bos@80: \item[\tplkwfilt{author}{person}] Any text, but most useful for the
bos@80: \tplkword{author} keyword. Yield the text before an email address.
bos@80: For example, ``\Verb+Bryan O'Sullivan +''
bos@80: becomes ``\Verb+Bryan O'Sullivan+''.
bos@80: \item[\tplkwfilt{date}{rfc822date}] \tplkword{date} keyword. Render a
bos@80: date using the same format used in email headers. Yields a string
bos@80: like ``\Verb+Mon, 04 Sep 2006 15:13:13 -0700+''.
bos@80: \item[\tplkwfilt{node}{short}] Changeset hash. Yield the short form
bos@80: of a changeset hash, i.e.~a 12-byte hexadecimal string.
bos@80: \item[\tplkwfilt{date}{shortdate}] \tplkword{date} keyword. Render
bos@80: the year, month, and day of the date. Yields a string like
bos@80: ``\Verb+2006-09-04+''.
bos@80: \item[\tplfilter{strip}] Any text. Strip all leading and trailing
bos@80: whitespace from the string.
bos@80: \item[\tplfilter{tabindent}] Any text. Yield the text, with every line
bos@80: except the first starting with a tab character.
bos@80: \item[\tplfilter{urlescape}] Any text. Escape all characters that are
bos@80: considered ``special'' by URL parsers. For example, \Verb+foo bar+
bos@80: becomes \Verb+foo%20bar+.
bos@80: \item[\tplkwfilt{author}{user}] Any text, but most useful for the
bos@80: \tplkword{author} keyword. Return the ``user'' portion of an email
bos@80: address. For example,
bos@80: ``\Verb+Bryan O'Sullivan +'' becomes
bos@80: ``\Verb+bos+''.
bos@80: \end{itemize}
bos@80:
bos@80: \begin{figure}
bos@80: \interaction{template.simple.manyfilters}
bos@80: \caption{Template filters in action}
bos@80: \label{fig:template:filters}
bos@80: \end{figure}
bos@80:
bos@80: \begin{note}
bos@80: If you try to apply a filter to a piece of data that it cannot
bos@80: process, Mercurial will fail and print a Python exception. For
bos@80: example, trying to run the output of the \tplkword{desc} keyword
bos@80: into the \tplkwfilt{date}{isodate} filter is not a good idea.
bos@80: \end{note}
bos@80:
bos@80: \subsection{Combining filters}
bos@80:
bos@80: It is easy to combine filters to yield output in the form you would
bos@80: like. The following chain of filters tidies up a description, then
bos@80: makes sure that it fits cleanly into 68 columns, then indents it by a
bos@80: further 8~characters (at least on Unix-like systems, where a tab is
bos@80: conventionally 8~characters wide).
bos@80:
bos@80: \interaction{template.simple.combine}
bos@80:
bos@80: Note the use of ``\Verb+\t+'' (a tab character) in the template to
bos@80: force the first line to be indented; this is necessary since
bos@80: \tplkword{tabindent} indents all lines \emph{except} the first.
bos@80:
bos@83: Keep in mind that the order of filters in a chain is significant. The
bos@83: first filter is applied to the result of the keyword; the second to
bos@83: the result of the first filter; and so on. For example, using
bos@83: \Verb+fill68|tabindent+ gives very different results from
bos@80: \Verb+tabindent|fill68+.
bos@76:
bos@83:
bos@83: \section{From templates to styles}
bos@83:
bos@83: A command line template provides a quick and simple way to format some
bos@83: output. Templates can become verbose, though, and it's useful to be
bos@83: able to give a template a name. A style file is a template with a
bos@83: name, stored in a file.
bos@83:
bos@83: More than that, using a style file unlocks the power of Mercurial's
bos@83: templating engine in ways that are not possible using the command line
bos@83: \hgopt{log}{--template} option.
bos@83:
bos@83: \subsection{The simplest of style files}
bos@83:
bos@83: Our simple style file contains just one line:
bos@83:
bos@83: \interaction{template.simple.rev}
bos@83:
bos@83: This tells Mercurial, ``if you're printing a changeset, use the text
bos@83: on the right as the template''.
bos@83:
bos@83: \subsection{Style file syntax}
bos@83:
bos@83: The syntax rules for a style file are simple.
bos@83:
bos@83: \begin{itemize}
bos@83: \item The file is processed one line at a time.
bos@83:
bos@83: \item Leading and trailing white space are ignored.
bos@83:
bos@83: \item Empty lines are skipped.
bos@83:
bos@83: \item If a line starts with either of the characters ``\texttt{\#}'' or
bos@83: ``\texttt{;}'', the entire line is treated as a comment, and skipped
bos@83: as if empty.
bos@83:
bos@83: \item A line starts with a keyword. This must start with an
bos@83: alphabetic character or underscore, and can subsequently contain any
bos@83: alphanumeric character or underscore. (In regexp notation, a
bos@83: keyword must match \Verb+[A-Za-z_][A-Za-z0-9_]*+.)
bos@83:
bos@83: \item The next element must be an ``\texttt{=}'' character, which can
bos@83: be preceded or followed by an arbitrary amount of white space.
bos@83:
bos@83: \item If the rest of the line starts and ends with matching quote
bos@83: characters (either single or double quote), it is treated as a
bos@83: template body.
bos@83:
bos@83: \item If the rest of the line \emph{does not} start with a quote
bos@83: character, it is treated as the name of a file; the contents of this
bos@83: file will be read and used as a template body.
bos@83: \end{itemize}
bos@83:
bos@83: \section{Style files by example}
bos@83:
bos@83: To illustrate how to write a style file, we will construct a few by
bos@83: example. Rather than provide a complete style file and walk through
bos@83: it, we'll mirror the usual process of developing a style file by
bos@83: starting with something very simple, and walking through a series of
bos@83: successively more complete examples.
bos@83:
bos@83: \subsection{Identifying mistakes in style files}
bos@83:
bos@83: If Mercurial encounters a problem in a style file you are working on,
bos@83: it prints a terse error message that, once you figure out what it
bos@83: means, is actually quite useful.
bos@83:
bos@83: \interaction{template.svnstyle.syntax.input}
bos@83:
bos@83: Notice that \filename{broken.style} attempts to define a
bos@83: \texttt{changeset} keyword, but forgets to give any content for it.
bos@83: When instructed to use this style file, Mercurial promptly complains.
bos@83:
bos@83: \interaction{template.svnstyle.syntax.error}
bos@83:
bos@83: This error message looks intimidating, but it is not too hard to
bos@83: follow.
bos@83:
bos@83: \begin{itemize}
bos@83: \item The first component is simply Mercurial's way of saying ``I am
bos@83: giving up''.
bos@83: \begin{codesample4}
bos@83: \textbf{abort:} broken.style:1: parse error
bos@83: \end{codesample4}
bos@83:
bos@83: \item Next comes the name of the style file that contains the error.
bos@83: \begin{codesample4}
bos@83: abort: \textbf{broken.style}:1: parse error
bos@83: \end{codesample4}
bos@83:
bos@83: \item Following the file name is the line number where the error was
bos@83: encountered.
bos@83: \begin{codesample4}
bos@83: abort: broken.style:\textbf{1}: parse error
bos@83: \end{codesample4}
bos@83:
bos@83: \item Finally, a description of what went wrong.
bos@83: \begin{codesample4}
bos@83: abort: broken.style:1: \textbf{parse error}
bos@83: \end{codesample4}
bos@83: The description of the problem is not always clear (as in this
bos@83: case), but even when it is cryptic, it is almost always trivial to
bos@83: visually inspect the offending line in the style file and see what
bos@83: is wrong.
bos@83: \end{itemize}
bos@83:
bos@83: \subsection{Uniquely identifying a repository}
bos@83:
bos@83: If you would like to be able to identify a Mercurial repository
bos@83: ``fairly uniquely'' using a short string as an identifier, you can
bos@83: use the first revision in the repository.
bos@83: \interaction{template.svnstyle.id}
bos@83: This is not guaranteed to be unique, but it is nevertheless useful in
bos@83: many cases.
bos@83: \begin{itemize}
bos@83: \item It will not work in a completely empty repository, because such
bos@83: a repository does not have a revision~zero.
bos@83: \item Neither will it work in the (extremely rare) case where a
bos@83: repository is a merge of two or more formerly independent
bos@83: repositories, and you still have those repositories around.
bos@83: \end{itemize}
bos@83: Here are some uses to which you could put this identifier:
bos@83: \begin{itemize}
bos@83: \item As a key into a table for a database that manages repositories
bos@83: on a server.
bos@83: \item As half of a \{\emph{repository~ID}, \emph{revision~ID}\} tuple.
bos@83: Save this information away when you run an automated build or other
bos@83: activity, so that you can ``replay'' the build later if necessary.
bos@83: \end{itemize}
bos@83:
bos@83: \subsection{Mimicking Subversion's output}
bos@83:
bos@83: Let's try to emulate the default output format used by another
bos@83: revision control tool, Subversion.
bos@83: \interaction{template.svnstyle.short}
bos@83:
bos@83: Since Subversion's output style is fairly simple, it is easy to
bos@83: copy-and-paste a hunk of its output into a file, and replace the text
bos@83: produced above by Subversion with the template values we'd like to see
bos@83: expanded.
bos@83: \interaction{template.svnstyle.template}
bos@83:
bos@83: There are a few small ways in which this template deviates from the
bos@83: output produced by Subversion.
bos@83: \begin{itemize}
bos@83: \item Subversion prints a ``readable'' date (the ``\texttt{Wed, 27 Sep
bos@83: 2006}'' in the example output above) in parentheses. Mercurial's
bos@83: templating engine does not provide a way to display a date in this
bos@83: format without also printing the time and time zone.
bos@83: \item We emulate Subversion's printing of ``separator'' lines full of
bos@83: ``\texttt{-}'' characters by ending the template with such a line.
bos@83: We use the templating engine's \tplkword{header} keyword to print a
bos@83: separator line as the first line of output (see below), thus
bos@83: achieving similar output to Subversion.
bos@83: \item Subversion's output includes a count in the header of the number
bos@83: of lines in the commit message. We cannot replicate this in
bos@83: Mercurial; the templating engine does not currently provide a filter
bos@83: that counts the number of items it is passed.
bos@83: \end{itemize}
bos@83: It took me no more than a minute or two of work to replace literal
bos@83: text from an example of Subversion's output with some keywords and
bos@83: filters to give the template above. The style file simply refers to
bos@83: the template.
bos@83: \interaction{template.svnstyle.style}
bos@83:
bos@83: We could have included the text of the template file directly in the
bos@83: style file by enclosing it in quotes and replacing the newlines with
bos@83: ``\texttt{\\n}'' sequences, but it would have made the style file too
bos@83: difficult to read. Readability is a good guide when you're trying to
bos@83: decide whether some text belongs in a style file, or in a template
bos@83: file that the style file points to. If the style file will look too
bos@83: big or cluttered if you insert a literal piece of text, drop it into a
bos@83: template instead.
bos@83:
bos@76: %%% Local Variables:
bos@76: %%% mode: latex
bos@76: %%% TeX-master: "00book"
bos@76: %%% End: