forked from drwahl/puppet-git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·115 lines (101 loc) · 3.32 KB
/
pre-commit
File metadata and controls
executable file
·115 lines (101 loc) · 3.32 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
git_root=`git rev-parse --show-toplevel`
failures=0
RC=0
subhook_root=$git_root/.git/hooks/commit_hooks
hook_dir="$(dirname $0)"
hook_symlink="$(readlink $0)"
# Figure out where commit hooks are if pre-commit is setup as a symlink
if [ ! -z "$hook_symlink" ]; then
subhook_root="$(dirname $hook_symlink)/commit_hooks"
fi
# If using submodules, we need to read proper subhook root
if [ -f "$git_root/.git" ]; then
IFS=": "
while read -r name value
do
if [ $name == "gitdir" ]; then
submodule_hookdir=$value
fi
done < "$git_root/.git"
if [ ! -z "$submodule_hookdir" ]; then
subhook_root="$git_root/$submodule_hookdir/hooks/commit_hooks"
fi
fi
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for changedfile in `git diff --cached --name-only --diff-filter=ACM`; do
#check puppet manifest syntax
if type puppet >/dev/null 2>&1; then
if [ $(echo $changedfile | grep -q '\.*.pp$'; echo $?) -eq 0 ]; then
${subhook_root}/puppet_manifest_syntax_check.sh $changedfile
RC=$?
if [ "$RC" -ne 0 ]; then
failures=`expr $failures + 1`
fi
fi
else
echo "puppet not installed. Skipping puppet syntax checks..."
fi
if type ruby >/dev/null 2>&1; then
#check erb (template file) syntax
if type erb >/dev/null 2>&1; then
if [ $(echo $changedfile | grep -q '\.*.erb$'; echo $?) -eq 0 ]; then
${subhook_root}/erb_template_syntax_check.sh $changedfile
RC=$?
if [ "$RC" -ne 0 ]; then
failures=`expr $failures + 1`
fi
fi
else
echo "erb not installed. Skipping erb template checks..."
fi
#check hiera data (yaml/yml/eyaml/eyml) syntax
if [ $(echo $changedfile | grep -q '\.*.e\?ya\?ml$'; echo $?) -eq 0 ]; then
${subhook_root}/yaml_syntax_check.sh $changedfile
RC=$?
if [ "$RC" -ne 0 ]; then
failures=`expr $failures + 1`
fi
fi
#check json (i.e. metadata.json) syntax
if [ $(echo $changedfile | grep -q '\.*.json$'; echo $?) -eq 0 ]; then
${subhook_root}/json_syntax_check.sh $changedfile
RC=$?
if [ "$RC" -ne 0 ]; then
failures=`expr $failures + 1`
fi
fi
else
echo "ruby not installed. Skipping erb/yaml/json checks..."
fi
#puppet manifest styleguide compliance
if type puppet-lint >/dev/null 2>&1; then
if [ $(echo $changedfile | grep -q '\.*.pp$' ; echo $?) -eq 0 ]; then
${subhook_root}/puppet_lint_checks.sh $changedfile
RC=$?
if [ "$RC" -ne 0 ]; then
failures=`expr $failures + 1`
fi
fi
else
echo "puppet-lint not installed. Skipping puppet-lint tests..."
fi
done
IFS=$SAVEIFS
#rspec test validation
if which rspec >/dev/null 2>&1; then
${subhook_root}/rspec_puppet_checks.sh
RC=$?
if [ "$RC" -ne 0 ]; then
failures=`expr $failures + 1`
fi
else
echo "rspec not installed. Skipping rspec-puppet tests..."
fi
#summary
if [ "$failures" -ne 0 ]; then
echo -e "$(tput setaf 1)Error: $failures subhooks failed. Aborting commit.$(tput sgr0)"
exit 1
fi
exit 0