diff --git a/lib/git_stats/git_data/blob.rb b/lib/git_stats/git_data/blob.rb index d4b717290..3c07c1dcf 100644 --- a/lib/git_stats/git_data/blob.rb +++ b/lib/git_stats/git_data/blob.rb @@ -15,6 +15,10 @@ module GitStats @content ||= Command.new(repo, "git cat-file blob #{self.hash}").run end + def extension + @ext ||= File.extname(filename) + end + def to_s "#{self.class} #@hash #@filename" end diff --git a/lib/git_stats/git_data/commit.rb b/lib/git_stats/git_data/commit.rb index 5b4540efb..5ade7ad51 100644 --- a/lib/git_stats/git_data/commit.rb +++ b/lib/git_stats/git_data/commit.rb @@ -15,6 +15,14 @@ module GitStats end end + def files_by_extension + @files_by_extension ||= files.inject({}) { |acc, f| acc[f.extension] ||= []; acc[f.extension] << f; acc } + end + + def lines_by_extension + @lines_by_extension ||= Hash[files_by_extension.map { |ext, files| [ext, files.map(&:lines_count).sum] }] + 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/lib/git_stats/hash_initializable.rb b/lib/git_stats/hash_initializable.rb index 5775c229a..864e44332 100644 --- a/lib/git_stats/hash_initializable.rb +++ b/lib/git_stats/hash_initializable.rb @@ -1,5 +1,6 @@ module HashInitializable def initialize(params = {}) + raise "pass a Hash to initialize #{self.class}" unless params.is_a? Hash params.each { |k, v| instance_variable_set("@#{k}", v) } end end \ No newline at end of file diff --git a/lib/git_stats/stats_view/view_data.rb b/lib/git_stats/stats_view/view_data.rb index 7ebbe0eba..ae7a943a3 100644 --- a/lib/git_stats/stats_view/view_data.rb +++ b/lib/git_stats/stats_view/view_data.rb @@ -42,6 +42,26 @@ module GitStats end end + def files_by_extension + @files_by_extension ||= LazyHighCharts::HighChart.new('graph') do |f| + f.chart(type: "column") + f.title(text: "files by extension") + f.xAxis(categories: repo.commits.last.files_by_extension.keys) + f.yAxis(min: 0, title: {text: 'Commits'}) + f.series(name: "commits", data: repo.commits.last.files_by_extension.values.map(&:count)) + end + end + + def lines_by_extension + @lines_by_extension ||= LazyHighCharts::HighChart.new('graph') do |f| + f.chart(type: "column") + f.title(text: "files by extension") + f.xAxis(categories: repo.commits.last.lines_by_extension.keys) + f.yAxis(min: 0, title: {text: 'Commits'}) + f.series(name: "commits", data: repo.commits.last.lines_by_extension.values) + end + end + def files_by_date @files_by_date ||= LazyHighCharts::HighChart.new('graph') do |f| f.title(text: "Files") diff --git a/spec/author_spec.rb b/spec/author_spec.rb index 168cafdfb..d57234eee 100644 --- a/spec/author_spec.rb +++ b/spec/author_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe GitStats::GitData::Author do - let(:repo) { GitStats::GitData::Repo.new("repo_path") } + let(:repo) { GitStats::GitData::Repo.new(path: "repo_path") } let(:author) { GitStats::GitData::Author.new(repo: repo, name: "author1", email: "author1@gmail.com") } let(:other_author) { GitStats::GitData::Author.new(repo: repo, name: "author2", email: "author2@gmail.com") } let(:my_commits) { 10.times.map { |i| double("my_commit #{i}", :author => author) } } diff --git a/spec/generator_spec.rb b/spec/generator_spec.rb index b14d227d9..9c34dfb61 100644 --- a/spec/generator_spec.rb +++ b/spec/generator_spec.rb @@ -19,7 +19,7 @@ describe GitStats::Generator do it 'should render all templates with view data for this repo' do repo = double('repo') - GitStats::GitData::Repo.should_receive(:new).with(repo_path).and_return(repo) + GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, git_command_observer: nil).and_return(repo) view_data = double('view_data') GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data) diff --git a/spec/repo_spec.rb b/spec/repo_spec.rb index 43755daed..d6c8b4766 100644 --- a/spec/repo_spec.rb +++ b/spec/repo_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe GitStats::GitData::Repo do - let(:repo) { GitStats::GitData::Repo.new("repo_path") } + let(:repo) { GitStats::GitData::Repo.new(path: "repo_path") } describe 'git output parsing' do before { diff --git a/templates/files.haml b/templates/files.haml index 52505d908..bf5661ae9 100644 --- a/templates/files.haml +++ b/templates/files.haml @@ -1 +1,2 @@ = high_chart("files_by_date", files_by_date) += high_chart("files_by_extension", files_by_extension) \ No newline at end of file diff --git a/templates/lines.haml b/templates/lines.haml index fa40ba037..1392e6835 100644 --- a/templates/lines.haml +++ b/templates/lines.haml @@ -1 +1,2 @@ -= high_chart("lines_by_date", lines_by_date) \ No newline at end of file += high_chart("lines_by_date", lines_by_date) += high_chart("lines_by_extension", lines_by_extension) \ No newline at end of file