diff --git a/lib/git_stats/base.rb b/lib/git_stats/base.rb index 86b52a686..e82b5b0e4 100644 --- a/lib/git_stats/base.rb +++ b/lib/git_stats/base.rb @@ -8,13 +8,12 @@ require 'git_stats/version' require 'git_stats/i18n' require 'git_stats/cli' require 'git_stats/generator' -require 'git_stats/validator' +require 'git_stats/command_runner' +require 'git_stats/command_parser' require 'git_stats/git_data/activity' require 'git_stats/git_data/author' require 'git_stats/git_data/blob' -require 'git_stats/git_data/command_parser' -require 'git_stats/git_data/command_runner' require 'git_stats/git_data/commit' require 'git_stats/git_data/repo' require 'git_stats/git_data/short_stat' diff --git a/lib/git_stats/command_parser.rb b/lib/git_stats/command_parser.rb new file mode 100644 index 000000000..e0a397966 --- /dev/null +++ b/lib/git_stats/command_parser.rb @@ -0,0 +1,31 @@ +# -*- encoding : utf-8 -*- +module GitStats + class CommandParser + def parse(command, result) + cmd, params = command.scan(/git (.*) (.*)/).first.map(&:split).flatten + send("parse_#{cmd.underscore}", result, params) + end + + def parse_shortlog(result, params) + result.lines.map do |line| + commits, name, email = line.scan(/(.*)\t(.*)<(.*)>/).first.map(&:strip) + {commits: commits.to_i, name: name, email: email} + end + end + + def parse_ls_tree(result, params) + result.lines.map do |line| + mode, type, sha, filename = line.scan(/(.*) (.*) (.*)\t(.*)/).first.map(&:strip) + {mode: mode, type: type, sha: sha, filename: filename} + end + end + + def parse_rev_list(result, params) + result.lines.map do |line| + sha, stamp, date, author_email = line.split('|').map(&:strip) + {sha: sha, stamp: stamp, date: date, author_email: author_email} + end + end + + end +end diff --git a/lib/git_stats/command_runner.rb b/lib/git_stats/command_runner.rb new file mode 100644 index 000000000..dbf2facce --- /dev/null +++ b/lib/git_stats/command_runner.rb @@ -0,0 +1,13 @@ +# -*- encoding : utf-8 -*- +module GitStats + class CommandRunner + def run(path, command) + execute(command, path).encode!('UTF-8', 'UTF-8', :invalid => :replace) + end + + private + def execute(command, path) + Dir.chdir(path) { %x[#{command}] } + end + end +end diff --git a/lib/git_stats/generator.rb b/lib/git_stats/generator.rb index c146939f7..8cee2ab0e 100644 --- a/lib/git_stats/generator.rb +++ b/lib/git_stats/generator.rb @@ -18,7 +18,12 @@ module GitStats def validate_repo_path(repo_path) - raise ArgumentError, "#{repo_path} is not a git repository" unless Validator.new.valid_repo_path?(repo_path) + raise ArgumentError, "#{repo_path} is not a git repository" unless valid_repo_path?(repo_path) + end + + + def valid_repo_path?(repo_path) + Dir.exists?("#{repo_path}/.git") || File.exists?("#{repo_path}/.git") || File.exists?("#{repo_path}/HEAD") end end diff --git a/lib/git_stats/git_data/command_parser.rb b/lib/git_stats/git_data/command_parser.rb deleted file mode 100644 index 756fd6a0e..000000000 --- a/lib/git_stats/git_data/command_parser.rb +++ /dev/null @@ -1,33 +0,0 @@ -# -*- encoding : utf-8 -*- -module GitStats - module GitData - class CommandParser - def parse(command, result) - cmd, params = command.scan(/git (.*) (.*)/).first.map(&:split).flatten - send("parse_#{cmd.underscore}", result, params) - end - - def parse_shortlog(result, params) - result.lines.map do |line| - commits, name, email = line.scan(/(.*)\t(.*)<(.*)>/).first.map(&:strip) - {commits: commits.to_i, name: name, email: email} - end - end - - def parse_ls_tree(result, params) - result.lines.map do |line| - mode, type, sha, filename = line.scan(/(.*) (.*) (.*)\t(.*)/).first.map(&:strip) - {mode: mode, type: type, sha: sha, filename: filename} - end - end - - def parse_rev_list(result, params) - result.lines.map do |line| - sha, stamp, date, author_email = line.split('|').map(&:strip) - {sha: sha, stamp: stamp, date: date, author_email: author_email} - end - end - - end - end -end diff --git a/lib/git_stats/git_data/command_runner.rb b/lib/git_stats/git_data/command_runner.rb deleted file mode 100644 index bf3a83685..000000000 --- a/lib/git_stats/git_data/command_runner.rb +++ /dev/null @@ -1,15 +0,0 @@ -# -*- encoding : utf-8 -*- -module GitStats - module GitData - class CommandRunner - def run(path, command) - execute(command, path).encode!('UTF-8', 'UTF-8', :invalid => :replace) - end - - private - def execute(command, path) - Dir.chdir(path) { %x[#{command}] } - end - end - end -end diff --git a/lib/git_stats/validator.rb b/lib/git_stats/validator.rb deleted file mode 100644 index a02333d7b..000000000 --- a/lib/git_stats/validator.rb +++ /dev/null @@ -1,10 +0,0 @@ -# -*- encoding : utf-8 -*- -module GitStats - class Validator - - def valid_repo_path?(repo_path) - Dir.exists?("#{repo_path}/.git") || File.exists?("#{repo_path}/HEAD") - end - - end -end \ No newline at end of file diff --git a/spec/git_data/generator_spec.rb b/spec/git_data/generator_spec.rb index b2aba9c0c..1d38b0e54 100644 --- a/spec/git_data/generator_spec.rb +++ b/spec/git_data/generator_spec.rb @@ -6,34 +6,7 @@ describe GitStats::Generator do let(:out_path) { 'out_path' } let(:generator) { GitStats::Generator.new(path: repo_path, out_path: out_path) } - before { Dir.stub(:exists? => true) } - it 'should raise exception if given repo path is not a git repository' do - Dir.should_receive(:exists?).with("#{repo_path}/.git").and_return(false) - expect { generator }.to raise_error(ArgumentError) - end - - it 'should pass command observer to repo' do - repo = double('repo') - GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, out_path: out_path).and_return(repo) - - observer = double('observer') - repo.should_receive(:add_command_observer).with(observer) - - generator.add_command_observer observer - end - - it 'should render all templates with view data for this repo' do - repo = double('repo') - GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, out_path: out_path).and_return(repo) - - view_data = double('view_data') - GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data) - - view = double('view') - GitStats::StatsView::View.should_receive(:new).with(view_data, out_path).and_return(view) - view.should_receive(:render_all) - - generator.render_all + expect { generator }.to raise_error end end