Skip to content

Instantly share code, notes, and snippets.

@penguwin
Last active December 29, 2020 14:50
Show Gist options
  • Save penguwin/7659ca3620fb73da63fdf2449de560da to your computer and use it in GitHub Desktop.
Save penguwin/7659ca3620fb73da63fdf2449de560da to your computer and use it in GitHub Desktop.
A git hook to prevent force pushing on master
#!/bin/bash
# Per-repo installation
# 1. Place this file in `.git/hooks/pre-push`
# 2. `chmod +x` it
clippy='
_________________________________________
/ It looks like youre trying to force \
| push on master... |
\ Are you sure you want to continue? /
-----------------------------------------
\
\
__
/ \
| |
@ @
| |
|| |/
|| ||
|\_/|
\___/
'
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
is_destructive='force|delete|\-f'
push_command=$(ps -ocommand= -p $PPID)
if [[ $push_command =~ $is_destructive ]] && [ $protected_branch = $current_branch ]
then
echo "${clippy}"
read -p '(y|N): ' -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
exit 0 # push will execute
fi
exit 1 # push will not execute
else
exit 0 # push will execute
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment