Ruby Grit: find commits between 2 branches

297 Views Asked by At

I would like to compare 2 branches and show the commits that exist in one but not the other. This works from command line git log --pretty=oneline branch_b ^branch_a --no-merges and gives me what I want but I would like to simulate the same thing in Grit to gain working with the object instead of strings. Is this possible in Grit?

1

There are 1 best solutions below

0
S. Christoffer Eliesen On

I think something like this should do the trick:

require 'grit'
repo = Grit::Repo.new(".")
repo.commits_between("branch_b", "branch_a")

See http://grit.rubyforge.org/classes/Grit/Repo.html#M000227 for doc ( http://grit.rubyforge.org/ for full doc).

This will give you commit objects in an array though, it doesn't format the output like the git command does. Also, it doesn't exclude the merge commits.

In order to get formatted output, try something like this:

puts repo.commits_between("branch_b", "branch_a").map { |c| ["*", c.id_abbrev, c.authored_date.to_date, c.short_message] * " " }