hgbook
diff en/ch10-hook.xml @ 808:178e66edacdb
Uncomment citation.
author | Giulio@puck |
---|---|
date | Sat Aug 15 11:38:07 2009 +0200 (2009-08-15) |
parents | 477d6a3e5023 |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/en/ch10-hook.xml Sat Aug 15 11:38:07 2009 +0200 1.3 @@ -0,0 +1,1928 @@ 1.4 +<!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : --> 1.5 + 1.6 +<chapter id="chap:hook"> 1.7 + <?dbhtml filename="handling-repository-events-with-hooks.html"?> 1.8 + <title>Handling repository events with hooks</title> 1.9 + 1.10 + <para id="x_1e6">Mercurial offers a powerful mechanism to let you perform 1.11 + automated actions in response to events that occur in a 1.12 + repository. In some cases, you can even control Mercurial's 1.13 + response to those events.</para> 1.14 + 1.15 + <para id="x_1e7">The name Mercurial uses for one of these actions is a 1.16 + <emphasis>hook</emphasis>. Hooks are called 1.17 + <quote>triggers</quote> in some revision control systems, but the 1.18 + two names refer to the same idea.</para> 1.19 + 1.20 + <sect1> 1.21 + <title>An overview of hooks in Mercurial</title> 1.22 + 1.23 + <para id="x_1e8">Here is a brief list of the hooks that Mercurial 1.24 + supports. We will revisit each of these hooks in more detail 1.25 + later, in <xref linkend="sec:hook:ref"/>.</para> 1.26 + 1.27 + <para id="x_1f6">Each of the hooks whose description begins with the word 1.28 + <quote>Controlling</quote> has the ability to determine whether 1.29 + an activity can proceed. If the hook succeeds, the activity may 1.30 + proceed; if it fails, the activity is either not permitted or 1.31 + undone, depending on the hook.</para> 1.32 + 1.33 + <itemizedlist> 1.34 + <listitem><para id="x_1e9"><literal role="hook">changegroup</literal>: This 1.35 + is run after a group of changesets has been brought into the 1.36 + repository from elsewhere.</para> 1.37 + </listitem> 1.38 + <listitem><para id="x_1ea"><literal role="hook">commit</literal>: This is 1.39 + run after a new changeset has been created in the local 1.40 + repository.</para> 1.41 + </listitem> 1.42 + <listitem><para id="x_1eb"><literal role="hook">incoming</literal>: This is 1.43 + run once for each new changeset that is brought into the 1.44 + repository from elsewhere. Notice the difference from 1.45 + <literal role="hook">changegroup</literal>, which is run 1.46 + once per <emphasis>group</emphasis> of changesets brought 1.47 + in.</para> 1.48 + </listitem> 1.49 + <listitem><para id="x_1ec"><literal role="hook">outgoing</literal>: This is 1.50 + run after a group of changesets has been transmitted from 1.51 + this repository.</para> 1.52 + </listitem> 1.53 + <listitem><para id="x_1ed"><literal role="hook">prechangegroup</literal>: 1.54 + This is run before starting to bring a group of changesets 1.55 + into the repository. 1.56 + </para> 1.57 + </listitem> 1.58 + <listitem><para id="x_1ee"><literal role="hook">precommit</literal>: 1.59 + Controlling. This is run before starting a commit. 1.60 + </para> 1.61 + </listitem> 1.62 + <listitem><para id="x_1ef"><literal role="hook">preoutgoing</literal>: 1.63 + Controlling. This is run before starting to transmit a group 1.64 + of changesets from this repository. 1.65 + </para> 1.66 + </listitem> 1.67 + <listitem><para id="x_1f0"><literal role="hook">pretag</literal>: 1.68 + Controlling. This is run before creating a tag. 1.69 + </para> 1.70 + </listitem> 1.71 + <listitem><para id="x_1f1"><literal 1.72 + role="hook">pretxnchangegroup</literal>: Controlling. This 1.73 + is run after a group of changesets has been brought into the 1.74 + local repository from another, but before the transaction 1.75 + completes that will make the changes permanent in the 1.76 + repository. 1.77 + </para> 1.78 + </listitem> 1.79 + <listitem><para id="x_1f2"><literal role="hook">pretxncommit</literal>: 1.80 + Controlling. This is run after a new changeset has been 1.81 + created in the local repository, but before the transaction 1.82 + completes that will make it permanent. 1.83 + </para> 1.84 + </listitem> 1.85 + <listitem><para id="x_1f3"><literal role="hook">preupdate</literal>: 1.86 + Controlling. This is run before starting an update or merge 1.87 + of the working directory. 1.88 + </para> 1.89 + </listitem> 1.90 + <listitem><para id="x_1f4"><literal role="hook">tag</literal>: This is run 1.91 + after a tag is created. 1.92 + </para> 1.93 + </listitem> 1.94 + <listitem><para id="x_1f5"><literal role="hook">update</literal>: This is 1.95 + run after an update or merge of the working directory has 1.96 + finished. 1.97 + </para> 1.98 + </listitem></itemizedlist> 1.99 + 1.100 + </sect1> 1.101 + <sect1> 1.102 + <title>Hooks and security</title> 1.103 + 1.104 + <sect2> 1.105 + <title>Hooks are run with your privileges</title> 1.106 + 1.107 + <para id="x_1f7">When you run a Mercurial command in a repository, and the 1.108 + command causes a hook to run, that hook runs on 1.109 + <emphasis>your</emphasis> system, under 1.110 + <emphasis>your</emphasis> user account, with 1.111 + <emphasis>your</emphasis> privilege level. Since hooks are 1.112 + arbitrary pieces of executable code, you should treat them 1.113 + with an appropriate level of suspicion. Do not install a hook 1.114 + unless you are confident that you know who created it and what 1.115 + it does. 1.116 + </para> 1.117 + 1.118 + <para id="x_1f8">In some cases, you may be exposed to hooks that you did 1.119 + not install yourself. If you work with Mercurial on an 1.120 + unfamiliar system, Mercurial will run hooks defined in that 1.121 + system's global <filename role="special">~/.hgrc</filename> 1.122 + file. 1.123 + </para> 1.124 + 1.125 + <para id="x_1f9">If you are working with a repository owned by another 1.126 + user, Mercurial can run hooks defined in that user's 1.127 + repository, but it will still run them as <quote>you</quote>. 1.128 + For example, if you <command role="hg-cmd">hg pull</command> 1.129 + from that repository, and its <filename 1.130 + role="special">.hg/hgrc</filename> defines a local <literal 1.131 + role="hook">outgoing</literal> hook, that hook will run 1.132 + under your user account, even though you don't own that 1.133 + repository. 1.134 + </para> 1.135 + 1.136 + <note> 1.137 + <para id="x_1fa"> This only applies if you are pulling from a repository 1.138 + on a local or network filesystem. If you're pulling over 1.139 + http or ssh, any <literal role="hook">outgoing</literal> 1.140 + hook will run under whatever account is executing the server 1.141 + process, on the server. 1.142 + </para> 1.143 + </note> 1.144 + 1.145 + <para id="x_1fb">To see what hooks are defined in a repository, 1.146 + use the <command role="hg-cmd">hg showconfig hooks</command> 1.147 + command. If you are working in one repository, but talking to 1.148 + another that you do not own (e.g. using <command 1.149 + role="hg-cmd">hg pull</command> or <command role="hg-cmd">hg 1.150 + incoming</command>), remember that it is the other 1.151 + repository's hooks you should be checking, not your own. 1.152 + </para> 1.153 + </sect2> 1.154 + 1.155 + <sect2> 1.156 + <title>Hooks do not propagate</title> 1.157 + 1.158 + <para id="x_1fc">In Mercurial, hooks are not revision controlled, and do 1.159 + not propagate when you clone, or pull from, a repository. The 1.160 + reason for this is simple: a hook is a completely arbitrary 1.161 + piece of executable code. It runs under your user identity, 1.162 + with your privilege level, on your machine. 1.163 + </para> 1.164 + 1.165 + <para id="x_1fd">It would be extremely reckless for any distributed 1.166 + revision control system to implement revision-controlled 1.167 + hooks, as this would offer an easily exploitable way to 1.168 + subvert the accounts of users of the revision control system. 1.169 + </para> 1.170 + 1.171 + <para id="x_1fe">Since Mercurial does not propagate hooks, if you are 1.172 + collaborating with other people on a common project, you 1.173 + should not assume that they are using the same Mercurial hooks 1.174 + as you are, or that theirs are correctly configured. You 1.175 + should document the hooks you expect people to use. 1.176 + </para> 1.177 + 1.178 + <para id="x_1ff">In a corporate intranet, this is somewhat easier to 1.179 + control, as you can for example provide a 1.180 + <quote>standard</quote> installation of Mercurial on an NFS 1.181 + filesystem, and use a site-wide <filename role="special">~/.hgrc</filename> file to define hooks that all users will 1.182 + see. However, this too has its limits; see below. 1.183 + </para> 1.184 + </sect2> 1.185 + 1.186 + <sect2> 1.187 + <title>Hooks can be overridden</title> 1.188 + 1.189 + <para id="x_200">Mercurial allows you to override a hook definition by 1.190 + redefining the hook. You can disable it by setting its value 1.191 + to the empty string, or change its behavior as you wish. 1.192 + </para> 1.193 + 1.194 + <para id="x_201">If you deploy a system- or site-wide <filename 1.195 + role="special">~/.hgrc</filename> file that defines some 1.196 + hooks, you should thus understand that your users can disable 1.197 + or override those hooks. 1.198 + </para> 1.199 + </sect2> 1.200 + 1.201 + <sect2> 1.202 + <title>Ensuring that critical hooks are run</title> 1.203 + 1.204 + <para id="x_202">Sometimes you may want to enforce a policy that you do not 1.205 + want others to be able to work around. For example, you may 1.206 + have a requirement that every changeset must pass a rigorous 1.207 + set of tests. Defining this requirement via a hook in a 1.208 + site-wide <filename role="special">~/.hgrc</filename> won't 1.209 + work for remote users on laptops, and of course local users 1.210 + can subvert it at will by overriding the hook. 1.211 + </para> 1.212 + 1.213 + <para id="x_203">Instead, you can set up your policies for use of Mercurial 1.214 + so that people are expected to propagate changes through a 1.215 + well-known <quote>canonical</quote> server that you have 1.216 + locked down and configured appropriately. 1.217 + </para> 1.218 + 1.219 + <para id="x_204">One way to do this is via a combination of social 1.220 + engineering and technology. Set up a restricted-access 1.221 + account; users can push changes over the network to 1.222 + repositories managed by this account, but they cannot log into 1.223 + the account and run normal shell commands. In this scenario, 1.224 + a user can commit a changeset that contains any old garbage 1.225 + they want. 1.226 + </para> 1.227 + 1.228 + <para id="x_205">When someone pushes a changeset to the server that 1.229 + everyone pulls from, the server will test the changeset before 1.230 + it accepts it as permanent, and reject it if it fails to pass 1.231 + the test suite. If people only pull changes from this 1.232 + filtering server, it will serve to ensure that all changes 1.233 + that people pull have been automatically vetted. 1.234 + </para> 1.235 + 1.236 + </sect2> 1.237 + </sect1> 1.238 + 1.239 + <sect1 id="sec:hook:simple"> 1.240 + <title>A short tutorial on using hooks</title> 1.241 + 1.242 + <para id="x_212">It is easy to write a Mercurial hook. Let's start with a 1.243 + hook that runs when you finish a <command role="hg-cmd">hg 1.244 + commit</command>, and simply prints the hash of the changeset 1.245 + you just created. The hook is called <literal 1.246 + role="hook">commit</literal>. 1.247 + </para> 1.248 + 1.249 + <para id="x_213">All hooks follow the pattern in this example.</para> 1.250 + 1.251 +&interaction.hook.simple.init; 1.252 + 1.253 + <para id="x_214">You add an entry to the <literal 1.254 + role="rc-hooks">hooks</literal> section of your <filename 1.255 + role="special">~/.hgrc</filename>. On the left is the name of 1.256 + the event to trigger on; on the right is the action to take. As 1.257 + you can see, you can run an arbitrary shell command in a hook. 1.258 + Mercurial passes extra information to the hook using environment 1.259 + variables (look for <envar>HG_NODE</envar> in the example). 1.260 + </para> 1.261 + 1.262 + <sect2> 1.263 + <title>Performing multiple actions per event</title> 1.264 + 1.265 + <para id="x_215">Quite often, you will want to define more than one hook 1.266 + for a particular kind of event, as shown below.</para> 1.267 + 1.268 +&interaction.hook.simple.ext; 1.269 + 1.270 + <para id="x_216">Mercurial lets you do this by adding an 1.271 + <emphasis>extension</emphasis> to the end of a hook's name. 1.272 + You extend a hook's name by giving the name of the hook, 1.273 + followed by a full stop (the 1.274 + <quote><literal>.</literal></quote> character), followed by 1.275 + some more text of your choosing. For example, Mercurial will 1.276 + run both <literal>commit.foo</literal> and 1.277 + <literal>commit.bar</literal> when the 1.278 + <literal>commit</literal> event occurs. 1.279 + </para> 1.280 + 1.281 + <para id="x_217">To give a well-defined order of execution when there are 1.282 + multiple hooks defined for an event, Mercurial sorts hooks by 1.283 + extension, and executes the hook commands in this sorted 1.284 + order. In the above example, it will execute 1.285 + <literal>commit.bar</literal> before 1.286 + <literal>commit.foo</literal>, and <literal>commit</literal> 1.287 + before both. 1.288 + </para> 1.289 + 1.290 + <para id="x_218">It is a good idea to use a somewhat descriptive 1.291 + extension when you define a new hook. This will help you to 1.292 + remember what the hook was for. If the hook fails, you'll get 1.293 + an error message that contains the hook name and extension, so 1.294 + using a descriptive extension could give you an immediate hint 1.295 + as to why the hook failed (see <xref 1.296 + linkend="sec:hook:perm"/> for an example). 1.297 + </para> 1.298 + 1.299 + </sect2> 1.300 + <sect2 id="sec:hook:perm"> 1.301 + <title>Controlling whether an activity can proceed</title> 1.302 + 1.303 + <para id="x_219">In our earlier examples, we used the <literal 1.304 + role="hook">commit</literal> hook, which is run after a 1.305 + commit has completed. This is one of several Mercurial hooks 1.306 + that run after an activity finishes. Such hooks have no way 1.307 + of influencing the activity itself. 1.308 + </para> 1.309 + 1.310 + <para id="x_21a">Mercurial defines a number of events that occur before an 1.311 + activity starts; or after it starts, but before it finishes. 1.312 + Hooks that trigger on these events have the added ability to 1.313 + choose whether the activity can continue, or will abort. 1.314 + </para> 1.315 + 1.316 + <para id="x_21b">The <literal role="hook">pretxncommit</literal> hook runs 1.317 + after a commit has all but completed. In other words, the 1.318 + metadata representing the changeset has been written out to 1.319 + disk, but the transaction has not yet been allowed to 1.320 + complete. The <literal role="hook">pretxncommit</literal> 1.321 + hook has the ability to decide whether the transaction can 1.322 + complete, or must be rolled back. 1.323 + </para> 1.324 + 1.325 + <para id="x_21c">If the <literal role="hook">pretxncommit</literal> hook 1.326 + exits with a status code of zero, the transaction is allowed 1.327 + to complete; the commit finishes; and the <literal 1.328 + role="hook">commit</literal> hook is run. If the <literal 1.329 + role="hook">pretxncommit</literal> hook exits with a 1.330 + non-zero status code, the transaction is rolled back; the 1.331 + metadata representing the changeset is erased; and the 1.332 + <literal role="hook">commit</literal> hook is not run. 1.333 + </para> 1.334 + 1.335 +&interaction.hook.simple.pretxncommit; 1.336 + 1.337 + <para id="x_21d">The hook in the example above checks that a commit comment 1.338 + contains a bug ID. If it does, the commit can complete. If 1.339 + not, the commit is rolled back. 1.340 + </para> 1.341 + 1.342 + </sect2> 1.343 + </sect1> 1.344 + <sect1> 1.345 + <title>Writing your own hooks</title> 1.346 + 1.347 + <para id="x_21e">When you are writing a hook, you might find it useful to run 1.348 + Mercurial either with the <option 1.349 + role="hg-opt-global">-v</option> option, or the <envar 1.350 + role="rc-item-ui">verbose</envar> config item set to 1.351 + <quote>true</quote>. When you do so, Mercurial will print a 1.352 + message before it calls each hook. 1.353 + </para> 1.354 + 1.355 + <sect2 id="sec:hook:lang"> 1.356 + <title>Choosing how your hook should run</title> 1.357 + 1.358 + <para id="x_21f">You can write a hook either as a normal 1.359 + program&emdash;typically a shell script&emdash;or as a Python 1.360 + function that is executed within the Mercurial process. 1.361 + </para> 1.362 + 1.363 + <para id="x_220">Writing a hook as an external program has the advantage 1.364 + that it requires no knowledge of Mercurial's internals. You 1.365 + can call normal Mercurial commands to get any added 1.366 + information you need. The trade-off is that external hooks 1.367 + are slower than in-process hooks. 1.368 + </para> 1.369 + 1.370 + <para id="x_221">An in-process Python hook has complete access to the 1.371 + Mercurial API, and does not <quote>shell out</quote> to 1.372 + another process, so it is inherently faster than an external 1.373 + hook. It is also easier to obtain much of the information 1.374 + that a hook requires by using the Mercurial API than by 1.375 + running Mercurial commands. 1.376 + </para> 1.377 + 1.378 + <para id="x_222">If you are comfortable with Python, or require high 1.379 + performance, writing your hooks in Python may be a good 1.380 + choice. However, when you have a straightforward hook to 1.381 + write and you don't need to care about performance (probably 1.382 + the majority of hooks), a shell script is perfectly fine. 1.383 + </para> 1.384 + 1.385 + </sect2> 1.386 + <sect2 id="sec:hook:param"> 1.387 + <title>Hook parameters</title> 1.388 + 1.389 + <para id="x_223">Mercurial calls each hook with a set of well-defined 1.390 + parameters. In Python, a parameter is passed as a keyword 1.391 + argument to your hook function. For an external program, a 1.392 + parameter is passed as an environment variable. 1.393 + </para> 1.394 + 1.395 + <para id="x_224">Whether your hook is written in Python or as a shell 1.396 + script, the hook-specific parameter names and values will be 1.397 + the same. A boolean parameter will be represented as a 1.398 + boolean value in Python, but as the number 1 (for 1.399 + <quote>true</quote>) or 0 (for <quote>false</quote>) as an 1.400 + environment variable for an external hook. If a hook 1.401 + parameter is named <literal>foo</literal>, the keyword 1.402 + argument for a Python hook will also be named 1.403 + <literal>foo</literal>, while the environment variable for an 1.404 + external hook will be named <literal>HG_FOO</literal>. 1.405 + </para> 1.406 + </sect2> 1.407 + 1.408 + <sect2> 1.409 + <title>Hook return values and activity control</title> 1.410 + 1.411 + <para id="x_225">A hook that executes successfully must exit with a status 1.412 + of zero if external, or return boolean <quote>false</quote> if 1.413 + in-process. Failure is indicated with a non-zero exit status 1.414 + from an external hook, or an in-process hook returning boolean 1.415 + <quote>true</quote>. If an in-process hook raises an 1.416 + exception, the hook is considered to have failed. 1.417 + </para> 1.418 + 1.419 + <para id="x_226">For a hook that controls whether an activity can proceed, 1.420 + zero/false means <quote>allow</quote>, while 1.421 + non-zero/true/exception means <quote>deny</quote>. 1.422 + </para> 1.423 + </sect2> 1.424 + 1.425 + <sect2> 1.426 + <title>Writing an external hook</title> 1.427 + 1.428 + <para id="x_227">When you define an external hook in your <filename 1.429 + role="special">~/.hgrc</filename> and the hook is run, its 1.430 + value is passed to your shell, which interprets it. This 1.431 + means that you can use normal shell constructs in the body of 1.432 + the hook. 1.433 + </para> 1.434 + 1.435 + <para id="x_228">An executable hook is always run with its current 1.436 + directory set to a repository's root directory. 1.437 + </para> 1.438 + 1.439 + <para id="x_229">Each hook parameter is passed in as an environment 1.440 + variable; the name is upper-cased, and prefixed with the 1.441 + string <quote><literal>HG_</literal></quote>. 1.442 + </para> 1.443 + 1.444 + <para id="x_22a">With the exception of hook parameters, Mercurial does not 1.445 + set or modify any environment variables when running a hook. 1.446 + This is useful to remember if you are writing a site-wide hook 1.447 + that may be run by a number of different users with differing 1.448 + environment variables set. In multi-user situations, you 1.449 + should not rely on environment variables being set to the 1.450 + values you have in your environment when testing the hook. 1.451 + </para> 1.452 + </sect2> 1.453 + 1.454 + <sect2> 1.455 + <title>Telling Mercurial to use an in-process hook</title> 1.456 + 1.457 + <para id="x_22b">The <filename role="special">~/.hgrc</filename> syntax 1.458 + for defining an in-process hook is slightly different than for 1.459 + an executable hook. The value of the hook must start with the 1.460 + text <quote><literal>python:</literal></quote>, and continue 1.461 + with the fully-qualified name of a callable object to use as 1.462 + the hook's value. 1.463 + </para> 1.464 + 1.465 + <para id="x_22c">The module in which a hook lives is automatically imported 1.466 + when a hook is run. So long as you have the module name and 1.467 + <envar>PYTHONPATH</envar> right, it should <quote>just 1.468 + work</quote>. 1.469 + </para> 1.470 + 1.471 + <para id="x_22d">The following <filename role="special">~/.hgrc</filename> 1.472 + example snippet illustrates the syntax and meaning of the 1.473 + notions we just described. 1.474 + </para> 1.475 + <programlisting>[hooks] 1.476 +commit.example = python:mymodule.submodule.myhook</programlisting> 1.477 + <para id="x_22e">When Mercurial runs the <literal>commit.example</literal> 1.478 + hook, it imports <literal>mymodule.submodule</literal>, looks 1.479 + for the callable object named <literal>myhook</literal>, and 1.480 + calls it. 1.481 + </para> 1.482 + </sect2> 1.483 + 1.484 + <sect2> 1.485 + <title>Writing an in-process hook</title> 1.486 + 1.487 + <para id="x_22f">The simplest in-process hook does nothing, but illustrates 1.488 + the basic shape of the hook API: 1.489 + </para> 1.490 + <programlisting>def myhook(ui, repo, **kwargs): 1.491 + pass</programlisting> 1.492 + <para id="x_230">The first argument to a Python hook is always a <literal 1.493 + role="py-mod-mercurial.ui">ui</literal> object. The second 1.494 + is a repository object; at the moment, it is always an 1.495 + instance of <literal 1.496 + role="py-mod-mercurial.localrepo">localrepository</literal>. 1.497 + Following these two arguments are other keyword arguments. 1.498 + Which ones are passed in depends on the hook being called, but 1.499 + a hook can ignore arguments it doesn't care about by dropping 1.500 + them into a keyword argument dict, as with 1.501 + <literal>**kwargs</literal> above. 1.502 + </para> 1.503 + 1.504 + </sect2> 1.505 + </sect1> 1.506 + <sect1> 1.507 + <title>Some hook examples</title> 1.508 + 1.509 + <sect2> 1.510 + <title>Writing meaningful commit messages</title> 1.511 + 1.512 + <para id="x_231">It's hard to imagine a useful commit message being very 1.513 + short. The simple <literal role="hook">pretxncommit</literal> 1.514 + hook of the example below will prevent you from committing a 1.515 + changeset with a message that is less than ten bytes long. 1.516 + </para> 1.517 + 1.518 +&interaction.hook.msglen.go; 1.519 + </sect2> 1.520 + 1.521 + <sect2> 1.522 + <title>Checking for trailing whitespace</title> 1.523 + 1.524 + <para id="x_232">An interesting use of a commit-related hook is to help you 1.525 + to write cleaner code. A simple example of <quote>cleaner 1.526 + code</quote> is the dictum that a change should not add any 1.527 + new lines of text that contain <quote>trailing 1.528 + whitespace</quote>. Trailing whitespace is a series of 1.529 + space and tab characters at the end of a line of text. In 1.530 + most cases, trailing whitespace is unnecessary, invisible 1.531 + noise, but it is occasionally problematic, and people often 1.532 + prefer to get rid of it. 1.533 + </para> 1.534 + 1.535 + <para id="x_233">You can use either the <literal 1.536 + role="hook">precommit</literal> or <literal 1.537 + role="hook">pretxncommit</literal> hook to tell whether you 1.538 + have a trailing whitespace problem. If you use the <literal 1.539 + role="hook">precommit</literal> hook, the hook will not know 1.540 + which files you are committing, so it will have to check every 1.541 + modified file in the repository for trailing white space. If 1.542 + you want to commit a change to just the file 1.543 + <filename>foo</filename>, but the file 1.544 + <filename>bar</filename> contains trailing whitespace, doing a 1.545 + check in the <literal role="hook">precommit</literal> hook 1.546 + will prevent you from committing <filename>foo</filename> due 1.547 + to the problem with <filename>bar</filename>. This doesn't 1.548 + seem right. 1.549 + </para> 1.550 + 1.551 + <para id="x_234">Should you choose the <literal 1.552 + role="hook">pretxncommit</literal> hook, the check won't 1.553 + occur until just before the transaction for the commit 1.554 + completes. This will allow you to check for problems only the 1.555 + exact files that are being committed. However, if you entered 1.556 + the commit message interactively and the hook fails, the 1.557 + transaction will roll back; you'll have to re-enter the commit 1.558 + message after you fix the trailing whitespace and run <command 1.559 + role="hg-cmd">hg commit</command> again. 1.560 + </para> 1.561 + 1.562 + &interaction.ch09-hook.ws.simple; 1.563 + 1.564 + <para id="x_235">In this example, we introduce a simple <literal 1.565 + role="hook">pretxncommit</literal> hook that checks for 1.566 + trailing whitespace. This hook is short, but not very 1.567 + helpful. It exits with an error status if a change adds a 1.568 + line with trailing whitespace to any file, but does not print 1.569 + any information that might help us to identify the offending 1.570 + file or line. It also has the nice property of not paying 1.571 + attention to unmodified lines; only lines that introduce new 1.572 + trailing whitespace cause problems. 1.573 + </para> 1.574 + 1.575 + &ch09-check_whitespace.py.lst; 1.576 + 1.577 + <para id="x_236">The above version is much more complex, but also more 1.578 + useful. It parses a unified diff to see if any lines add 1.579 + trailing whitespace, and prints the name of the file and the 1.580 + line number of each such occurrence. Even better, if the 1.581 + change adds trailing whitespace, this hook saves the commit 1.582 + comment and prints the name of the save file before exiting 1.583 + and telling Mercurial to roll the transaction back, so you can 1.584 + use the <option role="hg-opt-commit">-l filename</option> 1.585 + option to <command role="hg-cmd">hg commit</command> to reuse 1.586 + the saved commit message once you've corrected the problem. 1.587 + </para> 1.588 + 1.589 + &interaction.ch09-hook.ws.better; 1.590 + 1.591 + <para id="x_237">As a final aside, note in the example above the 1.592 + use of <command>sed</command>'s in-place editing feature to 1.593 + get rid of trailing whitespace from a file. This is concise 1.594 + and useful enough that I will reproduce it here (using 1.595 + <command>perl</command> for good measure).</para> 1.596 + <programlisting>perl -pi -e 's,\s+$,,' filename</programlisting> 1.597 + 1.598 + </sect2> 1.599 + </sect1> 1.600 + <sect1> 1.601 + <title>Bundled hooks</title> 1.602 + 1.603 + <para id="x_238">Mercurial ships with several bundled hooks. You can find 1.604 + them in the <filename class="directory">hgext</filename> 1.605 + directory of a Mercurial source tree. If you are using a 1.606 + Mercurial binary package, the hooks will be located in the 1.607 + <filename class="directory">hgext</filename> directory of 1.608 + wherever your package installer put Mercurial. 1.609 + </para> 1.610 + 1.611 + <sect2> 1.612 + <title><literal role="hg-ext">acl</literal>&emdash;access 1.613 + control for parts of a repository</title> 1.614 + 1.615 + <para id="x_239">The <literal role="hg-ext">acl</literal> extension lets 1.616 + you control which remote users are allowed to push changesets 1.617 + to a networked server. You can protect any portion of a 1.618 + repository (including the entire repo), so that a specific 1.619 + remote user can push changes that do not affect the protected 1.620 + portion. 1.621 + </para> 1.622 + 1.623 + <para id="x_23a">This extension implements access control based on the 1.624 + identity of the user performing a push, 1.625 + <emphasis>not</emphasis> on who committed the changesets 1.626 + they're pushing. It makes sense to use this hook only if you 1.627 + have a locked-down server environment that authenticates 1.628 + remote users, and you want to be sure that only specific users 1.629 + are allowed to push changes to that server. 1.630 + </para> 1.631 + 1.632 + <sect3> 1.633 + <title>Configuring the <literal role="hook">acl</literal> 1.634 + hook</title> 1.635 + 1.636 + <para id="x_23b">In order to manage incoming changesets, the <literal 1.637 + role="hg-ext">acl</literal> hook must be used as a 1.638 + <literal role="hook">pretxnchangegroup</literal> hook. This 1.639 + lets it see which files are modified by each incoming 1.640 + changeset, and roll back a group of changesets if they 1.641 + modify <quote>forbidden</quote> files. Example: 1.642 + </para> 1.643 + <programlisting>[hooks] 1.644 +pretxnchangegroup.acl = python:hgext.acl.hook</programlisting> 1.645 + 1.646 + <para id="x_23c">The <literal role="hg-ext">acl</literal> extension is 1.647 + configured using three sections. 1.648 + </para> 1.649 + 1.650 + <para id="x_23d">The <literal role="rc-acl">acl</literal> section has 1.651 + only one entry, <envar role="rc-item-acl">sources</envar>, 1.652 + which lists the sources of incoming changesets that the hook 1.653 + should pay attention to. You don't normally need to 1.654 + configure this section. 1.655 + </para> 1.656 + <itemizedlist> 1.657 + <listitem><para id="x_23e"><envar role="rc-item-acl">serve</envar>: 1.658 + Control incoming changesets that are arriving from a 1.659 + remote repository over http or ssh. This is the default 1.660 + value of <envar role="rc-item-acl">sources</envar>, and 1.661 + usually the only setting you'll need for this 1.662 + configuration item. 1.663 + </para> 1.664 + </listitem> 1.665 + <listitem><para id="x_23f"><envar role="rc-item-acl">pull</envar>: 1.666 + Control incoming changesets that are arriving via a pull 1.667 + from a local repository. 1.668 + </para> 1.669 + </listitem> 1.670 + <listitem><para id="x_240"><envar role="rc-item-acl">push</envar>: 1.671 + Control incoming changesets that are arriving via a push 1.672 + from a local repository. 1.673 + </para> 1.674 + </listitem> 1.675 + <listitem><para id="x_241"><envar role="rc-item-acl">bundle</envar>: 1.676 + Control incoming changesets that are arriving from 1.677 + another repository via a bundle. 1.678 + </para> 1.679 + </listitem></itemizedlist> 1.680 + 1.681 + <para id="x_242">The <literal role="rc-acl.allow">acl.allow</literal> 1.682 + section controls the users that are allowed to add 1.683 + changesets to the repository. If this section is not 1.684 + present, all users that are not explicitly denied are 1.685 + allowed. If this section is present, all users that are not 1.686 + explicitly allowed are denied (so an empty section means 1.687 + that all users are denied). 1.688 + </para> 1.689 + 1.690 + <para id="x_243">The <literal role="rc-acl.deny">acl.deny</literal> 1.691 + section determines which users are denied from adding 1.692 + changesets to the repository. If this section is not 1.693 + present or is empty, no users are denied. 1.694 + </para> 1.695 + 1.696 + <para id="x_244">The syntaxes for the <literal 1.697 + role="rc-acl.allow">acl.allow</literal> and <literal 1.698 + role="rc-acl.deny">acl.deny</literal> sections are 1.699 + identical. On the left of each entry is a glob pattern that 1.700 + matches files or directories, relative to the root of the 1.701 + repository; on the right, a user name. 1.702 + </para> 1.703 + 1.704 + <para id="x_245">In the following example, the user 1.705 + <literal>docwriter</literal> can only push changes to the 1.706 + <filename class="directory">docs</filename> subtree of the 1.707 + repository, while <literal>intern</literal> can push changes 1.708 + to any file or directory except <filename 1.709 + class="directory">source/sensitive</filename>. 1.710 + </para> 1.711 + <programlisting>[acl.allow] 1.712 +docs/** = docwriter 1.713 +[acl.deny] 1.714 +source/sensitive/** = intern</programlisting> 1.715 + 1.716 + </sect3> 1.717 + <sect3> 1.718 + <title>Testing and troubleshooting</title> 1.719 + 1.720 + <para id="x_246">If you want to test the <literal 1.721 + role="hg-ext">acl</literal> hook, run it with Mercurial's 1.722 + debugging output enabled. Since you'll probably be running 1.723 + it on a server where it's not convenient (or sometimes 1.724 + possible) to pass in the <option 1.725 + role="hg-opt-global">--debug</option> option, don't forget 1.726 + that you can enable debugging output in your <filename 1.727 + role="special">~/.hgrc</filename>: 1.728 + </para> 1.729 + <programlisting>[ui] 1.730 +debug = true</programlisting> 1.731 + <para id="x_247">With this enabled, the <literal 1.732 + role="hg-ext">acl</literal> hook will print enough 1.733 + information to let you figure out why it is allowing or 1.734 + forbidding pushes from specific users. 1.735 + </para> 1.736 + 1.737 + </sect3> </sect2> 1.738 + 1.739 + <sect2> 1.740 + <title><literal 1.741 + role="hg-ext">bugzilla</literal>&emdash;integration with 1.742 + Bugzilla</title> 1.743 + 1.744 + <para id="x_248">The <literal role="hg-ext">bugzilla</literal> extension 1.745 + adds a comment to a Bugzilla bug whenever it finds a reference 1.746 + to that bug ID in a commit comment. You can install this hook 1.747 + on a shared server, so that any time a remote user pushes 1.748 + changes to this server, the hook gets run. 1.749 + </para> 1.750 + 1.751 + <para id="x_249">It adds a comment to the bug that looks like this (you can 1.752 + configure the contents of the comment&emdash;see below): 1.753 + </para> 1.754 + <programlisting>Changeset aad8b264143a, made by Joe User 1.755 + <joe.user@domain.com> in the frobnitz repository, refers 1.756 + to this bug. For complete details, see 1.757 + http://hg.domain.com/frobnitz?cmd=changeset;node=aad8b264143a 1.758 + Changeset description: Fix bug 10483 by guarding against some 1.759 + NULL pointers</programlisting> 1.760 + <para id="x_24a">The value of this hook is that it automates the process of 1.761 + updating a bug any time a changeset refers to it. If you 1.762 + configure the hook properly, it makes it easy for people to 1.763 + browse straight from a Bugzilla bug to a changeset that refers 1.764 + to that bug. 1.765 + </para> 1.766 + 1.767 + <para id="x_24b">You can use the code in this hook as a starting point for 1.768 + some more exotic Bugzilla integration recipes. Here are a few 1.769 + possibilities: 1.770 + </para> 1.771 + <itemizedlist> 1.772 + <listitem><para id="x_24c">Require that every changeset pushed to the 1.773 + server have a valid bug ID in its commit comment. In this 1.774 + case, you'd want to configure the hook as a <literal 1.775 + role="hook">pretxncommit</literal> hook. This would 1.776 + allow the hook to reject changes that didn't contain bug 1.777 + IDs. 1.778 + </para> 1.779 + </listitem> 1.780 + <listitem><para id="x_24d">Allow incoming changesets to automatically 1.781 + modify the <emphasis>state</emphasis> of a bug, as well as 1.782 + simply adding a comment. For example, the hook could 1.783 + recognise the string <quote>fixed bug 31337</quote> as 1.784 + indicating that it should update the state of bug 31337 to 1.785 + <quote>requires testing</quote>. 1.786 + </para> 1.787 + </listitem></itemizedlist> 1.788 + 1.789 + <sect3 id="sec:hook:bugzilla:config"> 1.790 + <title>Configuring the <literal role="hook">bugzilla</literal> 1.791 + hook</title> 1.792 + 1.793 + <para id="x_24e">You should configure this hook in your server's 1.794 + <filename role="special">~/.hgrc</filename> as an <literal 1.795 + role="hook">incoming</literal> hook, for example as 1.796 + follows: 1.797 + </para> 1.798 + <programlisting>[hooks] 1.799 +incoming.bugzilla = python:hgext.bugzilla.hook</programlisting> 1.800 + 1.801 + <para id="x_24f">Because of the specialised nature of this hook, and 1.802 + because Bugzilla was not written with this kind of 1.803 + integration in mind, configuring this hook is a somewhat 1.804 + involved process. 1.805 + </para> 1.806 + 1.807 + <para id="x_250">Before you begin, you must install the MySQL bindings 1.808 + for Python on the host(s) where you'll be running the hook. 1.809 + If this is not available as a binary package for your 1.810 + system, you can download it from 1.811 + <citation>web:mysql-python</citation>. 1.812 + </para> 1.813 + 1.814 + <para id="x_251">Configuration information for this hook lives in the 1.815 + <literal role="rc-bugzilla">bugzilla</literal> section of 1.816 + your <filename role="special">~/.hgrc</filename>. 1.817 + </para> 1.818 + <itemizedlist> 1.819 + <listitem><para id="x_252"><envar 1.820 + role="rc-item-bugzilla">version</envar>: The version 1.821 + of Bugzilla installed on the server. The database 1.822 + schema that Bugzilla uses changes occasionally, so this 1.823 + hook has to know exactly which schema to use.</para> 1.824 + </listitem> 1.825 + <listitem><para id="x_253"><envar role="rc-item-bugzilla">host</envar>: 1.826 + The hostname of the MySQL server that stores your 1.827 + Bugzilla data. The database must be configured to allow 1.828 + connections from whatever host you are running the 1.829 + <literal role="hook">bugzilla</literal> hook on. 1.830 + </para> 1.831 + </listitem> 1.832 + <listitem><para id="x_254"><envar role="rc-item-bugzilla">user</envar>: 1.833 + The username with which to connect to the MySQL server. 1.834 + The database must be configured to allow this user to 1.835 + connect from whatever host you are running the <literal 1.836 + role="hook">bugzilla</literal> hook on. This user 1.837 + must be able to access and modify Bugzilla tables. The 1.838 + default value of this item is <literal>bugs</literal>, 1.839 + which is the standard name of the Bugzilla user in a 1.840 + MySQL database. 1.841 + </para> 1.842 + </listitem> 1.843 + <listitem><para id="x_255"><envar 1.844 + role="rc-item-bugzilla">password</envar>: The MySQL 1.845 + password for the user you configured above. This is 1.846 + stored as plain text, so you should make sure that 1.847 + unauthorised users cannot read the <filename 1.848 + role="special">~/.hgrc</filename> file where you 1.849 + store this information. 1.850 + </para> 1.851 + </listitem> 1.852 + <listitem><para id="x_256"><envar role="rc-item-bugzilla">db</envar>: 1.853 + The name of the Bugzilla database on the MySQL server. 1.854 + The default value of this item is 1.855 + <literal>bugs</literal>, which is the standard name of 1.856 + the MySQL database where Bugzilla stores its data. 1.857 + </para> 1.858 + </listitem> 1.859 + <listitem><para id="x_257"><envar 1.860 + role="rc-item-bugzilla">notify</envar>: If you want 1.861 + Bugzilla to send out a notification email to subscribers 1.862 + after this hook has added a comment to a bug, you will 1.863 + need this hook to run a command whenever it updates the 1.864 + database. The command to run depends on where you have 1.865 + installed Bugzilla, but it will typically look something 1.866 + like this, if you have Bugzilla installed in <filename 1.867 + class="directory">/var/www/html/bugzilla</filename>: 1.868 + </para> 1.869 + <programlisting>cd /var/www/html/bugzilla && 1.870 + ./processmail %s nobody@nowhere.com</programlisting> 1.871 + </listitem> 1.872 + <listitem><para id="x_258"> The Bugzilla 1.873 + <literal>processmail</literal> program expects to be 1.874 + given a bug ID (the hook replaces 1.875 + <quote><literal>%s</literal></quote> with the bug ID) 1.876 + and an email address. It also expects to be able to 1.877 + write to some files in the directory that it runs in. 1.878 + If Bugzilla and this hook are not installed on the same 1.879 + machine, you will need to find a way to run 1.880 + <literal>processmail</literal> on the server where 1.881 + Bugzilla is installed. 1.882 + </para> 1.883 + </listitem></itemizedlist> 1.884 + 1.885 + </sect3> 1.886 + <sect3> 1.887 + <title>Mapping committer names to Bugzilla user names</title> 1.888 + 1.889 + <para id="x_259">By default, the <literal 1.890 + role="hg-ext">bugzilla</literal> hook tries to use the 1.891 + email address of a changeset's committer as the Bugzilla 1.892 + user name with which to update a bug. If this does not suit 1.893 + your needs, you can map committer email addresses to 1.894 + Bugzilla user names using a <literal 1.895 + role="rc-usermap">usermap</literal> section. 1.896 + </para> 1.897 + 1.898 + <para id="x_25a">Each item in the <literal 1.899 + role="rc-usermap">usermap</literal> section contains an 1.900 + email address on the left, and a Bugzilla user name on the 1.901 + right. 1.902 + </para> 1.903 + <programlisting>[usermap] 1.904 +jane.user@example.com = jane</programlisting> 1.905 + <para id="x_25b">You can either keep the <literal 1.906 + role="rc-usermap">usermap</literal> data in a normal 1.907 + <filename role="special">~/.hgrc</filename>, or tell the 1.908 + <literal role="hg-ext">bugzilla</literal> hook to read the 1.909 + information from an external <filename>usermap</filename> 1.910 + file. In the latter case, you can store 1.911 + <filename>usermap</filename> data by itself in (for example) 1.912 + a user-modifiable repository. This makes it possible to let 1.913 + your users maintain their own <envar 1.914 + role="rc-item-bugzilla">usermap</envar> entries. The main 1.915 + <filename role="special">~/.hgrc</filename> file might look 1.916 + like this: 1.917 + </para> 1.918 + <programlisting># regular hgrc file refers to external usermap file 1.919 +[bugzilla] 1.920 +usermap = /home/hg/repos/userdata/bugzilla-usermap.conf</programlisting> 1.921 + <para id="x_25c">While the <filename>usermap</filename> file that it 1.922 + refers to might look like this: 1.923 + </para> 1.924 + <programlisting># bugzilla-usermap.conf - inside a hg repository 1.925 +[usermap] stephanie@example.com = steph</programlisting> 1.926 + 1.927 + </sect3> 1.928 + <sect3> 1.929 + <title>Configuring the text that gets added to a bug</title> 1.930 + 1.931 + <para id="x_25d">You can configure the text that this hook adds as a 1.932 + comment; you specify it in the form of a Mercurial template. 1.933 + Several <filename role="special">~/.hgrc</filename> entries 1.934 + (still in the <literal role="rc-bugzilla">bugzilla</literal> 1.935 + section) control this behavior. 1.936 + </para> 1.937 + <itemizedlist> 1.938 + <listitem><para id="x_25e"><literal>strip</literal>: The number of 1.939 + leading path elements to strip from a repository's path 1.940 + name to construct a partial path for a URL. For example, 1.941 + if the repositories on your server live under <filename 1.942 + class="directory">/home/hg/repos</filename>, and you 1.943 + have a repository whose path is <filename 1.944 + class="directory">/home/hg/repos/app/tests</filename>, 1.945 + then setting <literal>strip</literal> to 1.946 + <literal>4</literal> will give a partial path of 1.947 + <filename class="directory">app/tests</filename>. The 1.948 + hook will make this partial path available when 1.949 + expanding a template, as <literal>webroot</literal>. 1.950 + </para> 1.951 + </listitem> 1.952 + <listitem><para id="x_25f"><literal>template</literal>: The text of the 1.953 + template to use. In addition to the usual 1.954 + changeset-related variables, this template can use 1.955 + <literal>hgweb</literal> (the value of the 1.956 + <literal>hgweb</literal> configuration item above) and 1.957 + <literal>webroot</literal> (the path constructed using 1.958 + <literal>strip</literal> above). 1.959 + </para> 1.960 + </listitem></itemizedlist> 1.961 + 1.962 + <para id="x_260">In addition, you can add a <envar 1.963 + role="rc-item-web">baseurl</envar> item to the <literal 1.964 + role="rc-web">web</literal> section of your <filename 1.965 + role="special">~/.hgrc</filename>. The <literal 1.966 + role="hg-ext">bugzilla</literal> hook will make this 1.967 + available when expanding a template, as the base string to 1.968 + use when constructing a URL that will let users browse from 1.969 + a Bugzilla comment to view a changeset. Example: 1.970 + </para> 1.971 + <programlisting>[web] 1.972 +baseurl = http://hg.domain.com/</programlisting> 1.973 + 1.974 + <para id="x_261">Here is an example set of <literal 1.975 + role="hg-ext">bugzilla</literal> hook config information. 1.976 + </para> 1.977 + 1.978 + &ch10-bugzilla-config.lst; 1.979 + 1.980 + </sect3> 1.981 + <sect3> 1.982 + <title>Testing and troubleshooting</title> 1.983 + 1.984 + <para id="x_262">The most common problems with configuring the <literal 1.985 + role="hg-ext">bugzilla</literal> hook relate to running 1.986 + Bugzilla's <filename>processmail</filename> script and 1.987 + mapping committer names to user names. 1.988 + </para> 1.989 + 1.990 + <para id="x_263">Recall from <xref 1.991 + linkend="sec:hook:bugzilla:config"/> above that the user 1.992 + that runs the Mercurial process on the server is also the 1.993 + one that will run the <filename>processmail</filename> 1.994 + script. The <filename>processmail</filename> script 1.995 + sometimes causes Bugzilla to write to files in its 1.996 + configuration directory, and Bugzilla's configuration files 1.997 + are usually owned by the user that your web server runs 1.998 + under. 1.999 + </para> 1.1000 + 1.1001 + <para id="x_264">You can cause <filename>processmail</filename> to be run 1.1002 + with the suitable user's identity using the 1.1003 + <command>sudo</command> command. Here is an example entry 1.1004 + for a <filename>sudoers</filename> file. 1.1005 + </para> 1.1006 + <programlisting>hg_user = (httpd_user) 1.1007 +NOPASSWD: /var/www/html/bugzilla/processmail-wrapper %s</programlisting> 1.1008 + <para id="x_265">This allows the <literal>hg_user</literal> user to run a 1.1009 + <filename>processmail-wrapper</filename> program under the 1.1010 + identity of <literal>httpd_user</literal>. 1.1011 + </para> 1.1012 + 1.1013 + <para id="x_266">This indirection through a wrapper script is necessary, 1.1014 + because <filename>processmail</filename> expects to be run 1.1015 + with its current directory set to wherever you installed 1.1016 + Bugzilla; you can't specify that kind of constraint in a 1.1017 + <filename>sudoers</filename> file. The contents of the 1.1018 + wrapper script are simple: 1.1019 + </para> 1.1020 + <programlisting>#!/bin/sh 1.1021 +cd `dirname $0` && ./processmail "$1" nobody@example.com</programlisting> 1.1022 + <para id="x_267">It doesn't seem to matter what email address you pass to 1.1023 + <filename>processmail</filename>. 1.1024 + </para> 1.1025 + 1.1026 + <para id="x_268">If your <literal role="rc-usermap">usermap</literal> is 1.1027 + not set up correctly, users will see an error message from 1.1028 + the <literal role="hg-ext">bugzilla</literal> hook when they 1.1029 + push changes to the server. The error message will look 1.1030 + like this: 1.1031 + </para> 1.1032 + <programlisting>cannot find bugzilla user id for john.q.public@example.com</programlisting> 1.1033 + <para id="x_269">What this means is that the committer's address, 1.1034 + <literal>john.q.public@example.com</literal>, is not a valid 1.1035 + Bugzilla user name, nor does it have an entry in your 1.1036 + <literal role="rc-usermap">usermap</literal> that maps it to 1.1037 + a valid Bugzilla user name. 1.1038 + </para> 1.1039 + 1.1040 + </sect3> </sect2> 1.1041 + 1.1042 + <sect2> 1.1043 + <title><literal role="hg-ext">notify</literal>&emdash;send email 1.1044 + notifications</title> 1.1045 + 1.1046 + <para id="x_26a">Although Mercurial's built-in web server provides RSS 1.1047 + feeds of changes in every repository, many people prefer to 1.1048 + receive change notifications via email. The <literal 1.1049 + role="hg-ext">notify</literal> hook lets you send out 1.1050 + notifications to a set of email addresses whenever changesets 1.1051 + arrive that those subscribers are interested in. 1.1052 + </para> 1.1053 + 1.1054 + <para id="x_26b">As with the <literal role="hg-ext">bugzilla</literal> 1.1055 + hook, the <literal role="hg-ext">notify</literal> hook is 1.1056 + template-driven, so you can customise the contents of the 1.1057 + notification messages that it sends. 1.1058 + </para> 1.1059 + 1.1060 + <para id="x_26c">By default, the <literal role="hg-ext">notify</literal> 1.1061 + hook includes a diff of every changeset that it sends out; you 1.1062 + can limit the size of the diff, or turn this feature off 1.1063 + entirely. It is useful for letting subscribers review changes 1.1064 + immediately, rather than clicking to follow a URL. 1.1065 + </para> 1.1066 + 1.1067 + <sect3> 1.1068 + <title>Configuring the <literal role="hg-ext">notify</literal> 1.1069 + hook</title> 1.1070 + 1.1071 + <para id="x_26d">You can set up the <literal 1.1072 + role="hg-ext">notify</literal> hook to send one email 1.1073 + message per incoming changeset, or one per incoming group of 1.1074 + changesets (all those that arrived in a single pull or 1.1075 + push). 1.1076 + </para> 1.1077 + <programlisting>[hooks] 1.1078 +# send one email per group of changes 1.1079 +changegroup.notify = python:hgext.notify.hook 1.1080 +# send one email per change 1.1081 +incoming.notify = python:hgext.notify.hook</programlisting> 1.1082 + 1.1083 + <para id="x_26e">Configuration information for this hook lives in the 1.1084 + <literal role="rc-notify">notify</literal> section of a 1.1085 + <filename role="special">~/.hgrc</filename> file. 1.1086 + </para> 1.1087 + <itemizedlist> 1.1088 + <listitem><para id="x_26f"><envar role="rc-item-notify">test</envar>: 1.1089 + By default, this hook does not send out email at all; 1.1090 + instead, it prints the message that it 1.1091 + <emphasis>would</emphasis> send. Set this item to 1.1092 + <literal>false</literal> to allow email to be sent. The 1.1093 + reason that sending of email is turned off by default is 1.1094 + that it takes several tries to configure this extension 1.1095 + exactly as you would like, and it would be bad form to 1.1096 + spam subscribers with a number of <quote>broken</quote> 1.1097 + notifications while you debug your configuration. 1.1098 + </para> 1.1099 + </listitem> 1.1100 + <listitem><para id="x_270"><envar role="rc-item-notify">config</envar>: 1.1101 + The path to a configuration file that contains 1.1102 + subscription information. This is kept separate from 1.1103 + the main <filename role="special">~/.hgrc</filename> so 1.1104 + that you can maintain it in a repository of its own. 1.1105 + People can then clone that repository, update their 1.1106 + subscriptions, and push the changes back to your server. 1.1107 + </para> 1.1108 + </listitem> 1.1109 + <listitem><para id="x_271"><envar role="rc-item-notify">strip</envar>: 1.1110 + The number of leading path separator characters to strip 1.1111 + from a repository's path, when deciding whether a 1.1112 + repository has subscribers. For example, if the 1.1113 + repositories on your server live in <filename 1.1114 + class="directory">/home/hg/repos</filename>, and 1.1115 + <literal role="hg-ext">notify</literal> is considering a 1.1116 + repository named <filename 1.1117 + class="directory">/home/hg/repos/shared/test</filename>, 1.1118 + setting <envar role="rc-item-notify">strip</envar> to 1.1119 + <literal>4</literal> will cause <literal 1.1120 + role="hg-ext">notify</literal> to trim the path it 1.1121 + considers down to <filename 1.1122 + class="directory">shared/test</filename>, and it will 1.1123 + match subscribers against that. 1.1124 + </para> 1.1125 + </listitem> 1.1126 + <listitem><para id="x_272"><envar 1.1127 + role="rc-item-notify">template</envar>: The template 1.1128 + text to use when sending messages. This specifies both 1.1129 + the contents of the message header and its body. 1.1130 + </para> 1.1131 + </listitem> 1.1132 + <listitem><para id="x_273"><envar 1.1133 + role="rc-item-notify">maxdiff</envar>: The maximum 1.1134 + number of lines of diff data to append to the end of a 1.1135 + message. If a diff is longer than this, it is 1.1136 + truncated. By default, this is set to 300. Set this to 1.1137 + <literal>0</literal> to omit diffs from notification 1.1138 + emails. 1.1139 + </para> 1.1140 + </listitem> 1.1141 + <listitem><para id="x_274"><envar 1.1142 + role="rc-item-notify">sources</envar>: A list of 1.1143 + sources of changesets to consider. This lets you limit 1.1144 + <literal role="hg-ext">notify</literal> to only sending 1.1145 + out email about changes that remote users pushed into 1.1146 + this repository via a server, for example. See 1.1147 + <xref linkend="sec:hook:sources"/> for the sources you 1.1148 + can specify here. 1.1149 + </para> 1.1150 + </listitem></itemizedlist> 1.1151 + 1.1152 + <para id="x_275">If you set the <envar role="rc-item-web">baseurl</envar> 1.1153 + item in the <literal role="rc-web">web</literal> section, 1.1154 + you can use it in a template; it will be available as 1.1155 + <literal>webroot</literal>. 1.1156 + </para> 1.1157 + 1.1158 + <para id="x_276">Here is an example set of <literal 1.1159 + role="hg-ext">notify</literal> configuration information. 1.1160 + </para> 1.1161 + 1.1162 + &ch10-notify-config.lst; 1.1163 + 1.1164 + <para id="x_277">This will produce a message that looks like the 1.1165 + following: 1.1166 + </para> 1.1167 + 1.1168 + &ch10-notify-config-mail.lst; 1.1169 + 1.1170 + </sect3> 1.1171 + <sect3> 1.1172 + <title>Testing and troubleshooting</title> 1.1173 + 1.1174 + <para id="x_278">Do not forget that by default, the <literal 1.1175 + role="hg-ext">notify</literal> extension <emphasis>will not 1.1176 + send any mail</emphasis> until you explicitly configure it to do so, 1.1177 + by setting <envar role="rc-item-notify">test</envar> to 1.1178 + <literal>false</literal>. Until you do that, it simply 1.1179 + prints the message it <emphasis>would</emphasis> send. 1.1180 + </para> 1.1181 + 1.1182 + </sect3> 1.1183 + </sect2> 1.1184 + </sect1> 1.1185 + <sect1 id="sec:hook:ref"> 1.1186 + <title>Information for writers of hooks</title> 1.1187 + 1.1188 + <sect2> 1.1189 + <title>In-process hook execution</title> 1.1190 + 1.1191 + <para id="x_279">An in-process hook is called with arguments of the 1.1192 + following form: 1.1193 + </para> 1.1194 + <programlisting>def myhook(ui, repo, **kwargs): pass</programlisting> 1.1195 + <para id="x_27a">The <literal>ui</literal> parameter is a <literal 1.1196 + role="py-mod-mercurial.ui">ui</literal> object. The 1.1197 + <literal>repo</literal> parameter is a <literal 1.1198 + role="py-mod-mercurial.localrepo">localrepository</literal> 1.1199 + object. The names and values of the 1.1200 + <literal>**kwargs</literal> parameters depend on the hook 1.1201 + being invoked, with the following common features: 1.1202 + </para> 1.1203 + <itemizedlist> 1.1204 + <listitem><para id="x_27b">If a parameter is named 1.1205 + <literal>node</literal> or <literal>parentN</literal>, it 1.1206 + will contain a hexadecimal changeset ID. The empty string 1.1207 + is used to represent <quote>null changeset ID</quote> 1.1208 + instead of a string of zeroes. 1.1209 + </para> 1.1210 + </listitem> 1.1211 + <listitem><para id="x_27c">If a parameter is named 1.1212 + <literal>url</literal>, it will contain the URL of a 1.1213 + remote repository, if that can be determined. 1.1214 + </para> 1.1215 + </listitem> 1.1216 + <listitem><para id="x_27d">Boolean-valued parameters are represented as 1.1217 + Python <literal>bool</literal> objects. 1.1218 + </para> 1.1219 + </listitem></itemizedlist> 1.1220 + 1.1221 + <para id="x_27e">An in-process hook is called without a change to the 1.1222 + process's working directory (unlike external hooks, which are 1.1223 + run in the root of the repository). It must not change the 1.1224 + process's working directory, or it will cause any calls it 1.1225 + makes into the Mercurial API to fail. 1.1226 + </para> 1.1227 + 1.1228 + <para id="x_27f">If a hook returns a boolean <quote>false</quote> value, it 1.1229 + is considered to have succeeded. If it returns a boolean 1.1230 + <quote>true</quote> value or raises an exception, it is 1.1231 + considered to have failed. A useful way to think of the 1.1232 + calling convention is <quote>tell me if you fail</quote>. 1.1233 + </para> 1.1234 + 1.1235 + <para id="x_280">Note that changeset IDs are passed into Python hooks as 1.1236 + hexadecimal strings, not the binary hashes that Mercurial's 1.1237 + APIs normally use. To convert a hash from hex to binary, use 1.1238 + the <literal>bin</literal> function. 1.1239 + </para> 1.1240 + </sect2> 1.1241 + 1.1242 + <sect2> 1.1243 + <title>External hook execution</title> 1.1244 + 1.1245 + <para id="x_281">An external hook is passed to the shell of the user 1.1246 + running Mercurial. Features of that shell, such as variable 1.1247 + substitution and command redirection, are available. The hook 1.1248 + is run in the root directory of the repository (unlike 1.1249 + in-process hooks, which are run in the same directory that 1.1250 + Mercurial was run in). 1.1251 + </para> 1.1252 + 1.1253 + <para id="x_282">Hook parameters are passed to the hook as environment 1.1254 + variables. Each environment variable's name is converted in 1.1255 + upper case and prefixed with the string 1.1256 + <quote><literal>HG_</literal></quote>. For example, if the 1.1257 + name of a parameter is <quote><literal>node</literal></quote>, 1.1258 + the name of the environment variable representing that 1.1259 + parameter will be <quote><literal>HG_NODE</literal></quote>. 1.1260 + </para> 1.1261 + 1.1262 + <para id="x_283">A boolean parameter is represented as the string 1.1263 + <quote><literal>1</literal></quote> for <quote>true</quote>, 1.1264 + <quote><literal>0</literal></quote> for <quote>false</quote>. 1.1265 + If an environment variable is named <envar>HG_NODE</envar>, 1.1266 + <envar>HG_PARENT1</envar> or <envar>HG_PARENT2</envar>, it 1.1267 + contains a changeset ID represented as a hexadecimal string. 1.1268 + The empty string is used to represent <quote>null changeset 1.1269 + ID</quote> instead of a string of zeroes. If an environment 1.1270 + variable is named <envar>HG_URL</envar>, it will contain the 1.1271 + URL of a remote repository, if that can be determined. 1.1272 + </para> 1.1273 + 1.1274 + <para id="x_284">If a hook exits with a status of zero, it is considered to 1.1275 + have succeeded. If it exits with a non-zero status, it is 1.1276 + considered to have failed. 1.1277 + </para> 1.1278 + </sect2> 1.1279 + 1.1280 + <sect2> 1.1281 + <title>Finding out where changesets come from</title> 1.1282 + 1.1283 + <para id="x_285">A hook that involves the transfer of changesets between a 1.1284 + local repository and another may be able to find out 1.1285 + information about the <quote>far side</quote>. Mercurial 1.1286 + knows <emphasis>how</emphasis> changes are being transferred, 1.1287 + and in many cases <emphasis>where</emphasis> they are being 1.1288 + transferred to or from. 1.1289 + </para> 1.1290 + 1.1291 + <sect3 id="sec:hook:sources"> 1.1292 + <title>Sources of changesets</title> 1.1293 + 1.1294 + <para id="x_286">Mercurial will tell a hook what means are, or were, used 1.1295 + to transfer changesets between repositories. This is 1.1296 + provided by Mercurial in a Python parameter named 1.1297 + <literal>source</literal>, or an environment variable named 1.1298 + <envar>HG_SOURCE</envar>. 1.1299 + </para> 1.1300 + 1.1301 + <itemizedlist> 1.1302 + <listitem><para id="x_287"><literal>serve</literal>: Changesets are 1.1303 + transferred to or from a remote repository over http or 1.1304 + ssh. 1.1305 + </para> 1.1306 + </listitem> 1.1307 + <listitem><para id="x_288"><literal>pull</literal>: Changesets are 1.1308 + being transferred via a pull from one repository into 1.1309 + another. 1.1310 + </para> 1.1311 + </listitem> 1.1312 + <listitem><para id="x_289"><literal>push</literal>: Changesets are 1.1313 + being transferred via a push from one repository into 1.1314 + another. 1.1315 + </para> 1.1316 + </listitem> 1.1317 + <listitem><para id="x_28a"><literal>bundle</literal>: Changesets are 1.1318 + being transferred to or from a bundle. 1.1319 + </para> 1.1320 + </listitem></itemizedlist> 1.1321 + </sect3> 1.1322 + 1.1323 + <sect3 id="sec:hook:url"> 1.1324 + <title>Where changes are going&emdash;remote repository 1.1325 + URLs</title> 1.1326 + 1.1327 + <para id="x_28b">When possible, Mercurial will tell a hook the location 1.1328 + of the <quote>far side</quote> of an activity that transfers 1.1329 + changeset data between repositories. This is provided by 1.1330 + Mercurial in a Python parameter named 1.1331 + <literal>url</literal>, or an environment variable named 1.1332 + <envar>HG_URL</envar>. 1.1333 + </para> 1.1334 + 1.1335 + <para id="x_28c">This information is not always known. If a hook is 1.1336 + invoked in a repository that is being served via http or 1.1337 + ssh, Mercurial cannot tell where the remote repository is, 1.1338 + but it may know where the client is connecting from. In 1.1339 + such cases, the URL will take one of the following forms: 1.1340 + </para> 1.1341 + <itemizedlist> 1.1342 + <listitem><para id="x_28d"><literal>remote:ssh:1.2.3.4</literal>&emdash;remote 1.1343 + ssh client, at the IP address 1.1344 + <literal>1.2.3.4</literal>. 1.1345 + </para> 1.1346 + </listitem> 1.1347 + <listitem><para id="x_28e"><literal>remote:http:1.2.3.4</literal>&emdash;remote 1.1348 + http client, at the IP address 1.1349 + <literal>1.2.3.4</literal>. If the client is using SSL, 1.1350 + this will be of the form 1.1351 + <literal>remote:https:1.2.3.4</literal>. 1.1352 + </para> 1.1353 + </listitem> 1.1354 + <listitem><para id="x_28f">Empty&emdash;no information could be 1.1355 + discovered about the remote client. 1.1356 + </para> 1.1357 + </listitem></itemizedlist> 1.1358 + </sect3> 1.1359 + </sect2> 1.1360 + </sect1> 1.1361 + <sect1> 1.1362 + <title>Hook reference</title> 1.1363 + 1.1364 + <sect2 id="sec:hook:changegroup"> 1.1365 + <title><literal role="hook">changegroup</literal>&emdash;after 1.1366 + remote changesets added</title> 1.1367 + 1.1368 + <para id="x_290">This hook is run after a group of pre-existing changesets 1.1369 + has been added to the repository, for example via a <command 1.1370 + role="hg-cmd">hg pull</command> or <command role="hg-cmd">hg 1.1371 + unbundle</command>. This hook is run once per operation 1.1372 + that added one or more changesets. This is in contrast to the 1.1373 + <literal role="hook">incoming</literal> hook, which is run 1.1374 + once per changeset, regardless of whether the changesets 1.1375 + arrive in a group. 1.1376 + </para> 1.1377 + 1.1378 + <para id="x_291">Some possible uses for this hook include kicking off an 1.1379 + automated build or test of the added changesets, updating a 1.1380 + bug database, or notifying subscribers that a repository 1.1381 + contains new changes. 1.1382 + </para> 1.1383 + 1.1384 + <para id="x_292">Parameters to this hook: 1.1385 + </para> 1.1386 + <itemizedlist> 1.1387 + <listitem><para id="x_293"><literal>node</literal>: A changeset ID. The 1.1388 + changeset ID of the first changeset in the group that was 1.1389 + added. All changesets between this and 1.1390 + <literal role="tag">tip</literal>, inclusive, were added by a single 1.1391 + <command role="hg-cmd">hg pull</command>, <command 1.1392 + role="hg-cmd">hg push</command> or <command 1.1393 + role="hg-cmd">hg unbundle</command>. 1.1394 + </para> 1.1395 + </listitem> 1.1396 + <listitem><para id="x_294"><literal>source</literal>: A 1.1397 + string. The source of these changes. See <xref 1.1398 + linkend="sec:hook:sources"/> for details. 1.1399 + </para> 1.1400 + </listitem> 1.1401 + <listitem><para id="x_295"><literal>url</literal>: A URL. The 1.1402 + location of the remote repository, if known. See <xref 1.1403 + linkend="sec:hook:url"/> for more information. 1.1404 + </para> 1.1405 + </listitem></itemizedlist> 1.1406 + 1.1407 + <para id="x_296">See also: <literal 1.1408 + role="hook">incoming</literal> (<xref 1.1409 + linkend="sec:hook:incoming"/>), <literal 1.1410 + role="hook">prechangegroup</literal> (<xref 1.1411 + linkend="sec:hook:prechangegroup"/>), <literal 1.1412 + role="hook">pretxnchangegroup</literal> (<xref 1.1413 + linkend="sec:hook:pretxnchangegroup"/>) 1.1414 + </para> 1.1415 + </sect2> 1.1416 + 1.1417 + <sect2 id="sec:hook:commit"> 1.1418 + <title><literal role="hook">commit</literal>&emdash;after a new 1.1419 + changeset is created</title> 1.1420 + 1.1421 + <para id="x_297">This hook is run after a new changeset has been created. 1.1422 + </para> 1.1423 + 1.1424 + <para id="x_298">Parameters to this hook: 1.1425 + </para> 1.1426 + <itemizedlist> 1.1427 + <listitem><para id="x_299"><literal>node</literal>: A changeset ID. The 1.1428 + changeset ID of the newly committed changeset. 1.1429 + </para> 1.1430 + </listitem> 1.1431 + <listitem><para id="x_29a"><literal>parent1</literal>: A changeset ID. 1.1432 + The changeset ID of the first parent of the newly 1.1433 + committed changeset. 1.1434 + </para> 1.1435 + </listitem> 1.1436 + <listitem><para id="x_29b"><literal>parent2</literal>: A changeset ID. 1.1437 + The changeset ID of the second parent of the newly 1.1438 + committed changeset. 1.1439 + </para> 1.1440 + </listitem></itemizedlist> 1.1441 + 1.1442 + <para id="x_29c">See also: <literal 1.1443 + role="hook">precommit</literal> (<xref 1.1444 + linkend="sec:hook:precommit"/>), <literal 1.1445 + role="hook">pretxncommit</literal> (<xref 1.1446 + linkend="sec:hook:pretxncommit"/>) 1.1447 + </para> 1.1448 + </sect2> 1.1449 + 1.1450 + <sect2 id="sec:hook:incoming"> 1.1451 + <title><literal role="hook">incoming</literal>&emdash;after one 1.1452 + remote changeset is added</title> 1.1453 + 1.1454 + <para id="x_29d">This hook is run after a pre-existing changeset has been 1.1455 + added to the repository, for example via a <command 1.1456 + role="hg-cmd">hg push</command>. If a group of changesets 1.1457 + was added in a single operation, this hook is called once for 1.1458 + each added changeset. 1.1459 + </para> 1.1460 + 1.1461 + <para id="x_29e">You can use this hook for the same purposes as 1.1462 + the <literal role="hook">changegroup</literal> hook (<xref 1.1463 + linkend="sec:hook:changegroup"/>); it's simply more 1.1464 + convenient sometimes to run a hook once per group of 1.1465 + changesets, while other times it's handier once per changeset. 1.1466 + </para> 1.1467 + 1.1468 + <para id="x_29f">Parameters to this hook: 1.1469 + </para> 1.1470 + <itemizedlist> 1.1471 + <listitem><para id="x_2a0"><literal>node</literal>: A changeset ID. The 1.1472 + ID of the newly added changeset. 1.1473 + </para> 1.1474 + </listitem> 1.1475 + <listitem><para id="x_2a1"><literal>source</literal>: A 1.1476 + string. The source of these changes. See <xref 1.1477 + linkend="sec:hook:sources"/> for details. 1.1478 + </para> 1.1479 + </listitem> 1.1480 + <listitem><para id="x_2a2"><literal>url</literal>: A URL. The 1.1481 + location of the remote repository, if known. See <xref 1.1482 + linkend="sec:hook:url"/> for more information. 1.1483 + </para> 1.1484 + </listitem></itemizedlist> 1.1485 + 1.1486 + <para id="x_2a3">See also: <literal 1.1487 + role="hook">changegroup</literal> (<xref 1.1488 + linkend="sec:hook:changegroup"/>) <literal 1.1489 + role="hook">prechangegroup</literal> (<xref 1.1490 + linkend="sec:hook:prechangegroup"/>), <literal 1.1491 + role="hook">pretxnchangegroup</literal> (<xref 1.1492 + linkend="sec:hook:pretxnchangegroup"/>) 1.1493 + </para> 1.1494 + </sect2> 1.1495 + 1.1496 + <sect2 id="sec:hook:outgoing"> 1.1497 + <title><literal role="hook">outgoing</literal>&emdash;after 1.1498 + changesets are propagated</title> 1.1499 + 1.1500 + <para id="x_2a4">This hook is run after a group of changesets has been 1.1501 + propagated out of this repository, for example by a <command 1.1502 + role="hg-cmd">hg push</command> or <command role="hg-cmd">hg 1.1503 + bundle</command> command. 1.1504 + </para> 1.1505 + 1.1506 + <para id="x_2a5">One possible use for this hook is to notify administrators 1.1507 + that changes have been pulled. 1.1508 + </para> 1.1509 + 1.1510 + <para id="x_2a6">Parameters to this hook: 1.1511 + </para> 1.1512 + <itemizedlist> 1.1513 + <listitem><para id="x_2a7"><literal>node</literal>: A changeset ID. The 1.1514 + changeset ID of the first changeset of the group that was 1.1515 + sent. 1.1516 + </para> 1.1517 + </listitem> 1.1518 + <listitem><para id="x_2a8"><literal>source</literal>: A string. The 1.1519 + source of the of the operation (see <xref 1.1520 + linkend="sec:hook:sources"/>). If a remote 1.1521 + client pulled changes from this repository, 1.1522 + <literal>source</literal> will be 1.1523 + <literal>serve</literal>. If the client that obtained 1.1524 + changes from this repository was local, 1.1525 + <literal>source</literal> will be 1.1526 + <literal>bundle</literal>, <literal>pull</literal>, or 1.1527 + <literal>push</literal>, depending on the operation the 1.1528 + client performed. 1.1529 + </para> 1.1530 + </listitem> 1.1531 + <listitem><para id="x_2a9"><literal>url</literal>: A URL. The 1.1532 + location of the remote repository, if known. See <xref 1.1533 + linkend="sec:hook:url"/> for more information. 1.1534 + </para> 1.1535 + </listitem></itemizedlist> 1.1536 + 1.1537 + <para id="x_2aa">See also: <literal 1.1538 + role="hook">preoutgoing</literal> (<xref 1.1539 + linkend="sec:hook:preoutgoing"/>) 1.1540 + </para> 1.1541 + </sect2> 1.1542 + 1.1543 + <sect2 id="sec:hook:prechangegroup"> 1.1544 + <title><literal 1.1545 + role="hook">prechangegroup</literal>&emdash;before starting 1.1546 + to add remote changesets</title> 1.1547 + 1.1548 + <para id="x_2ab">This controlling hook is run before Mercurial begins to 1.1549 + add a group of changesets from another repository. 1.1550 + </para> 1.1551 + 1.1552 + <para id="x_2ac">This hook does not have any information about the 1.1553 + changesets to be added, because it is run before transmission 1.1554 + of those changesets is allowed to begin. If this hook fails, 1.1555 + the changesets will not be transmitted. 1.1556 + </para> 1.1557 + 1.1558 + <para id="x_2ad">One use for this hook is to prevent external changes from 1.1559 + being added to a repository. For example, you could use this 1.1560 + to <quote>freeze</quote> a server-hosted branch temporarily or 1.1561 + permanently so that users cannot push to it, while still 1.1562 + allowing a local administrator to modify the repository. 1.1563 + </para> 1.1564 + 1.1565 + <para id="x_2ae">Parameters to this hook: 1.1566 + </para> 1.1567 + <itemizedlist> 1.1568 + <listitem><para id="x_2af"><literal>source</literal>: A string. The 1.1569 + source of these changes. See <xref 1.1570 + linkend="sec:hook:sources"/> for details. 1.1571 + </para> 1.1572 + </listitem> 1.1573 + <listitem><para id="x_2b0"><literal>url</literal>: A URL. The 1.1574 + location of the remote repository, if known. See <xref 1.1575 + linkend="sec:hook:url"/> for more information. 1.1576 + </para> 1.1577 + </listitem></itemizedlist> 1.1578 + 1.1579 + <para id="x_2b1">See also: <literal 1.1580 + role="hook">changegroup</literal> (<xref 1.1581 + linkend="sec:hook:changegroup"/>), <literal 1.1582 + role="hook">incoming</literal> (<xref 1.1583 + linkend="sec:hook:incoming"/>), <literal 1.1584 + role="hook">pretxnchangegroup</literal> (<xref 1.1585 + linkend="sec:hook:pretxnchangegroup"/>) 1.1586 + </para> 1.1587 + </sect2> 1.1588 + 1.1589 + <sect2 id="sec:hook:precommit"> 1.1590 + <title><literal role="hook">precommit</literal>&emdash;before 1.1591 + starting to commit a changeset</title> 1.1592 + 1.1593 + <para id="x_2b2">This hook is run before Mercurial begins to commit a new 1.1594 + changeset. It is run before Mercurial has any of the metadata 1.1595 + for the commit, such as the files to be committed, the commit 1.1596 + message, or the commit date. 1.1597 + </para> 1.1598 + 1.1599 + <para id="x_2b3">One use for this hook is to disable the ability to commit 1.1600 + new changesets, while still allowing incoming changesets. 1.1601 + Another is to run a build or test, and only allow the commit 1.1602 + to begin if the build or test succeeds. 1.1603 + </para> 1.1604 + 1.1605 + <para id="x_2b4">Parameters to this hook: 1.1606 + </para> 1.1607 + <itemizedlist> 1.1608 + <listitem><para id="x_2b5"><literal>parent1</literal>: A changeset ID. 1.1609 + The changeset ID of the first parent of the working 1.1610 + directory. 1.1611 + </para> 1.1612 + </listitem> 1.1613 + <listitem><para id="x_2b6"><literal>parent2</literal>: A changeset ID. 1.1614 + The changeset ID of the second parent of the working 1.1615 + directory. 1.1616 + </para> 1.1617 + </listitem></itemizedlist> 1.1618 + <para id="x_2b7">If the commit proceeds, the parents of the working 1.1619 + directory will become the parents of the new changeset. 1.1620 + </para> 1.1621 + 1.1622 + <para id="x_2b8">See also: <literal role="hook">commit</literal> 1.1623 + (<xref linkend="sec:hook:commit"/>), <literal 1.1624 + role="hook">pretxncommit</literal> (<xref 1.1625 + linkend="sec:hook:pretxncommit"/>) 1.1626 + </para> 1.1627 + </sect2> 1.1628 + 1.1629 + <sect2 id="sec:hook:preoutgoing"> 1.1630 + <title><literal role="hook">preoutgoing</literal>&emdash;before 1.1631 + starting to propagate changesets</title> 1.1632 + 1.1633 + <para id="x_2b9">This hook is invoked before Mercurial knows the identities 1.1634 + of the changesets to be transmitted. 1.1635 + </para> 1.1636 + 1.1637 + <para id="x_2ba">One use for this hook is to prevent changes from being 1.1638 + transmitted to another repository. 1.1639 + </para> 1.1640 + 1.1641 + <para id="x_2bb">Parameters to this hook: 1.1642 + </para> 1.1643 + <itemizedlist> 1.1644 + <listitem><para id="x_2bc"><literal>source</literal>: A 1.1645 + string. The source of the operation that is attempting to 1.1646 + obtain changes from this repository (see <xref 1.1647 + linkend="sec:hook:sources"/>). See the documentation 1.1648 + for the <literal>source</literal> parameter to the 1.1649 + <literal role="hook">outgoing</literal> hook, in 1.1650 + <xref linkend="sec:hook:outgoing"/>, for possible values 1.1651 + of this parameter. 1.1652 + </para> 1.1653 + </listitem> 1.1654 + <listitem><para id="x_2bd"><literal>url</literal>: A URL. The 1.1655 + location of the remote repository, if known. See <xref 1.1656 + linkend="sec:hook:url"/> for more information. 1.1657 + </para> 1.1658 + </listitem></itemizedlist> 1.1659 + 1.1660 + <para id="x_2be">See also: <literal 1.1661 + role="hook">outgoing</literal> (<xref 1.1662 + linkend="sec:hook:outgoing"/>) 1.1663 + </para> 1.1664 + </sect2> 1.1665 + 1.1666 + <sect2 id="sec:hook:pretag"> 1.1667 + <title><literal role="hook">pretag</literal>&emdash;before 1.1668 + tagging a changeset</title> 1.1669 + 1.1670 + <para id="x_2bf">This controlling hook is run before a tag is created. If 1.1671 + the hook succeeds, creation of the tag proceeds. If the hook 1.1672 + fails, the tag is not created. 1.1673 + </para> 1.1674 + 1.1675 + <para id="x_2c0">Parameters to this hook: 1.1676 + </para> 1.1677 + <itemizedlist> 1.1678 + <listitem><para id="x_2c1"><literal>local</literal>: A boolean. Whether 1.1679 + the tag is local to this repository instance (i.e. stored 1.1680 + in <filename role="special">.hg/localtags</filename>) or 1.1681 + managed by Mercurial (stored in <filename 1.1682 + role="special">.hgtags</filename>). 1.1683 + </para> 1.1684 + </listitem> 1.1685 + <listitem><para id="x_2c2"><literal>node</literal>: A changeset ID. The 1.1686 + ID of the changeset to be tagged. 1.1687 + </para> 1.1688 + </listitem> 1.1689 + <listitem><para id="x_2c3"><literal>tag</literal>: A string. The name of 1.1690 + the tag to be created. 1.1691 + </para> 1.1692 + </listitem></itemizedlist> 1.1693 + 1.1694 + <para id="x_2c4">If the tag to be created is 1.1695 + revision-controlled, the <literal 1.1696 + role="hook">precommit</literal> and <literal 1.1697 + role="hook">pretxncommit</literal> hooks (<xref 1.1698 + linkend="sec:hook:commit"/> and <xref 1.1699 + linkend="sec:hook:pretxncommit"/>) will also be run. 1.1700 + </para> 1.1701 + 1.1702 + <para id="x_2c5">See also: <literal role="hook">tag</literal> 1.1703 + (<xref linkend="sec:hook:tag"/>) 1.1704 + </para> 1.1705 + </sect2> 1.1706 + 1.1707 + <sect2 id="sec:hook:pretxnchangegroup"> 1.1708 + <title><literal 1.1709 + role="hook">pretxnchangegroup</literal>&emdash;before 1.1710 + completing addition of remote changesets</title> 1.1711 + 1.1712 + <para id="x_2c6">This controlling hook is run before a 1.1713 + transaction&emdash;that manages the addition of a group of new 1.1714 + changesets from outside the repository&emdash;completes. If 1.1715 + the hook succeeds, the transaction completes, and all of the 1.1716 + changesets become permanent within this repository. If the 1.1717 + hook fails, the transaction is rolled back, and the data for 1.1718 + the changesets is erased. 1.1719 + </para> 1.1720 + 1.1721 + <para id="x_2c7">This hook can access the metadata associated with the 1.1722 + almost-added changesets, but it should not do anything 1.1723 + permanent with this data. It must also not modify the working 1.1724 + directory. 1.1725 + </para> 1.1726 + 1.1727 + <para id="x_2c8">While this hook is running, if other Mercurial processes 1.1728 + access this repository, they will be able to see the 1.1729 + almost-added changesets as if they are permanent. This may 1.1730 + lead to race conditions if you do not take steps to avoid 1.1731 + them. 1.1732 + </para> 1.1733 + 1.1734 + <para id="x_2c9">This hook can be used to automatically vet a group of 1.1735 + changesets. If the hook fails, all of the changesets are 1.1736 + <quote>rejected</quote> when the transaction rolls back. 1.1737 + </para> 1.1738 + 1.1739 + <para id="x_2ca">Parameters to this hook: 1.1740 + </para> 1.1741 + <itemizedlist> 1.1742 + <listitem><para id="x_2cb"><literal>node</literal>: A changeset ID. The 1.1743 + changeset ID of the first changeset in the group that was 1.1744 + added. All changesets between this and 1.1745 + <literal role="tag">tip</literal>, 1.1746 + inclusive, were added by a single <command 1.1747 + role="hg-cmd">hg pull</command>, <command 1.1748 + role="hg-cmd">hg push</command> or <command 1.1749 + role="hg-cmd">hg unbundle</command>. 1.1750 + </para> 1.1751 + </listitem> 1.1752 + <listitem><para id="x_2cc"><literal>source</literal>: A 1.1753 + string. The source of these changes. See <xref 1.1754 + linkend="sec:hook:sources"/> for details. 1.1755 + </para> 1.1756 + </listitem> 1.1757 + <listitem><para id="x_2cd"><literal>url</literal>: A URL. The 1.1758 + location of the remote repository, if known. See <xref 1.1759 + linkend="sec:hook:url"/> for more information. 1.1760 + </para> 1.1761 + </listitem></itemizedlist> 1.1762 + 1.1763 + <para id="x_2ce">See also: <literal 1.1764 + role="hook">changegroup</literal> (<xref 1.1765 + linkend="sec:hook:changegroup"/>), <literal 1.1766 + role="hook">incoming</literal> (<xref 1.1767 + linkend="sec:hook:incoming"/>), <literal 1.1768 + role="hook">prechangegroup</literal> (<xref 1.1769 + linkend="sec:hook:prechangegroup"/>) 1.1770 + </para> 1.1771 + </sect2> 1.1772 + 1.1773 + <sect2 id="sec:hook:pretxncommit"> 1.1774 + <title><literal role="hook">pretxncommit</literal>&emdash;before 1.1775 + completing commit of new changeset</title> 1.1776 + 1.1777 + <para id="x_2cf">This controlling hook is run before a 1.1778 + transaction&emdash;that manages a new commit&emdash;completes. 1.1779 + If the hook succeeds, the transaction completes and the 1.1780 + changeset becomes permanent within this repository. If the 1.1781 + hook fails, the transaction is rolled back, and the commit 1.1782 + data is erased. 1.1783 + </para> 1.1784 + 1.1785 + <para id="x_2d0">This hook can access the metadata associated with the 1.1786 + almost-new changeset, but it should not do anything permanent 1.1787 + with this data. It must also not modify the working 1.1788 + directory. 1.1789 + </para> 1.1790 + 1.1791 + <para id="x_2d1">While this hook is running, if other Mercurial processes 1.1792 + access this repository, they will be able to see the 1.1793 + almost-new changeset as if it is permanent. This may lead to 1.1794 + race conditions if you do not take steps to avoid them. 1.1795 + </para> 1.1796 + 1.1797 + <para id="x_2d2">Parameters to this hook:</para> 1.1798 + 1.1799 + <itemizedlist> 1.1800 + <listitem><para id="x_2d3"><literal>node</literal>: A changeset ID. The 1.1801 + changeset ID of the newly committed changeset. 1.1802 + </para> 1.1803 + </listitem> 1.1804 + <listitem><para id="x_2d4"><literal>parent1</literal>: A changeset ID. 1.1805 + The changeset ID of the first parent of the newly 1.1806 + committed changeset. 1.1807 + </para> 1.1808 + </listitem> 1.1809 + <listitem><para id="x_2d5"><literal>parent2</literal>: A changeset ID. 1.1810 + The changeset ID of the second parent of the newly 1.1811 + committed changeset. 1.1812 + </para> 1.1813 + </listitem></itemizedlist> 1.1814 + 1.1815 + <para id="x_2d6">See also: <literal 1.1816 + role="hook">precommit</literal> (<xref 1.1817 + linkend="sec:hook:precommit"/>) 1.1818 + </para> 1.1819 + </sect2> 1.1820 + 1.1821 + <sect2 id="sec:hook:preupdate"> 1.1822 + <title><literal role="hook">preupdate</literal>&emdash;before 1.1823 + updating or merging working directory</title> 1.1824 + 1.1825 + <para id="x_2d7">This controlling hook is run before an update 1.1826 + or merge of the working directory begins. It is run only if 1.1827 + Mercurial's normal pre-update checks determine that the update 1.1828 + or merge can proceed. If the hook succeeds, the update or 1.1829 + merge may proceed; if it fails, the update or merge does not 1.1830 + start. 1.1831 + </para> 1.1832 + 1.1833 + <para id="x_2d8">Parameters to this hook: 1.1834 + </para> 1.1835 + <itemizedlist> 1.1836 + <listitem><para id="x_2d9"><literal>parent1</literal>: A 1.1837 + changeset ID. The ID of the parent that the working 1.1838 + directory is to be updated to. If the working directory 1.1839 + is being merged, it will not change this parent. 1.1840 + </para> 1.1841 + </listitem> 1.1842 + <listitem><para id="x_2da"><literal>parent2</literal>: A 1.1843 + changeset ID. Only set if the working directory is being 1.1844 + merged. The ID of the revision that the working directory 1.1845 + is being merged with. 1.1846 + </para> 1.1847 + </listitem></itemizedlist> 1.1848 + 1.1849 + <para id="x_2db">See also: <literal role="hook">update</literal> 1.1850 + (<xref linkend="sec:hook:update"/>)</para> 1.1851 + </sect2> 1.1852 + 1.1853 + <sect2 id="sec:hook:tag"> 1.1854 + <title><literal role="hook">tag</literal>&emdash;after tagging a 1.1855 + changeset</title> 1.1856 + 1.1857 + <para id="x_2dc">This hook is run after a tag has been created. 1.1858 + </para> 1.1859 + 1.1860 + <para id="x_2dd">Parameters to this hook: 1.1861 + </para> 1.1862 + <itemizedlist> 1.1863 + <listitem><para id="x_2de"><literal>local</literal>: A boolean. Whether 1.1864 + the new tag is local to this repository instance (i.e. 1.1865 + stored in <filename 1.1866 + role="special">.hg/localtags</filename>) or managed by 1.1867 + Mercurial (stored in <filename 1.1868 + role="special">.hgtags</filename>). 1.1869 + </para> 1.1870 + </listitem> 1.1871 + <listitem><para id="x_2df"><literal>node</literal>: A changeset ID. The 1.1872 + ID of the changeset that was tagged. 1.1873 + </para> 1.1874 + </listitem> 1.1875 + <listitem><para id="x_2e0"><literal>tag</literal>: A string. The name of 1.1876 + the tag that was created. 1.1877 + </para> 1.1878 + </listitem></itemizedlist> 1.1879 + 1.1880 + <para id="x_2e1">If the created tag is revision-controlled, the <literal 1.1881 + role="hook">commit</literal> hook (section <xref 1.1882 + linkend="sec:hook:commit"/>) is run before this hook. 1.1883 + </para> 1.1884 + 1.1885 + <para id="x_2e2">See also: <literal role="hook">pretag</literal> 1.1886 + (<xref linkend="sec:hook:pretag"/>) 1.1887 + </para> 1.1888 + </sect2> 1.1889 + 1.1890 + <sect2 id="sec:hook:update"> 1.1891 + <title><literal role="hook">update</literal>&emdash;after 1.1892 + updating or merging working directory</title> 1.1893 + 1.1894 + <para id="x_2e3">This hook is run after an update or merge of the working 1.1895 + directory completes. Since a merge can fail (if the external 1.1896 + <command>hgmerge</command> command fails to resolve conflicts 1.1897 + in a file), this hook communicates whether the update or merge 1.1898 + completed cleanly. 1.1899 + </para> 1.1900 + 1.1901 + <itemizedlist> 1.1902 + <listitem><para id="x_2e4"><literal>error</literal>: A boolean. 1.1903 + Indicates whether the update or merge completed 1.1904 + successfully. 1.1905 + </para> 1.1906 + </listitem> 1.1907 + <listitem><para id="x_2e5"><literal>parent1</literal>: A changeset ID. 1.1908 + The ID of the parent that the working directory was 1.1909 + updated to. If the working directory was merged, it will 1.1910 + not have changed this parent. 1.1911 + </para> 1.1912 + </listitem> 1.1913 + <listitem><para id="x_2e6"><literal>parent2</literal>: A changeset ID. 1.1914 + Only set if the working directory was merged. The ID of 1.1915 + the revision that the working directory was merged with. 1.1916 + </para> 1.1917 + </listitem></itemizedlist> 1.1918 + 1.1919 + <para id="x_2e7">See also: <literal role="hook">preupdate</literal> 1.1920 + (<xref linkend="sec:hook:preupdate"/>) 1.1921 + </para> 1.1922 + 1.1923 + </sect2> 1.1924 + </sect1> 1.1925 +</chapter> 1.1926 + 1.1927 +<!-- 1.1928 +local variables: 1.1929 +sgml-parent-document: ("00book.xml" "book" "chapter") 1.1930 +end: 1.1931 +-->