Arthur Chang

Campfire and Git pushes

Previously when using Campfire, we used a library called Marshmallow, that joins a room and says what commit happened in an SVN repository.  Since moving over to a Git repository, we needed a new bot that would come and tell us what was happening.

I turned to an unofficial Campfire API called Tinder, and wrote a little bit of code to join a single room that has guest access turned on, to say what was just committed to the repository.

The code joins a room, looks for commits in Git, and "speaks".  It runs post-update, so put this in the /git/repo.git/hooks/post-update file.  Don't forget to chmod 770 the post-update file so it can be executed.  This hook is basically what is run when you do a push to the repository.

#!/usr/bin/env ruby

require 'rubygems'
require 'tinder' #require the tinder gem

# room: the subdomain you use for subdomain.campfirenow.com
# path: path to your repository you want to track
# hash: the guest hash, should be about 5 characters long.  you need to turn on guest access in campfire manually first.
# msg: a nice commit message, check the manual for git-log for some flags
#          %an is the author name, %ar is time since the commit, %s is the message, %H is the commit hash
config = {
  :room => 'SUBDOMAIN',
  :path => '/git/repo/path/repo.git',
    :hash => 'GUEST_HASH',
  :msg  => 'Commit by %an (%ar): \"%s\"%nChangeset: http://trac.yoursite.com/changeset/%H'
}

# create new campfire instance with the room
campfire = Tinder::Campfire.new(config[:room])
# get the guest room
room = campfire.find_room_by_guest_hash(config[:hash], 'gitbot')

# get all the commits
commits = `cd #{config[:path]} && git log -#{ARGV.size} --pretty=format:"#{config[:msg]}"`

# say something for each commit
commits.each_line do |commit|
  room.speak "#{commit}"
end

I added some comments above explaining what was going on.  You might also be curious about the msg hash.  The changeset link is going to a modified version of Trac, which I've installed a Git Plugin, so Trac uses our Git repository instead of our SVN repository.  There's probably better tracking systems out there, but we're just reusing what we had before.

Tagged  //   Campfire   code   Git   Tinder  

Remote Branches in Git

Here's a nice ruby script that will automatically create a remote tracking branch on your git repository! Assuming you have a remote bare Git repository:

Create a file in your script directory called create-branch with the contents below:

#!/bin/sh
# create-branch <branch_name>

if [ $# -ne 1 ]; then
echo 1>&2 Usage: $0 branch_name
exit 127
fi

echo [GIT] creating branch: $1
git push origin origin:refs/heads/$1
echo [GIT] updating with origin
git fetch origin
echo [GIT] checking out remote branch: origin/$1
git checkout --track -b $1 origin/$1
echo [GIT] pulling update from: origin/$1
git pull
echo [GIT] finished creating remote branch: origin/$1
echo [GIT] finished creating tracking branch: $1

 

Now to create your remote branch, change to your rails root directory and:

ruby script/create-branch <new_branch_name>

 

This is a great way to share branches with others and committing them to a repository.  Central shared repositories are more of the SVN or CVS like mindset, while Git is designed in a way where you are not constrained to a central repository.  I never really found a real world use case where I wanted people all chained together without any real central backed up repository, but hey, you never know.

Tagged  //   code   Git   remote branches