| 1 |
<?xml version="1.0" encoding="utf-8"?> |
| 2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 |
<meta name="generator" content="Docutils 0.21.2: https://docutils.sourceforge.io/" /> |
| 7 |
<title>Using Mercurial from scripts and automation</title> |
| 8 |
<link rel="stylesheet" href="../style.css" type="text/css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div class="document" id="using-mercurial-from-scripts-and-automation"> |
| 12 |
<span id="topic-scripting"></span> |
| 13 |
<h1 class="title">Using Mercurial from scripts and automation</h1> |
| 14 |
|
| 15 |
<div class="contents htmlonly topic" id="contents"> |
| 16 |
<p class="topic-title"><a class="reference internal" href="#top">Contents</a></p> |
| 17 |
<ul class="simple"> |
| 18 |
<li><a class="reference internal" href="#choosing-an-interface" id="toc-entry-1">Choosing an Interface</a></li> |
| 19 |
<li><a class="reference internal" href="#environment-variables" id="toc-entry-2">Environment Variables</a></li> |
| 20 |
<li><a class="reference internal" href="#command-line-flags" id="toc-entry-3">Command-line Flags</a></li> |
| 21 |
<li><a class="reference internal" href="#consuming-command-output" id="toc-entry-4">Consuming Command Output</a><ul> |
| 22 |
<li><a class="reference internal" href="#parsing-raw-command-output" id="toc-entry-5">Parsing Raw Command Output</a></li> |
| 23 |
<li><a class="reference internal" href="#using-templates-to-control-output" id="toc-entry-6">Using Templates to Control Output</a></li> |
| 24 |
<li><a class="reference internal" href="#command-server-output" id="toc-entry-7">Command Server Output</a></li> |
| 25 |
<li><a class="reference internal" href="#output-verbosity" id="toc-entry-8">Output Verbosity</a></li> |
| 26 |
</ul> |
| 27 |
</li> |
| 28 |
<li><a class="reference internal" href="#other-topics" id="toc-entry-9">Other Topics</a></li> |
| 29 |
</ul> |
| 30 |
</div> |
| 31 |
<p id="scripting">It is common for machines (as opposed to humans) to consume Mercurial. |
| 32 |
This help topic describes some of the considerations for interfacing |
| 33 |
machines with Mercurial.</p> |
| 34 |
<div class="section" id="choosing-an-interface"> |
| 35 |
<h1><a class="toc-backref" href="#contents">Choosing an Interface</a></h1> |
| 36 |
<p>Machines have a choice of several methods to interface with Mercurial. |
| 37 |
These include:</p> |
| 38 |
<ul class="simple"> |
| 39 |
<li>Executing the <tt class="docutils literal">hg</tt> process</li> |
| 40 |
<li>Querying a HTTP server</li> |
| 41 |
<li>Calling out to a command server</li> |
| 42 |
</ul> |
| 43 |
<p>Executing <tt class="docutils literal">hg</tt> processes is very similar to how humans interact with |
| 44 |
Mercurial in the shell. It should already be familiar to you.</p> |
| 45 |
<p><a class="reference external" href="hg-serve.html"><tt class="docutils literal">hg serve</tt></a> can be used to start a server. By default, this will start |
| 46 |
a "hgweb" HTTP server. This HTTP server has support for machine-readable |
| 47 |
output, such as JSON. For more, see <a class="reference external" href="topic-hgweb.html"><tt class="docutils literal">hg help hgweb</tt></a>.</p> |
| 48 |
<p><a class="reference external" href="hg-serve.html"><tt class="docutils literal">hg serve</tt></a> can also start a "command server." Clients can connect |
| 49 |
to this server and issue Mercurial commands over a special protocol. |
| 50 |
For more details on the command server, including links to client |
| 51 |
libraries, see <a class="reference external" href="https://www.mercurial-scm.org/wiki/CommandServer">https://www.mercurial-scm.org/wiki/CommandServer</a>.</p> |
| 52 |
<p><a class="reference external" href="hg-serve.html"><tt class="docutils literal">hg serve</tt></a> based interfaces (the hgweb and command servers) have the |
| 53 |
advantage over simple <tt class="docutils literal">hg</tt> process invocations in that they are |
| 54 |
likely more efficient. This is because there is significant overhead |
| 55 |
to spawn new Python processes.</p> |
| 56 |
<div class="admonition tip"> |
| 57 |
<p class="first admonition-title">Tip</p> |
| 58 |
<p class="last">If you need to invoke several <tt class="docutils literal">hg</tt> processes in short order and/or |
| 59 |
performance is important to you, use of a server-based interface |
| 60 |
is highly recommended.</p> |
| 61 |
</div> |
| 62 |
</div> |
| 63 |
<div class="section" id="environment-variables"> |
| 64 |
<h1><a class="toc-backref" href="#contents">Environment Variables</a></h1> |
| 65 |
<p>As documented in <a class="reference external" href="topic-environment.html"><tt class="docutils literal">hg help environment</tt></a>, various environment variables |
| 66 |
influence the operation of Mercurial. The following are particularly |
| 67 |
relevant for machines consuming Mercurial:</p> |
| 68 |
<dl class="docutils"> |
| 69 |
<dt>HGPLAIN</dt> |
| 70 |
<dd><p class="first">If not set, Mercurial's output could be influenced by configuration |
| 71 |
settings that impact its encoding, verbose mode, localization, etc.</p> |
| 72 |
<p class="last">It is highly recommended for machines to set this variable when |
| 73 |
invoking <tt class="docutils literal">hg</tt> processes.</p> |
| 74 |
</dd> |
| 75 |
<dt>HGENCODING</dt> |
| 76 |
<dd><p class="first">If not set, the locale used by Mercurial will be detected from the |
| 77 |
environment. If the determined locale does not support display of |
| 78 |
certain characters, Mercurial may render these character sequences |
| 79 |
incorrectly (often by using "?" as a placeholder for invalid |
| 80 |
characters in the current locale).</p> |
| 81 |
<p class="last">Explicitly setting this environment variable is a good practice to |
| 82 |
guarantee consistent results. "utf-8" is a good choice on UNIX-like |
| 83 |
environments.</p> |
| 84 |
</dd> |
| 85 |
<dt>HGRCPATH</dt> |
| 86 |
<dd><p class="first">If not set, Mercurial will inherit config options from config files |
| 87 |
using the process described in <a class="reference external" href="hgrc.5.html"><tt class="docutils literal">hg help config</tt></a>. This includes |
| 88 |
inheriting user or system-wide config files.</p> |
| 89 |
<p class="last">When utmost control over the Mercurial configuration is desired, the |
| 90 |
value of <tt class="docutils literal">HGRCPATH</tt> can be set to an explicit file with known good |
| 91 |
configs. In rare cases, the value can be set to an empty file or the |
| 92 |
null device (often <tt class="docutils literal">/dev/null</tt>) to bypass loading of any user or |
| 93 |
system config files. Note that these approaches can have unintended |
| 94 |
consequences, as the user and system config files often define things |
| 95 |
like the username and extensions that may be required to interface |
| 96 |
with a repository.</p> |
| 97 |
</dd> |
| 98 |
<dt>HGRCSKIPREPO</dt> |
| 99 |
<dd><p class="first">When set, the .hg/hgrc from repositories are not read.</p> |
| 100 |
<p class="last">Note that not reading the repository's configuration can have |
| 101 |
unintended consequences, as the repository config files can define |
| 102 |
things like extensions that are required for access to the |
| 103 |
repository.</p> |
| 104 |
</dd> |
| 105 |
</dl> |
| 106 |
</div> |
| 107 |
<div class="section" id="command-line-flags"> |
| 108 |
<h1><a class="toc-backref" href="#contents">Command-line Flags</a></h1> |
| 109 |
<p>Mercurial's default command-line parser is designed for humans, and is not |
| 110 |
robust against malicious input. For instance, you can start a debugger by |
| 111 |
passing <tt class="docutils literal"><span class="pre">--debugger</span></tt> as an option value:</p> |
| 112 |
<pre class="literal-block"> |
| 113 |
$ REV=--debugger sh -c 'hg log -r "$REV"' |
| 114 |
</pre> |
| 115 |
<p>This happens because several command-line flags need to be scanned without |
| 116 |
using a concrete command table, which may be modified while loading repository |
| 117 |
settings and extensions.</p> |
| 118 |
<p>Since Mercurial 4.4.2, the parsing of such flags may be restricted by setting |
| 119 |
<tt class="docutils literal"><span class="pre">HGPLAIN=+strictflags</span></tt>. When this feature is enabled, all early options |
| 120 |
(e.g. <tt class="docutils literal"><span class="pre">-R/--repository</span></tt>, <tt class="docutils literal"><span class="pre">--cwd</span></tt>, <tt class="docutils literal"><span class="pre">--config</span></tt>) must be specified first |
| 121 |
amongst the other global options, and cannot be injected to an arbitrary |
| 122 |
location:</p> |
| 123 |
<pre class="literal-block"> |
| 124 |
$ HGPLAIN=+strictflags hg -R "$REPO" log -r "$REV" |
| 125 |
</pre> |
| 126 |
<p>In earlier Mercurial versions where <tt class="docutils literal">+strictflags</tt> isn't available, you |
| 127 |
can mitigate the issue by concatenating an option value with its flag:</p> |
| 128 |
<pre class="literal-block"> |
| 129 |
$ hg log -r"$REV" --keyword="$KEYWORD" |
| 130 |
</pre> |
| 131 |
</div> |
| 132 |
<div class="section" id="consuming-command-output"> |
| 133 |
<h1><a class="toc-backref" href="#contents">Consuming Command Output</a></h1> |
| 134 |
<p>It is common for machines to need to parse the output of Mercurial |
| 135 |
commands for relevant data. This section describes the various |
| 136 |
techniques for doing so.</p> |
| 137 |
<div class="section" id="parsing-raw-command-output"> |
| 138 |
<h2><a class="toc-backref" href="#contents">Parsing Raw Command Output</a></h2> |
| 139 |
<p>Likely the simplest and most effective solution for consuming command |
| 140 |
output is to simply invoke <tt class="docutils literal">hg</tt> commands as you would as a user and |
| 141 |
parse their output.</p> |
| 142 |
<p>The output of many commands can easily be parsed with tools like |
| 143 |
<tt class="docutils literal">grep</tt>, <tt class="docutils literal">sed</tt>, and <tt class="docutils literal">awk</tt>.</p> |
| 144 |
<p>A potential downside with parsing command output is that the output |
| 145 |
of commands can change when Mercurial is upgraded. While Mercurial |
| 146 |
does generally strive for strong backwards compatibility, command |
| 147 |
output does occasionally change. Having tests for your automated |
| 148 |
interactions with <tt class="docutils literal">hg</tt> commands is generally recommended, but is |
| 149 |
even more important when raw command output parsing is involved.</p> |
| 150 |
</div> |
| 151 |
<div class="section" id="using-templates-to-control-output"> |
| 152 |
<h2><a class="toc-backref" href="#contents">Using Templates to Control Output</a></h2> |
| 153 |
<p>Many <tt class="docutils literal">hg</tt> commands support templatized output via the |
| 154 |
<tt class="docutils literal"><span class="pre">-T/--template</span></tt> argument. For more, see <a class="reference external" href="hg.1.html#templates"><tt class="docutils literal">hg help templates</tt></a>.</p> |
| 155 |
<p>Templates are useful for explicitly controlling output so that |
| 156 |
you get exactly the data you want formatted how you want it. For |
| 157 |
example, <tt class="docutils literal">log <span class="pre">-T</span> <span class="pre">{node}\n</span></tt> can be used to print a newline |
| 158 |
delimited list of changeset nodes instead of a human-tailored |
| 159 |
output containing authors, dates, descriptions, etc.</p> |
| 160 |
<div class="admonition tip"> |
| 161 |
<p class="first admonition-title">Tip</p> |
| 162 |
<p class="last">If parsing raw command output is too complicated, consider |
| 163 |
using templates to make your life easier.</p> |
| 164 |
</div> |
| 165 |
<p>The <tt class="docutils literal"><span class="pre">-T/--template</span></tt> argument allows specifying pre-defined styles. |
| 166 |
Mercurial ships with the machine-readable styles <tt class="docutils literal">cbor</tt>, <tt class="docutils literal">json</tt>, |
| 167 |
and <tt class="docutils literal">xml</tt>, which provide CBOR, JSON, and XML output, respectively. |
| 168 |
These are useful for producing output that is machine readable as-is.</p> |
| 169 |
<p>(Mercurial 5.0 is required for CBOR style.)</p> |
| 170 |
<div class="admonition important"> |
| 171 |
<p class="first admonition-title">Important</p> |
| 172 |
<p>The <tt class="docutils literal">json</tt> and <tt class="docutils literal">xml</tt> styles are considered experimental. While |
| 173 |
they may be attractive to use for easily obtaining machine-readable |
| 174 |
output, their behavior may change in subsequent versions.</p> |
| 175 |
<p class="last">These styles may also exhibit unexpected results when dealing with |
| 176 |
certain encodings. Mercurial treats things like filenames as a |
| 177 |
series of bytes and normalizing certain byte sequences to JSON |
| 178 |
or XML with certain encoding settings can lead to surprises.</p> |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
<div class="section" id="command-server-output"> |
| 182 |
<h2><a class="toc-backref" href="#contents">Command Server Output</a></h2> |
| 183 |
<p>If using the command server to interact with Mercurial, you are likely |
| 184 |
using an existing library/API that abstracts implementation details of |
| 185 |
the command server. If so, this interface layer may perform parsing for |
| 186 |
you, saving you the work of implementing it yourself.</p> |
| 187 |
</div> |
| 188 |
<div class="section" id="output-verbosity"> |
| 189 |
<h2><a class="toc-backref" href="#contents">Output Verbosity</a></h2> |
| 190 |
<p>Commands often have varying output verbosity, even when machine |
| 191 |
readable styles are being used (e.g. <tt class="docutils literal"><span class="pre">-T</span> json</tt>). Adding |
| 192 |
<tt class="docutils literal"><span class="pre">-v/--verbose</span></tt> and <tt class="docutils literal"><span class="pre">--debug</span></tt> to the command's arguments can |
| 193 |
increase the amount of data exposed by Mercurial.</p> |
| 194 |
<p>An alternate way to get the data you need is by explicitly specifying |
| 195 |
a template.</p> |
| 196 |
</div> |
| 197 |
</div> |
| 198 |
<div class="section" id="other-topics"> |
| 199 |
<h1><a class="toc-backref" href="#contents">Other Topics</a></h1> |
| 200 |
<dl class="docutils"> |
| 201 |
<dt>revsets</dt> |
| 202 |
<dd><p class="first">Revisions sets is a functional query language for selecting a set |
| 203 |
of revisions. Think of it as SQL for Mercurial repositories. Revsets |
| 204 |
are useful for querying repositories for specific data.</p> |
| 205 |
<p class="last">See <a class="reference external" href="hg.1.html#revsets"><tt class="docutils literal">hg help revsets</tt></a> for more.</p> |
| 206 |
</dd> |
| 207 |
<dt>share extension</dt> |
| 208 |
<dd><p class="first">The <tt class="docutils literal">share</tt> extension provides functionality for sharing |
| 209 |
repository data across several working copies. It can even |
| 210 |
automatically "pool" storage for logically related repositories when |
| 211 |
cloning.</p> |
| 212 |
<p>Configuring the <tt class="docutils literal">share</tt> extension can lead to significant resource |
| 213 |
utilization reduction, particularly around disk space and the |
| 214 |
network. This is especially true for continuous integration (CI) |
| 215 |
environments.</p> |
| 216 |
<p class="last">See <a class="reference external" href="hg.1.html#-e"><tt class="docutils literal">hg help <span class="pre">-e</span> share</tt></a> for more.</p> |
| 217 |
</dd> |
| 218 |
</dl> |
| 219 |
</div> |
| 220 |
</div> |
| 221 |
</body> |
| 222 |
</html> |