Skip to content

Instantly share code, notes, and snippets.

@danfinnie
Created February 27, 2013 19:25
Show Gist options
  • Save danfinnie/5050848 to your computer and use it in GitHub Desktop.
Save danfinnie/5050848 to your computer and use it in GitHub Desktop.
Git History Analyzer
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Command');
data.addColumn('number', 'Invocations');
data.addRows(<%= data %>);
// Set chart options
var options = {'title':'The Git Commands I Use',
height: '800'
}
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
#! /usr/bin/env ruby
require 'pp'
require 'erb'
data = Hash.new { |h, k| h[k] = 0 }
# Given a git list of arguments and command and options, extract the command.
# Examples:
# "commit --amend" --> "commit"
# '--work-tree="~/.git" gc --aggressive' --> "gc"
def extract_command str
match = str.prepend(" ").match(/(?<=\s)[a-zA-Z]+/)
return match ? match[0].downcase : nil
end
# File.expand_path will convert the tilde to the home directory.
File.open(File.expand_path('~/.bash_history'), 'r') do |f|
f.each_line do |line|
if line =~ /^git\s+(.*)/
cmd = extract_command($1)
data[cmd] += 1 if cmd
end
end
end
# Convert aliases
`git config --list`.each_line do |line|
if line =~ /^alias\.([^=]+)=(\S+)/
# Hash#delete does not respect the constructor that
# we passed at the start.
aliased_cmd = extract_command($2)
shortcut_cmd = $1.downcase
data[aliased_cmd] += data.delete(shortcut_cmd) if data.has_key? shortcut_cmd
end
end
data = data.to_a
template = ERB.new(File.open('git-history.html.erb').read)
output = template.result
File.open("git-history.html", "w+") do |f|
f.puts output
end
pp data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment