Commit fb49c94e authored by Andrew Newdigate's avatar Andrew Newdigate Committed by Sean McGivern

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

parent 7ab4efa8
...@@ -206,12 +206,18 @@ class Repository ...@@ -206,12 +206,18 @@ class Repository
end end
def branch_exists?(branch_name) 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 end
def ref_exists?(ref) def ref_exists?(ref)
rugged.references.exist?(ref) !!raw_repository&.ref_exists?(ref)
rescue Rugged::ReferenceError rescue ArgumentError
false false
end end
...@@ -266,6 +272,7 @@ class Repository ...@@ -266,6 +272,7 @@ class Repository
def expire_branches_cache def expire_branches_cache
expire_method_caches(%i(branch_names branch_count)) expire_method_caches(%i(branch_names branch_count))
@local_branches = nil @local_branches = nil
@branch_exists_memo = nil
end end
def expire_statistics_caches def expire_statistics_caches
......
...@@ -201,6 +201,19 @@ module Gitlab ...@@ -201,6 +201,19 @@ module Gitlab
end end
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 # Returns true if the given tag exists
# #
# name - The name of the tag as a String. # name - The name of the tag as a String.
...@@ -989,6 +1002,16 @@ module Gitlab ...@@ -989,6 +1002,16 @@ module Gitlab
raw_output.compact raw_output.compact
end 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 # Returns true if the given ref name exists
# #
# Ref names must start with `refs/`. # Ref names must start with `refs/`.
......
...@@ -71,7 +71,7 @@ module Gitlab ...@@ -71,7 +71,7 @@ module Gitlab
end end
def ref_exists?(ref_name) 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 = GitalyClient.call(@storage, :ref_service, :ref_exists, request)
response.value response.value
rescue GRPC::InvalidArgument => e rescue GRPC::InvalidArgument => e
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
feature 'Contributions Calendar', :js do feature 'Contributions Calendar', :js do
let(:user) { create(:user) } 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) } let(:issue_note) { create(:note, project: contributed_project) }
# Ex/ Sunday Jan 1, 2016 # Ex/ Sunday Jan 1, 2016
......
...@@ -17,7 +17,7 @@ feature 'Dashboard > Activity' do ...@@ -17,7 +17,7 @@ feature 'Dashboard > Activity' do
end end
context 'event filters', :js do context 'event filters', :js do
let(:project) { create(:project) } let(:project) { create(:project, :repository) }
let(:merge_request) do let(:merge_request) do
create(:merge_request, author: user, source_project: project, target_project: project) create(:merge_request, author: user, source_project: project, target_project: project)
......
...@@ -1110,6 +1110,34 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -1110,6 +1110,34 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
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 describe '#tag_exists?' do
shared_examples 'checks the existence of tags' do shared_examples 'checks the existence of tags' do
it 'returns true for an existing tag' do it 'returns true for an existing tag' do
......
...@@ -1356,7 +1356,7 @@ describe User do ...@@ -1356,7 +1356,7 @@ describe User do
end end
it "excludes push event if branch has been deleted" do 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) expect(subject.recent_push).to eq(nil)
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