Last active
October 2, 2020 16:33
-
-
Save hartfordfive/9670011 to your computer and use it in GitHub Desktop.
Server-side pre-receive hook to validate puppet files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| COMMAND='puppet parser validate' | |
| TEMPDIR=`mktemp -d` | |
| echo "### Attempting to validate puppet files... ####" | |
| # See https://www.kernel.org/pub/software/scm/git/docs/githooks.html#pre-receive | |
| oldrev=$1 | |
| newrev=$2 | |
| refname=$3 | |
| while read oldrev newrev refname; do | |
| files=`git diff --name-only ${oldrev} ${newrev}` | |
| for file in ${files}; do | |
| object=`git ls-tree --full-name -r ${newrev} | egrep "(\s)${file}\$" | awk '{ print $3 }'` | |
| if [ -z ${object} ]; | |
| then | |
| continue; | |
| fi | |
| mkdir -p "${TEMPDIR}/`dirname ${file}`" &>/dev/null | |
| git cat-file blob ${object} > ${TEMPDIR}/${file} | |
| done; | |
| done | |
| # Now loop over each file in the temp dir to parse them for valid syntax | |
| files_found=`find ${TEMPDIR} -name '*.pp'` | |
| for fname in ${files_found}; do | |
| ${COMMAND} ${fname} | |
| if [[ $? -ne 0 ]]; | |
| then | |
| echo "ERROR: parser failed on ${fname}" | |
| bad_file=1 | |
| fi | |
| done; | |
| rm -rf ${TEMPDIR} &> /dev/null | |
| if [[ $bad_file -eq 1 ]] | |
| then | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rather search for a file's object and then retrieve the object, in a similar situation I chose to directly retrieve the file from the revision using git-show
git show ${newrev}:${file} > ${TEMPDIR}/${file}