Skip to content

Instantly share code, notes, and snippets.

@jwhitmire
Created May 17, 2018 19:24
Show Gist options
  • Save jwhitmire/c231869ea6b322e61da4b8a94863f5a0 to your computer and use it in GitHub Desktop.
Save jwhitmire/c231869ea6b322e61da4b8a94863f5a0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def format_commit_info(timestamp, time_desc, commit_id, message, ref_name, upstream)
[
"#{timestamp.strftime("%y %b %d")}, #{timestamp.strftime("%l:%M%p").downcase}",
"(#{time_desc})",
commit_id,
message,
ref_name,
upstream || ''
]
end
def render_commit_info(timestamp, time_desc, commit_id, message, ref_name, upstream, merged)
[
timestamp,
time_desc,
colorize(upstream, :purple),
colorize(ref_name, :green),
colorize(commit_id, merged ? :gray : :cyan),
colorize(message.strip, message[/^Temp/] ? :red : :gray)
].join(' ')
end
ANSI_COLORS = {
:black => 30,
:blue => 34,
:cyan => 36,
:gray => 90,
:green => 32,
:purple => 35,
:red => 31,
:white => 37,
:yellow => 33
}
def colorize(message, color=nil)
if color
"\e[#{ANSI_COLORS[color]}m#{message}\e[0m"
else
message
end
end
upstreams = Hash[`git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads`.strip.split("\n").collect{|e| e.split(" <- ")}.collect{|a| [a[0], a[1] ? a[1].split('/')[0] : '']}]
commit_info = `git branch #{ARGV.join(' ')} | cut -c 3-`.strip.split("\n").reject {|ref_name|
ref_name[' -> ']
}.map {|ref_name|
`git log --no-walk --pretty=format:"%ct\n%cr\n%h\n%s" '#{ref_name}' --`.strip.split("\n").push(ref_name).push(upstreams[ref_name])
}.map {|commit_info|
[Time.at(commit_info.shift.to_i)].concat(commit_info)
}.sort_by {|commit_info|
commit_info.first # unix timestamp
}.reverse.map {|commit_info|
format_commit_info(*commit_info)
}.transpose.map {|column|
max_col_length = column.sort_by {|i| i.length }.last.length
column.map {|i| i.ljust(max_col_length) }
}.transpose.map {|commit_info|
commit_info.push(
`git merge-base HEAD #{commit_info[2]}`.chomp[0...7] == commit_info[2]
)
}.each {|commit_info|
puts render_commit_info(*commit_info)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment