Skip to content

Instantly share code, notes, and snippets.

Created February 24, 2013 14:30
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# Adapted from a puppet pre-commit hook at:
# install this as .git/hooks/pre-commit to check DNS zone files
# for errors before committing changes.
[ "$SKIP_PRECOMMIT_HOOK" = 1 ] && exit 0
# Make sure we're at top level of repository.
cd $(git rev-parse --show-toplevel)
trap 'rm -rf $tmpdir $tmpfile1' EXIT INT HUP
tmpdir=$(mktemp -d precommitXXXXXX)
tmpfile1=$(mktemp errXXXXXX)
echo "$(basename $0): Validating changes."
# Here we copy files out of the index into a temporary directory. This
# protects us from a the situation in which we have staged an invalid
# zone file using ``git add`` but corrected the changes in the
# working directory. If we checked the files "in place", we would
# fail to detect the errors.
git diff-index --cached --name-only HEAD |
grep '^db\.' |
git checkout-index --stdin --prefix=$tmpdir/
find $tmpdir -type f -name 'db.*' |
while read zonefile; do
zone=`echo $zonefile | sed -e "s/^${tmpdir}\/db\.\(.*\)$/\1/"`
named-checkzone -q $zone $zonefile
# If named-checkzone reports an error, get some output:
if [ $? -ne 0 ]; then
named-checkzone $zone $zonefile | sed "s#$tmpdir/##" >> $tmpfile1 2>&1
if [ -s "$tmpfile1" ]; then
echo Error: Zone file problem:
echo ----------------------------
cat $tmpfile1
echo ----------------------------
exit $rc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment