## Description [plain_text]

This script is intended for those who wish to push changesets
to lighthouse at push time, and not with a post-commit hook.
Because there's no point in having a distributed SCM if every
commit you do istantaneously goes public to lighthouse.

So, this script adds a new "git lh" command that fetches all the
revs between the remote origin and the current HEAD, and pushes
all of them to lighthouse.

This means that you should first issue git lh and then git push.

## Installation [plain_text]

Put the source file alongside the git command, naming it "git-lh".

## Configuration [plain_text]

Add the following to your .git/config:

[lighthouse]
token = your_lh_api_token
account = your_lh_account
project = your_lh_project_id

## Credits [plain_text]

Original guide: http://github.com/guides/integrating-git-commit-messages-in-lighthouse/
Original script: http://pastie.org/178549
Author: http://github.com/vjt

## Added goodies [plain_text]

A nice diffstat in lighthouse changeset messages ;-)
Don't want to issue two commands? Add this simple script:

## git-lh-push [bash]
#!/bin/bash
git lh && git push

## Ruby Source [ruby]

#!/usr/bin/env ruby
# Install this script as "git-lh", alongside your git command (which git if you're unsure)
# Based on http://pastie.org/178549 by <unknown>
# Modified by vjt@openssl.it (http://github.com/vjt)

require 'yaml'
require 'cgi'
require 'net/http'

git = `which git`.strip
revs = `#{git} log --pretty=format:%H origin..HEAD`.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+)\/(\w+)\.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