Skip to content

Instantly share code, notes, and snippets.

@dfop02
Created June 14, 2023 13:25
Show Gist options
  • Save dfop02/afc445546c90130828b2f5357ce0d2d4 to your computer and use it in GitHub Desktop.
Save dfop02/afc445546c90130828b2f5357ce0d2d4 to your computer and use it in GitHub Desktop.
pre-commit to avoid commit forbidden words in any branch
#!/bin/bash
# You can change for any forbbiden words and file extensions you want
FILES='(rb|haml|js)'
FORBIDDEN='(binding.pry|debugger)'
# Change for y or n if you prefer verbose or not
VERBOSE=y
# Colors
GREP_COLOR='4;5;37;41'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
if [[ $VERBOSE = 'y' ]]; then
echo -e "${GREEN}[Forbidden Words] -->${NC} Checking if there is any ${RED}$(echo "${FORBIDDEN[*]//|/, }")${NC} on commited files... "
fi
# search for the forbidden words on git added files *.{rb|js|haml}
if [[ $(git diff --cached --name-only | grep -E $FILES) ]]; then
FOUND=$(git diff --cached --name-only | grep -E $FILES | xargs grep --color --with-filename -n -E $FORBIDDEN)
if [[ ! -z $FOUND ]]; then
echo "Failed - Forbidden word founded!"
echo -e "${RED}${FOUND}${NC}" | tr -s ' '
exit 1
fi
fi
if [[ $VERBOSE = 'y' ]]; then
echo -e "${green}[Forbidden Words] --> 👍 approved.${NC}"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment