Created
September 29, 2014 20:14
-
-
Save abrader/d5e7581c11f36583cc06 to your computer and use it in GitHub Desktop.
<git repo>/.git/hooks/pre-commit for Puppet developers
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/sh | |
| # If we don't have a HEAD, then this is the first commit and we can't do any of this | |
| git show > /dev/null 2>&1 | |
| if [ $? -ne 0 ]; then exit 0; fi | |
| # first stash any on-disk changes so we're actually validating | |
| # what's staged to be committed and not just what's on disk. | |
| git diff --full-index --binary > /tmp/stash.$$ | |
| git stash -q --keep-index | |
| EXITCODE=0 | |
| for file in `git diff-index --cached --diff-filter=AM --name-only HEAD` | |
| do | |
| echo "Validating ${file}..." | |
| case "${file##*.}" in | |
| "pp") puppet parser validate ${file} | |
| ;; | |
| "erb") /opt/puppet/bin/erb -P -x -T '-' ${file} | /opt/puppet/bin/ruby -c >/dev/null | |
| ;; | |
| esac | |
| EXITCODE=$((EXITCODE + $?)) | |
| done | |
| if [ $EXITCODE -ne 0 ] | |
| then | |
| echo | |
| echo "################################################################" | |
| echo -e "### \033[31mPlease fix the errors above before committing your code.\033[0m ###" | |
| echo "################################################################" | |
| if [[ -s /tmp/stash.$$ ]] | |
| then | |
| echo "### ###" | |
| echo -e "### \033[31mDid you remember to git add your updated code?\033[0m ###" | |
| echo "### ###" | |
| echo "################################################################" | |
| fi | |
| echo | |
| fi | |
| # now recover diffs to get back to pre commit state if needed. | |
| if [[ -s /tmp/stash.$$ ]] | |
| then | |
| git apply --whitespace=nowarn < /tmp/stash.$$ && git stash drop -q | |
| rm /tmp/stash.$$ | |
| fi | |
| exit $EXITCODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment