lib/churn/churn_calculator.rb in churn-0.0.9 vs lib/churn/churn_calculator.rb in churn-0.0.10

- old
+ new

@@ -7,38 +7,46 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'source_control' require 'git_analyzer' require 'svn_analyzer' +require 'hg_analyzer' require 'location_mapping' require 'churn_history' module Churn class ChurnCalculator + # intialized the churn calculator object def initialize(options={}) start_date = options.fetch(:start_date) { '3 months ago' } @minimum_churn_count = options.fetch(:minimum_churn_count) { 5 } @source_control = set_source_control(start_date) @changes = {} @revision_changes = {} @class_changes = {} @method_changes = {} end + # prepares the data for the given project to be reported. + # reads git/svn logs analyzes the output, generates a report and either formats as a nice string or returns hash. + # @param [Bolean] format to return the data, true for string or false for hash + # @return [Object] returns either a pretty string or a hash representing the chrun of the project def report(print = true) self.emit self.analyze print ? self.to_s : self.to_h end + # Emits various data from source control to be analyses later... Currently this is broken up like this as a throwback to metric_fu def emit @changes = parse_log_for_changes.reject {|file, change_count| change_count < @minimum_churn_count} @revisions = parse_log_for_revision_changes end + # Analyze the source control data, filter, sort, and find more information on the editted files def analyze @changes = @changes.to_a.sort {|x,y| y[1] <=> x[1]} @changes = @changes.map {|file_path, times_changed| {:file_path => file_path, :times_changed => times_changed }} calculate_revision_changes @@ -47,10 +55,11 @@ @method_changes = @method_changes.map {|method, times_changed| {'method' => method, 'times_changed' => times_changed }} @class_changes = @class_changes.to_a.sort {|x,y| y[1] <=> x[1]} @class_changes = @class_changes.map {|klass, times_changed| {'klass' => klass, 'times_changed' => times_changed }} end + # collect all the data into a single hash data structure. def to_h hash = {:churn => {:changes => @changes}} hash[:churn][:class_churn] = @class_changes hash[:churn][:method_churn] = @method_changes #detail the most recent changes made this revision @@ -63,10 +72,11 @@ #TODO crappy place to do this but save hash to revision file but while entirely under metric_fu only choice ChurnHistory.store_revision_history(@revisions.first, hash) hash end + # Pretty print the data as a string for the user def to_s hash = to_h result = seperator result +="* Revision Changes \n" result += seperator @@ -105,12 +115,18 @@ def self.git? system("git branch") end + def self.hg? + system("hg branch") + end + def set_source_control(start_date) if self.class.git? GitAnalyzer.new(start_date) + elsif self.class.hg? + HgAnalyzer.new(start_date) elsif File.exist?(".svn") SvnAnalyzer.new(start_date) else raise "Churning requires a subversion or git repo" end