-
-
Save dpick/852487 to your computer and use it in GitHub Desktop.
This file contains 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
# Print the vaportrail of a ruby file in a git repo, i.e., | |
# the complexity level at each commit. | |
# | |
# Requires: >= bash ?.? | |
# >= git 1.7.1 | |
# >= ruby 1.9.2 | |
# >= flog 2.5.0 | |
# >= gruff 0.3.6 | |
# | |
require 'rubygems' | |
require 'flog' | |
require 'time' | |
require 'gruff' | |
TEMP_FILE = "/tmp/vaportrail_junk.rb" | |
class VaporTrail | |
class Reporter < ::StringIO | |
# borrowed from Chad Fowler | |
SCORE_LINE_DETECTOR = /^\s+([^:]+).*flog total$/ | |
def score | |
Float(string.scan(SCORE_LINE_DETECTOR).flatten.first) | |
end | |
end | |
def self.trail_for(filename) | |
trail = new(filename).generate_datestamped_complexities.sort | |
trail | |
end | |
def initialize(filename) | |
@filename = filename | |
end | |
def generate_datestamped_complexities | |
complexities = {} | |
sha1s_of_commits.each do |sha1| | |
complexities[commit_date_of(sha1)] = complexity_of(sha1) | |
end | |
complexities | |
end | |
def commit_date_of(sha1) | |
Time.parse(`git show -s --format=%cd #{sha1}`).strftime("%D") | |
end | |
def complexity_of(sha1) | |
`git show #{sha1}:#{@filename} > #{TEMP_FILE}` | |
flogger = Flog.new | |
reporter = Reporter.new | |
flogger.flog(TEMP_FILE) | |
flogger.report(reporter) | |
reporter.score | |
end | |
def sha1s_of_commits | |
@commits ||= `git log #{@filename}| grep ^commit`.split(/\n/).map(&:split).map {|fields| fields[sha1_columm = 1] } | |
end | |
end | |
def create_graph(trail) | |
dates, flog_values = trail.transpose | |
g = Gruff::Line.new | |
g.title = "Flog Vs. Date" | |
g.data("Flog", flog_values) | |
labels = {} | |
dates.each_index { |i| labels[i] = dates[i] } | |
g.labels = labels | |
g.write('flog.png') | |
end | |
if ARGV.size != 1 | |
puts "help: vaportrail <rubyfilename>" | |
puts " run in the folder of a git repo" | |
exit | |
end | |
trail = VaporTrail.trail_for(ARGV[0]) | |
create_graph(trail) | |
puts trail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The axis with the dates looks pretty terrible when there are more than a few commits, maybe I'll fix that tomorrow.