Commit 24d68225 authored by Matija Čupić's avatar Matija Čupić

Move Project#resolve_ref to Repository

parent d12bc3bf
...@@ -1160,21 +1160,6 @@ class Project < ActiveRecord::Base ...@@ -1160,21 +1160,6 @@ class Project < ActiveRecord::Base
end end
end end
def resolve_ref(ref)
tag_exists = repository.tag_exists?(ref)
branch_exists = repository.branch_exists?(ref)
if tag_exists && branch_exists
nil
elsif tag_exists
Gitlab::Git::TAG_REF_PREFIX + ref
elsif branch_exists
Gitlab::Git::BRANCH_REF_PREFIX + ref
else
ref
end
end
def root_ref?(branch) def root_ref?(branch)
repository.root_ref == branch repository.root_ref == branch
end end
...@@ -1752,7 +1737,7 @@ class Project < ActiveRecord::Base ...@@ -1752,7 +1737,7 @@ class Project < ActiveRecord::Base
end end
def protected_for?(ref) def protected_for?(ref)
resolved_ref = resolve_ref(ref) resolved_ref = repository.resolve_ref(ref)
return false unless resolved_ref return false unless resolved_ref
ref_name = Gitlab::Git.ref_name(resolved_ref) ref_name = Gitlab::Git.ref_name(resolved_ref)
......
...@@ -182,6 +182,21 @@ class Repository ...@@ -182,6 +182,21 @@ class Repository
tags.find { |tag| tag.name == name } tags.find { |tag| tag.name == name }
end end
def resolve_ref(ref)
tag_exists = tag_exists?(ref)
branch_exists = branch_exists?(ref)
if tag_exists && branch_exists
nil
elsif tag_exists
Gitlab::Git::TAG_REF_PREFIX + ref
elsif branch_exists
Gitlab::Git::BRANCH_REF_PREFIX + ref
else
ref
end
end
def add_branch(user, branch_name, ref) def add_branch(user, branch_name, ref)
branch = raw_repository.add_branch(branch_name, user: user, target: ref) branch = raw_repository.add_branch(branch_name, user: user, target: 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.resolve_ref(@command.origin_ref) unless @command.project.repository.resolve_ref(@command.origin_ref)
return error('Ref is ambiguous') return error('Ref is ambiguous')
end end
end end
......
...@@ -2568,7 +2568,7 @@ describe Project do ...@@ -2568,7 +2568,7 @@ describe Project do
subject { project.protected_for?('ref') } subject { project.protected_for?('ref') }
before do before do
allow(project).to receive(:resolve_ref).and_return(ref) allow(project.repository).to receive(:resolve_ref).and_return(ref)
end end
context 'when ref is ambiguous' do context 'when ref is ambiguous' do
...@@ -2796,55 +2796,6 @@ describe Project do ...@@ -2796,55 +2796,6 @@ describe Project do
end end
end end
describe '#resolve_ref' do
let(:project) { create(:project, :repository) }
subject { project.resolve_ref(ref) }
context 'when ref is full ref' do
let(:ref) { 'refs/heads/master' }
it 'returns the ref' do
is_expected.to eq(ref)
end
end
context 'when ref is a tag or branch name' do
let(:ref) { 'ref' }
context 'when ref is ambiguous' do
before do
project.repository.add_tag(project.creator, ref, 'master')
project.repository.add_branch(project.creator, ref, 'master')
end
it 'returns nil' do
is_expected.to eq(nil)
end
end
context 'when ref is tag name' do
before do
project.repository.add_tag(project.creator, ref, 'master')
end
it 'returns the tag ref' do
is_expected.to eq("refs/tags/#{ref}")
end
end
context 'when ref is branch name' do
before do
project.repository.add_branch(project.creator, ref, 'master')
end
it 'returns the branch ref' do
is_expected.to eq("refs/heads/#{ref}")
end
end
end
end
describe '#http_url_to_repo' do describe '#http_url_to_repo' do
let(:project) { create(:project) } let(:project) { create(:project) }
......
...@@ -1005,6 +1005,53 @@ describe Repository do ...@@ -1005,6 +1005,53 @@ describe Repository do
end end
end end
describe '#resolve_ref' do
subject { repository.resolve_ref(ref) }
context 'when ref is full ref' do
let(:ref) { 'refs/heads/master' }
it 'returns the ref' do
is_expected.to eq(ref)
end
end
context 'when ref is a tag or branch name' do
let(: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 'returns nil' do
is_expected.to eq(nil)
end
end
context 'when ref is tag name' do
before do
repository.add_tag(project.creator, ref, 'master')
end
it 'returns the tag ref' do
is_expected.to eq("refs/tags/#{ref}")
end
end
context 'when ref is branch name' do
before do
repository.add_branch(project.creator, ref, 'master')
end
it 'returns the branch ref' do
is_expected.to eq("refs/heads/#{ref}")
end
end
end
end
describe '#add_branch' do describe '#add_branch' do
let(:branch_name) { 'new_feature' } let(:branch_name) { 'new_feature' }
let(:target) { 'master' } let(:target) { 'master' }
......
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