Initial commit, ppg and ppg-initdevenv
[puppet-git.git] / ppg-initdevenv
blob812c17503c0f677134bec35435535dfd51cc8b3d
1 #!/bin/bash
3 GIT_DIR=$(git rev-parse --git-dir)
5 if [ -z "$GIT_DIR" ]; then
6 echo >&2 "Not in a git checkout"
7 exit 1
8 fi
10 cat << 'EOF' > $GIT_DIR/hooks/pre-commit.tmp$$
11 #!/bin/sh
13 # based on http://projects.puppetlabs.com/projects/1/wiki/Puppet_Version_Control
15 syntax_errors=0
16 error_msg=$(mktemp /tmp/error_msg.XXXXXX)
18 if git rev-parse --quiet --verify HEAD > /dev/null
19 then
20 against=HEAD
21 else
22 # Initial commit: diff against an empty tree object
23 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
26 # Get list of new/modified manifest and template files to check (in git index)
27 for indexfile in `git diff-index --diff-filter=AM --name-only --cached $against | egrep '\.(pp|erb)'`
29 # Don't check empty files
30 if [ `git cat-file -s :0:$indexfile` -gt 0 ]
31 then
32 case $indexfile in
33 *.pp )
34 # Check puppet manifest syntax
35 # Puppet 2.7.20 or newer
36 git cat-file blob :0:$indexfile | puppet parser validate > $error_msg ;;
37 *.erb )
38 # Check ERB template syntax
39 # -P : ignore lines which start with "%"
40 git cat-file blob :0:$indexfile | erb -P -x -T - | ruby -c 2> $error_msg > /dev/null ;;
41 esac
42 if [ "$?" -ne 0 ]
43 then
44 echo -n "$indexfile: "
45 cat $error_msg
46 syntax_errors=`expr $syntax_errors + 1`
49 done
51 rm -f $error_msg
53 if [ "$syntax_errors" -ne 0 ]
54 then
55 echo "Error: $syntax_errors syntax errors found, aborting commit."
56 exit 1
58 EOF
60 if ! cmp $GIT_DIR/hooks/pre-commit $GIT_DIR/hooks/pre-commit.tmp$$ ; then
61 cp $GIT_DIR/hooks/pre-commit{,.backup-$(date +%Y%m%d%H%M)}
62 mv $GIT_DIR/hooks/pre-commit.tmp$$ $GIT_DIR/hooks/pre-commit
64 chmod ugo+x $GIT_DIR/hooks/pre-commit