From 1637696c5ee6de9a0652511a6d523bb95c94a02b Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sun, 14 Sep 2014 18:21:45 +0100 Subject: [PATCH] Add a pre-commit hook based on pre-receive check --- pre-commit | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 pre-commit diff --git a/pre-commit b/pre-commit new file mode 100755 index 0000000..8557b03 --- /dev/null +++ b/pre-commit @@ -0,0 +1,96 @@ +#!/bin/sh + +warned=0 +final_ret=0 + +# Check for ruby binary +which ruby >/dev/null 2>&1 +if [[ $? -ne 0 ]]; then + echo "Ruby not found" 1>&2 + exit 1 +fi + +# Check for Puppet binary +which puppet >/dev/null 2>&1 +if [[ $? -ne 0 ]]; then + echo "Puppet not found" 1>&2 + exit 1 +fi + +# Check for puppet-lint +which puppet-lint >/dev/null 2>&1 +if [[ $? -ne 0 ]]; then + echo "puppet-lint not found" 1>&2 + exit 1 +fi + +# Check for erb +which erb >/dev/null 2>&1 +if [[ $? -ne 0 ]]; then + echo "erb not found" 1>&2 + exit 1 +fi + +styleguide_url='http://docs.puppetlabs.com/guides/style_guide.html' +if [ -f ".puppetstyle.url" ]; then + styleguide_url=$(cat ".puppetstyle.url") +fi + +if git-rev-parse --verify HEAD >/dev/null 2>&1; then + against=HEAD +else + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +for FILE in $(git diff-index --name-only $against) ; do + + echo "${FILE}" + + case "${FILE}" in + *.pp ) + puppet parser validate "${FILE}" + rc=$? + if [[ $rc != 0 ]]; then + echo -e "\e[0;31m'puppet parser validate' failed on ${FILE} - push denied. Run tests locally and confirm they pass before pushing. \e[0m" + final_ret=1 + fi + puppet-lint --fail-on-warnings --with-filename "${FILE}" + rc=$? + if [[ $rc != 0 ]]; then + if [[ $warned = 0 ]]; then + echo -e "\e[0;33m" "Please follow the puppet module styleguide (${styleguide_url})." "\e[0m" + echo -e "\e[0;33m" "The above ERROR/WARNING messages are informational only. The commit has not been blocked for this reason. In the future, these may cause your commit to be refused." "\e[0m" + warned=1 + fi + echo -e "\e[0;33m" "Style guide compliance errors in ${FILE}:" "\e[0m" + fi + ;; + *.erb ) + cat "${FILE}" | erb -x -T - | ruby -c 2>&1 >/dev/null + rc=$? + if [[ $rc != 0 ]]; then + echo -e "\e[0;31m" "Ruby syntax checker failed on template ${FILE}:" "\e[0m" + final_ret=1 + fi + ;; + *.yml|*.yaml) + # syntax YAML files, https://ttboj.wordpress.com/2013/08/25/finding-yaml-errors-in-puppet/ + ruby -ryaml -e "YAML.parse(File.open('${FILE}'))" + if [[ $? -ne 0 ]]; then + echo -e "\e[0;31m" "YAML syntax error in ${FILE}" "\e[0m" + final_ret=1 + fi + ;; + *.json) + ruby -rrubygems -e "require 'json'; JSON.parse(File.open('${FILE}').read)" + if [[ $? -ne 0 ]]; then + echo -e "\e[0;31m" "JSON syntax error in ${FILE}" "\e[0m" + final_ret=1 + fi + ;; + esac + + +done + +exit $final_ret