Create a gist now

Instantly share code, notes, and snippets.

@whatcould /post-receive Secret forked from foca/post-receive
Created Feb 6, 2009

What would you like to do?
Generic Integrity post-receive-hook for non-github repos (fixed)
#!/usr/bin/env ruby
# Replace the following by the url given to you in your project's edit page
POST_RECEIVE_URL = 'http://localhost:4567/my_fancy_project/push'
# Set user and password if Integrity is protected with basic auth
USER = ""
PASSWORD = ""
#######################################################################
## ##
## == DON'T EDIT ANYTHING BELOW UNLESS YOU KNOW WHAT YOU'RE DOING == ##
## ##
#######################################################################
require 'net/http'
require 'uri'
require 'rubygems'
require 'git'
require 'daemons'
require 'logger'
require 'json'
before, after, ref = STDIN.gets.split
g = Git.open(Dir.pwd, :repository => Dir.pwd)
if before == '400'
commits = [g.gcommit(after)]
else
commits = g.log.between(before,after)
end
commit_list = commits.inject([]) do |list,commit|
# get list of added / removed / modified paths
# breaks with newer versions of schacon/ruby-git gem
# when lib.command.diff(@from,@to) always passes @to as a string, and tries to git diff '-p' 'sha' '' -- chokes on the ''
# (and newer forks too -- diff is broken there, parses diffs wrong and gives code bits in diff_file.type)
path_list = {'modified'=>[],'new'=>[],'deleted'=>[]}
# diff = g.diff(commit.sha)
# diff.each do |diff_file|
# path_list[diff_file.type] << diff_file.path
# end
list << {
'id' => commit.sha,
'message' => commit.message,
'timestamp' => commit.date.xmlschema,
# 'url' => 'commit_url',
'added' => path_list['new'],
'removed' => path_list['deleted'],
'modified' => path_list['modified'],
'author' => {
'name' => commit.author.name,
'email' => commit.author.email
}
}
end
payload = {
'payload' => {
'before' => before,
'after' => after,
'ref' => ref,
'commits' => commit_list
}.to_json
}
Daemons.daemonize
url = URI.parse(POST_RECEIVE_URL)
req = Net::HTTP::Post.new(url.path)
req.basic_auth(USER, PASSWORD) unless (USER.empty? || PASSWORD.empty?)
req.set_form_data(payload,';')
res = Net::HTTP.new(url.host, url.port).start {|http|
http.read_timeout = 3600
http.request(req)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment