Created
June 20, 2012 02:38
-
-
Save AquaGeek/2957833 to your computer and use it in GitHub Desktop.
Git pre receive hooks
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 | |
| # From: http://rand9.com/blog/git-pre-receive-hook-for-rejecting-a-bad-gemfile/ | |
| BRANCHES = %w( master stable ) | |
| REJECT_OPTIONS = %w( git tag branch path ) | |
| old_sha, new_sha, ref = STDIN.read.split(' ') | |
| exit 0 unless BRANCHES.include?(ref.split('/').last) | |
| diff = %x{ git diff-index --cached --name-only #{old_sha} 2> /dev/null } | |
| if diff.is_a?(String) | |
| diff = diff.split("\n") | |
| end | |
| if diff.detect{|file| file =~ /^Gemfile$/} | |
| tree = %x{ git ls-tree --full-name #{new_sha} Gemfile 2> /dev/null }.split(" ") | |
| contents = %x{ git cat-file blob #{tree[2]} 2> /dev/null } | |
| invalid_lines = contents.each_line.select do |line| | |
| line =~ /\b(#{REJECT_OPTIONS.join('|')})\b/ | |
| end | |
| unless invalid_lines.empty? | |
| puts | |
| puts '> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' | |
| puts '> ---- PUSH REJECTED by origin ----' | |
| puts '>' | |
| puts "> You've specified an invalid option for #{invalid_lines.size} gem definitions in the Gemfile" | |
| puts "> Invalid options are: #{REJECT_OPTIONS.join(', ')}" | |
| puts '>' | |
| puts "> The offending gems:" | |
| puts ">\t" + invalid_lines.join(">\t") | |
| puts '>' | |
| puts '> To fix:' | |
| puts ">\t* Remove the offending options" | |
| puts ">\t* bundle install" | |
| puts ">\t* Run tests" | |
| puts ">\t* Ammend previous commit (git add . && git commit --amend)" | |
| puts ">\t* git push origin #{ref.split('/').last}" | |
| puts '>' | |
| puts '> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' | |
| puts | |
| exit 1 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment