Commit c393d88d authored by Ahmad Sherif's avatar Ahmad Sherif

Migrate Gitlab::Git::Repository#commit_count to Gitaly

Closes gitaly#355
parent 5af1fcd6
...@@ -386,7 +386,7 @@ gem 'vmstat', '~> 2.3.0' ...@@ -386,7 +386,7 @@ gem 'vmstat', '~> 2.3.0'
gem 'sys-filesystem', '~> 1.1.6' gem 'sys-filesystem', '~> 1.1.6'
# Gitaly GRPC client # Gitaly GRPC client
gem 'gitaly', '~> 0.9.0' gem 'gitaly', '~> 0.13.0'
gem 'toml-rb', '~> 0.3.15', require: false gem 'toml-rb', '~> 0.3.15', require: false
......
...@@ -278,7 +278,7 @@ GEM ...@@ -278,7 +278,7 @@ GEM
po_to_json (>= 1.0.0) po_to_json (>= 1.0.0)
rails (>= 3.2.0) rails (>= 3.2.0)
gherkin-ruby (0.3.2) gherkin-ruby (0.3.2)
gitaly (0.9.0) gitaly (0.13.0)
google-protobuf (~> 3.1) google-protobuf (~> 3.1)
grpc (~> 1.0) grpc (~> 1.0)
github-linguist (4.7.6) github-linguist (4.7.6)
...@@ -980,7 +980,7 @@ DEPENDENCIES ...@@ -980,7 +980,7 @@ DEPENDENCIES
gettext (~> 3.2.2) gettext (~> 3.2.2)
gettext_i18n_rails (~> 1.8.0) gettext_i18n_rails (~> 1.8.0)
gettext_i18n_rails_js (~> 1.2.0) gettext_i18n_rails_js (~> 1.2.0)
gitaly (~> 0.9.0) gitaly (~> 0.13.0)
github-linguist (~> 4.7.0) github-linguist (~> 4.7.0)
gitlab-flowdock-git-hook (~> 1.0.1) gitlab-flowdock-git-hook (~> 1.0.1)
gitlab-markup (~> 1.5.1) gitlab-markup (~> 1.5.1)
......
...@@ -565,11 +565,17 @@ module Gitlab ...@@ -565,11 +565,17 @@ module Gitlab
# Return total commits count accessible from passed ref # Return total commits count accessible from passed ref
def commit_count(ref) def commit_count(ref)
walker = Rugged::Walker.new(rugged) gitaly_migrate(:commit_count) do |is_enabled|
walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE) if is_enabled
oid = rugged.rev_parse_oid(ref) gitaly_commit_client.commit_count(ref)
walker.push(oid) else
walker.count walker = Rugged::Walker.new(rugged)
walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
oid = rugged.rev_parse_oid(ref)
walker.push(oid)
walker.count
end
end
end end
# Sets HEAD to the commit specified by +ref+; +ref+ can be a branch or # Sets HEAD to the commit specified by +ref+; +ref+ can be a branch or
......
...@@ -56,6 +56,15 @@ module Gitlab ...@@ -56,6 +56,15 @@ module Gitlab
entry entry
end end
def commit_count(ref)
request = Gitaly::CountCommitsRequest.new(
repository: @gitaly_repo,
revision: ref
)
GitalyClient.call(@repository.storage, :commit_service, :count_commits, request).count
end
private private
def commit_diff_request_params(commit, options = {}) def commit_diff_request_params(commit, options = {})
......
...@@ -3,6 +3,20 @@ require "spec_helper" ...@@ -3,6 +3,20 @@ require "spec_helper"
describe Gitlab::Git::Repository, seed_helper: true do describe Gitlab::Git::Repository, seed_helper: true do
include Gitlab::EncodingHelper include Gitlab::EncodingHelper
shared_examples 'wrapping gRPC errors' do |gitaly_client_class, gitaly_client_method|
it 'wraps gRPC not found error' do
expect_any_instance_of(gitaly_client_class).to receive(gitaly_client_method)
.and_raise(GRPC::NotFound)
expect { subject }.to raise_error(Gitlab::Git::Repository::NoRepository)
end
it 'wraps gRPC unknown error' do
expect_any_instance_of(gitaly_client_class).to receive(gitaly_client_method)
.and_raise(GRPC::Unknown)
expect { subject }.to raise_error(Gitlab::Git::CommandError)
end
end
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) }
describe "Respond to" do describe "Respond to" do
...@@ -35,16 +49,8 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -35,16 +49,8 @@ describe Gitlab::Git::Repository, seed_helper: true do
repository.root_ref repository.root_ref
end end
it 'wraps GRPC not found' do it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :default_branch_name do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:default_branch_name) subject { repository.root_ref }
.and_raise(GRPC::NotFound)
expect { repository.root_ref }.to raise_error(Gitlab::Git::Repository::NoRepository)
end
it 'wraps GRPC exceptions' do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:default_branch_name)
.and_raise(GRPC::Unknown)
expect { repository.root_ref }.to raise_error(Gitlab::Git::CommandError)
end end
end end
...@@ -130,17 +136,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -130,17 +136,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
subject subject
end end
it 'wraps GRPC not found' do it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :branch_names
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:branch_names)
.and_raise(GRPC::NotFound)
expect { subject }.to raise_error(Gitlab::Git::Repository::NoRepository)
end
it 'wraps GRPC other exceptions' do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:branch_names)
.and_raise(GRPC::Unknown)
expect { subject }.to raise_error(Gitlab::Git::CommandError)
end
end end
describe '#tag_names' do describe '#tag_names' do
...@@ -168,17 +164,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -168,17 +164,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
subject subject
end end
it 'wraps GRPC not found' do it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :tag_names
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:tag_names)
.and_raise(GRPC::NotFound)
expect { subject }.to raise_error(Gitlab::Git::Repository::NoRepository)
end
it 'wraps GRPC exceptions' do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:tag_names)
.and_raise(GRPC::Unknown)
expect { subject }.to raise_error(Gitlab::Git::CommandError)
end
end end
shared_examples 'archive check' do |extenstion| shared_examples 'archive check' do |extenstion|
...@@ -406,8 +392,21 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -406,8 +392,21 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
describe '#commit_count' do describe '#commit_count' do
it { expect(repository.commit_count("master")).to eq(25) } shared_examples 'counting commits' do
it { expect(repository.commit_count("feature")).to eq(9) } it { expect(repository.commit_count("master")).to eq(25) }
it { expect(repository.commit_count("feature")).to eq(9) }
end
context 'when Gitaly commit_count feature is enabled' do
it_behaves_like 'counting commits'
it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Commit, :commit_count do
subject { repository.commit_count('master') }
end
end
context 'when Gitaly commit_count feature is disabled', skip_gitaly_mock: true do
it_behaves_like 'counting commits'
end
end end
describe "#reset" do describe "#reset" do
...@@ -1266,16 +1265,8 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -1266,16 +1265,8 @@ describe Gitlab::Git::Repository, seed_helper: true do
@repo.local_branches @repo.local_branches
end end
it 'wraps GRPC not found' do it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :local_branches do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:local_branches) subject { @repo.local_branches }
.and_raise(GRPC::NotFound)
expect { @repo.local_branches }.to raise_error(Gitlab::Git::Repository::NoRepository)
end
it 'wraps GRPC exceptions' do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:local_branches)
.and_raise(GRPC::Unknown)
expect { @repo.local_branches }.to raise_error(Gitlab::Git::CommandError)
end 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