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
|
|
|
|
|
|
|
|
attr_reader :path, :git_command_observer
|
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
|
2012-10-19 16:57:49 +02:00
|
|
|
@authors ||= run_and_parse('git shortlog -se HEAD').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 16:57:49 +02:00
|
|
|
@commits ||= run('git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').lines.map do |commit_line|
|
2012-10-09 22:34:02 +02:00
|
|
|
hash, stamp, date, author_email = commit_line.split('|').map(&:strip)
|
2012-10-19 16:39:39 +02:00
|
|
|
author = authors.by_email(author_email)
|
2012-10-09 22:34:02 +02:00
|
|
|
|
|
|
|
date = DateTime.parse(date)
|
2012-10-13 14:31:52 +02:00
|
|
|
Commit.new(repo: self, hash: hash, stamp: stamp, date: date, author: author)
|
|
|
|
end.sort_by! { |e| e.date }
|
|
|
|
end
|
|
|
|
|
2012-10-19 15:51:24 +02:00
|
|
|
def commit_range
|
|
|
|
@first_commit ? "#{@first_commit}..#{last_commit}" : last_commit
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit
|
|
|
|
@last_commit ||= 'HEAD'
|
|
|
|
end
|
|
|
|
|
2012-10-13 14:31:52 +02:00
|
|
|
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
|
2012-10-19 16:57:49 +02:00
|
|
|
@project_version ||= run('git rev-parse --short HEAD')
|
2012-10-09 22:34:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_name
|
2012-10-13 15:32:06 +02:00
|
|
|
@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)
|
|
|
|
in_repo_dir { CommandRunner.run(command) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_and_parse(command)
|
|
|
|
result = run(command)
|
|
|
|
command_parser.parse(command, result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def command_parser
|
|
|
|
@command_parser ||= CommandParser.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_repo_dir
|
|
|
|
Dir.chdir(path) { yield }
|
|
|
|
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-09 22:34:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|