Skip to content

Instantly share code, notes, and snippets.

@Flink
Created June 29, 2011 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Flink/1053820 to your computer and use it in GitHub Desktop.
Save Flink/1053820 to your computer and use it in GitHub Desktop.
git/lighthouse push commits working with differents branches and ruby 1.9.2
#!/usr/bin/env ruby
require 'yaml'
require 'cgi'
require 'net/http'
git = `which git`.strip
current_branch = `#{git} branch |grep '*'|sed 's/^\*\s*//'`.strip
first = ARGV.shift || "origin/#{current_branch}"
last = ARGV.shift || 'HEAD'
revs = `#{git} log --pretty=format:%H --no-merges #{first}..#{last}`.split(/\n/).reverse
if revs.size.zero?
puts "Already up-to-date."
exit 1
end
LIGHTHOUSE_TOKEN = `#{git} config --get lighthouse.token`.chomp
LIGHTHOUSE_ACCOUNT = `#{git} config --get lighthouse.account`.chomp
LIGHTHOUSE_PROJECT = `#{git} config --get lighthouse.project`.chomp
GITHUB_ACCOUNT, GITHUB_REPOSITORY = `#{git} remote -v`.scan(/(\w+)\/(\S+)\.git/).flatten
puts "* Pushing #{revs.size} revisions to lighthouse"
revs.each do |revision|
author = `#{git} show --pretty=format:"%an" #{revision} | sed q`.chomp
log = `#{git} show --pretty=format:"%s" #{revision} | sed q`.chomp
date = `#{git} show --pretty=format:"%aD" #{revision} | sed q`.chomp
diffstat = `#{git} diff --stat #{revision}^..#{revision}`.chomp.gsub(/\n/, '\& ')
changed = `#{git} diff-tree -r --name-status #{revision} | sed -n '$p'`
changes = changed.split("\n").inject([]) { |memo, line| memo << [$1, $2] if line.strip =~ /(\w)\s+(.*)/ }.to_yaml
xml = <<-EOF
<changeset>
<title>#{CGI.escapeHTML("%s committed new changes to %s" % [author, GITHUB_REPOSITORY])}</title>
<body>
#{CGI.escapeHTML(log)}\n
#{"http://github.com/%s/%s/commit/%s" % [ GITHUB_ACCOUNT, GITHUB_REPOSITORY, revision ]}\n
#{CGI.escapeHTML(diffstat)}
</body>
<changes type="yaml">#{CGI.escapeHTML(changes)}</changes>
<revision>#{CGI.escapeHTML(revision)}</revision>
<changed-at type="datetime">#{CGI.escapeHTML(date.split('(').first.strip)}</changed-at>
</changeset>
EOF
request = Net::HTTP::Post.new("/projects/#{LIGHTHOUSE_PROJECT}/changesets.xml")
request.set_content_type "application/xml"
request.basic_auth LIGHTHOUSE_TOKEN, "x"
request.body = xml
response = Net::HTTP.start("#{LIGHTHOUSE_ACCOUNT}.lighthouseapp.com").request(request)
puts " Pushed rev #{revision} (#{log}). response: #{response.message}"
sleep 0.3
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment