Skip to content

Instantly share code, notes, and snippets.

@EvidentlyCube
Created May 27, 2022 08:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvidentlyCube/04ebfadc71eddf23a6b1e344596d547f to your computer and use it in GitHub Desktop.
Save EvidentlyCube/04ebfadc71eddf23a6b1e344596d547f to your computer and use it in GitHub Desktop.
Pre commit hook that helps you avoid accidentally committing debug code + prevent commits to certain branches
#!/bin/bash
confirm() {
while true; do
read -p "$1 y/n " -n 1 yn < /dev/tty
case $yn in
[Yy]* ) echo ;break;;
[Nn]* ) echo ;exit 1;;
* ) echo ;echo "Please answer y or n.";;
esac
done
}
prevent_branch_commit () {
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "$1" ]; then
confirm "Are you sure you want to commit to $1?"
fi
}
confirm_change () {
VAR=$(git diff --cached | grep -wi "$1")
if [ ! -z "$VAR" ]; then
git --no-pager grep -C 3 -n --break --cached -i "$1" $(git diff --cached --name-only)
echo
echo -e "\e[1;41m'$1' found in staged files!\e[1;40m"
confirm "Do you really want to commit?"
fi
}
# Prevent making accidental commits to certain branches
prevent_branch_commit "master"
# Prevent accidentally commiting debug code
confirm_change "var_dump"
confirm_change "exit;"
confirm_change "exit\("
confirm_change "exit \("
confirm_change "debug_print_backtrace"
confirm_change "console.log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment