Internetbureau Holder

Browsing Git in Ruby

Jeroen Bulters di 03 aug 10

Recently I’ve been working on a project which involves a lot of interaction with git repositories. When I started hacking around, all interaction with the repository was performed through `shell` commands which – of course – works; but can not really be called idiomatic Ruby.

Luckily I’m not the only one who thinks so. During a presentation by Chris Wanstrath (detailing the stack GitHub uses) I was pointed to Scott Chacon’s GRIT.

Grit is an object oriented wrapper for interacting with any git repository and has only one dependency, git itself (which you have installed… RIGHT?).

Through grit it is possible to walk through a repository in a way we are all quite familiar with. For example, the following snippet gives me all commits made by me during the past 14 days in a repository.

require 'rubygems'
require 'grit'
require 'optparse'

# OptParse code omitted for brevity
# The option hash is supposed to have a committer, branch and days
# (number of days to check) value.
options = {}
options[:committer] = 'Jeroen Bulters'
options[:branch] = 'master'
options[:days] = 14

repo = Grit::Repo.new(Dir.pwd)

# Collect commits

# Ok, I admit, this is rancid, should use rails' fixnum ext.
start_date = (Time.now - (options[:days] * 60 * 60 * 24))

commits = repo.commits.select{|c| c.date > start_date}
filtered_commits = commits.select{|c| c.committer.to_s == options[:committer]}
puts "Found #{filtered_commits.size} commits..."

Although this is a fairly straightforward example, we should not forget that a git repository is a ‘simple’ directed acyclic graph. So get out your favorite graph traversal algorithm and apply it in some
way to your repositories (shortest path between root-commit and master anyone?), it’s easy, it’s fun and it doesn’t hurt!

Gepost in hor |  0 reacties

Plaats je reactie





Welcome to Holland On Rails

This weblog is the official Ruby techblog from the guys at Holder, a Ruby development company. Holder is also the company behind the RubyAndRails Europe Conference in Amsterdam.

Recente Jobs


Bekijk alle jobs »»

Gereedschapskist

Onmisbare tools voor
iedere developer!
Ruby On Rails
Framework voor de web 2.0 developer. Eindelijk vooruitgang!
TextMate
Editor for true pro's
Typ, tab, top :-)
Nee, niet voor Win.
Made On A Mac
En nou is het over met die saaie grijze Windows bak van je!

Auteurs op deze site

Chris Obdam

'Less is more' evangelist, past dit ook dagelijks toe op zijn tandenborstel.

Chiel Wester

Snelheidswonder op Ruby wielen. Leuk om mee te pair-programmen ;-) Recommend Me

Stephan Kaag

Het eerste Rails coreteam- member uit Nederland? Rails evangelist van het eerste uur.

Paul Engel

Én Rails programmeren én interfaces designen? Je zou hem superman kunnen noemen..

Dax Huiberts

Official Zip-Programmer, skinny code is helemaal zijn ding. Haalt meer code weg dan dat er bij komt.

Freek Monteban

Het nieuwste telg uit het Holland on Rails nest! Hij doet niets anders meer!

Johan Vermeulen

De stylesheet-koning uit de kop van Noord-Holland!