command runner refactored

This commit is contained in:
Tomasz Gieniusz 2012-10-19 16:57:49 +02:00
parent 01eb78a25e
commit 29681665cb
8 changed files with 42 additions and 48 deletions

View File

@ -1,31 +0,0 @@
module GitStats
module GitData
class Command
attr_reader :repo, :command
def initialize(repo, command, command_parser = CommandParser.new)
@repo = repo
@command = command
@command_parser = command_parser
end
def run
result = run_in_repo { %x[#@command] }
repo.git_command_observer.try(:call, @command, result)
result
end
def run_and_parse
@command_parser.parse(command, run)
end
def run_in_repo
Dir.chdir(@repo.path) { yield }
end
def to_s
"#{self.class} #@command"
end
end
end
end

View File

@ -0,0 +1,9 @@
module GitStats
module GitData
class CommandRunner
def self.run(command)
%x[#{command}]
end
end
end
end

View File

@ -8,7 +8,7 @@ module GitStats
attr_reader :repo, :hash, :stamp, :date, :author
def files
@files ||= Command.new(repo, "git ls-tree -r #{self.hash}").run_and_parse.map do |file|
@files ||= repo.run_and_parse("git ls-tree -r #{self.hash}").map do |file|
Blob.new(repo: repo, filename: file[:filename], hash: file[:hash])
end
end
@ -22,11 +22,11 @@ module GitStats
end
def files_count
@files_count ||= Command.new(repo, "git ls-tree -r --name-only #{self.hash} | wc -l").run.to_i
@files_count ||= repo.run("git ls-tree -r --name-only #{self.hash} | wc -l").to_i
end
def lines_count
@lines_count ||= Command.new(repo, "git diff --shortstat `git hash-object -t tree /dev/null` #{self.hash}").run.lines.map do |line|
@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
end.sum
end

View File

@ -13,13 +13,13 @@ module GitStats
end
def authors
@authors ||= Command.new(self, 'git shortlog -se HEAD').run_and_parse.map do |author|
@authors ||= run_and_parse('git shortlog -se HEAD').map do |author|
Author.new(repo: self, name: author[:name], email: author[:email])
end.extend(ByFieldFinder)
end
def commits
@commits ||= Command.new(self, 'git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').run.lines.map do |commit_line|
@commits ||= run('git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').lines.map do |commit_line|
hash, stamp, date, author_email = commit_line.split('|').map(&:strip)
author = authors.by_email(author_email)
@ -45,13 +45,30 @@ module GitStats
end
def project_version
@project_version ||= Command.new(self, 'git rev-parse --short HEAD').run
@project_version ||= run('git rev-parse --short HEAD')
end
def project_name
@project_name ||= File.basename(path)
end
def run(command)
in_repo_dir { CommandRunner.run(command) }
end
def run_and_parse(command)
result = run(command)
command_parser.parse(command, result)
end
def command_parser
@command_parser ||= CommandParser.new
end
def in_repo_dir
Dir.chdir(path) { yield }
end
def to_s
"#{self.class} #@path"
end

View File

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

View File

@ -1,12 +1,12 @@
require 'spec_helper'
describe GitStats::GitData::Commit do
let(:commit) { build(:commit) }
let(:commit) { build(:commit, hash: 'abc') }
describe 'git output parsing' do
context 'parsing git ls-tree output' do
before {
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return("100644 blob 5ade7ad51a75ee7db4eb06cecd3918d38134087d lib/git_stats/git_data/commit.rb
commit.repo.should_receive(:run).with('git ls-tree -r abc').and_return("100644 blob 5ade7ad51a75ee7db4eb06cecd3918d38134087d lib/git_stats/git_data/commit.rb
100644 blob db01e94677a8f72289848e507a52a43de2ea109a lib/git_stats/git_data/repo.rb
100644 blob 1463eacb3ac9f95f21f360f1eb935a84a9ee0895 templates/activity.haml
100644 blob 31d8b960a67f195bdedaaf9e7aa70b2389f3f1a8 templates/assets/bootstrap/css/bootstrap.min.css

View File

@ -29,22 +29,23 @@ describe GitStats::GitData::Repo do
end
describe 'git output parsing' do
it 'should parse git shortlog output to authors hash' do
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return(" 156 John Doe <john.doe@gmail.com>
before do
repo.should_receive(:run).with('git shortlog -se HEAD').and_return(" 156 John Doe <john.doe@gmail.com>
53 Joe Doe <joe.doe@gmail.com>
")
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
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return("e4412c3|1348603824|2012-09-25 22:10:24 +0200|john.doe@gmail.com
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.stub(authors: expected_authors)
repo.commits.should == [
GitStats::GitData::Commit.new(
repo: repo, hash: "5eab339", stamp: "1345835073", date: DateTime.parse("2012-08-24 21:04:33 +0200"),

View File

@ -13,9 +13,7 @@ describe GitStats::GitData::ShortStat do
{content: '', expect: [0, 0, 0]},
].each do |test|
it "#{test[:content]} parsing" do
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return("abc some commit
#{test[:content]}
")
commit.repo.should_receive(:run).with("git show --shortstat --oneline abc").and_return("abc some commit\n#{test[:content]}")
commit.short_stat.should be_a(GitStats::GitData::ShortStat)