mirror of
https://github.com/tomgi/git_stats.git
synced 2025-01-03 11:12:11 +01:00
command parser for shortlog
This commit is contained in:
parent
0cb7b52591
commit
67af71960f
3 changed files with 27 additions and 5 deletions
|
@ -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
|
||||
|
|
17
lib/git_stats/git_data/command_parser.rb
Normal file
17
lib/git_stats/git_data/command_parser.rb
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue