| 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>fix</title> |
| 8 |
<link rel="stylesheet" href="../style.css" type="text/css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div class="document" id="fix"> |
| 12 |
<span id="ext-fix"></span> |
| 13 |
<h1 class="title">fix</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="#description" id="toc-entry-1">Description</a></li> |
| 19 |
<li><a class="reference internal" href="#commands" id="toc-entry-2">Commands</a><ul> |
| 20 |
<li><a class="reference internal" href="#file-content-management" id="toc-entry-3">File content management</a></li> |
| 21 |
</ul> |
| 22 |
</li> |
| 23 |
</ul> |
| 24 |
</div> |
| 25 |
<p>rewrite file content in changesets or working copy (EXPERIMENTAL)</p> |
| 26 |
<div class="section" id="description"> |
| 27 |
<h1><a class="toc-backref" href="#contents">Description</a></h1> |
| 28 |
<p>Provides a command that runs configured tools on the contents of modified files, |
| 29 |
writing back any fixes to the working copy or replacing changesets.</p> |
| 30 |
<p>Fixer tools are run in the repository's root directory. This allows them to read |
| 31 |
configuration files from the working copy, or even write to the working copy. |
| 32 |
The working copy is not updated to match the revision being fixed. In fact, |
| 33 |
several revisions may be fixed in parallel. Writes to the working copy are not |
| 34 |
amended into the revision being fixed; fixer tools MUST always read content to |
| 35 |
be fixed from stdin, and write fixed file content back to stdout.</p> |
| 36 |
<p>Here is an example configuration that causes <a class="reference external" href="hg-fix.html"><tt class="docutils literal">hg fix</tt></a> to apply automatic |
| 37 |
formatting fixes to modified lines in C++ code:</p> |
| 38 |
<pre class="literal-block"> |
| 39 |
[fix] |
| 40 |
clang-format:command=clang-format --assume-filename={rootpath} |
| 41 |
clang-format:linerange=--lines={first}:{last} |
| 42 |
clang-format:pattern=set:**.cpp or **.hpp |
| 43 |
</pre> |
| 44 |
<p>The :command suboption forms the first part of the shell command that will be |
| 45 |
used to fix a file. The content of the file is passed on standard input, and the |
| 46 |
fixed file content is expected on standard output. Any output on standard error |
| 47 |
will be displayed as a warning. If the exit status is not zero, the file will |
| 48 |
not be affected. A placeholder warning is displayed if there is a non-zero exit |
| 49 |
status but no standard error output. Some values may be substituted into the |
| 50 |
command:</p> |
| 51 |
<pre class="literal-block"> |
| 52 |
{rootpath} The path of the file being fixed, relative to the repo root |
| 53 |
{basename} The name of the file being fixed, without the directory path |
| 54 |
</pre> |
| 55 |
<p>If the :linerange suboption is set, the tool will only be run if there are |
| 56 |
changed lines in a file. The value of this suboption is appended to the shell |
| 57 |
command once for every range of changed lines in the file. Some values may be |
| 58 |
substituted into the command:</p> |
| 59 |
<pre class="literal-block"> |
| 60 |
{first} The 1-based line number of the first line in the modified range |
| 61 |
{last} The 1-based line number of the last line in the modified range |
| 62 |
</pre> |
| 63 |
<p>Deleted sections of a file will be ignored by :linerange, because there is no |
| 64 |
corresponding line range in the version being fixed.</p> |
| 65 |
<p>By default, tools that set :linerange will only be executed if there is at least |
| 66 |
one changed line range. This is meant to prevent accidents like running a code |
| 67 |
formatter in such a way that it unexpectedly reformats the whole file. If such a |
| 68 |
tool needs to operate on unchanged files, it should set the :skipclean suboption |
| 69 |
to false.</p> |
| 70 |
<p>The :pattern suboption determines which files will be passed through each |
| 71 |
configured tool. See <a class="reference external" href="topic-patterns.html"><tt class="docutils literal">hg help patterns</tt></a> for possible values. However, all |
| 72 |
patterns are relative to the repo root, even if that text says they are relative |
| 73 |
to the current working directory. If there are file arguments to <a class="reference external" href="hg-fix.html"><tt class="docutils literal">hg fix</tt></a>, the |
| 74 |
intersection of these patterns is used.</p> |
| 75 |
<p>There is also a configurable limit for the maximum size of file that will be |
| 76 |
processed by <a class="reference external" href="hg-fix.html"><tt class="docutils literal">hg fix</tt></a>:</p> |
| 77 |
<pre class="literal-block"> |
| 78 |
[fix] |
| 79 |
maxfilesize = 2MB |
| 80 |
</pre> |
| 81 |
<p>Normally, execution of configured tools will continue after a failure (indicated |
| 82 |
by a non-zero exit status). It can also be configured to abort after the first |
| 83 |
such failure, so that no files will be affected if any tool fails. This abort |
| 84 |
will also cause <a class="reference external" href="hg-fix.html"><tt class="docutils literal">hg fix</tt></a> to exit with a non-zero status:</p> |
| 85 |
<pre class="literal-block"> |
| 86 |
[fix] |
| 87 |
failure = abort |
| 88 |
</pre> |
| 89 |
<p>When multiple tools are configured to affect a file, they execute in an order |
| 90 |
defined by the :priority suboption. The priority suboption has a default value |
| 91 |
of zero for each tool. Tools are executed in order of descending priority. The |
| 92 |
execution order of tools with equal priority is unspecified. For example, you |
| 93 |
could use the 'sort' and 'head' utilities to keep only the 10 smallest numbers |
| 94 |
in a text file by ensuring that 'sort' runs before 'head':</p> |
| 95 |
<pre class="literal-block"> |
| 96 |
[fix] |
| 97 |
sort:command = sort -n |
| 98 |
head:command = head -n 10 |
| 99 |
sort:pattern = numbers.txt |
| 100 |
head:pattern = numbers.txt |
| 101 |
sort:priority = 2 |
| 102 |
head:priority = 1 |
| 103 |
</pre> |
| 104 |
<p>To account for changes made by each tool, the line numbers used for incremental |
| 105 |
formatting are recomputed before executing the next tool. So, each tool may see |
| 106 |
different values for the arguments added by the :linerange suboption.</p> |
| 107 |
<p>Each fixer tool is allowed to return some metadata in addition to the fixed file |
| 108 |
content. The metadata must be placed before the file content on stdout, |
| 109 |
separated from the file content by a zero byte. The metadata is parsed as a JSON |
| 110 |
value (so, it should be UTF-8 encoded and contain no zero bytes). A fixer tool |
| 111 |
is expected to produce this metadata encoding if and only if the :metadata |
| 112 |
suboption is true:</p> |
| 113 |
<pre class="literal-block"> |
| 114 |
[fix] |
| 115 |
tool:command = tool --prepend-json-metadata |
| 116 |
tool:metadata = true |
| 117 |
</pre> |
| 118 |
<p>The metadata values are passed to hooks, which can be used to print summaries or |
| 119 |
perform other post-fixing work. The supported hooks are:</p> |
| 120 |
<pre class="literal-block"> |
| 121 |
"postfixfile" |
| 122 |
Run once for each file in each revision where any fixer tools made changes |
| 123 |
to the file content. Provides "$HG_REV" and "$HG_PATH" to identify the file, |
| 124 |
and "$HG_METADATA" with a map of fixer names to metadata values from fixer |
| 125 |
tools that affected the file. Fixer tools that didn't affect the file have a |
| 126 |
value of None. Only fixer tools that executed are present in the metadata. |
| 127 |
|
| 128 |
"postfix" |
| 129 |
Run once after all files and revisions have been handled. Provides |
| 130 |
"$HG_REPLACEMENTS" with information about what revisions were created and |
| 131 |
made obsolete. Provides a boolean "$HG_WDIRWRITTEN" to indicate whether any |
| 132 |
files in the working copy were updated. Provides a list "$HG_METADATA" |
| 133 |
mapping fixer tool names to lists of metadata values returned from |
| 134 |
executions that modified a file. This aggregates the same metadata |
| 135 |
previously passed to the "postfixfile" hook. |
| 136 |
</pre> |
| 137 |
<p>You can specify a list of directories to search the tool command in using the |
| 138 |
<cite>fix.extra-bin-paths</cite> configuration.</p> |
| 139 |
</div> |
| 140 |
<div class="section" id="commands"> |
| 141 |
<h1><a class="toc-backref" href="#contents">Commands</a></h1> |
| 142 |
<div class="section" id="file-content-management"> |
| 143 |
<h2><a class="toc-backref" href="#contents">File content management</a></h2> |
| 144 |
<div class="section" id="fix-1"> |
| 145 |
<h3>fix</h3> |
| 146 |
<p>rewrite file content in changesets or working directory:</p> |
| 147 |
<pre class="literal-block"> |
| 148 |
hg fix [OPTION]... [FILE]... |
| 149 |
</pre> |
| 150 |
<p>Runs any configured tools to fix the content of files. (See |
| 151 |
<a class="reference external" href="hg.1.html#-e"><tt class="docutils literal">hg help <span class="pre">-e</span> fix</tt></a> for details about configuring tools.) Only affects files |
| 152 |
with changes, unless file arguments are provided. Only affects changed lines |
| 153 |
of files, unless the --whole flag is used. Some tools may always affect the |
| 154 |
whole file regardless of --whole.</p> |
| 155 |
<p>If --working-dir is used, files with uncommitted changes in the working copy |
| 156 |
will be fixed. Note that no backup are made.</p> |
| 157 |
<p>If revisions are specified with --source, those revisions and their |
| 158 |
descendants will be checked, and they may be replaced with new revisions |
| 159 |
that have fixed file content. By automatically including the descendants, |
| 160 |
no merging, rebasing, or evolution will be required. If an ancestor of the |
| 161 |
working copy is included, then the working copy itself will also be fixed, |
| 162 |
and the working copy will be updated to the fixed parent.</p> |
| 163 |
<p>When determining what lines of each file to fix at each revision, the whole |
| 164 |
set of revisions being fixed is considered, so that fixes to earlier |
| 165 |
revisions are not forgotten in later ones. The --base flag can be used to |
| 166 |
override this default behavior, though it is not usually desirable to do so.</p> |
| 167 |
<p>Options:</p> |
| 168 |
<table class="docutils option-list" frame="void" rules="none"> |
| 169 |
<col class="option" /> |
| 170 |
<col class="description" /> |
| 171 |
<tbody valign="top"> |
| 172 |
<tr><td class="option-group"> |
| 173 |
<kbd><span class="option">--all</span></kbd></td> |
| 174 |
<td>fix all non-public non-obsolete revisions</td></tr> |
| 175 |
<tr><td class="option-group" colspan="2"> |
| 176 |
<kbd><span class="option">--base <var><REV[+]></var></span></kbd></td> |
| 177 |
</tr> |
| 178 |
<tr><td> </td><td>revisions to diff against (overrides automatic selection, and applies to every revision being fixed)</td></tr> |
| 179 |
<tr><td class="option-group" colspan="2"> |
| 180 |
<kbd><span class="option">-r</span>, <span class="option">--rev <var><REV[+]></var></span></kbd></td> |
| 181 |
</tr> |
| 182 |
<tr><td> </td><td>revisions to fix (ADVANCED)</td></tr> |
| 183 |
<tr><td class="option-group" colspan="2"> |
| 184 |
<kbd><span class="option">-s</span>, <span class="option">--source <var><REV[+]></var></span></kbd></td> |
| 185 |
</tr> |
| 186 |
<tr><td> </td><td>fix the specified revisions and their descendants</td></tr> |
| 187 |
<tr><td class="option-group" colspan="2"> |
| 188 |
<kbd><span class="option">-w</span>, <span class="option">--working-dir</span></kbd></td> |
| 189 |
</tr> |
| 190 |
<tr><td> </td><td>fix the working directory</td></tr> |
| 191 |
<tr><td class="option-group"> |
| 192 |
<kbd><span class="option">--whole</span></kbd></td> |
| 193 |
<td>always fix every line of a file</td></tr> |
| 194 |
</tbody> |
| 195 |
</table> |
| 196 |
<p>[+] marked option can be specified multiple times</p> |
| 197 |
</div> |
| 198 |
</div> |
| 199 |
</div> |
| 200 |
</div> |
| 201 |
</body> |
| 202 |
</html> |