Bryan O'Sullivan  committed 012df94

Start hook examples. First is for trailing whitespace.

  • Participants
  • Parent commits 7ac8576
  • Branches default

Comments (0)

Files changed (4)

 	examples/run-example \
 	examples/daily.files \
 	examples/hook.simple \
+	examples/ \
 	examples/mq.qinit-help \
 	examples/mq.diff \
 	examples/mq.tarball \

File en/examples/data/

+import os, re, sys
+count = 0
+for line in os.popen('hg export tip'):
+    # remember the name of the file that this diff affects
+    m = re.match(r'^--- [^/]/([^\t])', line)
+    if m: 
+	filename =
+	continue
+    # remember the line number
+    m = re.match(r'^@@ -(\d+),')
+    if m:
+        linenum =
+        continue
+    linenum += 1
+    # check for an added line with trailing whitespace
+    m = re.match(r'^\+.*\s$', line)
+    if m:
+	print >> sys.stderr, ('%s:%d: trailing whitespace introduced' %
+                              (filename, linenum))
+        count += 1
+if count:
+    # save the commit message so we don't need to retype it
+    os.system('hg tip --template "{desc}" > .hg/')
+    print >> sys.stderr, 'commit message saved to .hg/'

File en/examples/

+cp $EXAMPLE_DIR/data/ .
+hg init a
+cd a
+echo '[hooks]' > .hg/hgrc
+echo "pretxncommit.whitespace = hg export tip | (! grep -qP '^\\+.*[ \\t]$')" >> .hg/hgrc
+#$ name: simple
+cat .hg/hgrc
+echo 'a ' > a
+hg commit -A -m 'test with trailing whitespace'
 doesn't care about by dropping them into a keyword argument dict, as
 with \texttt{**kwargs} above.
+\section{Some hook examples}
+\subsection{Enforcing coding guidelines in your own repository}
+An interesting use of a commit-related hook is to help you to write
+cleaner code.  A simple example of ``cleaner code'' is the dictum that
+a change should not add any new lines of text that contain ``trailing
+whitespace''.  Trailing whitespace is a series of space and tab
+characters at the end of a line of text.  In most cases, trailing
+whitespace is unnecessary, invisible noise, but it is occasionally
+problematic, and people tend to prefer to get rid of it.
+You can use either the \hook{precommit} or \hook{pretxncommit} hook to
+tell whether you have a trailing whitespace problem.  If you use the
+\hook{precommit} hook, the hook will not know which files you are
+committing, so it will have to check every modified file in the
+repository for trailing white space.  If you want to commit a change
+to just the file \filename{foo}, but the file \filename{bar} contains
+trailing whitespace, doing a check in the \hook{precommit} hook will
+prevent you from committing \filename{foo} due to the problem with
+\filename{bar}.  This doesn't seem right.
+Should you choose the \hook{pretxncommit} hook, the check won't occur
+until just before the transaction for the commit completes.  This will
+allow you to check for problems only the exact files that are being
+committed.  However, if you entered the commit message interactively
+and the hook fails, the transaction will roll back; you'll have to
+re-enter the commit message after you fix the trailing whitespace and
+run \hgcmd{commit} again.
+  \interaction{}
+  \caption{A simple hook that checks for trailing whitespace}
+  \label{ex:hook:ws.simple}
+Figure~\ref{ex:hook:ws.simple} introduces a simple \hook{pretxncommit}
+hook that checks for trailing whitespace.  This hook is short, but not
+very helpful.  It exits with an error status if a change adds a line
+with trailing whitespace to any file, but does not print any
+information that might help us to identify the offending file or line.
 \section{Hook reference}