Commit 7528b2d2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'osw-fallback-on-blank-refs' into 'master'

Avoid Gitaly RPC errors when fetching diff stats

Closes gitaly#1393

See merge request gitlab-org/gitlab-ce!22995
parents 1cccfca1 039df026
---
title: Avoid Gitaly RPC errors when fetching diff stats
merge_request: 22995
author:
type: fixed
...@@ -419,13 +419,17 @@ module Gitlab ...@@ -419,13 +419,17 @@ module Gitlab
end end
def diff_stats(left_id, right_id) def diff_stats(left_id, right_id)
if [left_id, right_id].any? { |ref| ref.blank? || Gitlab::Git.blank_ref?(ref) }
return empty_diff_stats
end
stats = wrapped_gitaly_errors do stats = wrapped_gitaly_errors do
gitaly_commit_client.diff_stats(left_id, right_id) gitaly_commit_client.diff_stats(left_id, right_id)
end end
Gitlab::Git::DiffStatsCollection.new(stats) Gitlab::Git::DiffStatsCollection.new(stats)
rescue CommandError, TypeError rescue CommandError, TypeError
Gitlab::Git::DiffStatsCollection.new([]) empty_diff_stats
end end
# Returns a RefName for a given SHA # Returns a RefName for a given SHA
...@@ -962,6 +966,10 @@ module Gitlab ...@@ -962,6 +966,10 @@ module Gitlab
private private
def empty_diff_stats
Gitlab::Git::DiffStatsCollection.new([])
end
def uncached_has_local_branches? def uncached_has_local_branches?
wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_repository_client.has_local_branches? gitaly_repository_client.has_local_branches?
......
...@@ -1095,12 +1095,26 @@ describe Gitlab::Git::Repository, :seed_helper do ...@@ -1095,12 +1095,26 @@ describe Gitlab::Git::Repository, :seed_helper do
end end
it 'returns no Gitaly::DiffStats when there is a nil SHA' do it 'returns no Gitaly::DiffStats when there is a nil SHA' do
expect_any_instance_of(Gitlab::GitalyClient::CommitService)
.not_to receive(:diff_stats)
collection = repository.diff_stats(nil, 'master') collection = repository.diff_stats(nil, 'master')
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection) expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
expect(collection).to be_a(Enumerable) expect(collection).to be_a(Enumerable)
expect(collection.to_a).to be_empty expect(collection.to_a).to be_empty
end end
it 'returns no Gitaly::DiffStats when there is a BLANK_SHA' do
expect_any_instance_of(Gitlab::GitalyClient::CommitService)
.not_to receive(:diff_stats)
collection = repository.diff_stats(Gitlab::Git::BLANK_SHA, 'master')
expect(collection).to be_a(Gitlab::Git::DiffStatsCollection)
expect(collection).to be_a(Enumerable)
expect(collection.to_a).to be_empty
end
end end
describe "#ls_files" do describe "#ls_files" do
......
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