comparison en/hook.tex @ 49:18210d46491f

More hook content.
author Bryan O'Sullivan <bos@serpentine.com>
date Tue, 25 Jul 2006 00:18:31 -0700
parents 012df94a02fe
children 497aa3c9d4ce
comparison
equal deleted inserted replaced
48:e8ab09bf5d7d 49:18210d46491f
413 doesn't care about by dropping them into a keyword argument dict, as 413 doesn't care about by dropping them into a keyword argument dict, as
414 with \texttt{**kwargs} above. 414 with \texttt{**kwargs} above.
415 415
416 \section{Some hook examples} 416 \section{Some hook examples}
417 417
418 \subsection{Enforcing coding guidelines in your own repository} 418 \subsection{Writing meaningful commit messages}
419
420 It's hard to imagine a useful commit message being very short. The
421 simple \hook{pretxncommit} hook of figure~\ref{ex:hook:msglen.run}
422 will prevent you from committing a changeset with a message that is
423 less than ten bytes long.
424
425 \begin{figure}[ht]
426 \interaction{hook.msglen.run}
427 \caption{A hook that forbids overly short commit messages}
428 \label{ex:hook:msglen.run}
429 \end{figure}
430
431 \subsection{Checking for trailing whitespace}
419 432
420 An interesting use of a commit-related hook is to help you to write 433 An interesting use of a commit-related hook is to help you to write
421 cleaner code. A simple example of ``cleaner code'' is the dictum that 434 cleaner code. A simple example of ``cleaner code'' is the dictum that
422 a change should not add any new lines of text that contain ``trailing 435 a change should not add any new lines of text that contain ``trailing
423 whitespace''. Trailing whitespace is a series of space and tab 436 whitespace''. Trailing whitespace is a series of space and tab
424 characters at the end of a line of text. In most cases, trailing 437 characters at the end of a line of text. In most cases, trailing
425 whitespace is unnecessary, invisible noise, but it is occasionally 438 whitespace is unnecessary, invisible noise, but it is occasionally
426 problematic, and people tend to prefer to get rid of it. 439 problematic, and people often prefer to get rid of it.
427 440
428 You can use either the \hook{precommit} or \hook{pretxncommit} hook to 441 You can use either the \hook{precommit} or \hook{pretxncommit} hook to
429 tell whether you have a trailing whitespace problem. If you use the 442 tell whether you have a trailing whitespace problem. If you use the
430 \hook{precommit} hook, the hook will not know which files you are 443 \hook{precommit} hook, the hook will not know which files you are
431 committing, so it will have to check every modified file in the 444 committing, so it will have to check every modified file in the
451 464
452 Figure~\ref{ex:hook:ws.simple} introduces a simple \hook{pretxncommit} 465 Figure~\ref{ex:hook:ws.simple} introduces a simple \hook{pretxncommit}
453 hook that checks for trailing whitespace. This hook is short, but not 466 hook that checks for trailing whitespace. This hook is short, but not
454 very helpful. It exits with an error status if a change adds a line 467 very helpful. It exits with an error status if a change adds a line
455 with trailing whitespace to any file, but does not print any 468 with trailing whitespace to any file, but does not print any
456 information that might help us to identify the offending file or line. 469 information that might help us to identify the offending file or
470 line. It also has the nice property of not paying attention to
471 unmodified lines; only lines that introduce new trailing whitespace
472 cause problems.
473
474 \begin{figure}[ht]
475 \interaction{hook.ws.better}
476 \caption{A better trailing whitespace hook}
477 \label{ex:hook:ws.better}
478 \end{figure}
479
480 The example of figure~\ref{ex:hook:ws.better} is much more complex,
481 but also more useful. It parses a unified diff to see if any lines
482 add trailing whitespace, and prints the name of the file and the line
483 number of each such occurrence. Even better, if the change adds
484 trailing whitespace, this hook saves the commit comment and prints the
485 name of the save file before exiting and telling Mercurial to roll the
486 transaction back, so you can use
487 \hgcmdargs{commit}{\hgopt{commit}{-l}~\emph{filename}} to reuse the
488 saved commit message once you've corrected the problem.
489
490 As a final aside, note in figure~\ref{ex:hook:ws.better} the use of
491 \command{perl}'s in-place editing feature to get rid of trailing
492 whitespace from a file. This is concise and useful enough that I will
493 reproduce it here.
494 \begin{codesample2}
495 perl -pi -e 's,\\s+$,,' filename
496 \end{codesample2}
497
498 \section{Bundled hooks}
499
500 Mercurial ships with several bundled hooks. You can find them in the
501 \dirname{hgext} directory of a Mercurial source tree. If you are
502 using a Mercurial binary package, the hooks will be located in the
503 \dirname{hgext} directory of wherever your package installer put
504 Mercurial.
505
506 \subsection{\hgext{acl}---access control for parts of a repository}
507
508 The \hgext{acl} extension lets you control which remote users are
509 allowed to push changesets to a networked server. You can protect any
510 portion of a repository (including the entire repo), so that a
511 specific remote user can push changes that do not affect the protected
512 portion.
513
514 This extension implements access control based on the identity of the
515 user performing a push, \emph{not} on who committed the changesets
516 they're pushing. (If access control based on committer was to work
517 properly, it would require commits to be cryptographically signed,
518 which is an onerous and hence unusual policy to enforce.)
519
520 XXX More help.
457 521
458 \section{Hook reference} 522 \section{Hook reference}
459 \label{sec:hook:ref} 523 \label{sec:hook:ref}
460 524
461 \subsection{In-process hook execution} 525 \subsection{In-process hook execution}