Skip to content

Instantly share code, notes, and snippets.

@pariser
Forked from jaredatron/git-branch-details
Last active March 23, 2016 16:59
Show Gist options
  • Save pariser/b080ff075404c0c3f637 to your computer and use it in GitHub Desktop.
Save pariser/b080ff075404c0c3f637 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'time'
require 'colored'
require 'pry'
SPECIAL_BRANCHES = %w(
production
staging
beta
integration
).freeze
def get_current_branch
`git rev-parse --abbrev-ref HEAD`.chomp
end
def get_branches
`git branch`.chomp.gsub(/^../, '').split("\n")
end
def get_branch_info(ref)
data = `git log -1 --format="%h,%an,%ae,%ci,%cr" #{ref}`.chomp.split(',')
data = %w(sha author_name author_email latest_commit_time latest_commit_ago).zip(data).to_h
data['latest_commit_time'] = Time.parse(data['latest_commit_time'])
data['merged into origin/master'] = `git branch -r --contains #{ref}`.chomp.gsub(/^../, '').split("\n").include?('origin/master') ? 'yes' : 'no'
data
end
def print(branches, *columns)
column_widths = columns.each_with_object({}) do |column, hash|
hash[column] = (branches.map { |b| b[column].length } + [column.length]).max + 5
end
current_branch = get_current_branch
print_format = column_widths.map { |_, width| "%-#{width}s" }.join(" ") + "\n"
x = print_format % columns
puts x
puts '-' * x.length
branches.each do |branch|
format_strings = []
values = []
columns.each do |column|
color =
if column == 'name' && current_branch == branch[column]
:green
elsif column == 'name' && !SPECIAL_BRANCHES.index(branch[column]).nil?
:yellow
else
:to_s
end
unformatted_width = branch[column].length
formatted_value = branch[column].send(color)
formatted_width = formatted_value.length
column_width = column_widths[column] + formatted_width - unformatted_width
format_strings << "%-#{column_width}s"
values << formatted_value
end
print_format = format_strings.join(" ") + "\n"
printf print_format, *values
end
end
branches = get_branches
branches.map! do |branch_name|
{
'name' => branch_name
}.merge(get_branch_info(branch_name))
end
branches.sort_by! { |b| b['latest_commit_time'] }.reverse!
print(branches, 'name', 'latest_commit_ago', 'author_name', 'merged into origin/master')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment