aboutsummaryrefslogtreecommitdiff
blob: e6b16897ec5949bf8f19e445a0843ce388c94281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# gentoo-infra: infra/githooks.git:local/update.secondary.d/puppet-validate
# Taken from http://projects.puppetlabs.com/projects/puppet/wiki/puppet_version_control#Git-Update-Hook

NOBOLD="\033[0m"
BOLD="\033[1m"
BLACK="\033[30m"
GREY="\033[0m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"

# For Puppet 2.7.x:
# syntax_check="puppet parser validate --ignoreimport"
#
# NOTE: There is an outstanding bug against `puppet parser` which causes
#       the --ignoreimport option to turn the syntax check into a no-op. Until
#       the bug is resolved, the syntax check hook should not include the
#       --ignoreimport option and will only work correctly on manifests which
#       do not contain "import" lines.
#       See http://projects.puppetlabs.com/issues/9670
#
syntax_check="puppet parser validate"
tmp=$(mktemp /tmp/git.update.XXXXXX)
log=$(mktemp /tmp/git.update.log.XXXXXX)
tree=$(mktemp /tmp/git.diff-tree.XXXXXX)

git diff-tree -r "$2" "$3" > "$tree"

echo
echo diff-tree:
cat "$tree"

exit_status=0

while read old_mode new_mode old_sha1 new_sha1 status name; do
    # skip lines showing parent commit
    test -z "$new_sha1" && continue
    # skip deletions
    [ "$new_sha1" = "0000000000000000000000000000000000000000" ] && continue
    # Only test .pp files
    if [[ $name =~ [.]pp$ ]]; then
      git cat-file blob "$new_sha1" > "$tmp"
      set -o pipefail
      $syntax_check "$tmp" 2>&1 | sed "s|$tmp|$name|"> "$log"
      if [[ $? != 0 ]]; then
        echo
        echo -e "$(cat "$log" | sed 's|JOJOMOJO|'\\"${RED}""${name}"\\"${NOBOLD}"'|')" >&2
        echo -e "For more details run this: ${CYAN} git diff $old_sha1 $new_sha1 ${NOBOLD}" >&2 
        echo
        exit_status=1
      fi
    fi
done < "$tree"

rm -f "$log" "$tmp" "$tree"
exit $exit_status