diff --git a/lib/git_stats/git_data/command.rb b/lib/git_stats/git_data/command.rb deleted file mode 100644 index 02c0140c4..000000000 --- a/lib/git_stats/git_data/command.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/lib/git_stats/git_data/command_runner.rb b/lib/git_stats/git_data/command_runner.rb new file mode 100644 index 000000000..47f2693ce --- /dev/null +++ b/lib/git_stats/git_data/command_runner.rb @@ -0,0 +1,9 @@ +module GitStats + module GitData + class CommandRunner + def self.run(command) + %x[#{command}] + end + end + end +end \ No newline at end of file diff --git a/lib/git_stats/git_data/commit.rb b/lib/git_stats/git_data/commit.rb index 7c1fb41d0..acb1042b5 100644 --- a/lib/git_stats/git_data/commit.rb +++ b/lib/git_stats/git_data/commit.rb @@ -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 diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index dfaa12682..114395902 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -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 diff --git a/lib/git_stats/git_data/short_stat.rb b/lib/git_stats/git_data/short_stat.rb index 8a4c4cefe..5b5efea9e 100644 --- a/lib/git_stats/git_data/short_stat.rb +++ b/lib/git_stats/git_data/short_stat.rb @@ -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 diff --git a/spec/commit_spec.rb b/spec/commit_spec.rb index 281d1d468..e31937343 100644 --- a/spec/commit_spec.rb +++ b/spec/commit_spec.rb @@ -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 diff --git a/spec/repo_spec.rb b/spec/repo_spec.rb index 26aec9f93..11f863c6d 100644 --- a/spec/repo_spec.rb +++ b/spec/repo_spec.rb @@ -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 + before do + repo.should_receive(:run).with('git shortlog -se HEAD').and_return(" 156 John Doe 53 Joe Doe ") + 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"), diff --git a/spec/short_stat_spec.rb b/spec/short_stat_spec.rb index efb133e9c..1a4917bc5 100644 --- a/spec/short_stat_spec.rb +++ b/spec/short_stat_spec.rb @@ -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)