From 4dc51655059ee2f5ad2186ad5cb249dcbecaf477 Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Fri, 19 Oct 2012 20:02:47 +0200 Subject: [PATCH] invoking repo git command with correct commits range --- lib/git_stats/git_data/repo.rb | 8 ++--- spec/repo_spec.rb | 66 +++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index b8a62487e..e8e3a8562 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -15,13 +15,13 @@ module GitStats end def authors - @authors ||= run_and_parse('git shortlog -se HEAD').map do |author| + @authors ||= run_and_parse("git shortlog -se #{commit_range}").map do |author| Author.new(repo: self, name: author[:name], email: author[:email]) end.extend(ByFieldFinder) end def commits - @commits ||= run('git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').lines.map do |commit_line| + @commits ||= run("git rev-list --pretty=format:'%h|%at|%ai|%aE' #{commit_range} | grep -v commit").lines.map do |commit_line| hash, stamp, date, author_email = commit_line.split('|').map(&:strip) author = authors.by_email(author_email) @@ -35,7 +35,7 @@ module GitStats end def commit_range - @first_commit_hash ? "#{@first_commit_hash}..#{last_commit_hash}" : last_commit_hash + @first_commit_hash ? "#@first_commit_hash..#{last_commit_hash}" : last_commit_hash end def last_commit_hash @@ -51,7 +51,7 @@ module GitStats end def project_version - @project_version ||= run('git rev-parse --short HEAD').strip + @project_version ||= run("git rev-parse --short #{commit_range}").strip end def project_name diff --git a/spec/repo_spec.rb b/spec/repo_spec.rb index fddcfce0e..2dc1903f5 100644 --- a/spec/repo_spec.rb +++ b/spec/repo_spec.rb @@ -26,37 +26,61 @@ describe GitStats::GitData::Repo do repo = build(:repo, first_commit_hash: 'abc', last_commit_hash: 'def') repo.commit_range.should == 'abc..def' end + + context 'git commands using range' do + let(:repo) { build(:repo, first_commit_hash: 'abc', last_commit_hash: 'def') } + + it 'should affect authors command' do + repo.should_receive(:run).with('git shortlog -se abc..def').and_return("") + repo.authors + end + + it 'should affect commits command' do + repo.should_receive(:run).with("git rev-list --pretty=format:'%h|%at|%ai|%aE' abc..def | grep -v commit").and_return("") + repo.commits + end + + it 'should affect project version command' do + repo.should_receive(:run).with('git rev-parse --short abc..def').and_return("") + repo.project_version + end + end end describe 'git output parsing' do - before do - repo.should_receive(:run).with('git shortlog -se HEAD').and_return(" 156 John Doe + context 'invoking authors command' do + before do + repo.should_receive(:run).with('git shortlog -se HEAD').and_return(" 156 John Doe 53 Joe Doe ") - end + end + it 'should parse git shortlog output to authors hash' do + repo.authors.should == expected_authors + end - it 'should parse git shortlog output to authors hash' do - repo.authors.should == expected_authors - end - - it 'should parse git revlist output to date sorted commits array' do - repo.should_receive(:run).with('git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').and_return( - "e4412c3|1348603824|2012-09-25 22:10:24 +0200|john.doe@gmail.com + it 'should parse git revlist output to date sorted commits array' do + repo.should_receive(:run).with("git rev-list --pretty=format:'%h|%at|%ai|%aE' HEAD | grep -v commit").and_return( + "e4412c3|1348603824|2012-09-25 22:10:24 +0200|john.doe@gmail.com 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 ") - repo.commits.should == [ - GitStats::GitData::Commit.new( - repo: repo, hash: "5eab339", stamp: "1345835073", date: DateTime.parse("2012-08-24 21:04:33 +0200"), - author: repo.authors.by_email("john.doe@gmail.com")), - GitStats::GitData::Commit.new( - repo: repo, hash: "ce34874", stamp: "1347482927", date: DateTime.parse("2012-09-12 22:48:47 +0200"), - author: repo.authors.by_email("joe.doe@gmail.com")), - GitStats::GitData::Commit.new( - repo: repo, hash: "e4412c3", stamp: "1348603824", date: DateTime.parse("2012-09-25 22:10:24 +0200"), - author: repo.authors.by_email("john.doe@gmail.com")) - ] + repo.commits.should == [ + GitStats::GitData::Commit.new( + repo: repo, hash: "5eab339", stamp: "1345835073", date: DateTime.parse("2012-08-24 21:04:33 +0200"), + author: repo.authors.by_email("john.doe@gmail.com")), + GitStats::GitData::Commit.new( + repo: repo, hash: "ce34874", stamp: "1347482927", date: DateTime.parse("2012-09-12 22:48:47 +0200"), + author: repo.authors.by_email("joe.doe@gmail.com")), + GitStats::GitData::Commit.new( + repo: repo, hash: "e4412c3", stamp: "1348603824", date: DateTime.parse("2012-09-25 22:10:24 +0200"), + author: repo.authors.by_email("john.doe@gmail.com")) + ] + end + end + it 'should parse git rev-parse command to project version' do + repo.should_receive(:run).with('git rev-parse --short HEAD').and_return('xyz') + repo.project_version.should == 'xyz' end end end \ No newline at end of file