Luke Melia

software dev

April 26, 2008

Finding code to hurt

After listening to Ryan Davis' inspiring talk, "Hurting Code for Fun & Profit" at GoRuCo today, I wrote this rake task to figure out what code to hurt. It looks though your git log, and takes the 15 .rb files in your Rails' app/ directory and runs them through flog, so you can see which are the most complicated.

RUBY:
  1. namespace :analyze do 
  2.   namespace :commits do
  3.     desc 'Flog the most commonly revised files in the git history'
  4.     task :most_changed_files do
  5.       counts = Hash.new(0)
  6.       IO.popen("PAGER=cat git log --name-only --pretty=oneline 2>&1") do |pipe|
  7.         while (!pipe.eof) do
  8.           line = pipe.readline
  9.           next unless line =~ /^(app|lib).*\.rb$/
  10.           counts[line.chomp] += 1
  11.         end
  12.       end
  13.       counts.sort_by{|item| item.last}.reverse.first(15).each do |item|
  14.         flog_score = `flog -s #{item.first}`.to_f.round
  15.         puts "#{item.first} (in #{item.last} commits) (Flog: #{flog_score})"
  16.       end
  17.     end
  18.   end
  19. end

Install it with sake from here.

UPDATE: If you're interested in watching Ryan's stellar talk, it will be up on Confreaks soon, or you can try the RubyConf edition of the talk.

Leave a Reply

LukeMelia.com created 1999. ··· Luke Melia created 1976. ··· Live With Passion!