Skip to content

Instantly share code, notes, and snippets.

@redsquirrel
Forked from michaelfeathers/vaportrails.rb
Created March 3, 2011 05:33
Show Gist options
  • Save redsquirrel/852384 to your computer and use it in GitHub Desktop.
Save redsquirrel/852384 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# 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
#
require 'flog'
require 'time'
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(filenames)
columns = ["datetime"] + filenames
columns + new(filenames).generate_datestamped_complexities.sort_by {|array| array.first }
end
def initialize(filenames)
@filenames = filenames
end
def generate_datestamped_complexities
complexities = []
@filenames.each_with_index do |filename, i|
sha1s_of_commits_for(filename).each do |sha1|
commit_data = [commit_date_of(sha1)]
add_padding_to(commit_data, index)
commit_data << complexity_of(sha1, filename)
add_padding_to(commit_data, @filenames.size - (@filenames.size - (index + 1)))
complexities << commit_data
end
end
complexities
end
def add_padding_to(commit_data, count)
padding.times { commit_data << nil }
end
def commit_date_of(sha1)
Time.parse(`git show -s --format=%cd #{sha1}`).strftime("%D %T")
end
def complexity_of(sha1, filename)
`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_for(filename)
`git log #{filename} | grep ^commit`.split(/\n/).map(&:split).map {|fields| fields[sha1_columm = 1] }
end
end
if ARGV.empty?
puts "help: vaportrail <rubyfilename1> <rubyfilename2> ..."
puts " run in the folder of a git repo"
exit
end
p VaporTrail.trail_for(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment