Commit a1a2ce4a authored by Robert Speicher's avatar Robert Speicher

Merge branch 'gitaly-519-commit-stats' into 'master'

Migrate Git::CommitStats to Gitaly

Closes gitaly#519

See merge request !14077
parents f0b4e547 25c34608
......@@ -352,7 +352,7 @@ module Gitlab
end
def stats
Gitlab::Git::CommitStats.new(self)
Gitlab::Git::CommitStats.new(@repository, self)
end
def to_patch(options = {})
......
......@@ -10,12 +10,29 @@ module Gitlab
# Instantiate a CommitStats object
#
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/323
def initialize(commit)
def initialize(repo, commit)
@id = commit.id
@additions = 0
@deletions = 0
@total = 0
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
diff.each_patch do |p|
......
......@@ -204,6 +204,14 @@ module Gitlab
response.sum(&:data)
end
def commit_stats(revision)
request = Gitaly::CommitStatsRequest.new(
repository: @gitaly_repo,
revision: GitalyClient.encode(revision)
)
GitalyClient.call(@repository.storage, :commit_service, :commit_stats, request)
end
private
def commit_diff_request_params(commit, options = {})
......
......@@ -401,7 +401,7 @@ describe Gitlab::Git::Commit, seed_helper: true do
end
end
describe '#stats' do
shared_examples '#stats' do
subject { commit.stats }
describe '#additions' do
......@@ -415,6 +415,14 @@ describe Gitlab::Git::Commit, seed_helper: true do
end
end
describe '#stats with gitaly on' do
it_should_behave_like '#stats'
end
describe '#stats with gitaly disabled', skip_gitaly_mock: true do
it_should_behave_like '#stats'
end
describe '#to_diff' do
subject { commit.to_diff }
......
......@@ -165,4 +165,29 @@ describe Gitlab::GitalyClient::CommitService do
expect(subject).to eq("my diff")
end
end
describe '#commit_stats' do
let(:request) do
Gitaly::CommitStatsRequest.new(
repository: repository_message, revision: revision
)
end
let(:response) do
Gitaly::CommitStatsResponse.new(
oid: revision,
additions: 11,
deletions: 15
)
end
subject { described_class.new(repository).commit_stats(revision) }
it 'sends an RPC request' do
expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:commit_stats)
.with(request, kind_of(Hash)).and_return(response)
expect(subject.additions).to eq(11)
expect(subject.deletions).to eq(15)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment