Skip to content

Commit 0d54866

Browse files
garethhumphriesworldpaykrzyzakp
authored andcommitted
Various small enhancements (detail below) (drwahl#97)
* Add lint-skip-on-prior-failure, fix regex filematching, fix indendation and consistent test use, allow puppet_lint environmet override. * Remove leading 0, add {}s around bash variables used in strings
1 parent 86f4687 commit 0d54866

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

pre-commit

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ else
2929
fi
3030

3131
# If using submodules, we need to read proper subhook root
32-
if [[ -f "$git_root/.git" ]]; then
32+
if [[ -f "${git_root}/.git" ]]; then
3333
IFS=": "
3434
while read -r name value
3535
do
36-
if [[ $name == "gitdir" ]]; then
37-
submodule_hookdir=$value
36+
if [[ "$name" == "gitdir" ]]; then
37+
submodule_hookdir="$value"
3838
fi
39-
done < "$git_root/.git"
39+
done < "${git_root}/.git"
4040
if [[ ! -z "$submodule_hookdir" ]]; then
41-
subhook_root="$git_root/$submodule_hookdir/hooks/commit_hooks"
41+
subhook_root="${git_root}/${submodule_hookdir}/hooks/commit_hooks"
4242
fi
4343
fi
4444

4545
# Decide if we want puppet-lint
46-
CHECK_PUPPET_LINT="enabled"
46+
CHECK_PUPPET_LINT=${CHECK_PUPPET_LINT:-"enabled"}
4747
if [[ -e ${subhook_root}/config.cfg ]] ; then
4848
source "${subhook_root}/config.cfg"
4949
fi
@@ -57,13 +57,13 @@ SAVEIFS=$IFS
5757
IFS=$(echo -en "\n\b")
5858

5959
if [ $CHECK_ALL ]; then
60-
files_to_check=$(find $git_root -path $git_root/.git -prune -o -print)
60+
files_to_check=$(find "$git_root" -path "${git_root}/.git" -prune -o -print)
6161
else
6262
files_to_check=$(git diff --cached --name-only --diff-filter=ACM)
6363
fi
6464

6565
# On cygwin/windows, puppet is a symlink to native puppet install, which needs windows paths.
66-
if [ "$OSTYPE" == "cygwin" ] && file -L $(which puppet) 2>/dev/null | grep -q 'DOS batch file' && type cygpath >/dev/null 2>&1; then
66+
if [[ "$OSTYPE" == "cygwin" ]] && file -L $(which puppet) 2>/dev/null | grep -q 'DOS batch file' && type cygpath >/dev/null 2>&1; then
6767
USE_NATIVE="YES"
6868
fi
6969
for changedfile in $files_to_check; do
@@ -75,13 +75,13 @@ for changedfile in $files_to_check; do
7575
fi
7676
#check puppet manifest syntax
7777
if type puppet >/dev/null 2>&1; then
78-
if [[ $(echo "$changedfile" | grep -q '\.*\.epp$'; echo $?) -eq 0 ]]; then
78+
if echo "$changedfile" | grep -iq '\.epp$'; then
7979
${subhook_root}/puppet_epp_syntax_check.sh "$changedfile"
8080
RC=$?
8181
if [[ "$RC" -ne 0 ]]; then
8282
failures=$((failures + 1))
8383
fi
84-
elif [[ $(echo "$changedfile" | grep -q '\.*\.pp$'; echo $?) -eq 0 ]]; then
84+
elif echo "$changedfile" | grep -iq '\.pp$'; then
8585
${subhook_root}/puppet_manifest_syntax_check.sh "$changedfile_native" "" "$USE_PUPPET_FUTURE_PARSER"
8686
RC=$?
8787
if [[ "$RC" -ne 0 ]]; then
@@ -95,7 +95,7 @@ for changedfile in $files_to_check; do
9595
if type ruby >/dev/null 2>&1; then
9696
#check erb (template file) syntax
9797
if type erb >/dev/null 2>&1; then
98-
if [[ $(echo "$changedfile" | grep -q '\.*.erb$'; echo $?) -eq 0 ]]; then
98+
if echo "$changedfile" | grep -iq '\.erb$'; then
9999
${subhook_root}/erb_template_syntax_check.sh "$changedfile"
100100
RC=$?
101101
if [[ "$RC" -ne 0 ]]; then
@@ -107,7 +107,7 @@ for changedfile in $files_to_check; do
107107
fi
108108

109109
#check hiera data (yaml/yml/eyaml/eyml) syntax
110-
if [[ $(echo "$changedfile" | grep -q '\.*.e\?ya\?ml$'; echo $?) -eq 0 ]]; then
110+
if echo "$changedfile" | grep -iq '\.e\?ya\?ml$'; then
111111
${subhook_root}/yaml_syntax_check.sh "$changedfile"
112112
RC=$?
113113
if [[ "$RC" -ne 0 ]]; then
@@ -116,7 +116,7 @@ for changedfile in $files_to_check; do
116116
fi
117117

118118
#check json (i.e. metadata.json) syntax
119-
if [[ $(echo "$changedfile" | grep -q '\.*.json$'; echo $?) -eq 0 ]]; then
119+
if echo "$changedfile" | grep -iq '\.json$'; then
120120
${subhook_root}/json_syntax_check.sh "$changedfile"
121121
RC=$?
122122
if [[ "$RC" -ne 0 ]]; then
@@ -130,12 +130,16 @@ for changedfile in $files_to_check; do
130130
#puppet manifest styleguide compliance
131131
if [[ "$CHECK_PUPPET_LINT" != "disabled" ]] ; then
132132
if type puppet-lint >/dev/null 2>&1; then
133-
if [[ $(echo "$changedfile" | grep -q '\.*\.pp$' ; echo $?) -eq 0 ]]; then
133+
if echo "$changedfile" | grep -iq '\.pp$'; then
134+
if [[ "$failures" -eq 0 ]]; then
134135
${subhook_root}/puppet_lint_checks.sh "$CHECK_PUPPET_LINT" "$changedfile"
135136
RC=$?
136137
if [[ "$RC" -ne 0 ]]; then
137138
failures=$((failures + 1))
138139
fi
140+
else
141+
echo "Skipping puppet-lint check due to prior errors."
142+
fi
139143
fi
140144
else
141145
echo "puppet-lint not installed. Skipping puppet-lint tests..."
@@ -172,7 +176,7 @@ fi
172176

173177
#summary
174178
if [[ "$failures" -ne 0 ]]; then
175-
echo -e "$(tput setaf 1)Error: $failures subhooks failed. Please fix above errors.$(tput sgr0)"
179+
echo -e "$(tput setaf 1)Error: ${failures} subhooks failed. Please fix above errors.$(tput sgr0)"
176180
exit 1
177181
fi
178182

0 commit comments

Comments
 (0)