зеркало из https://github.com/github/grit.git
added commit_stats stuff
This commit is contained in:
Родитель
f9dafa9f76
Коммит
e9b9a0f7e7
|
@ -0,0 +1,104 @@
|
|||
module Grit
|
||||
|
||||
class CommitStats
|
||||
|
||||
attr_reader :id, :files, :additions, :deletions, :total
|
||||
|
||||
# Instantiate a new CommitStats
|
||||
# +id+ is the id of the commit
|
||||
# +files+ is an array of :
|
||||
# [ [filename, adds, deletes, total],
|
||||
# [filename, adds, deletes, total],
|
||||
# [filename, adds, deletes, total] ]
|
||||
#
|
||||
# Returns Grit::CommitStats (baked)
|
||||
def initialize(repo, id, files)
|
||||
@repo = repo
|
||||
@id = id
|
||||
@files = files
|
||||
@additions = files.inject(0) { |total, a| total += a[1] }
|
||||
@deletions = files.inject(0) { |total, a| total += a[2] }
|
||||
@total = files.inject(0) { |total, a| total += a[3] }
|
||||
end
|
||||
|
||||
# Find all commit stats matching the given criteria.
|
||||
# +repo+ is the Repo
|
||||
# +ref+ is the ref from which to begin (SHA1 or name) or nil for --all
|
||||
# +options+ is a Hash of optional arguments to git
|
||||
# :max_count is the maximum number of commits to fetch
|
||||
# :skip is the number of commits to skip
|
||||
#
|
||||
# Returns assoc array [sha, Grit::Commit[] (baked)]
|
||||
def self.find_all(repo, ref, options = {})
|
||||
allowed_options = [:max_count, :skip, :since]
|
||||
|
||||
default_options = {:numstat => true}
|
||||
actual_options = default_options.merge(options)
|
||||
|
||||
if ref
|
||||
output = repo.git.log(actual_options, ref)
|
||||
else
|
||||
output = repo.git.log(actual_options.merge(:all => true))
|
||||
end
|
||||
|
||||
self.list_from_string(repo, output)
|
||||
end
|
||||
|
||||
# Parse out commit information into an array of baked Commit objects
|
||||
# +repo+ is the Repo
|
||||
# +text+ is the text output from the git command (raw format)
|
||||
#
|
||||
# Returns assoc array [sha, Grit::Commit[] (baked)]
|
||||
def self.list_from_string(repo, text)
|
||||
lines = text.split("\n")
|
||||
|
||||
commits = []
|
||||
|
||||
while !lines.empty?
|
||||
id = lines.shift.split.last
|
||||
|
||||
lines.shift
|
||||
lines.shift
|
||||
lines.shift
|
||||
|
||||
message_lines = []
|
||||
message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/
|
||||
|
||||
lines.shift while lines.first && lines.first.empty?
|
||||
|
||||
files = []
|
||||
while lines.first =~ /^(\d+)\s+(\d+)\s+(.+)/
|
||||
(additions, deletions, filename) = lines.shift.split
|
||||
additions = additions.to_i
|
||||
deletions = deletions.to_i
|
||||
total = additions + deletions
|
||||
files << [filename, additions, deletions, total]
|
||||
end
|
||||
|
||||
lines.shift while lines.first && lines.first.empty?
|
||||
|
||||
commits << [id, CommitStats.new(repo, id, files)]
|
||||
end
|
||||
|
||||
commits
|
||||
end
|
||||
|
||||
# Pretty object inspection
|
||||
def inspect
|
||||
%Q{#<Grit::CommitStats "#{@id}">}
|
||||
end
|
||||
|
||||
# private
|
||||
|
||||
def to_hash
|
||||
{
|
||||
'id' => id,
|
||||
'files' => files,
|
||||
'additions' => additions,
|
||||
'deletions' => deletions,
|
||||
'total' => total
|
||||
}
|
||||
end
|
||||
end # CommitStats
|
||||
|
||||
end # Grit
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
# These classes translate the raw binary data kept in the sha encoded files
|
||||
# into parsed data that can then be used in another fashion
|
||||
require 'stringio'
|
||||
|
||||
module Grit
|
||||
module GitRuby
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
class TestCommitStats < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
File.expects(:exist?).returns(true)
|
||||
@r = Repo.new(GRIT_REPO)
|
||||
|
||||
Git.any_instance.expects(:log).returns(fixture('log'))
|
||||
@stats = @r.commit_stats
|
||||
end
|
||||
|
||||
def test_commit_stats
|
||||
assert_equal 3, @stats.size
|
||||
end
|
||||
|
||||
# to_hash
|
||||
|
||||
def test_to_hash
|
||||
expected = {
|
||||
"files"=>
|
||||
[["examples/ex_add_commit.rb", 13, 0, 13],
|
||||
["examples/ex_index.rb", 1, 1, 2]],
|
||||
"total"=>15,
|
||||
"additions"=>14,
|
||||
"id"=>"a49b96b339c525d7fd455e0ad4f6fe7b550c9543",
|
||||
"deletions"=>1
|
||||
}
|
||||
|
||||
assert_equal expected, @stats.assoc('a49b96b339c525d7fd455e0ad4f6fe7b550c9543')[1].to_hash
|
||||
end
|
||||
|
||||
end
|
Загрузка…
Ссылка в новой задаче