Skip to content

Instantly share code, notes, and snippets.

@dpick
Forked from michaelfeathers/vaportrails.rb
Created March 3, 2011 08:03
Show Gist options
  • Save dpick/852487 to your computer and use it in GitHub Desktop.
Save dpick/852487 to your computer and use it in GitHub Desktop.
# 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
@dpick
Copy link
Author

dpick commented Mar 3, 2011

The axis with the dates looks pretty terrible when there are more than a few commits, maybe I'll fix that tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment