Skip to content

Instantly share code, notes, and snippets.

@nowells
Created December 1, 2011 18:51
Show Gist options
  • Save nowells/1418945 to your computer and use it in GitHub Desktop.
Save nowells/1418945 to your computer and use it in GitHub Desktop.
A tool to display all branches that have dependancies in your repository and list out what branches have been merged and are therefore dependancies.
#!/usr/bin/env ruby
begin
require 'rainbow'
rescue LoadError
module Rainbow
def color(color)
self
end
def background(color)
self
end
end
String.send(:include, Rainbow)
end
class Branch
def self.branches
@@branches ||= Hash[
`git branch -r`.split("\n")
.map { |x| x.strip }
.select { |x| x !~ /\bmaster\b/ }
.map { |x| [x, Branch.new(x)] }
]
end
def initialize(base_name)
@base_name = base_name.strip
end
def base_name
@base_name
end
def name
@name ||= @base_name.gsub(/^(heads|head|origin|remotes\/[^\/]+)\//, '').strip
end
def dependancies
@dependancies ||= branch_dependancies
end
def description
@description ||= `git log origin/master..#{base_name} --format="%B"`
end
def rev_list
@rev_list ||= extract_rev_list(`git rev-list #{base_name} --not origin/master --format="%H"`)
end
def master_merge_base
@master_merge_base ||= `git merge-base origin/master #{base_name}`.strip
end
def merge_base(branch)
`git merge-base #{branch.base_name} #{base_name}`.strip
end
def behind(branch)
extract_rev_list(`git rev-list #{branch.base_name} --not #{base_name}`).count
end
private
def branch_dependancies
Branch.branches.select do |name, branch|
if branch.base_name.eql?(base_name)
false
else
if !rev_list.member?(merge_base(branch))
false
else
#!master_merge_base.eql?(merge_base(branch))
!((rev_list - branch.rev_list).empty?) && !(description.scan(branch.name).empty?)
end
end
end
end
def extract_rev_list(data)
data.split("\n")
.select { |x| x !~ /^commit / }
.map { |x| x.gsub(/commit /, '').split(' ').last.strip }
end
end
puts "Branch dependancies:"
Branch.branches.each do |name, branch|
if !branch.dependancies.empty?
puts " #{name}".color(:yellow)
branch.dependancies.map do |sub_name, sub_branch|
behind = branch.behind(sub_branch)
color = :green
if behind > 0
color = :red
end
puts " #{sub_branch.name}".color(color)
end
puts ""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment