Skip to content

Instantly share code, notes, and snippets.

@lfborjas
Forked from ryanflorence/post-receive.rb
Created May 18, 2011 01:15
Show Gist options
  • Save lfborjas/977811 to your computer and use it in GitHub Desktop.
Save lfborjas/977811 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
%w(rubygems grit json pathname).each{|d| require d}
# Aside from removing Ruby on Rails specific code this is taken verbatim from
# mislav's git-deploy (http://github.com/mislav/git-deploy) and it's awesome
# - Ryan Florence (http://ryanflorence.com)
#
# Install this hook to a remote repository with a working tree, when you push
# to it, this hook will reset the head so the files are updated
if ENV['GIT_DIR'] == '.'
# this means the script has been called as a hook, not manually.
# get the proper GIT_DIR so we can descend into the working copy dir;
# if we don't then `git reset --hard` doesn't affect the working tree.
Dir.chdir('..')
ENV['GIT_DIR'] = '.git'
end
cmd = %(bash -c "[ -f /etc/profile ] && source /etc/profile; echo $PATH")
envpath = IO.popen(cmd, 'r') { |io| io.read.chomp }
ENV['PATH'] = envpath
# find out the current branch
head = `git symbolic-ref HEAD`.chomp
# abort if we're on a detached head
exit unless $?.success?
oldrev = newrev = nil
null_ref = '0' * 40
# read the STDIN to detect if this push changed the current branch
while newrev.nil? and gets
# each line of input is in form of "<oldrev> <newrev> <refname>"
revs = $_.split
oldrev, newrev = revs if head == revs.pop
end
# abort if there's no update, or in case the branch is deleted
exit if newrev.nil? or newrev == null_ref
# update the working copy
`umask 002 && git reset --hard`
# post to the app
stdins = []; stdins << $_ while gets
old_revision = stdins[0]
new_revision = stdins[1]
ref = stdins[2]
repo = Repo.init_bare_or_open(ENV["GIT_DIR"])
info = {}
#populate the commit info:
new_commit = repo.commit new_revision
info[:repo] = Pathname.new(repo.path).basename.to_s.gsub(".git", "")
info[:commiter] = "#{new_commit.author.name} <#{new_commit.author.email}>"
info[:when] = new_commit.date
info[:stats] = new_commit.stats.to_hash
resp = Net::HTTP.post_form "http://deploy-classio.heroku.com/push", {:payload => JSON.unparse(info)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment