Last active
August 29, 2015 14:13
-
-
Save urmastalimaa/e5b317780714f7bf86eb to your computer and use it in GitHub Desktop.
Git pre-push hook to protect master.
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/sh | |
# derived from https://gist.github.com/pixelhandler/5718585 | |
# Called by "git push" after it has checked the remote status, | |
# but before anything has been pushed. | |
# | |
# If this script exits with a non-zero status nothing will be pushed. | |
# | |
# Steps to install, from the root directory of your repo... | |
# 1. Copy the file into your repo at `.git/hooks/pre-push` | |
# 2. Set executable permissions, run `chmod +x .git/hooks/pre-push` | |
# 3. Or, use `rake hooks:pre_push` to install | |
# | |
# Try a force push to master, you should get a message `*** [Policy] never force push...` | |
# | |
# The commands below will not be allowed... | |
# `git push --force origin master` | |
# `git push --delete origin master` | |
# `git push origin :master` | |
# | |
# Nor will a force push while on the master branch be allowed... | |
# `git co master` | |
# `git push --force origin` | |
# | |
# Requires git 1.8.2 or newer | |
# | |
# Git 1.8.2 release notes cover the new pre-push hook: | |
# <https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt> | |
# | |
# See Sample pre-push script: | |
# <https://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks--pre-push.sample> | |
protected_branch='master' | |
policy='[Policy] Never force push or delete the '$protected_branch' branch! (Prevented with pre-push hook.)' | |
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') | |
push_pid=$(ps -p $PPID -o ppid=) | |
push_parent_pid=$(ps -p $push_pid -o ppid=) | |
push_command=$(ps -ocommand= -p $push_pid) | |
push_parent_command=$(ps -ocommand= -p $push_parent_pid) | |
is_destructive='force|delete|\-f' | |
will_remove_protected_branch=':'$protected_branch | |
do_exit(){ | |
echo $policy | |
exit 1 | |
} | |
if [[ $push_command =~ $is_destructive ]] || [[ $push_parent_command =~ $is_destructive ]]; then | |
if [ $current_branch = $protected_branch ]; then | |
do_exit | |
fi | |
if [[ $push_command =~ $protected_branch ]] || [[ $push_parent_command =~ $protected_branch ]]; then | |
do_exit | |
fi | |
fi | |
if [[ $push_command =~ $will_remove_protected_branch ]] || [[ $push_parent_command =~ $will_remove_protected_branch ]]; then | |
do_exit | |
fi | |
unset do_exit | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usable with git https://github.com/icefox/git-hooks/tree/master/git_hooks (checks parent command as well).
Derived from https://gist.github.com/pixelhandler/5718585
Create file
~/.git_hooks/pre-push/protect_master
with gist content.Add execute permission.
Clone
https://github.com/icefox/git-hooks/tree/master/git_hooks
Add
git_hooks
to PATHRun
git hooks --install
in desired repo to link hooks in~/.git_hooks/
.Run
git hooks --uninstall
in desired repo to remove.Check
https://github.com/icefox/git-hooks
for more information.