Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Put checks into functions

  • Loading branch information...
commit a32e0c78744bcba015242ef51d053d2ecd8f2c7a 1 parent 6acef37
@ssm authored
Showing with 47 additions and 13 deletions.
  1. +47 −13 puppet/git/pre-commit
View
60 puppet/git/pre-commit
@@ -5,14 +5,17 @@
# Stig Sandbeck Mathisen <ssm@fnord.no>
+# Requirements:
+# - puppet 2.7 or newer for .pp files
+# - ruby and erb for .erb files
+
# Todo:
-# - Emit correct filename in .pp checks instead of /dev/fd/<num>
# - Check for invalid characters with "iconv"
errors=0
message=$(mktemp /tmp/error_msg.XXXXXX)
-if git-rev-parse --verify HEAD >/dev/null 2>&1
+if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
@@ -20,32 +23,62 @@ else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
-## Check: whitespace
+#------------------------------
+check_erb() {
+ file="$1"
+ msg=$(mktemp /tmp/erb.XXXXXXXX)
+ git cat-file blob ":0:${file}" | erb -x -P -T - | ruby -c >$msg 2>&1
+ status=$?
+
+ if [ "$status" != "0" ]; then
+ cat $msg | sed -n '1 s|^-\(:[0-9][0-9]*:\)|'"${file}"'\1|p; 2,3p'
+ fi
+ rm -f $msg
+ return $status
+}
+
+#------------------------------
+check_pp() {
+ file="$1"
+ msg=$(mktemp /tmp/pp.XXXXXXXX)
+ puppet parser validate --color=fase \
@cosimo
cosimo added a note

Here I think it should be "--color=false"

@cosimo
cosimo added a note

Long time ago we modified this check to make it work with Puppet 2.6 too. Reporting it here just in case:

puppet_version=$(puppet --version | tr -d '\.' | sed -e 's/^0+//')          
if [ "$puppet_version" -ge "270" ]; then                                    
    # Version is >= 2.7                                                     
    puppet parser validate --color=false \                                  
        <(git cat-file blob ":0:${file}") >$msg 2>&1                        
else                                                                        
    # Version is < 2.7                                                      
    puppet --parseonly \                                                    
        <(git cat-file blob ":0:${file}") >$msg 2>&1                        
fi                                                                        
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ <(git cat-file blob ":0:${file}") >$msg 2>&1
+ status=$?
+
+
+ if [ "$status" != "0" ]; then
+ cat $msg | sed -n \
+ -e 's/^err: Could not parse for environment [a-z]*: //' \
+ -e '1 s|\(.*\) at [a-z/0-9]*:\([0-9]*\)$|'"${file}"':\2: \1|p'
+ fi
+ rm -f $msg
+ return $status
+}
+
+#------------------------------
+# Check: whitespace
git diff-index --check --cached $against -- > $message
if [ "$?" -ne "0" ]; then
cat $message
- errors=$(expr $errrors + 1)
+ (( errors ++ ))
fi
-## Check: syntax
-for file in $(git diff-index --cached --diff-filter=AM --name-only --cached $against); do
+#------------------------------
+# Check: syntax for misc files
+for file in $(git diff-index --cached --diff-filter=AM --name-only $against); do
# Do not check empty files
if [ $(git cat-file -s :0:"${file}") -gt 0 ]; then
case "$file" in
*.pp)
- # TODO: rewrite /dev/fd/foo:line to ${file}:line
- puppet parser validate --ignoreimport \
- <(git cat-file blob :0:"${file}") > $message
+ check_pp $file > $message
;;
*.erb)
- git cat-file blob :0:"${file}" \
- | erb -x -P -T - | ruby -c > $message
+ check_erb $file > $message
;;
esac
if [ "$?" -ne "0" ]; then
- echo "[${file}]"
cat $message
- errors=$(expr $errrors + 1)
+ (( errors ++ ))
fi
fi
done
@@ -53,6 +86,7 @@ done
rm -rf $message
if [ "$errors" -ne "0" ]; then
+ echo
echo "Error: $errors errors found, aborting commit."
exit 1
fi
@cosimo

Here I think it should be "--color=false"

@cosimo

Long time ago we modified this check to make it work with Puppet 2.6 too. Reporting it here just in case:

puppet_version=$(puppet --version | tr -d '\.' | sed -e 's/^0+//')          
if [ "$puppet_version" -ge "270" ]; then                                    
    # Version is >= 2.7                                                     
    puppet parser validate --color=false \                                  
        <(git cat-file blob ":0:${file}") >$msg 2>&1                        
else                                                                        
    # Version is < 2.7                                                      
    puppet --parseonly \                                                    
        <(git cat-file blob ":0:${file}") >$msg 2>&1                        
fi                                                                        
Please sign in to comment.
Something went wrong with that request. Please try again.