Skip to content

Instantly share code, notes, and snippets.

@emcniece
Last active October 2, 2015 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emcniece/421d225ac1aa1264a22e to your computer and use it in GitHub Desktop.
Save emcniece/421d225ac1aa1264a22e to your computer and use it in GitHub Desktop.
String-check pre-commit hook
#!/bin/sh
# Pre commit hook that prevents FORBIDDEN code from being commited.
# Add unwanted code to the FORBIDDEN array as necessary, single-space separated.
# Ensure executable: "chmod +x .git/hooks/pre-commit"
FILES_PATTERN='\.(rb|js|coffee|txt)(\..+)?$'
FORBIDDEN=( \.only )
FAILED=false
for i in "${FORBIDDEN[@]}"
do
git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n '${i}' && \
echo 'COMMIT REJECTED Found' $i 'references. Please remove them before commiting' && FAILED=true
done
if [ "$FAILED" = true ] ; then
exit 1
fi
exit
@emcniece
Copy link
Author

emcniece commented Oct 2, 2015

FORBIDDEN is a regex... plan accordingly with your periods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment