#!/usr/bin/env ruby
require 'rubygems'
gem 'tinder'
require 'tinder'
#
# This script runs as a git post-receive hook.
repo = ENV['SSH_ORIGINAL_COMMAND'].split(" ")[1]
# Todo: safe config in YML
# Todo: hash of repo -> auth details
campfire = Tinder::Campfire.new 'your-campfire-room'
campfire.login 'username', 'password'
# Todo: join a specific room by name
room = campfire.rooms[0]
# You don't need to change anything below here
# =====================================================================
GIT = `which git`.strip
# Touches a file in the repository containing a timestamp of the
# last commit that staypuft posted to the room.
filename = File.join(File.dirname(__FILE__), repo[/[\w.]+/] + ".log")
if File.exist?(filename)
last_revision = Time.parse File.open(filename) { |f| f.read.strip }
else
room.speak "Debug: Couldn't find previous revision file"
last_revision = Time.now - 5
end
text = `#{GIT} log --all --since="#{last_revision}"`
File.open(filename, "w+") { |f| f.write Time.now.utc }
lines = text.split("\n\ncommit ")
room.speak "Updated #{lines.size} commits since #{last_revision}"
lines.each do |line|
revision = line[/([a-f0-9]{32})/]
commit_author = `#{GIT} show --pretty=format:"%an" #{revision} | sed q`.chomp
commit_log = `#{GIT} show --pretty=format:"%s" #{revision} | sed q`.chomp
commit_date = `#{GIT} show --pretty=format:"%aD" #{revision} | sed q`.chomp
commit_changed = `#{GIT}-diff-tree --name-status #{revision} | sed -n '$p'`
commit_changes = commit_changed.split("\n").inject([]) do |memo, line|
if line.strip =~ /(\w)\s+(.*)/
memo << [$1, $2]
end
end.to_yaml
room.speak "#{commit_author}: #{repo}:..#{revision.last(5)} >> #{commit_log}"
room.paste commit_changed
end