Mercurial_ The Definitive Guide - Bryan O'Sullivan [85]
that some might consider it so) of goodbye.
[59ae2fd35d8a] [mytag]
* hello:
added hello
[6ed500684dd0]
You will not be shocked to learn that Mercurial’s default output style is named default.
Setting a Default Style
You can modify the output style that Mercurial uses for every command by editing your ~/.hgrc file and naming the style you would prefer to use.
[ui]
style = compact
If you write a style of your own, you can use it by either providing the path to your style file, or copying your style file into a location where Mercurial can find it (typically the templates subdirectory of your Mercurial install directory).
Commands That Support Styles and Templates
All of Mercurial’s “log-like” commands let you use styles and templates: hg incoming, hg log, hg outgoing, and hg tip.
As of this writing, these are the only commands that support styles and templates. Since these are the most important commands that need customizable output, there has been little pressure from the Mercurial user community to add style and template support to other commands.
The Basics of Templating
At its simplest, a Mercurial template is a piece of text. Some of the text never changes, while other parts are expanded, or replaced with new text, when necessary.
Before we continue, let’s look again at a simple example of Mercurial’s normal output.
$ hg log -r1
changeset: 1:59ae2fd35d8a
tag: mytag
user: Bryan O'Sullivan date: Tue May 05 06:44:45 2009 +0000 summary: added line to end of < Now, let’s run the same command, but using a template to change its output. $ hg log -r1 --template 'i saw a changeset\n' i saw a changeset The example above illustrates the simplest possible template; it’s just a piece of static text, printed once for each changeset. The --template option to the hg log command tells Mercurial to use the given text as the template when printing each changeset. Notice that the template string above ends with the text \n. This is an escape sequence, telling Mercurial to print a newline at the end of each template item. If you omit this newline, Mercurial will run each piece of output together. See Escape Sequences for more details. A template that prints a fixed string of text all the time isn’t very useful; let’s try something a bit more complex. $ hg log --template 'i saw a changeset: {desc}\n' i saw a changeset: Added tag v0.1 for changeset cefd14841d41 i saw a changeset: Added tag mytag for changeset 59ae2fd35d8a i saw a changeset: added line to end of < in addition, added a file with the helpful name (at least i hope that some might consider it so) of goodbye. i saw a changeset: added hello As you can see, the string {desc} in the template has been replaced in the output with the description of each changeset. Every time Mercurial finds text enclosed in curly braces ({ and }), it will try to replace the braces and text with the expansion of whatever is inside. To print a literal curly brace, you must escape it, as described in Escape Sequences. Common Template Keywords You can start writing simple templates immediately using the keywords below: author: String. The unmodified author of the changeset. branches: String. The name of the branch on which the changeset was committed. Will be empty if the branch name was default. date: Date information. The date when the changeset was committed. This is not human-readable; you must pass it through a filter that will render it appropriately. See Filtering Keywords to Change Their Results for more information on filters. The date is expressed as a pair of numbers. The first number is a Unix UTC timestamp (seconds since January 1, 1970); the second is the offset of the committer’s timezone from UTC, in seconds. desc: String. The text of the changeset description. files: List of strings. All files modified, added, or removed by this changeset. file_adds: List of strings. Files added by this changeset. file_dels: List of strings. Files removed by