Skip to content

Instantly share code, notes, and snippets.

@klcodanr
Last active September 8, 2023 13:34
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 klcodanr/9cb36294d9e76c0b392b3209241ad900 to your computer and use it in GitHub Desktop.
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
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 test@email.com && 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" ]
#!/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