command parser for shortlog

This commit is contained in:
Tomasz Gieniusz 2012-10-16 21:44:33 +02:00
parent 0cb7b52591
commit 67af71960f
3 changed files with 27 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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