mirror of
https://github.com/tomgi/git_stats.git
synced 2024-11-10 21:27:05 +01:00
command runner refactored
This commit is contained in:
parent
01eb78a25e
commit
29681665cb
@ -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
|
|
9
lib/git_stats/git_data/command_runner.rb
Normal file
9
lib/git_stats/git_data/command_runner.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module GitStats
|
||||||
|
module GitData
|
||||||
|
class CommandRunner
|
||||||
|
def self.run(command)
|
||||||
|
%x[#{command}]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -8,7 +8,7 @@ module GitStats
|
|||||||
attr_reader :repo, :hash, :stamp, :date, :author
|
attr_reader :repo, :hash, :stamp, :date, :author
|
||||||
|
|
||||||
def files
|
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])
|
Blob.new(repo: repo, filename: file[:filename], hash: file[:hash])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -22,11 +22,11 @@ module GitStats
|
|||||||
end
|
end
|
||||||
|
|
||||||
def files_count
|
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
|
end
|
||||||
|
|
||||||
def lines_count
|
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
|
line[/(\d+) insertions?/, 1].to_i
|
||||||
end.sum
|
end.sum
|
||||||
end
|
end
|
||||||
|
@ -13,13 +13,13 @@ module GitStats
|
|||||||
end
|
end
|
||||||
|
|
||||||
def authors
|
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])
|
Author.new(repo: self, name: author[:name], email: author[:email])
|
||||||
end.extend(ByFieldFinder)
|
end.extend(ByFieldFinder)
|
||||||
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.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)
|
hash, stamp, date, author_email = commit_line.split('|').map(&:strip)
|
||||||
author = authors.by_email(author_email)
|
author = authors.by_email(author_email)
|
||||||
|
|
||||||
@ -45,13 +45,30 @@ module GitStats
|
|||||||
end
|
end
|
||||||
|
|
||||||
def project_version
|
def project_version
|
||||||
@project_version ||= Command.new(self, 'git rev-parse --short HEAD').run
|
@project_version ||= run('git rev-parse --short HEAD')
|
||||||
end
|
end
|
||||||
|
|
||||||
def project_name
|
def project_name
|
||||||
@project_name ||= File.basename(path)
|
@project_name ||= File.basename(path)
|
||||||
end
|
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
|
def to_s
|
||||||
"#{self.class} #@path"
|
"#{self.class} #@path"
|
||||||
end
|
end
|
||||||
|
@ -14,7 +14,7 @@ module GitStats
|
|||||||
|
|
||||||
private
|
private
|
||||||
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 = commit.repo.run("git show --shortstat --oneline #{commit.hash}").lines.to_a[1]
|
||||||
if stat_line.blank?
|
if stat_line.blank?
|
||||||
@files_changed = @insertions = @deletions = 0
|
@files_changed = @insertions = @deletions = 0
|
||||||
else
|
else
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe GitStats::GitData::Commit do
|
describe GitStats::GitData::Commit do
|
||||||
let(:commit) { build(:commit) }
|
let(:commit) { build(:commit, hash: 'abc') }
|
||||||
|
|
||||||
describe 'git output parsing' do
|
describe 'git output parsing' do
|
||||||
context 'parsing git ls-tree output' do
|
context 'parsing git ls-tree output' do
|
||||||
before {
|
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 db01e94677a8f72289848e507a52a43de2ea109a lib/git_stats/git_data/repo.rb
|
||||||
100644 blob 1463eacb3ac9f95f21f360f1eb935a84a9ee0895 templates/activity.haml
|
100644 blob 1463eacb3ac9f95f21f360f1eb935a84a9ee0895 templates/activity.haml
|
||||||
100644 blob 31d8b960a67f195bdedaaf9e7aa70b2389f3f1a8 templates/assets/bootstrap/css/bootstrap.min.css
|
100644 blob 31d8b960a67f195bdedaaf9e7aa70b2389f3f1a8 templates/assets/bootstrap/css/bootstrap.min.css
|
||||||
|
@ -29,22 +29,23 @@ describe GitStats::GitData::Repo do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe 'git output parsing' do
|
describe 'git output parsing' do
|
||||||
it 'should parse git shortlog output to authors hash' do
|
before do
|
||||||
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return(" 156 John Doe <john.doe@gmail.com>
|
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>
|
53 Joe Doe <joe.doe@gmail.com>
|
||||||
")
|
")
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should parse git shortlog output to authors hash' do
|
||||||
repo.authors.should == expected_authors
|
repo.authors.should == expected_authors
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should parse git revlist output to date sorted commits array' do
|
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
|
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.stub(authors: expected_authors)
|
|
||||||
|
|
||||||
repo.commits.should == [
|
repo.commits.should == [
|
||||||
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: "5eab339", stamp: "1345835073", date: DateTime.parse("2012-08-24 21:04:33 +0200"),
|
||||||
|
@ -13,9 +13,7 @@ describe GitStats::GitData::ShortStat do
|
|||||||
{content: '', expect: [0, 0, 0]},
|
{content: '', expect: [0, 0, 0]},
|
||||||
].each do |test|
|
].each do |test|
|
||||||
it "#{test[:content]} parsing" do
|
it "#{test[:content]} parsing" do
|
||||||
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return("abc some commit
|
commit.repo.should_receive(:run).with("git show --shortstat --oneline abc").and_return("abc some commit\n#{test[:content]}")
|
||||||
#{test[:content]}
|
|
||||||
")
|
|
||||||
|
|
||||||
|
|
||||||
commit.short_stat.should be_a(GitStats::GitData::ShortStat)
|
commit.short_stat.should be_a(GitStats::GitData::ShortStat)
|
||||||
|
Loading…
Reference in New Issue
Block a user