Created
April 16, 2016 00:40
-
-
Save jaredatron/9408f0d0a61e99ca38bf18bd5422667c to your computer and use it in GitHub Desktop.
git-branch-details
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'time' | |
require 'colored' | |
require 'parallel' | |
class GitBranchDetails | |
COLUMNS = [ | |
'name', | |
'latest_commit_ago', | |
'author_name', | |
'merged into origin/master', | |
'travis', | |
] | |
def initialize(branch_names=nil) | |
@branch_names = branch_names unless branch_names.empty? | |
end | |
def print_report | |
print_formats = COLUMNS.map do |column| | |
max = (branches.map{|b| b[column].to_s.length } + [column.length]).max | |
"%-#{max+5}s" | |
end | |
print_format = print_formats.join(' ')+"\n" | |
x = sprintf print_format, *COLUMNS | |
puts x | |
puts '-' * x.length | |
branches.each do |branch| | |
line = sprintf(print_format, *branch.values_at(*COLUMNS)) | |
print line | |
end | |
end | |
def branch_names | |
@branch_names ||= `git branch`.chomp.gsub(/^../,'').split("\n") | |
end | |
def branches | |
@branches ||= begin | |
branch_names.reject! do |branch_name| | |
ignored_branches.include?(branch_name) | |
end | |
branches = Parallel.map(branch_names, in_processes: branch_names.length) do |branch_name| | |
get_branch_details(branch_name) | |
end | |
branches.sort_by!{|b| b['latest_commit_time'] }.reverse! | |
branches | |
end | |
end | |
def travis_details | |
`travis requests --pro` | |
end | |
def get_branch_details(branch_name) | |
details = `git log -1 --format="%h,%an,%ae,%ci,%cr" #{branch_name}`.chomp.split(',') | |
branch = %w{sha author_name author_email latest_commit_time latest_commit_ago}.zip(details).to_h | |
branch['name'] = branch_name | |
branch['latest_commit_time'] = Time.parse(branch['latest_commit_time']) | |
branch['merged into origin/master'] = `git branch -r --contains #{branch_name}`.chomp.gsub(/^../,'').split("\n").include?('origin/master') ? 'yes'.green : 'no'.yellow | |
branch['travis'] = get_travis_state(branch_name) | |
# branch['travis'] = `travis requests --pro` | |
branch | |
end | |
def get_travis_state(branch_name) | |
travis_show = `travis show #{branch_name} 2>&1` | |
if travis_show =~ /^resource not found/i | |
return 'not found'.yellow | |
end | |
travis_state = travis_show[/State:\s+(.+)\n/,1] | |
case travis_state | |
when 'passed'; 'passed'.green | |
when 'started'; 'started'.green | |
when 'failed'; 'failed'.red | |
when 'canceled'; 'canceled'.yellow | |
else; "unknown #{travis_state}" | |
end | |
end | |
def ignored_branches | |
%w{master staging production} | |
end | |
end | |
GitBranchDetails.new(ARGV).print_report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment