gitlab-org--gitlab-foss/lib/gitlab/git/commit_stats.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

# Gitaly note: JV: 1 RPC, migration in progress.
2017-01-04 13:43:06 -05:00
# Gitlab::Git::CommitStats counts the additions, deletions, and total changes
# in a commit.
module Gitlab
module Git
class CommitStats
attr_reader :id, :additions, :deletions, :total
# Instantiate a CommitStats object
#
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/323
2017-09-06 06:55:16 -04:00
def initialize(repo, commit)
2017-01-04 13:43:06 -05:00
@id = commit.id
@additions = 0
@deletions = 0
@total = 0
2017-09-06 06:55:16 -04:00
repo.gitaly_migrate(:commit_stats) do |is_enabled|
if is_enabled
gitaly_stats(repo, commit)
else
rugged_stats(commit)
end
end
end
def gitaly_stats(repo, commit)
stats = repo.gitaly_commit_client.commit_stats(@id)
@additions = stats.additions
@deletions = stats.deletions
@total = @additions + @deletions
end
def rugged_stats(commit)
diff = commit.rugged_diff_from_parent
2017-01-04 13:43:06 -05:00
diff.each_patch do |p|
# TODO: Use the new Rugged convenience methods when they're released
@additions += p.stat[0]
@deletions += p.stat[1]
@total += p.changes
end
end
end
end
end