diff --git a/bin/git_stats b/bin/git_stats index 36db0405e..8311ddbc1 100755 --- a/bin/git_stats +++ b/bin/git_stats @@ -7,9 +7,5 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib) # start up the CLI require "git_stats/cli" -begin - cli = GitStats::CLI.new - cli.start(*ARGV) -rescue ArgumentError => e - puts e.message -end \ No newline at end of file +cli = GitStats::CLI.new +cli.start(*ARGV) diff --git a/lib/git_stats/git_data/activity.rb b/lib/git_stats/git_data/activity.rb index 1704400a2..77fc78589 100644 --- a/lib/git_stats/git_data/activity.rb +++ b/lib/git_stats/git_data/activity.rb @@ -5,15 +5,37 @@ module GitStats attr_reader :by_hour, :by_wday, :by_wday_hour, :by_month, :by_year, :by_year_week def initialize(commits) - @by_hour = @by_wday = @by_month = @by_year = Hash.new(0) - @by_wday_hour = @by_year_week = Hash.new { |h, k| h[k] = Hash.new(0) } - add_commits(commits) end + def by_hour + @by_hour ||= default_hash + end + + def by_wday + @by_wday ||= default_hash + end + + def by_wday_hour + @by_wday_hour ||= default_double_hash + end + + def by_month + @by_month ||= default_hash + end + + def by_year + @by_year ||= default_hash + end + + def by_year_month + @by_year_week ||= default_double_hash + end + private def add_commits(commits) - commits.values.each do |commit| + commits = commits.values if commits.is_a? Hash + commits.each do |commit| add_commit_at(commit.date) end end @@ -24,7 +46,15 @@ module GitStats self.by_wday_hour[date.wday][date.hour] += 1 self.by_month[date.month] += 1 self.by_year[date.year] += 1 - self.by_year_week[date.year][date.cweek] += 1 + self.by_year_month[date.year][date.month] += 1 + end + + def default_hash + Hash.new(0) + end + + def default_double_hash + Hash.new { |h, k| h[k] = Hash.new(0) } end end diff --git a/lib/git_stats/git_data/commit.rb b/lib/git_stats/git_data/commit.rb index a76bebd31..b1cbb6074 100644 --- a/lib/git_stats/git_data/commit.rb +++ b/lib/git_stats/git_data/commit.rb @@ -7,11 +7,6 @@ module GitStats attr_reader :repo, :hash, :stamp, :date, :author - def gather_all_data - files_count - short_stat - end - def files_count @files_count ||= Command.new(repo, "git ls-tree -r --name-only #{self.hash} | wc -l").run.to_i end diff --git a/spec/activity_spec.rb b/spec/activity_spec.rb new file mode 100644 index 000000000..4ba6baad0 --- /dev/null +++ b/spec/activity_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe GitStats::GitData::Activity do + let(:dates) { [ + '10.05.2012 12:37', + '10.05.2012 13:53', + '06.05.2012 13:23', + '15.06.2011 15:02', + '27.09.2011 15:34' + ] } + let(:commits) { dates.map { |d| double(:date => DateTime.parse(d)) } } + let(:activity) { GitStats::GitData::Activity.new(commits) } + + it 'by_hour should count commits by hour' do + activity.by_hour.should == {12 => 1, 13 => 2, 15 => 2} + end + + it 'by_wday should count commits by day of week where 0 = sunday, 1 = monday, ...' do + activity.by_wday.should == {0 => 1, 2 => 1, 3 => 1, 4 => 2} + end + + it 'by_wday_hour should count commits by day of week and by hour' do + activity.by_wday_hour.should == {0 => {13 => 1}, 2 => {15 => 1}, 3 => {15 => 1}, 4 => {12 => 1, 13 => 1}} + end + + it 'by_month should count commits by month' do + activity.by_month.should == {5 => 3, 6 => 1, 9 => 1} + end + + it 'by_year should count commits by year' do + activity.by_year.should == {2011 => 2, 2012 => 3} + end + + it 'by_year_month should count commits by day of year and by month' do + activity.by_year_month.should == {2011 => {6 => 1, 9 => 1}, 2012 => {5 => 3}} + end +end \ No newline at end of file