git_stats/lib/git_stats/git_data/commit.rb

64 lines
1.8 KiB
Ruby
Raw Normal View History

2012-10-23 20:37:42 +02:00
# -*- encoding : utf-8 -*-
2012-10-09 22:34:02 +02:00
require 'git_stats/hash_initializable'
module GitStats
module GitData
class Commit
include HashInitializable
2012-10-20 22:38:11 +02:00
attr_reader :repo, :sha, :stamp, :date, :author
2012-10-09 22:34:02 +02:00
def files
@files ||= repo.run_and_parse("git ls-tree -r #{self.sha} -- #{repo.tree_path}").map do |file|
2012-10-20 22:38:11 +02:00
Blob.new(repo: repo, filename: file[:filename], sha: file[:sha])
2012-10-19 20:27:41 +02:00
end.extend(ByFieldFinder)
end
2012-10-20 22:38:11 +02:00
def binary_files
@binary_files ||= files.select { |f| f.binary? }
end
def text_files
@text_files ||= files - binary_files
end
2012-10-13 19:00:31 +02:00
def files_by_extension
@files_by_extension ||= files.inject({}) { |acc, f| acc[f.extension] ||= []; acc[f.extension] << f; acc }
end
2012-10-19 17:35:33 +02:00
def files_by_extension_count
2012-10-20 00:30:00 +02:00
@files_by_extension_count ||= Hash[files_by_extension.map { |ext, files| [ext, files.count] }]
2012-10-19 17:35:33 +02:00
end
def lines_by_extension
2012-10-20 21:28:25 +02:00
@lines_by_extension ||= Hash[files_by_extension.map { |ext, files|
[ext, files.map(&:lines_count).sum]
}.delete_if { |ext, lines_count| lines_count == 0 }]
2012-10-13 19:00:31 +02:00
end
2012-10-09 22:34:02 +02:00
def files_count
@files_count ||= repo.run("git ls-tree -r --name-only #{self.sha} -- #{repo.tree_path}| wc -l").to_i
2012-10-20 21:28:25 +02:00
end
2012-10-13 16:52:25 +02:00
def lines_count
@lines_count ||= repo.run("git diff --shortstat `git hash-object -t tree /dev/null` #{self.sha} -- #{repo.tree_path}").lines.map do |line|
2012-10-13 17:57:15 +02:00
line[/(\d+) insertions?/, 1].to_i
end.sum
2012-10-13 16:52:25 +02:00
end
2012-10-09 22:34:02 +02:00
def short_stat
@short_stat ||= ShortStat.new(self)
end
2012-10-13 12:38:07 +02:00
def to_s
2012-10-20 22:38:11 +02:00
"#{self.class} #@sha"
2012-10-13 12:38:07 +02:00
end
2012-10-13 13:27:15 +02:00
def ==(other)
2012-10-20 22:38:11 +02:00
[self.repo, self.sha, self.stamp, self.date, self.author] ==
[other.repo, other.sha, other.stamp, other.date, other.author]
2012-10-13 13:27:15 +02:00
end
2012-10-09 22:34:02 +02:00
end
end
2012-10-23 20:37:42 +02:00
end