lib/churn/churn_calculator.rb in churn-0.0.33 vs lib/churn/churn_calculator.rb in churn-0.0.34

- old
+ new

@@ -42,15 +42,43 @@ # 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 + if @churn_options.history + generate_history + else + self.emit + self.analyze + print ? self.to_s : self.to_h + end end + # this method generates the past history of a churn project from first commit to current + # running the report for oldest commits first so they are built up correctly + def generate_history + if @source_control.is_a?(GitAnalyzer) + begin + history_starting_point = Chronic.parse(@churn_options.history) + @source_control.get_commit_history.each do |commit| + `git checkout #{commit}` + commit_date = `git show -s --format="%ci"` + commit_date = Time.parse(commit_date) + next if commit_date < history_starting_point + #7776000 == 3.months without adding active support depenancy + start_date = (commit_date - 7776000) + `churn -s "#{start_date}"` + end + ensure + `git checkout master` + end + "churn history complete, this has munipulated git please make sure you are back on HEAD where you expect to be" + else + raise "currently generate history only supports git" + end + 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 || @ignore_files.include?(file) } @revisions = parse_log_for_revision_changes end @@ -85,10 +113,15 @@ #TODO crappy place to do this but save hash to revision file but while entirely under metric_fu only choice ChurnHistory.store_revision_history(first_revision, hash) hash end + def to_s + ChurnCalculator.to_s(to_h[:churn]) + end + + # Pretty print the data as a string for the user def self.to_s(hash) result = seperator result +="* Revision Changes \n" result += seperator result += "Files: \n" @@ -108,18 +141,14 @@ result += "\nMethods: \n" method_churn = collect_items(hash[:method_churn], 'method') result += display_array(method_churn) end - # Pretty print the data as a string for the user - def to_s - ChurnCalculator.to_s(to_h[:churn]) - end - private def self.collect_items(collection, match) + return [] unless collection collection.map {|item| (item.delete(match) || {}).merge(item) } end def sort_changes(changes) changes.to_a.sort! {|first,second| second[1] <=> first[1]} @@ -135,23 +164,19 @@ def self.seperator "*"*70+"\n" end - def seperator - ChurnCalculator.seperator - end - def self.git? - system("git branch") + !!(`git branch 2>&1` && $?.success?) end - + def self.hg? - system("hg branch") + !!(`hg branch 2>&1` && $?.success?) end - + def self.bzr? - system("bzr nick") + !!(`bzr nick 2>&1` && $?.success?) end def set_source_control(start_date) if self.class.git? GitAnalyzer.new(start_date)