hgbook

changeset 41:d1a3394f8bcf

More hook content.
author Bryan O'Sullivan <bos@serpentine.com>
date Thu Jul 20 19:42:50 2006 -0700 (2006-07-20)
parents b2fe9964b21b
children cbfa08bcf181 b727a63518d4
files en/hook.tex
line diff
     1.1 --- a/en/hook.tex	Wed Jul 19 23:25:30 2006 -0700
     1.2 +++ b/en/hook.tex	Thu Jul 20 19:42:50 2006 -0700
     1.3 @@ -11,103 +11,73 @@
     1.4  
     1.5  \section{An overview of hooks in Mercurial}
     1.6  
     1.7 -Here is a brief list of the hooks that Mercurial supports. For each
     1.8 -hook, we indicate when it is run, and a few examples of common tasks
     1.9 -you can use it for.  We will revisit each of these hooks in more
    1.10 -detail later.
    1.11 +Here is a brief list of the hooks that Mercurial supports.  We will
    1.12 +revisit each of these hooks in more detail later, in
    1.13 +section~\ref{sec:hook:ref}.
    1.14 +
    1.15  \begin{itemize}
    1.16  \item[\small\hook{changegroup}] This is run after a group of
    1.17 -  changesets has been brought into the repository from elsewhere.  In
    1.18 -  other words, it is run after a \hgcmd{pull} or \hgcmd{push} into a
    1.19 -  repository, but not after a \hgcmd{commit}.  You can use this for
    1.20 -  performing an action once for the entire group of newly arrived
    1.21 -  changesets.  For example, you could use this hook to send out email
    1.22 -  notifications, or kick off an automated build or test.
    1.23 +  changesets has been brought into the repository from elsewhere.
    1.24  \item[\small\hook{commit}] This is run after a new changeset has been
    1.25 -  created in the local repository, typically using the \hgcmd{commit}
    1.26 -  command.
    1.27 +  created in the local repository.
    1.28  \item[\small\hook{incoming}] This is run once for each new changeset
    1.29    that is brought into the repository from elsewhere.  Notice the
    1.30    difference from \hook{changegroup}, which is run once per
    1.31 -  \emph{group} of changesets brought in.  You can use this for the
    1.32 -  same purposes as the \hook{changegroup} hook; it's simply more
    1.33 -  convenient sometimes to run a hook once per group of changesets,
    1.34 -  while othher times it's handier once per changeset.
    1.35 +  \emph{group} of changesets brought in.
    1.36  \item[\small\hook{outgoing}] This is run after a group of changesets
    1.37 -  has been transmitted from this repository to another.  You can use
    1.38 -  this, for example, to notify subscribers every time changes are
    1.39 -  cloned or pulled from the repository.
    1.40 +  has been transmitted from this repository.
    1.41  \item[\small\hook{prechangegroup}] This is run before starting to
    1.42 -  bring a group of changesets into the repository.  It cannot see the
    1.43 -  actual changesets, because they have not yet been transmitted.  If
    1.44 -  it fails, the changesets will not be transmitted.  You can use this
    1.45 -  hook to ``lock down'' a repository against incoming changes.
    1.46 -\item[\small\hook{precommit}] This is run before starting a commit.
    1.47 -  It cannot tell what files are included in the commit, or any other
    1.48 -  information about the commit.  If it fails, the commit will not be
    1.49 -  allowed to start.  You can use this to perform a build and require
    1.50 -  it to complete successfully before a commit can proceed, or
    1.51 -  automatically enforce a requirement that modified files pass your
    1.52 -  coding style guidelines.
    1.53 -\item[\small\hook{preoutgoing}] This is run before starting to
    1.54 -  transmit a group of changesets from this repository.  You can use
    1.55 -  this to lock a repository against clones or pulls from remote
    1.56 -  clients.
    1.57 -\item[\small\hook{pretag}] This is run before creating a tag.  If it
    1.58 -  fails, the tag will not be created.  You can use this to enforce a
    1.59 -  uniform tag naming convention.
    1.60 -\item[\small\hook{pretxnchangegroup}] This is run after a group of
    1.61 -  changesets has been brought into the local repository from another,
    1.62 -  but before the transaction completes that will make the changes
    1.63 -  permanent in the repository.  If it fails, the transaction will be
    1.64 -  rolled back and the changes will disappear from the local
    1.65 -  repository.  You can use this to automatically check newly arrived
    1.66 -  changes and, for example, roll them back if the group as a whole
    1.67 -  does not build or pass your test suite.
    1.68 -\item[\small\hook{pretxncommit}] This is run after a new changeset has
    1.69 -  been created in the local repository, but before the transaction
    1.70 -  completes that will make it permanent.  Unlike the \hook{precommit}
    1.71 -  hook, this hook can see which changes are present in the changeset,
    1.72 -  and it can also see all other changeset metadata, such as the commit
    1.73 -  message.  You can use this to require that a commit message follows
    1.74 -  your local conventions, or that a changeset builds cleanly.
    1.75 -\item[\small\hook{preupdate}] This is run before starting an update or
    1.76 -  merge of the working directory.
    1.77 +  bring a group of changesets into the repository.
    1.78 +\item[\small\hook{precommit}] Controlling. This is run before starting
    1.79 +  a commit.
    1.80 +\item[\small\hook{preoutgoing}] Controlling. This is run before
    1.81 +  starting to transmit a group of changesets from this repository.
    1.82 +\item[\small\hook{pretag}] Controlling. This is run before creating a tag.
    1.83 +\item[\small\hook{pretxnchangegroup}] Controlling. This is run after a
    1.84 +  group of changesets has been brought into the local repository from
    1.85 +  another, but before the transaction completes that will make the
    1.86 +  changes permanent in the repository.
    1.87 +\item[\small\hook{pretxncommit}] Controlling. This is run after a new
    1.88 +  changeset has been created in the local repository, but before the
    1.89 +  transaction completes that will make it permanent.
    1.90 +\item[\small\hook{preupdate}] Controlling. This is run before starting
    1.91 +  an update or merge of the working directory.
    1.92  \item[\small\hook{tag}] This is run after a tag is created.
    1.93  \item[\small\hook{update}] This is run after an update or merge of the
    1.94    working directory has finished.
    1.95  \end{itemize}
    1.96 -Each of the hooks with a ``\texttt{pre}'' prefix has the ability to
    1.97 -\emph{control} an activity.  If the hook succeeds, the activity may
    1.98 -proceed; if it fails, the activity is either not permitted or undone,
    1.99 -depending on the hook.
   1.100 +Each of the hooks whose description begins with the word
   1.101 +``Controlling'' has the ability to determine whether an activity can
   1.102 +proceed.  If the hook succeeds, the activity may proceed; if it fails,
   1.103 +the activity is either not permitted or undone, depending on the hook.
   1.104  
   1.105  \section{Hooks and security}
   1.106  
   1.107  \subsection{Hooks are run with your privileges}
   1.108  
   1.109  When you run a Mercurial command in a repository, and the command
   1.110 -causes a hook to run, that hook runs on your system, under your user
   1.111 -account, with your privilege level.  Since hooks are arbitrary pieces
   1.112 -of executable code, you should treat them with an appropriate level of
   1.113 -suspicion.  Do not install a hook unless you are confident that you
   1.114 -know who created it and what it does.
   1.115 +causes a hook to run, that hook runs on \emph{your} system, under
   1.116 +\emph{your} user account, with \emph{your} privilege level.  Since
   1.117 +hooks are arbitrary pieces of executable code, you should treat them
   1.118 +with an appropriate level of suspicion.  Do not install a hook unless
   1.119 +you are confident that you know who created it and what it does.
   1.120  
   1.121  In some cases, you may be exposed to hooks that you did not install
   1.122  yourself.  If you work with Mercurial on an unfamiliar system,
   1.123  Mercurial will run hooks defined in that system's global \hgrc\ file.
   1.124  
   1.125  If you are working with a repository owned by another user, Mercurial
   1.126 -will run hooks defined in that repository.  For example, if you
   1.127 -\hgcmd{pull} from that repository, and its \sfilename{.hg/hgrc}
   1.128 -defines a local \hook{outgoing} hook, that hook will run under your
   1.129 -user account, even though you don't own that repository.
   1.130 +can run hooks defined in that user's repository, but it will still run
   1.131 +them as ``you''.  For example, if you \hgcmd{pull} from that
   1.132 +repository, and its \sfilename{.hg/hgrc} defines a local
   1.133 +\hook{outgoing} hook, that hook will run under your user account, even
   1.134 +though you don't own that repository.
   1.135  
   1.136  \begin{note}
   1.137    This only applies if you are pulling from a repository on a local or
   1.138    network filesystem.  If you're pulling over http or ssh, any
   1.139 -  \hook{outgoing} hook will run under the account of the server
   1.140 -  process, on the server.
   1.141 +  \hook{outgoing} hook will run under whatever account is executing
   1.142 +  the server process, on the server.
   1.143  \end{note}
   1.144  
   1.145  XXX To see what hooks are defined in a repository, use the
   1.146 @@ -178,6 +148,68 @@
   1.147  ensure that all changes that people pull have been automatically
   1.148  vetted.
   1.149  
   1.150 +\section{Using hooks with shared access to a repository}
   1.151 +
   1.152 +If you want to use hooks to so some automated work in a repository
   1.153 +that a number of people have ahred access to, you need to be careful
   1.154 +in how you do this.
   1.155 +
   1.156 +Mercurial only locks a repository when it is writing to the
   1.157 +repository, and only the parts of Mercurial that write to the
   1.158 +repository pay attention to locks.  Write locks are necessary to
   1.159 +prevent multiple simultaneous writers from scribbling on each other's
   1.160 +work, corrupting the repository.
   1.161 +
   1.162 +Because Mercurial is careful with the order in which it reads and
   1.163 +writes data, it does not need to acquire a lock when it wants to read
   1.164 +data from the repository.  The parts of Mercurial that read from the
   1.165 +repository never pay attention to locks.  This lockless reading scheme
   1.166 +greatly increases performance and concurrency.
   1.167 +
   1.168 +With great performance comes a trade-off, though, one which has the
   1.169 +potential to cause you trouble unless you're aware of it.  To describe
   1.170 +this requires a little detail about how Mercurial adds changesets to a
   1.171 +repository and reads those changes.
   1.172 +
   1.173 +When Mercurial \emph{writes} metadata, it writes it straight into the
   1.174 +destination file.  It writes file data first, then manifest data
   1.175 +(which contains pointers to the new file data), then changelog data
   1.176 +(which contains pointers to the new manifest data).  Before the first
   1.177 +write to each file, it stores a record of where the end of the file
   1.178 +was in its transaction log.  If the transaction must be rolled back,
   1.179 +Mercurial simply truncates each file back to te size it was before the
   1.180 +transaction began.
   1.181 +
   1.182 +When Mercurial \emph{reads} metadata, it reads the changelog first,
   1.183 +then everything else.  Since a reader will only access parts of the
   1.184 +manifest or file metadata that it can see in the changelog, it can
   1.185 +never see partially written data.
   1.186 +
   1.187 +Some controlling hooks (\hook{pretxncommit} and
   1.188 +\hook{pretxnchangegroup}) run when a transaction is almost complete.
   1.189 +All of the metadata has been written, but Mercurial can still roll the
   1.190 +transaction back and cause the newly-written data to disappear.
   1.191 +
   1.192 +If one of these hooks runs for long, it opens a window in which a
   1.193 +reader can see the metadata for changesets that are, strictly
   1.194 +speaking, not yet permanent.  The longer the hook runs, the bigger the
   1.195 +window.
   1.196 +
   1.197 +A good use for the \hook{pretxnchangegroup} hook would be to
   1.198 +automatically build and test incoming changes before they are accepted
   1.199 +into the repository, so that you can guarantee that nobody can push
   1.200 +changes to this repository that ``break the build''.  But if a client
   1.201 +can pull changes while they're being tested, the usefulness of the
   1.202 +test is zero; someone can pull untested changes.
   1.203 +
   1.204 +The safest answer to this challenge is to set up such a ``gatekeeper''
   1.205 +repository as \emph{unidirectional}.  It can take changes pushed in
   1.206 +from the outside, but nobody can pull changes from it.  Use the
   1.207 +\hook{preoutgoing} hook to lock it down.  Configure a
   1.208 +\hook{changegroup} hook so that if a build or test succeeds, the hook
   1.209 +will push the new changes out to another repository that people
   1.210 +\emph{can} pull from.
   1.211 +
   1.212  \section{A short tutorial on using hooks}
   1.213  \label{sec:hook:simple}
   1.214  
   1.215 @@ -382,7 +414,7 @@
   1.216  with \texttt{**kwargs} above.
   1.217  
   1.218  \section{Hook reference}
   1.219 -
   1.220 +\label{sec:hook:ref}
   1.221  
   1.222  \subsection{In-process hook execution}
   1.223  
   1.224 @@ -445,7 +477,13 @@
   1.225  This hook is run after a group of pre-existing changesets has been
   1.226  added to the repository, for example via a \hgcmd{pull} or
   1.227  \hgcmd{unbundle}.  This hook is run once per operation that added one
   1.228 -or more changesets.
   1.229 +or more changesets.  This is in contrast to the \hook{incoming} hook,
   1.230 +which is run once per changeset, regardless of whether the changesets
   1.231 +arrive in a group.
   1.232 +
   1.233 +Some possible uses for this hook include kicking off an automated
   1.234 +build or test of the added changesets, updating a bug database, or
   1.235 +notifying subscribers that a repository contains new changes.
   1.236  
   1.237  Parameters to this hook:
   1.238  \begin{itemize}
   1.239 @@ -485,6 +523,11 @@
   1.240  was added in a single operation, this hook is called once for each
   1.241  added changeset.
   1.242  
   1.243 +You can use this hook for the same purposes as the \hook{changegroup}
   1.244 +hook (section~\ref{sec:hook:changegroup}); it's simply more convenient
   1.245 +sometimes to run a hook once per group of changesets, while othher
   1.246 +times it's handier once per changeset.
   1.247 +
   1.248  Parameters to this hook:
   1.249  \begin{itemize}
   1.250  \item[\texttt{node}] A changeset ID.  The ID of the newly added
   1.251 @@ -500,6 +543,9 @@
   1.252  of this repository, for example by a \hgcmd{push} or \hgcmd{bundle}
   1.253  command.
   1.254  
   1.255 +One possible use for this hook is to notify administrators that
   1.256 +changes have been pulled.
   1.257 +
   1.258  Parameters to this hook:
   1.259  \begin{itemize}
   1.260  \item[\texttt{node}] A changeset ID.  The changeset ID of the first
   1.261 @@ -517,6 +563,18 @@
   1.262  \subsection{The \hook{prechangegroup} hook}
   1.263  \label{sec:hook:prechangegroup}
   1.264  
   1.265 +This controlling hook is run before Mercurial begins to add a group of
   1.266 +changesets from another repository.
   1.267 +
   1.268 +This hook does not have any information about the changesets to be
   1.269 +added, because it is run before transmission of those changesets is
   1.270 +allowed to begin.  If this hook fails, the changesets will not be
   1.271 +transmitted.
   1.272 +
   1.273 +One use for this hook is to prevent external changes from being added
   1.274 +to a repository, for example to ``freeze'' a server-hosted branch
   1.275 +temporarily or permanently.
   1.276 +
   1.277  This hook is not passed any parameters.
   1.278  
   1.279  See also: \hook{changegroup} (section~\ref{sec:hook:changegroup}),
   1.280 @@ -526,8 +584,15 @@
   1.281  \subsection{The \hook{precommit} hook}
   1.282  \label{sec:hook:precommit}
   1.283  
   1.284 -This hook is invoked before Mercurial has obtained any of the metadata
   1.285 -for the commit, such as the commit message or date.
   1.286 +This hook is run before Mercurial begins to commit a new changeset.
   1.287 +It is run before Mercurial has any of the metadata for the commit,
   1.288 +such as the files to be committed, the commit message, or the commit
   1.289 +date.
   1.290 +
   1.291 +One use for this hook is to disable the ability to commit new
   1.292 +changesets, while still allowing incoming changesets.  Another is to
   1.293 +run a build or test, and only allow the commit to begin if the build
   1.294 +or test succeeds.
   1.295  
   1.296  Parameters to this hook:
   1.297  \begin{itemize}
   1.298 @@ -548,6 +613,9 @@
   1.299  This hook is invoked before Mercurial knows the identities of the
   1.300  changesets to be transmitted.
   1.301  
   1.302 +One use for this hook is to prevent changes from being transmitted to
   1.303 +another repository.
   1.304 +
   1.305  Parameters to this hook:
   1.306  \begin{itemize}
   1.307  \item[\texttt{source}] A string.  The source of the operation that is
   1.308 @@ -562,6 +630,10 @@
   1.309  \subsection{The \hook{pretag} hook}
   1.310  \label{sec:hook:pretag}
   1.311  
   1.312 +This controlling hook is run before a tag is created.  If the hook
   1.313 +succeeds, creation of the tag proceeds.  If the hook fails, the tag is
   1.314 +not created.
   1.315 +
   1.316  Parameters to this hook:
   1.317  \begin{itemize}
   1.318  \item[\texttt{local}] A boolean.  Whether the tag is local to this
   1.319 @@ -580,6 +652,26 @@
   1.320  \subsection{The \hook{pretxnchangegroup} hook}
   1.321  \label{sec:hook:pretxnchangegroup}
   1.322  
   1.323 +This controlling hook is run before a transaction---that manages the
   1.324 +addition of a group of new changesets from outside the
   1.325 +repository---completes.  If the hook succeeds, the transaction
   1.326 +completes, and all of the changesets become permanent within this
   1.327 +repository.  If the hook fails, the transaction is rolled back, and
   1.328 +the data for the changesets is erased.
   1.329 +
   1.330 +This hook can access the metadata associated with the almost-added
   1.331 +changesets, but it should not do anything permanent with this data.
   1.332 +It must also not modify the working directory.
   1.333 +
   1.334 +While this hook is running, if other Mercurial processes access this
   1.335 +repository, they will be able to see the almost-added changesets as if
   1.336 +they are permanent.  This may lead to race conditions if you do not
   1.337 +take steps to avoid them.
   1.338 +
   1.339 +This hook can be used to automatically vet a group of changesets.  If
   1.340 +the hook fails, all of the changesets are ``rejected'' when the
   1.341 +transaction rolls back.
   1.342 +
   1.343  Parameters to this hook are the same as for the \hook{changegroup}
   1.344  hook; see section~\ref{sec:hook:changegroup} for details.
   1.345  
   1.346 @@ -590,6 +682,21 @@
   1.347  \subsection{The \hook{pretxncommit} hook}
   1.348  \label{sec:hook:pretxncommit}
   1.349  
   1.350 +This controlling hook is run before a transaction---that manages a new
   1.351 +commit---completes.  If the hook succeeds, the transaction completes
   1.352 +and the changeset becomes permanent within this repository.  If the
   1.353 +hook fails, the transaction is rolled back, and the commit data is
   1.354 +erased.
   1.355 +
   1.356 +This hook can access the metadata associated with the almost-new
   1.357 +changeset, but it should not do anything permanent with this data.  It
   1.358 +must also not modify the working directory.
   1.359 +
   1.360 +While this hook is running, if other Mercurial processes access this
   1.361 +repository, they will be able to see the almost-new changeset as if it
   1.362 +is permanent.  This may lead to race conditions if you do not take
   1.363 +steps to avoid them.
   1.364 +
   1.365  Parameters to this hook are the same as for the \hook{commit} hook;
   1.366  see section~\ref{sec:hook:commit} for details.
   1.367  
   1.368 @@ -598,6 +705,12 @@
   1.369  \subsection{The \hook{preupdate} hook}
   1.370  \label{sec:hook:preupdate}
   1.371  
   1.372 +This controlling hook is run before an update or merge of the working
   1.373 +directory begins.  It is run only if Mercurial's normal pre-update
   1.374 +checks determine that the update or merge can proceed.  If the hook
   1.375 +succeeds, the update or merge may proceed; if it fails, the update or
   1.376 +merge does not start.
   1.377 +
   1.378  Parameters to this hook:
   1.379  \begin{itemize}
   1.380  \item[\texttt{parent1}] A changeset ID.  The ID of the parent that the
   1.381 @@ -613,17 +726,24 @@
   1.382  \subsection{The \hook{tag} hook}
   1.383  \label{sec:hook:tag}
   1.384  
   1.385 +This hook is run after a tag has been created.
   1.386 +
   1.387  Parameters to this hook are the same as for the \hook{pretag} hook;
   1.388  see section~\ref{sec:hook:pretag} for details.
   1.389  
   1.390  If the created tag is revision-controlled, the \hook{commit} hook
   1.391 -(section~\ref{sec:hook:commit}) will also be run.
   1.392 +(section~\ref{sec:hook:commit}) is run before this hook.
   1.393  
   1.394  See also: \hook{pretag} (section~\ref{sec:hook:pretag})
   1.395  
   1.396  \subsection{The \hook{update} hook}
   1.397  \label{sec:hook:update}
   1.398  
   1.399 +This hook is run after an update or merge of the working directory
   1.400 +completes.  Since a merge can fail (if the external \command{hgmerge}
   1.401 +command fails to resolve conflicts in a file), this hook communicates
   1.402 +whether the update or merge completed cleanly.
   1.403 +
   1.404  \begin{itemize}
   1.405  \item[\texttt{error}] A boolean.  Indicates whether the update or
   1.406    merge completed successfully.