git_stats/lib/git_stats/git_data/repo.rb

106 lines
2.6 KiB
Ruby
Raw Normal View History

2012-10-13 17:20:06 +02:00
require 'git_stats/hash_initializable'
2012-10-09 22:34:02 +02:00
module GitStats
module GitData
class Repo
2012-10-13 17:20:06 +02:00
include HashInitializable
2012-10-19 17:35:33 +02:00
attr_reader :path
delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension, :files_count, :lines_count, to: :last_commit
2012-10-09 22:34:02 +02:00
2012-10-13 17:20:06 +02:00
def initialize(params)
super(params)
@path = File.expand_path(@path)
2012-10-09 22:34:02 +02:00
end
2012-10-12 18:20:07 +02:00
def authors
@authors ||= run_and_parse("git shortlog -se #{commit_range}").map do |author|
2012-10-19 16:39:39 +02:00
Author.new(repo: self, name: author[:name], email: author[:email])
end.extend(ByFieldFinder)
2012-10-09 22:34:02 +02:00
end
2012-10-12 18:20:07 +02:00
def commits
2012-10-19 21:17:16 +02:00
@commits ||= run_and_parse("git rev-list --pretty=format:'%h|%at|%ai|%aE' #{commit_range} | grep -v commit").map do |commit_line|
Commit.new(
repo: self,
hash: commit_line[:hash],
stamp: commit_line[:stamp],
date: DateTime.parse(commit_line[:date]),
author: authors.by_email(commit_line[:author_email])
)
2012-10-19 20:27:41 +02:00
end.sort_by! { |e| e.date }.extend(ByFieldFinder)
end
2012-10-19 17:35:33 +02:00
def last_commit
commits.last
end
2012-10-19 15:51:24 +02:00
def commit_range
@first_commit_hash ? "#@first_commit_hash..#{last_commit_hash}" : last_commit_hash
2012-10-19 15:51:24 +02:00
end
2012-10-19 17:35:33 +02:00
def last_commit_hash
@last_commit_hash ||= 'HEAD'
2012-10-19 15:51:24 +02:00
end
def short_stats
@short_stats ||= commits.map(&:short_stat)
2012-10-09 22:34:02 +02:00
end
def activity
2012-10-12 18:24:28 +02:00
@activity ||= Activity.new(commits)
2012-10-09 22:34:02 +02:00
end
def project_version
@project_version ||= run("git rev-parse --short #{commit_range}").strip
2012-10-09 22:34:02 +02:00
end
def project_name
@project_name ||= File.basename(path)
2012-10-09 22:34:02 +02:00
end
2012-10-12 18:20:07 +02:00
2012-10-19 16:57:49 +02:00
def run(command)
2012-10-19 21:11:47 +02:00
result = command_runner.run(path, command)
invoke_command_observers(command, result)
result
2012-10-19 16:57:49 +02:00
end
def run_and_parse(command)
result = run(command)
command_parser.parse(command, result)
end
2012-10-19 21:11:47 +02:00
def command_runner
@command_runner ||= CommandRunner.new
end
2012-10-19 16:57:49 +02:00
def command_parser
@command_parser ||= CommandParser.new
end
2012-10-19 21:11:47 +02:00
def add_command_observer(proc=nil, &block)
command_observers << block if block_given?
command_observers << proc if proc
2012-10-19 16:57:49 +02:00
end
2012-10-13 12:38:07 +02:00
def to_s
"#{self.class} #@path"
end
2012-10-13 13:27:15 +02:00
def ==(other)
self.path == other.path
end
2012-10-19 21:11:47 +02:00
private
def command_observers
@command_observers ||= []
end
def invoke_command_observers(command, result)
command_observers.each { |o| o.call(command, result) }
end
2012-10-09 22:34:02 +02:00
end
end
end