Last active
December 14, 2015 15:49
-
-
Save shostakovich/5110413 to your computer and use it in GitHub Desktop.
Pre-push hook that rejects force pushes to master. Requires Git 1.8.2 and above.
This file contains 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
[push] | |
default = current | |
[alias] | |
pfush = "push --force" |
This file contains 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
#!/usr/bin/env ruby | |
# Pre-push hook that rejects force pushes to master. | |
# Requires Git > 1.8.2 | |
class PushRejecter | |
def run | |
if pushing_to?(:master) and forced_push? | |
reject | |
else | |
allow | |
end | |
end | |
def pushing_to?(branch) | |
`git branch | grep "*" | sed "s/* //"`.match(branch.to_s) | |
end | |
def forced_push? | |
`ps -ocommand= -p #{Process.ppid}`.match(/force|pfush/) | |
end | |
def reject | |
puts "Force push to master rejected." | |
exit(1) | |
end | |
def allow | |
exit(0) | |
end | |
end | |
PushRejecter.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the pre-push script very helpful, I wanted to use bash instead of ruby so based on this created https://gist.github.com/pixelhandler/5718585