Skip to content

Instantly share code, notes, and snippets.

@samuelkadolph
Last active December 14, 2015 22:28
Show Gist options
  • Save samuelkadolph/5158200 to your computer and use it in GitHub Desktop.
Save samuelkadolph/5158200 to your computer and use it in GitHub Desktop.
Creates a neat little chart for your git log based on keywords you provide.
#!/usr/bin/env ruby
require "date"
require "digest/md5"
require "erb"
require "optparse"
require "ostruct"
require "shellwords"
require "tempfile"
options = { :authors => [], :year => 2012 }
op = OptionParser.new do |op|
op.banner << " keyword..."
op.on("--author email", String, "Adds an author to the check") { |s| options[:authors] << s }
op.on("--year yyyy", Integer, "Sets the year to check") { |i| options[:year] = i }
end
op.parse!(ARGV)
if ARGV.empty?
puts op
exit 1
end
if options[:authors].empty?
options[:authors] = [`git config --global user.email`.chomp]
end
after = "#{options[:year]}-01-01"
authors = options[:authors].map { |a| "--author=#{a.shellescape}"}.join(" ")
before = "#{options[:year]+1}-01-01"
keywords = ARGV
months = {}
1.upto(12) { |i| months[i] = Hash.new { |h, k| h[k] = 0 } }
keywords.each do |keyword|
commits = `git log --after=#{after} --before=#{before} --format="format:%at" #{authors} --grep=#{keyword.shellescape} --format="format:%at" #{authors} --grep=#{keyword.shellescape}`.chomp
timestamps = commits.lines.map { |l| Time.at(l.to_i) }
timestamps.each { |t| months[t.month][keyword] += 1 }
end
context = OpenStruct.new
context.keywords = keywords
context.months = months
html = ERB.new(DATA.read).result(context.instance_exec { binding })
filename = "gitchart-#{Digest::MD5.hexdigest(html)}.html"
File.open(filename, "w") { |file| file << html }
system("open", filename)
__END__
<html>
<head>
<style type="text/css">
#chart { height: 100%; width: 100% }
</style>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(function() {
var data = google.visualization.arrayToDataTable([
["Year", "<%= keywords.join('", "') %>"],
<% months.sort.each do |m, v| %>
["<%= Date::MONTHNAMES[m] %>", <%= keywords.map { |k| v[k] }.join(', ') %>],
<% end %>
]);
var options = {
title: "Git Log",
vAxis: { title: "Commits" },
hAxis: { title: "Month" }
};
var chart = new google.visualization.ColumnChart(document.getElementById("chart"));
chart.draw(data, options);
});
</script>
</head>
<body>
<div id="chart"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment