hgbook

view en/hook.tex @ 37:9fd0c59b009a

Add to hook chapter.
Document each macro in 99defs.tex.
author Bryan O'Sullivan <bos@serpentine.com>
date Mon Jul 17 00:01:01 2006 -0700 (2006-07-17)
parents c0979ed1eabd
children b49a7dd4e564
line source
1 \chapter{Handling repository events with hooks}
2 \label{chap:hook}
4 Mercurial offers a powerful mechanism to let you perform automated
5 actions in response to events that occur in a repository. In some
6 cases, you can even control Mercurial's response to those events.
8 The name Mercurial uses for one of these actions is a \emph{hook}.
9 Hooks are called ``triggers'' in some revision control systems, but
10 the two names refer to the same idea.
12 \section{A short tutorial on using hooks}
13 \label{sec:hook:simple}
15 It is easy to write a Mercurial hook. Let's start with a hook that
16 runs when you finish a \hgcmd{commit}, and simply prints the hash of
17 the changeset you just created. The hook is called \hook{commit}.
19 \begin{figure}[ht]
20 \interaction{hook.simple.init}
21 \caption{A simple hook that runs when a changeset is committed}
22 \label{ex:hook:init}
23 \end{figure}
25 All hooks follow the pattern in example~\ref{ex:hook:init}. You add
26 an entry to the \rcsection{hooks} section of your \hgrc\. On the left
27 is the name of the event to trigger on; on the right is the action to
28 take. As you can see, you can run an arbitrary shell command in a
29 hook. Mercurial passes extra information to the hook using
30 environment variables (look for \envar{HG\_NODE} in the example).
32 \subsection{Performing multiple actions per event}
34 Quite often, you will want to define more than one hook for a
35 particular kind of event, as shown in example~\ref{ex:hook:ext}.
36 Mercurial lets you do this by adding an \emph{extension} to the end of
37 a hook's name. You extend a hook's name by giving the name of the
38 hook, followed by a full stop (the ``\texttt{.}'' character), followed
39 by some more text of your choosing. For example, Mercurial will run
40 both \texttt{commit.foo} and \texttt{commit.bar} when the
41 \texttt{commit} event occurs.
43 \begin{figure}[ht]
44 \interaction{hook.simple.ext}
45 \caption{Defining a second \hook{commit} hook}
46 \label{ex:hook:ext}
47 \end{figure}
49 To give a well-defined order of execution when there are multiple
50 hooks defined for an event, Mercurial sorts hooks by extension, and
51 executes the hook commands in this sorted order. In the above
52 example, it will execute \texttt{commit.bar} before
53 \texttt{commit.foo}, and \texttt{commit} before both.
55 It is a good idea to use a somewhat descriptive extension when you
56 define a new hook. This will help you to remember what the hook was
57 for. If the hook fails, you'll get an error message that contains the
58 hook name and extension, so using a descriptive extension could give
59 you an immediate hint as to why the hook failed (see
60 section~\ref{sec:hook:perm} for an example).
62 \subsection{Controlling whether an activity can proceed}
63 \label{sec:hook:perm}
65 In our earlier examples, we used the \hook{commit} hook, which is
66 run after a commit has completed. This is one of several Mercurial
67 hooks that run after an activity finishes. Such hooks have no way of
68 influencing the activity itself.
70 Mercurial defines a number of events that occur before an activity
71 starts; or after it starts, but before it finishes. Hooks that
72 trigger on these events have the added ability to choose whether the
73 activity can continue, or will abort.
75 The \hook{pretxncommit} hook runs after a commit has all but
76 completed. In other words, the metadata representing the changeset
77 has been written out to disk, but the transaction has not yet been
78 allowed to complete. The \hook{pretxncommit} hook has the ability to
79 decide whether the transaction can complete, or must be rolled back.
81 If the \hook{pretxncommit} hook exits with a status code of zero, the
82 transaction is allowed to complete; the commit finishes; and the
83 \hook{commit} hook is run. If the \hook{pretxncommit} hook exits with
84 a non-zero status code, the transaction is rolled back; the metadata
85 representing the changeset is erased; and the \hook{commit} hook is
86 not run.
88 \begin{figure}[ht]
89 \interaction{hook.simple.pretxncommit}
90 \caption{Using the \hook{pretxncommit} hook to control commits}
91 \label{ex:hook:pretxncommit}
92 \end{figure}
94 The hook in example~\ref{ex:hook:pretxncommit} checks that a commit
95 comment contains a bug ID. If it does, the commit can complete. If
96 not, the commit is rolled back.
98 \section{Writing your own hooks}
100 When you are writing a hook, you might find it useful to run Mercurial
101 either with the \hggopt{-v} option, or the \rcitem{ui}{verbose} config
102 item set to ``true''. When you do so, Mercurial will print a message
103 before it calls each hook.
105 \subsection{Choosing how your hook should run}
106 \label{sec:hook:lang}
108 You can write a hook either as a normal program---typically a shell
109 script---or as a Python function that is executed within the Mercurial
110 process.
112 Writing a hook as an external program has the advantage that it
113 requires no knowledge of Mercurial's internals. You can call normal
114 Mercurial commands to get any added information you need. The
115 trade-off is that external hooks are slower than in-process hooks.
117 An in-process Python hook has complete access to the Mercurial API,
118 and does not ``shell out'' to another process, so it is inherently
119 faster than an external hook. It is also easier to obtain much of the
120 information that a hook requires by using the Mercurial API than by
121 running Mercurial commands.
123 If you are comfortable with Python, or require high performance,
124 writing your hooks in Python may be a good choice. However, when you
125 have a straightforward hook to write and you don't need to care about
126 performance (probably the majority of hooks), a shell script is
127 perfectly fine.
129 \subsection{Hook parameters}
130 \label{sec:hook:param}
132 Mercurial calls each hook with a set of well-defined parameters. In
133 Python, a parameter is passed as a keyword argument to your hook
134 function. For an external program, a parameter is passed as an
135 environment variable.
137 Whether your hook is written in Python or as a shell script, the
138 hook-specific parameter names and values will be the same. A boolean
139 parameter will be represented as a boolean value in Python, but as the
140 number 1 (for ``true'') or 0 (for ``false'') as an environment
141 variable for an external hook. If a hook parameter is named
142 \texttt{foo}, the keyword argument for a Python hook will also be
143 named \texttt{foo} Python, while the environment variable for an
144 external hook will be named \texttt{HG\_FOO}.
146 \subsection{Hook return values and activity control}
148 A hook that executes successfully must exit with a status of zero if
149 external, or return boolean ``false'' if in-process. Failure is
150 indicated with a non-zero exit status from an external hook, or an
151 in-process hook returning boolean ``true''. If an in-process hook
152 raises an exception, the hook is considered to have failed.
154 For a hook that controls whether an activity can proceed, zero/false
155 means ``allow'', while non-zero/true/exception means ``deny''.
157 \subsection{Writing an external hook}
159 When you define an external hook in your \hgrc\ and the hook is run,
160 its value is passed to your shell, which interprets it. This means
161 that you can use normal shell constructs in the body of the hook.
163 An executable hook is always run with its current directory set to a
164 repository's root directory.
166 Each hook parameter is passed in as an environment variable; the name
167 is upper-cased, and prefixed with the string ``\texttt{HG\_}''.
169 With the exception of hook parameters, Mercurial does not set or
170 modify any environment variables when running a hook. This is useful
171 to remember if you are writing a site-wide hook that may be run by a
172 number of different users with differing environment variables set.
173 In multi-user situations, you should not rely on environment variables
174 being set to the values you have in your environment when testing the
175 hook.
177 \subsection{Telling Mercurial to use an in-process hook}
179 The \hgrc\ syntax for defining an in-process hook is slightly
180 different than for an executable hook. The value of the hook must
181 start with the text ``\texttt{python:}'', and continue with the
182 fully-qualified name of a callable object to use as the hook's value.
184 The module in which a hook lives is automatically imported when a hook
185 is run. So long as you have the module name and \envar{PYTHONPATH}
186 right, it should ``just work''.
188 The following \hgrc\ example snippet illustrates the syntax and
189 meaning of the notions we just described.
190 \begin{codesample2}
191 [hooks]
192 commit.example = python:mymodule.submodule.myhook
193 \end{codesample2}
194 When Mercurial runs the \texttt{commit.example} hook, it imports
195 \texttt{mymodule.submodule}, looks for the callable object named
196 \texttt{myhook}, and calls it.
198 \subsection{Writing an in-process hook}
200 The simplest in-process hook does nothing, but illustrates the basic
201 shape of the hook API:
202 \begin{codesample2}
203 def myhook(ui, repo, **kwargs):
204 pass
205 \end{codesample2}
206 The first argument to a Python hook is always a
207 \pymodclass{mercurial.ui}{ui} object. The second is a repository object;
208 at the moment, it is always an instance of
209 \pymodclass{mercurial.localrepo}{localrepository}. Following these two
210 arguments are other keyword arguments. Which ones are passed in
211 depends on the hook being called, but a hook can ignore arguments it
212 doesn't care about by dropping them into a keyword argument dict, as
213 with \texttt{**kwargs} above.
216 %%% Local Variables:
217 %%% mode: latex
218 %%% TeX-master: "00book"
219 %%% End: