Skip to content

Instantly share code, notes, and snippets.

@localshred
Created August 29, 2012 02:29
Show Gist options
  • Save localshred/3506213 to your computer and use it in GitHub Desktop.
Save localshred/3506213 to your computer and use it in GitHub Desktop.
Reject commits if updating non-dev branch and the Gemfile was changed and contains a list of rejectable words (e.g. git, tag, branch, path, etc.)
#!/usr/bin/env ruby
BRANCHES = %w( qa stage 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 }
#puts diff.inspect
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