Commit 1957099b authored by Sean McGivern's avatar Sean McGivern

Merge branch 'an-gitaly-ref-exists-extras' into 'master'

Delegate Repository::branch_exists? and ref_exists? to Gitlab::Git

Closes gitaly#488

See merge request !13725
parents 7ab4efa8 fb49c94e
......@@ -206,12 +206,18 @@ class Repository
end
def branch_exists?(branch_name)
branch_names.include?(branch_name)
return false unless raw_repository
@branch_exists_memo ||= Hash.new do |hash, key|
hash[key] = raw_repository.branch_exists?(key)
end
@branch_exists_memo[branch_name]
end
def ref_exists?(ref)
rugged.references.exist?(ref)
rescue Rugged::ReferenceError
!!raw_repository&.ref_exists?(ref)
rescue ArgumentError
false
end
......@@ -266,6 +272,7 @@ class Repository
def expire_branches_cache
expire_method_caches(%i(branch_names branch_count))
@local_branches = nil
@branch_exists_memo = nil
end
def expire_statistics_caches
......
......@@ -201,6 +201,19 @@ module Gitlab
end
end
# Returns true if the given ref name exists
#
# Ref names must start with `refs/`.
def ref_exists?(ref_name)
gitaly_migrate(:ref_exists) do |is_enabled|
if is_enabled
gitaly_ref_exists?(ref_name)
else
rugged_ref_exists?(ref_name)
end
end
end
# Returns true if the given tag exists
#
# name - The name of the tag as a String.
......@@ -989,6 +1002,16 @@ module Gitlab
raw_output.compact
end
# Returns true if the given ref name exists
#
# Ref names must start with `refs/`.
def rugged_ref_exists?(ref_name)
raise ArgumentError, 'invalid refname' unless ref_name.start_with?('refs/')
rugged.references.exist?(ref_name)
rescue Rugged::ReferenceError
false
end
# Returns true if the given ref name exists
#
# Ref names must start with `refs/`.
......
......@@ -71,7 +71,7 @@ module Gitlab
end
def ref_exists?(ref_name)
request = Gitaly::RefExistsRequest.new(repository: @gitaly_repo, ref: ref_name)
request = Gitaly::RefExistsRequest.new(repository: @gitaly_repo, ref: GitalyClient.encode(ref_name))
response = GitalyClient.call(@storage, :ref_service, :ref_exists, request)
response.value
rescue GRPC::InvalidArgument => e
......
......@@ -2,7 +2,7 @@ require 'spec_helper'
feature 'Contributions Calendar', :js do
let(:user) { create(:user) }
let(:contributed_project) { create(:project, :public) }
let(:contributed_project) { create(:project, :public, :repository) }
let(:issue_note) { create(:note, project: contributed_project) }
# Ex/ Sunday Jan 1, 2016
......
......@@ -17,7 +17,7 @@ feature 'Dashboard > Activity' do
end
context 'event filters', :js do
let(:project) { create(:project) }
let(:project) { create(:project, :repository) }
let(:merge_request) do
create(:merge_request, author: user, source_project: project, target_project: project)
......
......@@ -1110,6 +1110,34 @@ describe Gitlab::Git::Repository, seed_helper: true do
end
end
describe '#ref_exists?' do
shared_examples 'checks the existence of refs' do
it 'returns true for an existing tag' do
expect(repository.ref_exists?('refs/heads/master')).to eq(true)
end
it 'returns false for a non-existing tag' do
expect(repository.ref_exists?('refs/tags/THIS_TAG_DOES_NOT_EXIST')).to eq(false)
end
it 'raises an ArgumentError for an empty string' do
expect { repository.ref_exists?('') }.to raise_error(ArgumentError)
end
it 'raises an ArgumentError for an invalid ref' do
expect { repository.ref_exists?('INVALID') }.to raise_error(ArgumentError)
end
end
context 'when Gitaly ref_exists feature is enabled' do
it_behaves_like 'checks the existence of refs'
end
context 'when Gitaly ref_exists feature is disabled', skip_gitaly_mock: true do
it_behaves_like 'checks the existence of refs'
end
end
describe '#tag_exists?' do
shared_examples 'checks the existence of tags' do
it 'returns true for an existing tag' do
......
......@@ -1356,7 +1356,7 @@ describe User do
end
it "excludes push event if branch has been deleted" do
allow_any_instance_of(Repository).to receive(:branch_names).and_return(['foo'])
allow_any_instance_of(Repository).to receive(:branch_exists?).with('master').and_return(false)
expect(subject.recent_push).to eq(nil)
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