repo.commits is now date sorted array, index page with simple stats

This commit is contained in:
Tomasz Gieniusz 2012-10-13 14:31:52 +02:00
parent 2d7a7db33c
commit 9ca2ece131
6 changed files with 51 additions and 23 deletions

View File

@ -8,7 +8,7 @@ module GitStats
attr_reader :repo, :name, :email attr_reader :repo, :name, :email
def commits def commits
@commits ||= repo.commits.select { |hash, commit| commit.author == self } @commits ||= repo.commits.select { |commit| commit.author == self }
end end
def activity def activity

View File

@ -8,22 +8,24 @@ module GitStats
end end
def authors def authors
@authors ||= Command.new(self, 'git shortlog -se HEAD').run.lines.inject({}) do |authors, line| @authors ||= Hash[Command.new(self, 'git shortlog -se HEAD').run.lines.map do |line|
name, email = line.split(/\t/)[1].strip.scan(/(.*)<(.*)>/).first.map(&:strip) name, email = line.split(/\t/)[1].strip.scan(/(.*)<(.*)>/).first.map(&:strip)
authors[email] = Author.new(repo: self, name: name, email: email) [email, Author.new(repo: self, name: name, email: email)]
authors end]
end
end end
def commits def commits
@commits ||= Command.new(self, 'git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').run.lines.inject({}) do |commits, commit_line| @commits ||= Command.new(self, 'git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').run.lines.map do |commit_line|
hash, stamp, date, author_email = commit_line.split('|').map(&:strip) hash, stamp, date, author_email = commit_line.split('|').map(&:strip)
author = authors[author_email] author = authors[author_email]
date = DateTime.parse(date) date = DateTime.parse(date)
commits[hash] = Commit.new(repo: self, hash: hash, stamp: stamp, date: date, author: author) Commit.new(repo: self, hash: hash, stamp: stamp, date: date, author: author)
commits end.sort_by! { |e| e.date }
end end
def short_stats
@short_stats ||= commits.map(&:short_stat)
end end
def activity def activity

View File

@ -11,7 +11,7 @@ module GitStats
def calculate_stat def calculate_stat
stat_line = Command.new(commit.repo, "git show --shortstat --oneline #{commit.hash}").run.lines.to_a[1] stat_line = Command.new(commit.repo, "git show --shortstat --oneline #{commit.hash}").run.lines.to_a[1]
if stat_line.blank? if stat_line.blank?
@files_changed, @insertions, @deletions = 0 @files_changed = @insertions = @deletions = 0
else else
@files_changed = stat_line[/(\d+) files? changed/, 1].to_i @files_changed = stat_line[/(\d+) files? changed/, 1].to_i
@insertions = stat_line[/(\d+) insertions?/, 1].to_i @insertions = stat_line[/(\d+) insertions?/, 1].to_i

View File

@ -4,10 +4,10 @@ describe GitStats::GitData::Author do
let(:repo) { GitStats::GitData::Repo.new(path: "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(: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(:other_author) { GitStats::GitData::Author.new(repo: repo, name: "author2", email: "author2@gmail.com") }
let(:my_commits) { Hash[10.times.map { |i| ["my #{i}", double("my_commit #{i}", :author => author)] }] } let(:my_commits) { 10.times.map { |i| double("my_commit #{i}", :author => author) } }
let(:other_commits) { Hash[10.times.map { |i| ["other #{i}", double("other_commit #{i}", :author => other_author)] }] } let(:other_commits) { 10.times.map { |i| double("other_commit #{i}", :author => other_author) } }
before { repo.stub(:commits => my_commits.merge(other_commits)) } before { repo.stub(:commits => my_commits + other_commits) }
it 'commits should give repo commits filtered to this author' do it 'commits should give repo commits filtered to this author' do
author.commits.should == my_commits author.commits.should == my_commits

View File

@ -19,7 +19,7 @@ describe GitStats::GitData::Repo do
} }
end end
it 'should parse git revlist output to commits hash' do it 'should parse git revlist output to date sorted commits array' do
GitStats::GitData::Command.should_receive(:new).with( GitStats::GitData::Command.should_receive(:new).with(
repo, 'git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').and_return( repo, 'git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').and_return(
double(:run => "e4412c3|1348603824|2012-09-25 22:10:24 +0200|john.doe@gmail.com double(:run => "e4412c3|1348603824|2012-09-25 22:10:24 +0200|john.doe@gmail.com
@ -27,17 +27,17 @@ ce34874|1347482927|2012-09-12 22:48:47 +0200|joe.doe@gmail.com
5eab339|1345835073|2012-08-24 21:04:33 +0200|john.doe@gmail.com 5eab339|1345835073|2012-08-24 21:04:33 +0200|john.doe@gmail.com
")) "))
repo.commits.should == { repo.commits.should == [
"e4412c3" => GitStats::GitData::Commit.new( GitStats::GitData::Commit.new(
repo: repo, hash: "e4412c3", stamp: "1348603824", date: DateTime.parse("2012-09-25 22:10:24 +0200"), repo: repo, hash: "5eab339", stamp: "1345835073", date: DateTime.parse("2012-08-24 21:04:33 +0200"),
author: repo.authors["john.doe@gmail.com"]), author: repo.authors["john.doe@gmail.com"]),
"ce34874" => GitStats::GitData::Commit.new( GitStats::GitData::Commit.new(
repo: repo, hash: "ce34874", stamp: "1347482927", date: DateTime.parse("2012-09-12 22:48:47 +0200"), repo: repo, hash: "ce34874", stamp: "1347482927", date: DateTime.parse("2012-09-12 22:48:47 +0200"),
author: repo.authors["joe.doe@gmail.com"]), author: repo.authors["joe.doe@gmail.com"]),
"5eab339" => GitStats::GitData::Commit.new( GitStats::GitData::Commit.new(
repo: repo, hash: "5eab339", stamp: "1345835073", date: DateTime.parse("2012-08-24 21:04:33 +0200"), repo: repo, hash: "e4412c3", stamp: "1348603824", date: DateTime.parse("2012-09-25 22:10:24 +0200"),
author: repo.authors["john.doe@gmail.com"]) author: repo.authors["john.doe@gmail.com"])
} ]
end end
end end
end end

View File

@ -1,2 +1,28 @@
%p= repo.project_name %table{:class => "table table-hover table-bordered"}
%p= repo.project_version %tr
%td Project name
%td= repo.project_name
%tr
%td Project version
%td= repo.project_version
%tr
%td Generated at
%td= DateTime.now.to_formatted_s(:long)
%tr
%td Generator
%td= "GitStats #{GitStats::VERSION}"
%tr
%td Report period
%td= repo.commits.map(&:date).minmax.map {|d| d.to_formatted_s(:long)}.join(" .. ")
%tr
%td Total files
%td= repo.commits.last.files_count
%tr
%td Total lines
%td= "#{repo.short_stats.map(&:insertions).sum} insertions, #{repo.short_stats.map(&:deletions).sum} deletions"
%tr
%td Total commits
%td= repo.commits.size
%tr
%td Authors
%td= repo.authors.size