comparison en/hook.tex @ 44:012df94a02fe

Start hook examples. First is for trailing whitespace.
author Bryan O'Sullivan <bos@serpentine.com>
date Sun, 23 Jul 2006 23:25:52 -0700
parents d1a3394f8bcf
children 18210d46491f
comparison
equal deleted inserted replaced
43:7ac85766db0f 44:012df94a02fe
411 arguments are other keyword arguments. Which ones are passed in 411 arguments are other keyword arguments. Which ones are passed in
412 depends on the hook being called, but a hook can ignore arguments it 412 depends on the hook being called, but a hook can ignore arguments it
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}
417
418 \subsection{Enforcing coding guidelines in your own repository}
419
420 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
422 a change should not add any new lines of text that contain ``trailing
423 whitespace''. Trailing whitespace is a series of space and tab
424 characters at the end of a line of text. In most cases, trailing
425 whitespace is unnecessary, invisible noise, but it is occasionally
426 problematic, and people tend to prefer to get rid of it.
427
428 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
430 \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
432 repository for trailing white space. If you want to commit a change
433 to just the file \filename{foo}, but the file \filename{bar} contains
434 trailing whitespace, doing a check in the \hook{precommit} hook will
435 prevent you from committing \filename{foo} due to the problem with
436 \filename{bar}. This doesn't seem right.
437
438 Should you choose the \hook{pretxncommit} hook, the check won't occur
439 until just before the transaction for the commit completes. This will
440 allow you to check for problems only the exact files that are being
441 committed. However, if you entered the commit message interactively
442 and the hook fails, the transaction will roll back; you'll have to
443 re-enter the commit message after you fix the trailing whitespace and
444 run \hgcmd{commit} again.
445
446 \begin{figure}[ht]
447 \interaction{hook.ws.simple}
448 \caption{A simple hook that checks for trailing whitespace}
449 \label{ex:hook:ws.simple}
450 \end{figure}
451
452 Figure~\ref{ex:hook:ws.simple} introduces a simple \hook{pretxncommit}
453 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
455 with trailing whitespace to any file, but does not print any
456 information that might help us to identify the offending file or line.
457
416 \section{Hook reference} 458 \section{Hook reference}
417 \label{sec:hook:ref} 459 \label{sec:hook:ref}
418 460
419 \subsection{In-process hook execution} 461 \subsection{In-process hook execution}
420 462