flock_multi: narrow down exceptions reported as error conditions
[puppet-git.git] / ppg-validate-staged-files
blob5d1c841e22efbf5d0d8f21bd4b9393f88a2845e4
1 #!/bin/bash
2 ##
3 ## Originally from http://projects.puppetlabs.com/projects/1/wiki/Puppet_Version_Control
4 ## with minor edits
5 ##
6 syntax_errors=0
7 error_msg=$(mktemp /tmp/error_msg.XXXXXX)
9 if git rev-parse --quiet --verify HEAD > /dev/null
10 then
11 against=HEAD
12 else
13 # Initial commit: diff against an empty tree object
14 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
17 # Get list of new/modified manifest and template files to check (in git index)
18 for indexfile in $(git diff-index --diff-filter=AM --name-only --cached $against | egrep '\.(pp|erb)')
20 # Don't check empty files
21 if [ $(git cat-file -s :0:$indexfile) -gt 0 ]
22 then
23 case $indexfile in
24 *.pp )
25 # Check puppet manifest syntax
26 # Puppet 2.7.20 or newer
27 git cat-file blob :0:$indexfile | puppet parser validate > $error_msg ;;
28 *.erb )
29 # Check ERB template syntax
30 # -P : ignore lines which start with "%"
31 git cat-file blob :0:$indexfile | erb -P -x -T - | ruby -c 2> $error_msg > /dev/null ;;
32 esac
33 if [ "$?" -ne 0 ]
34 then
35 echo -n "$indexfile: "
36 cat $error_msg
37 syntax_errors=`expr $syntax_errors + 1`
40 done
42 rm -f $error_msg
44 if [ "$syntax_errors" -ne 0 ]
45 then
46 echo "Error: $syntax_errors syntax errors found, aborting commit."
47 exit 1