Commit c9d6c02f authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Add Gitaly diff stats RPC client

This adds a basic interface to fetch diff statistics given two SHAs.
It's a requirement for #49399 #20282 and #19232.
parent 1cdab683
---
title: Add Gitaly diff stats RPC client
merge_request: 21732
author:
type: changed
# frozen_string_literal: true
module Gitlab
module Git
class DiffStatsCollection
include Enumerable
def initialize(diff_stats)
@collection = diff_stats
end
def each(&block)
@collection.each(&block)
end
end
end
end
...@@ -463,6 +463,16 @@ module Gitlab ...@@ -463,6 +463,16 @@ module Gitlab
Gitlab::Git::DiffCollection.new(iterator, options) Gitlab::Git::DiffCollection.new(iterator, options)
end end
def diff_stats(left_id, right_id)
stats = wrapped_gitaly_errors do
gitaly_commit_client.diff_stats(left_id, right_id)
end
Gitlab::Git::DiffStatsCollection.new(stats)
rescue CommandError
Gitlab::Git::DiffStatsCollection.new([])
end
# Returns a RefName for a given SHA # Returns a RefName for a given SHA
def ref_name_for_sha(ref_path, sha) def ref_name_for_sha(ref_path, sha)
raise ArgumentError, "sha can't be empty" unless sha.present? raise ArgumentError, "sha can't be empty" unless sha.present?
......
...@@ -172,6 +172,17 @@ module Gitlab ...@@ -172,6 +172,17 @@ module Gitlab
consume_commits_response(response) consume_commits_response(response)
end end
def diff_stats(left_commit_sha, right_commit_sha)
request = Gitaly::DiffStatsRequest.new(
repository: @gitaly_repo,
left_commit_id: left_commit_sha,
right_commit_id: right_commit_sha
)
response = GitalyClient.call(@repository.storage, :diff_service, :diff_stats, request, timeout: GitalyClient.medium_timeout)
response.flat_map(&:stats)
end
def find_all_commits(opts = {}) def find_all_commits(opts = {})
request = Gitaly::FindAllCommitsRequest.new( request = Gitaly::FindAllCommitsRequest.new(
repository: @gitaly_repo, repository: @gitaly_repo,
......
...@@ -1229,6 +1229,32 @@ describe Gitlab::Git::Repository, :seed_helper do ...@@ -1229,6 +1229,32 @@ describe Gitlab::Git::Repository, :seed_helper do
end end
end end
describe '#diff_stats' do
let(:left_commit_id) { 'feature' }
let(:right_commit_id) { 'master' }
it 'returns a DiffStatsCollection' do
collection = repository.diff_stats(left_commit_id, right_commit_id)
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
expect(collection).to be_a(Enumerable)
end
it 'yields Gitaly::DiffStats objects' do
collection = repository.diff_stats(left_commit_id, right_commit_id)
expect(collection.to_a).to all(be_a(Gitaly::DiffStats))
end
it 'returns no Gitaly::DiffStats when SHAs are invalid' do
collection = repository.diff_stats('foo', 'bar')
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
expect(collection).to be_a(Enumerable)
expect(collection.to_a).to be_empty
end
end
describe "#ls_files" do describe "#ls_files" do
let(:master_file_paths) { repository.ls_files("master") } let(:master_file_paths) { repository.ls_files("master") }
let(:utf8_file_paths) { repository.ls_files("ls-files-utf8") } let(:utf8_file_paths) { repository.ls_files("ls-files-utf8") }
......
...@@ -118,6 +118,22 @@ describe Gitlab::GitalyClient::CommitService do ...@@ -118,6 +118,22 @@ describe Gitlab::GitalyClient::CommitService do
end end
end end
describe '#diff_stats' do
let(:left_commit_id) { 'master' }
let(:right_commit_id) { 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660' }
it 'sends an RPC request' do
request = Gitaly::DiffStatsRequest.new(repository: repository_message,
left_commit_id: left_commit_id,
right_commit_id: right_commit_id)
expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:diff_stats)
.with(request, kind_of(Hash)).and_return([])
described_class.new(repository).diff_stats(left_commit_id, right_commit_id)
end
end
describe '#tree_entries' do describe '#tree_entries' do
let(:path) { '/' } let(:path) { '/' }
......
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