Skip to content

Instantly share code, notes, and snippets.

@jeekl
Created March 4, 2013 16:30
Show Gist options
  • Save jeekl/5083506 to your computer and use it in GitHub Desktop.
Save jeekl/5083506 to your computer and use it in GitHub Desktop.
git update-hook script for checking versions of cookbooks and rejecting on non version bump
#!/usr/bin/env ruby
# Compares semantic versions of cookbooks, by looking at the metadata.rb file and
# rejects pushes if the cookbook version has not been bumped. If running this
# script from another script, remeber to pass along the parameters to this
# script.
#
# Requires a gem install semantic.
require 'rubygems'
require 'semantic'
refname = ARGV[0]
oldrev = ARGV[1]
newrev = ARGV[2]
# Continue if we can't diff
if newrev == ('0' * 40)
# delete branch
exit 0
elsif oldrev == ('0' * 40)
# new branch
exit 0
elsif not %x(git ls-tree --name-only -r #{oldrev}).match("metadata.rb")
# If the repo does not contain a metadata.rb file, it's probably not a cookbook,
# and we should continue.
#puts "Did not find any metadata.rb file - cannot determine versions."
#puts "Is this even a cookbook?!"
#puts "Continuing..."
exit 0
end
# TODO: cleanup
# Get the versions from the metadata.rb file.
oldrev_array = %x(git cat-file blob #{oldrev}:metadata.rb).split("\n")
newrev_array = %x(git cat-file blob #{newrev}:metadata.rb).split("\n")
oldrev_grep = oldrev_array.grep(/version\s*"(.*)"/)
newrev_grep = newrev_array.grep(/version\s*"(.*)"/)
oldrev_version = oldrev_grep.to_s[/version.*\"(.*)\"/,1]
newrev_version = newrev_grep.to_s[/version.*\"(.*)\"/,1]
old_version = Semantic::Version.new(oldrev_version)
new_version = Semantic::Version.new(newrev_version)
# Could possibly replace ansi escape codes with gem-colorize or something...
unless new_version > old_version
puts "\e[31mYou tried to push version #{new_version} of your cookbook,\e[0m"
puts "\e[31mbut that version of the cookbook seems to already exist.\e[0m"
puts "\e[31mHave you forgotten to bump the version of your cookbook?\e[0m"
exit 1 # Exit if new_version is not larger than old_version
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment