Created
August 25, 2014 15:38
-
-
Save dubilla/252e4c8d03b9db87a909 to your computer and use it in GitHub Desktop.
.git/hooks/pre-push
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
#!/usr/bin/env ruby | |
# Read this blog to know more about this script. | |
# | |
# http://blog.bigbinary.com/2013/09/19/do-not-allow-force-pusht-to-master.html | |
class PrePushHandler | |
def handle | |
reject if pushing_to_master? && forced_push? | |
end | |
private | |
def pushing_to_master? | |
current_branch == 'master' | |
end | |
def current_branch | |
result = %x{git branch}.split("\n") | |
if result.empty? | |
feedback "It seems your app is not a git repository." | |
else | |
result.select { |b| b =~ /^\*/ }.first.split(" ").last.strip | |
end | |
end | |
def reject | |
messages = ["Your attempt to FORCE PUSH to MASTER has been rejected."] | |
messages << "If you still want to FORCE PUSH then you need to ignore the pre_push git hook by executing following command." | |
messages << "git push master --force --no-verify" | |
feedback messages | |
end | |
def forced_push? | |
ppid = Process.ppid | |
cmd = "ps -ocommand= -p #{ppid}" | |
output = `#{cmd}` | |
output.match(/--force|-f/) | |
end | |
def feedback messages | |
puts "*"*40 | |
[messages].flatten.each do |message| | |
puts message | |
end | |
puts "*"*40 | |
exit 1 | |
end | |
end | |
PrePushHandler.new.handle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment