Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jsanti/118466 to your computer and use it in GitHub Desktop.
Save jsanti/118466 to your computer and use it in GitHub Desktop.
Integrity git post-receive hook (Ruby 1.8)
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'json'
# EDIT POST_RECEIVE_URL
POST_RECEIVE_URL = 'http://hostname:8910/repository-name/push'
old_head, new_head, ref = ARGV
revision_text = `git-rev-list --pretty=format:'Author: %an <%ae>%nDate: %cd%n%s%n' #{new_head} ^#{old_head}`
revisions = []
revision_text.split(/\n\n/).each { |commit|
s = commit.split(/\n/)
s[0] =~ /commit (\w+)/
sha1 = $1
s[1] =~ /Author: (.*) <(.+?)>/
author_name, author_email = $1, $2
s[2] =~ /Date: +(.+?) -0/
timestamp = $1
message = s[3].strip
revisions << {
'id' => sha1,
'author' => {
'email' => author_email,
'name' => author_name,
},
'message' => message,
'timestamp' => timestamp,
}
}
if revisions.empty?
exit 0
end
payload = {
'payload' => {
"ref" => ref,
"commits" => revisions,
}.to_json
}
if pid = fork
Process.detach pid
else
Net::HTTP.post_form( URI.parse( POST_RECEIVE_URL ), payload )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment