PatientsLikeMe Tech Blog

We’re a team of Ruby on Rails developers and UX practitioners at PatientsLikeMe, where patients share data about their treatments, symptoms, and disease outcomes. We’re classically trained ninjas, pirates, rockstars, and dinosaur hunters. Our keyboards are magic wands — with lots of buttons, so they’re even better than regular magic wands. Meet the Team ↓

Spring Cleaning Git

Use branches for any length of time and you’re bound to end up with a long and cumbersome list hanging around.  Cleaning those up takes separating the branches that have been merged into your master branch and have outlived their utility and those that have work remaining.

To solve this problem, I wrote a zombie branch checker using the grit gem. It will print out the names of all remote branches along with the last person committing to the branch, whether or not that branch has been merged into master, and the number of commits on the branch that don’t exist in master.

Before running this, you should run “git remote prune origin” so your local copy of the repository does not include branches that have already been deleted.

require ‘rubygems’
require ‘grit’
include Grit
 
MASTER = ‘origin/master’
 
repo = Repo.new(‘.’)
 
puts "branch,last committer, last commit date, merged into master, number of diff commits"
 
repo.remotes.each do |b|
  next if b.name == MASTER || b.name == "origin/HEAD"
  last_commit = repo.commits(b.name, 1).first
  commit_diff = repo.commits_between(MASTER,b.name)
  puts "#{b.name}, #{last_commit.committer.name}, #{last_commit.date.strftime(‘%Y-%m-%d’)}, #{commit_diff.empty?}, #{commit_diff.size} "
end

Comments are closed.