Skip to content

Instantly share code, notes, and snippets.

@freshtonic
Created January 17, 2012 01:03
Show Gist options
  • Select an option

  • Save freshtonic/1623926 to your computer and use it in GitHub Desktop.

Select an option

Save freshtonic/1623926 to your computer and use it in GitHub Desktop.
Git update hook to disallow fast-forwards for master and release branches
#!/usr/bin/env ruby
$refname = ARGV[0]
$oldrev = ARGV[1]
$newrev = ARGV[2]
$user = ENV['USER']
puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
NON_FASTFORWARD_BLACKLIST = [/\brelease\b/, /\bmaster\b/]
# enforces fast-forward only pushes
def check_fast_forward
if NON_FASTFORWARD_BLACKLIST.any?{ |b| $refname =~ b}
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"
exit 1
end
end
end
check_fast_forward
@freshtonic
Copy link
Copy Markdown
Author

Make sure you mark it as executable

@freshtonic
Copy link
Copy Markdown
Author

It should live in .git/hooks and be named 'update'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment