I recently started a project on github. I\'ve managed to setup automatic testing after each commit using Travis. But now I would like to setup a pre-commit hook with jshint
A similar script to the @igor's one with some improvements:
#!/bin/sh
#
# Run JSHint validation before commit.
RED='\033[0;31m'
REDBOLD='\033[1;31m'
ORANGE='\033[0;33m'
NC='\033[0m' # No Color
files=$(git diff --cached --name-only | grep .js)
pass=true
totalErrors=0
if [ "$files" != "" ]; then
for file in ${files}; do
result=$(jshint ${file})
if [ "$result" != "" ]; then
echo "${RED}$result${NC}"
pass=false
totalErrors=$((totalErrors+1))
fi
echo ""
done
fi
if $pass; then
exit 0
else
echo "${ORANGE}===== ${totalErrors} JSHint Error${NC}"
echo ""
echo "${REDBOLD}COMMIT FAILED: Some JavaScript files are invalid. Please fix errors and try committing again.${NC}"
echo ""
echo " (use -n option \"git commit -n -m \" to avoid call pre-commit hook and JSHint check)"
echo ""
exit 1
fi