#!/bin/bash # # Hook script to clean up code and build docs. # Only .js files are checked. # Called by "git commit" with no arguments. # The hook should exit with non-zero status if it wants to stop the commit. # Loop over staged files and clean them up. while IFS= read -rd '' file do # Special checks for JavaScript sources: if [[ $file == *.js && $file != *.min.js && $file != public/* && $file != docs/* && $file != build/* ]] then echo "Checking: '$file'" len1=`wc -c < "$file"` # Remove trailing whitespace on lines. sed -i 's/\s\+$//' "$file" len2=`wc -c < "$file"` [ $len1 != $len2 ] && echo "Removed trailing whitespace in '$file'." len1=$len2 # Remove leading and trailing blank lines, ensure that file ends in newline. sed -i '/./,$!d;:a;/^\n*$/{$d;N;};/\n$/ba;$a\' "$file" len2=`wc -c < "$file"` [ $len1 != $len2 ] && echo "Removed surrounding empty lines in '$file'." len1=$len2 # Remove trailing empty lines. sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' "$file" len2=`wc -c < "$file"` [ $len1 != $len2 ] && echo "Removed trailing whitespace in '$file'." len1=$len2 # Check for tabs. if grep -q $'\t' "$file" then echo "ABORT: Found tab character in '$file'!" exit 1 fi # Remove debugger statements which are alone on a line. sed -i '/^\s*debugger;\{0,1\}$/d' "$file" len2=`wc -c < "$file"` [ $len1 != $len2 ] && echo "Removed debugger statements in '$file'." len1=$len2 # Check for debugger statements which are not alone on a line. if grep -q 'debugger' "$file" then echo "ABORT: Found complicated debugger statement in '$file'!" exit 1 fi # Check for lines wider than 80 characters. lines=$(grep -n '.\{81,\}' "$file" | cut -d: -f1 | paste -sd',') if [ -n "$lines" ] then echo "ABORT: More than 80 characters in '$file' on line(s) $lines!" exit 1 fi fi # Keep these changes. git add "$file" done < <(git diff --cached --name-only --diff-filter=ACM -z) # Build documentation if possible. if [ -x bin/make.js -a -d docs ] then node bin/make.js doc git add -u docs/ fi