From 67af71960fdeb51eb3db322c006d19e3eb5fd30f Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Tue, 16 Oct 2012 21:44:33 +0200 Subject: [PATCH] command parser for shortlog --- lib/git_stats/git_data/command.rb | 7 ++++++- lib/git_stats/git_data/command_parser.rb | 17 +++++++++++++++++ lib/git_stats/git_data/repo.rb | 8 ++++---- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 lib/git_stats/git_data/command_parser.rb diff --git a/lib/git_stats/git_data/command.rb b/lib/git_stats/git_data/command.rb index f98d9c1e8..02c0140c4 100644 --- a/lib/git_stats/git_data/command.rb +++ b/lib/git_stats/git_data/command.rb @@ -3,9 +3,10 @@ module GitStats class Command attr_reader :repo, :command - def initialize(repo, command) + def initialize(repo, command, command_parser = CommandParser.new) @repo = repo @command = command + @command_parser = command_parser end def run @@ -14,6 +15,10 @@ module GitStats result end + def run_and_parse + @command_parser.parse(command, run) + end + def run_in_repo Dir.chdir(@repo.path) { yield } end diff --git a/lib/git_stats/git_data/command_parser.rb b/lib/git_stats/git_data/command_parser.rb new file mode 100644 index 000000000..72488ca21 --- /dev/null +++ b/lib/git_stats/git_data/command_parser.rb @@ -0,0 +1,17 @@ +module GitStats + module GitData + class CommandParser + def parse(command, result) + cmd, params = command.scan(/git (.*) (.*)/).first.map(&:split).flatten + send("parse_#{cmd}", 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 + end + end +end \ No newline at end of file diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index 27d72e176..db01e9467 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -13,10 +13,10 @@ module GitStats end def authors - @authors ||= Hash[Command.new(self, 'git shortlog -se HEAD').run.lines.map do |line| - name, email = line.split(/\t/)[1].strip.scan(/(.*)<(.*)>/).first.map(&:strip) - [email, Author.new(repo: self, name: name, email: email)] - end] + @authors ||= Command.new(self, 'git shortlog -se HEAD').run_and_parse.inject({}) do |hash, author| + hash[author[:email]] = Author.new(repo: self, name: author[:name], email: author[:email]) + hash + end end def commits