-
-
Save edblack1/00050b9d66bfab1532c47bb8f2af4df1 to your computer and use it in GitHub Desktop.
pre receive hook
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 | |
| # pre-receive hook to block 'push -f' on master | |
| # For Phabricator, save this as: | |
| # $REPO/hooks/pre-receive-phabricator.d/no_push_f_master.rb | |
| # Remember to chmod +x | |
| # Ref: http://git-scm.com/book/en/Customizing-Git-An-Example-Git-Enforced-Policy | |
| args = STDIN.readline.chomp.split(" ") | |
| $oldrev, $newrev, $refspec = args | |
| # paranoid input check: | |
| SHA_RE = /^[\da-f]{40}$/i | |
| if $oldrev !~ SHA_RE or $newrev !~ SHA_RE then | |
| puts "invalid input?" | |
| exit 1 | |
| end | |
| # enforces fast-forward only pushes: | |
| def check_fast_forward | |
| missed_refs = `git rev-list #{$newrev}..#{$oldrev}` | |
| missed_ref_count = missed_refs.split("\n").size | |
| if missed_ref_count > 0 | |
| puts "[POLICY] Cannot push a non fast-forward reference" | |
| puts "[POLICY] missed ref count: #{missed_ref_count}" | |
| exit 1 | |
| end | |
| end | |
| # don't allow deletes | |
| def check_delete | |
| if /0{40}/ =~ $newrev then | |
| puts "[POLICY] delete rejected" | |
| exit 1 | |
| end | |
| end | |
| # enforcing on master | |
| if $refspec == "refs/heads/master" | |
| puts "[POLICY:master]" | |
| check_delete | |
| check_fast_forward | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment