Skip to content

Instantly share code, notes, and snippets.

@KarimP
Last active August 29, 2015 14:11
Show Gist options
  • Save KarimP/cb8cb0a17802cd568cd6 to your computer and use it in GitHub Desktop.
Save KarimP/cb8cb0a17802cd568cd6 to your computer and use it in GitHub Desktop.
JSHint pre-commit git hook with a prompt to continue when there are errors
#!/bin/bash
#
# Run JSHint validation before commit
# Path to custom jshint config file if you have one (relative to root directory of repository)
jshint_config_file=""
# Get all changed js files (minus any minfied files)
files=$(git diff --cached --name-only --diff-filter=ACMR -- *.js **/*.js | grep -v 'min')
pass=true;
jshint_cmd="jshint"
if [ "${jshint_config_file}" != "" ]; then
jshint_cmd+=" --config ${root_dir}/${jshint_config_file}"
fi
echo -e "\nValidating JavaScript:\n"
# Run JSHint on all changed .js files
if [ "$files" != "" ]; then
for file in ${files}; do
${jshint_cmd} ${file}
if [[ $? -ne 0 ]]; then
pass=false
fi
done
fi
if $pass; then
echo -e "\nJSHint Passed\n"
exit 0
# Prompt to carry on with commit if there are JSHint errors
else
exec < /dev/tty #need this since you can't do regular stdin during a git commit
read -p "Some JavaScript files are invalid. Would you like to continue? (Y/N): " yn
while true; do
case $yn in
[Yy]* )
echo -e "\nContinuing commit with invalid JavaScript files...\n"
exec <& -
exit 0;;
[Nn]* )
echo -e "\nCommit Failed :("
echo -e "Please fix errors and try again."
exec <& -
exit 1;;
* )
read -p "Please enter a valid response(Y/N): " yn
esac
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment