lines added/deleted by author charts

This commit is contained in:
Tomasz Gieniusz 2012-10-20 21:28:25 +02:00
parent af3d93aab9
commit 6d45e3fd57
11 changed files with 91 additions and 9 deletions

View File

@ -11,6 +11,18 @@ module GitStats
@commits ||= repo.commits.select { |commit| commit.author == self }
end
def lines_added
short_stats.map(&:insertions).sum
end
def lines_deleted
short_stats.map(&:deletions).sum
end
def short_stats
commits.map(&:short_stat)
end
def activity
@activity ||= Activity.new(commits)
end

View File

@ -8,7 +8,7 @@ module GitStats
attr_reader :repo, :hash, :filename
def lines_count
@lines_count ||= repo.run("git cat-file blob #{self.hash} | wc -l").to_i
@lines_count ||= binary? ? 0 : repo.run("git cat-file blob #{self.hash} | wc -l").to_i
end
def content
@ -19,6 +19,10 @@ module GitStats
@ext ||= File.extname(filename)
end
def binary?
repo.run("git cat-file blob #{self.hash} | grep -m 1 '^'") =~ /Binary file/
end
def to_s
"#{self.class} #@hash #@filename"
end

View File

@ -22,13 +22,23 @@ module GitStats
end
def lines_by_extension
@lines_by_extension ||= Hash[files_by_extension.map { |ext, files| [ext, files.map(&:lines_count).sum] }]
@lines_by_extension ||= Hash[files_by_extension.map { |ext, files|
[ext, files.map(&:lines_count).sum]
}.delete_if { |ext, lines_count| lines_count == 0 }]
end
def files_count
@files_count ||= repo.run("git ls-tree -r --name-only #{self.hash} | wc -l").to_i
end
def binary_files_count
files.find_all { |f| f.binary? }.size
end
def text_files_count
files_count - binary_files_count
end
def lines_count
@lines_count ||= repo.run("git diff --shortstat `git hash-object -t tree /dev/null` #{self.hash}").lines.map do |line|
line[/(\d+) insertions?/, 1].to_i

View File

@ -7,7 +7,8 @@ module GitStats
attr_reader :path
delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension, :files_count, :lines_count, to: :last_commit
delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension,
:files_count, :binary_files_count, :text_files_count, :lines_count, to: :last_commit
def initialize(params)
super(params)
@ -36,6 +37,14 @@ module GitStats
commits.map(&:date).minmax
end
def lines_added_by_author
Hash[authors.map { |author| [author, author.lines_added] }]
end
def lines_deleted_by_author
Hash[authors.map { |author| [author, author.lines_deleted] }]
end
def files_count_each_day
@files_count_each_day ||= commits_period_range.map { |day|
files_count_at day

View File

@ -34,7 +34,7 @@ module GitStats
def day_chart(params)
common_params(params)
type "datetime"
xAxis(type: "datetime")
series(
type: "area",
name: "commits",

View File

@ -2,7 +2,8 @@ module GitStats
module StatsView
module Charts
class All
delegate :files_by_extension, :lines_by_extension, :files_by_date, :lines_by_date, to: :repo_charts
delegate :files_by_extension, :lines_by_extension, :files_by_date, :lines_by_date,
:lines_added_by_author, :lines_deleted_by_author, to: :repo_charts
delegate :by_authors_wday, to: :authors_charts
delegate :activity_by_hour, to: :activity_charts

View File

@ -48,6 +48,26 @@ module GitStats
end
end
def lines_added_by_author
Chart.new do |f|
f.column_hash_chart(
data: Hash[@repo.lines_added_by_author.map {|a, l| [a.email, l]}],
title: :lines_added_by_author.t,
y_text: :lines.t
)
end
end
def lines_deleted_by_author
Chart.new do |f|
f.column_hash_chart(
data: Hash[@repo.lines_deleted_by_author.map {|a, l| [a.email, l]}],
title: :lines_deleted_by_author.t,
y_text: :lines.t
)
end
end
end
end
end

View File

@ -0,0 +1,24 @@
require 'spec_helper'
describe GitStats::GitData::Blob do
let(:repo) { double }
let(:png_blob) { GitStats::GitData::Blob.new(filename: 'abc.png', hash: 'hash_png', repo: repo) }
let(:txt_blob) { GitStats::GitData::Blob.new(filename: 'abc.txt', hash: 'hash_txt', repo: repo) }
it 'should return 0 as lines count when files is binary' do
png_blob.should_receive(:binary?).and_return true
png_blob.lines_count.should == 0
end
it 'should return actual lines count when files is not binary' do
txt_blob.should_receive(:binary?).and_return false
repo.should_receive(:run).with("git cat-file blob hash_txt | wc -l").and_return 42
txt_blob.lines_count.should == 42
end
it 'should invoke grep to check if file is binary' do
repo.should_receive(:run).with("git cat-file blob hash_png | grep -m 1 '^'").and_return "Binary file matches"
png_blob.should be_binary
end
end

View File

@ -37,10 +37,10 @@ describe GitStats::GitData::Commit do
GitStats::GitData::Blob.should_receive(:new).and_return(
double(lines_count: 40, extension: '.rb'),
double(lines_count: 60, extension: '.rb'),
double(lines_count: 10, extension: '.haml'),
double(lines_count: 0, extension: '.haml'),
double(lines_count: 20, extension: '.css'),
)
commit.lines_by_extension.should == {'.rb' => 100, '.haml' => 10, '.css' => 20}
commit.lines_by_extension.should == {'.rb' => 100, '.css' => 20}
end
end
end

View File

@ -1 +1,3 @@
= high_chart("by_authors_wday", charts.by_authors_wday)
= high_chart("by_authors_wday", charts.by_authors_wday)
= high_chart("lines_added_by_author", charts.lines_added_by_author)
= high_chart("lines_deleted_by_author", charts.lines_deleted_by_author)

View File

@ -16,7 +16,7 @@
%td= repo.commits_period.map {|d| d.to_formatted_s(:long)}.join(" .. ")
%tr
%td= :total_files.t
%td= repo.files_count
%td= "#{repo.files_count} (binary: #{repo.binary_files_count}, text: #{repo.text_files_count})"
%tr
%td= :total_lines.t
%td= "#{repo.lines_count} lines (#{repo.short_stats.map(&:insertions).sum} insertions, #{repo.short_stats.map(&:deletions).sum} deletions)"