Last active
June 6, 2017 20:56
-
-
Save brunoluiz/c6ccad2c104ac61edfe37fdf233dad0a to your computer and use it in GitHub Desktop.
git-pre-commit-words-hookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Nikolaos Dimopoulos | |
# Based on code by Remigijus Jarmalavičius | |
# Checks the files to be committed for the presence of print_r(), var_dump() and die() | |
# The array below can be extended for further checks | |
# Bruno's modifications: | |
# - Adds some JS checkup | |
# - Enable checkup in more languages | |
# - Fix the bug on the git status command | |
# - Add ESLint | |
# - Removes useless logs | |
# Original on: https://github.com/niden/Git-Pre-Commit-Hook-for-certain-words | |
extensions='.js$\|.php$' | |
checks[1]="var_dump(" | |
checks[2]="print_r(" | |
checks[3]="die" | |
checks[4]="console.log(" | |
checks[5]=".only(" | |
element_count=${#checks[@]} | |
let "element_count = $element_count + 1" | |
ROOT_DIR="$(pwd)/" | |
LIST=$(git diff --name-only --staged) | |
ERRORS_BUFFER="" | |
for file in $LIST | |
do | |
if [ "$file" == '#' ]; then | |
continue | |
fi | |
if [ $(echo "$file" | grep 'modified') ]; then | |
FILE_ACTION="modified" | |
elif [ $(echo "$file" | grep 'added') ]; then | |
FILE_ACTION="added" | |
else | |
EXTENSION=$(echo "$file" | grep "$extensions$") | |
if [ "$EXTENSION" != "" ]; then | |
index=1 | |
while [ "$index" -lt "$element_count" ] | |
do | |
# echo "Checking $FILE_ACTION file: $file [${checks[$index]}]" | |
ERRORS=$(grep "${checks[$index]}" $ROOT_DIR$file >&1) | |
if [ "$ERRORS" != "" ]; then | |
if [ "$ERRORS_BUFFER" != "" ]; then | |
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS" | |
else | |
ERRORS_BUFFER="$ERRORS" | |
fi | |
echo "${checks[$index]} found in file: $file " | |
fi | |
let "index = $index + 1" | |
done | |
fi | |
fi | |
done | |
# If an error exists, then print it and block the commit | |
if [ "$ERRORS_BUFFER" != "" ]; then | |
echo | |
echo "These errors were found in try-to-commit files: " | |
echo -e $ERRORS_BUFFER | |
echo | |
echo "Can't commit, fix errors first." | |
exit 1 | |
fi | |
# Check if ESLinter is installed | |
if [ ! -f "./node_modules/eslint/bin/eslint.js" ]; then | |
echo "ESLinter is not installed!" | |
exit 0 | |
fi | |
# Run ESLint | |
./node_modules/eslint/bin/eslint.js $LIST | |
if [ $? -ne 0 ]; then | |
exit 1; | |
else | |
exit 0; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment