Commit 350ea9c5 authored by Matija Čupić's avatar Matija Čupić

Implement Repository#ambiguous_ref?

This implements Repository#ambiguous_ref? and checks if a ref is
ambiguous before trying to resolve the ref in Project#protected_for?
parent d307dccb
...@@ -1737,10 +1737,10 @@ class Project < ActiveRecord::Base ...@@ -1737,10 +1737,10 @@ class Project < ActiveRecord::Base
end end
def protected_for?(ref) def protected_for?(ref)
resolved_ref = repository.resolve_ref(ref) return false unless ref && !repository.ambiguous_ref?(ref)
return false unless resolved_ref
ref_name = Gitlab::Git.ref_name(resolved_ref) resolved_ref = repository.resolve_ref(ref)
ref_name = resolved_ref == ref ? Gitlab::Git.ref_name(resolved_ref) : ref
if Gitlab::Git.branch_ref?(resolved_ref) if Gitlab::Git.branch_ref?(resolved_ref)
ProtectedBranch.protected?(self, ref_name) ProtectedBranch.protected?(self, ref_name)
......
...@@ -182,15 +182,14 @@ class Repository ...@@ -182,15 +182,14 @@ class Repository
tags.find { |tag| tag.name == name } tags.find { |tag| tag.name == name }
end end
def resolve_ref(ref) def ambiguous_ref?(ref)
tag_exists = tag_exists?(ref) tag_exists?(ref) && branch_exists?(ref)
branch_exists = branch_exists?(ref) end
if tag_exists && branch_exists def resolve_ref(ref)
nil if tag_exists?(ref)
elsif tag_exists
Gitlab::Git::TAG_REF_PREFIX + ref Gitlab::Git::TAG_REF_PREFIX + ref
elsif branch_exists elsif branch_exists?(ref)
Gitlab::Git::BRANCH_REF_PREFIX + ref Gitlab::Git::BRANCH_REF_PREFIX + ref
else else
ref ref
......
...@@ -17,7 +17,7 @@ module Gitlab ...@@ -17,7 +17,7 @@ module Gitlab
return error('Commit not found') return error('Commit not found')
end end
unless @command.project.repository.resolve_ref(@command.origin_ref) if @command.project.repository.ambiguous_ref?(@command.origin_ref)
return error('Ref is ambiguous') return error('Ref is ambiguous')
end end
end end
......
...@@ -2565,14 +2565,23 @@ describe Project do ...@@ -2565,14 +2565,23 @@ describe Project do
describe '#protected_for?' do describe '#protected_for?' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
subject { project.protected_for?('ref') } subject { project.protected_for?(ref) }
before do context 'when ref is nil' do
allow(project.repository).to receive(:resolve_ref).and_return(ref) let(:ref) { nil }
it 'returns false' do
is_expected.to be_falsey
end
end end
context 'when ref is ambiguous' do context 'when ref is ambiguous' do
let(:ref) { nil } let(:ref) { 'ref' }
before do
project.repository.add_branch(project.creator, ref, 'master')
project.repository.add_tag(project.creator, ref, 'master')
end
it 'returns false' do it 'returns false' do
is_expected.to be_falsey is_expected.to be_falsey
...@@ -2580,7 +2589,7 @@ describe Project do ...@@ -2580,7 +2589,7 @@ describe Project do
end end
context 'when the ref is not protected' do context 'when the ref is not protected' do
let(:ref) { 'refs/heads/master' } let(:ref) { 'master' }
before do before do
stub_application_setting( stub_application_setting(
...@@ -2593,7 +2602,7 @@ describe Project do ...@@ -2593,7 +2602,7 @@ describe Project do
end end
context 'when the ref is a protected branch' do context 'when the ref is a protected branch' do
let(:ref) { 'refs/heads/master' } let(:ref) { 'master' }
before do before do
create(:protected_branch, name: 'master', project: project) create(:protected_branch, name: 'master', project: project)
...@@ -2605,7 +2614,7 @@ describe Project do ...@@ -2605,7 +2614,7 @@ describe Project do
end end
context 'when the ref is a protected tag' do context 'when the ref is a protected tag' do
let(:ref) { 'refs/tags/v1.0.0' } let(:ref) { 'v1.0.0' }
before do before do
create(:protected_tag, name: 'v1.0.0', project: project) create(:protected_tag, name: 'v1.0.0', project: project)
......
...@@ -1005,7 +1005,36 @@ describe Repository do ...@@ -1005,7 +1005,36 @@ describe Repository do
end end
end end
describe '#ambiguous_ref?' do
let(:ref) { 'ref' }
subject { repository.ambiguous_ref?(ref) }
context 'when ref is ambiguous' do
before do
repository.add_tag(project.creator, ref, 'master')
repository.add_branch(project.creator, ref, 'master')
end
it 'should be true' do
is_expected.to eq(true)
end
end
context 'when ref is not ambiguous' do
before do
repository.add_tag(project.creator, ref, 'master')
end
it 'should be false' do
is_expected.to eq(false)
end
end
end
describe '#resolve_ref' do describe '#resolve_ref' do
let(:ref) { 'ref' }
subject { repository.resolve_ref(ref) } subject { repository.resolve_ref(ref) }
context 'when ref is full ref' do context 'when ref is full ref' do
...@@ -1016,38 +1045,23 @@ describe Repository do ...@@ -1016,38 +1045,23 @@ describe Repository do
end end
end end
context 'when ref is a tag or branch name' do context 'when ref is tag name' do
let(:ref) { 'ref' } before do
repository.add_tag(project.creator, ref, 'master')
context 'when ref is ambiguous' do
before do
repository.add_tag(project.creator, ref, 'master')
repository.add_branch(project.creator, ref, 'master')
end
it 'returns nil' do
is_expected.to eq(nil)
end
end end
context 'when ref is tag name' do it 'returns the tag ref' do
before do is_expected.to eq("refs/tags/#{ref}")
repository.add_tag(project.creator, ref, 'master')
end
it 'returns the tag ref' do
is_expected.to eq("refs/tags/#{ref}")
end
end end
end
context 'when ref is branch name' do context 'when ref is branch name' do
before do before do
repository.add_branch(project.creator, ref, 'master') repository.add_branch(project.creator, ref, 'master')
end end
it 'returns the branch ref' do it 'returns the branch ref' do
is_expected.to eq("refs/heads/#{ref}") is_expected.to eq("refs/heads/#{ref}")
end
end end
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