Last active
September 8, 2023 13:34
-
-
Save klcodanr/9cb36294d9e76c0b392b3209241ad900 to your computer and use it in GitHub Desktop.
pre-push GIT hook for warning on pushes to main / master / release branches
This file contains hidden or 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
FROM ubuntu:focal | |
# Install Dependencies | |
RUN apt-get update && apt-get install -y \ | |
curl \ | |
git \ | |
vim \ | |
&& rm -rf /var/lib/apt/lists/* | |
# Setup GIT | |
RUN git config --global user.email [email protected] && git config --global user.name "Test User" | |
# Setup Pre-Push Hook | |
RUN mkdir -p ~/.githooks && \ | |
curl https://gist.githubusercontent.com/klcodanr/9cb36294d9e76c0b392b3209241ad900/raw/915f2d42d8dec5a52e1ce72b69e22d17925d6356/pre-push --output ~/.githooks/pre-push && \ | |
chmod +x ~/.githooks/pre-push && \ | |
git config --global core.hooksPath ~/.githooks/ | |
# Setup test repostitory | |
RUN mkdir ~/remote && \ | |
cd ~/remote && \ | |
git init && \ | |
echo "Hello World" > README.md && \ | |
git add -A && \ | |
git commit -m "Initial commit" && \ | |
cd .. && \ | |
git clone ./remote ./local | |
ENTRYPOINT [ "/bin/bash" ] |
This file contains hidden or 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 | |
check_branch() { | |
if [[ "$remote_ref" == *"$1"* ]]; then | |
echo -en "\033[1;33mYou're about to push to $remote_ref, is that what you intended? [y|n] \033[0m" | |
echo -en "\033[1m" | |
read -n 1 -r < /dev/tty | |
echo -en "\033[0m" | |
echo | |
if echo $REPLY | grep -E '^[Yy]$' > /dev/null; then | |
exit 0 # push will execute | |
fi | |
exit 1 # push will not execute | |
fi | |
} | |
if read local_ref local_sha remote_ref remote_sha; then | |
check_branch master | |
check_branch main | |
check_branch release | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment