public
Description: A useful campfire bot, also, tasty.
Homepage: http://projects.entp.com/staypuft
Clone URL: git://github.com/courtenay/staypuft.git
staypuft / git-campfire.rb
100644 61 lines (43 sloc) 1.782 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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