Commit c691e07a authored by Douwe Maan's avatar Douwe Maan

Merge branch '37356-relative-submodule-link' into 'master'

Fix git submodule link for subgroup projects with relative path

Closes #37356

See merge request gitlab-org/gitlab-ce!21154
parents bdae30d8 f4f8270c
...@@ -64,8 +64,7 @@ module SubmoduleHelper ...@@ -64,8 +64,7 @@ module SubmoduleHelper
end end
def relative_self_url?(url) def relative_self_url?(url)
# (./)?(../repo.git) || (./)?(../../project/repo.git) ) url.start_with?('../', './')
url =~ %r{\A((\./)?(\.\./))(?!(\.\.)|(.*/)).*(\.git)?\z} || url =~ %r{\A((\./)?(\.\./){2})(?!(\.\.))([^/]*)/(?!(\.\.)|(.*/)).*(\.git)?\z}
end end
def standard_links(host, namespace, project, commit) def standard_links(host, namespace, project, commit)
...@@ -73,25 +72,29 @@ module SubmoduleHelper ...@@ -73,25 +72,29 @@ module SubmoduleHelper
[base, [base, '/tree/', commit].join('')] [base, [base, '/tree/', commit].join('')]
end end
def relative_self_links(url, commit, project) def relative_self_links(relative_path, commit, project)
url.rstrip! relative_path.rstrip!
# Map relative links to a namespace and project absolute_project_path = "/" + project.full_path
# For example:
# ../bar.git -> same namespace, repo bar # Resolve `relative_path` to target path
# ../foo/bar.git -> namespace foo, repo bar # Assuming `absolute_project_path` is `/g1/p1`:
# ../../foo/bar/baz.git -> namespace bar, repo baz # ../p2.git -> /g1/p2
components = url.split('/') # ../g2/p3.git -> /g1/g2/p3
base = components.pop.gsub(/.git$/, '') # ../../g3/g4/p4.git -> /g3/g4/p4
namespace = components.pop.gsub(/^\.\.$/, '') submodule_project_path = File.absolute_path(relative_path, absolute_project_path)
target_namespace_path = File.dirname(submodule_project_path)
if namespace.empty?
namespace = project.namespace.full_path if target_namespace_path == '/' || target_namespace_path.start_with?(absolute_project_path)
return [nil, nil]
end end
target_namespace_path.sub!(%r{^/}, '')
submodule_base = File.basename(submodule_project_path, '.git')
begin begin
[ [
namespace_project_path(namespace, base), namespace_project_path(target_namespace_path, submodule_base),
namespace_project_tree_path(namespace, base, commit) namespace_project_tree_path(target_namespace_path, submodule_base, commit)
] ]
rescue ActionController::UrlGenerationError rescue ActionController::UrlGenerationError
[nil, nil] [nil, nil]
......
---
title: Fix git submodule link for subgroup projects with relative path
merge_request: 21154
author:
type: fixed
...@@ -162,42 +162,77 @@ describe SubmoduleHelper do ...@@ -162,42 +162,77 @@ describe SubmoduleHelper do
end end
context 'submodules with relative links' do context 'submodules with relative links' do
let(:group) { create(:group, name: "Master Project", path: "master-project") } let(:group) { create(:group, name: "top group", path: "top-group") }
let(:project) { create(:project, group: group) } let(:project) { create(:project, group: group) }
let(:commit_id) { sample_commit[:id] } let(:repo) { double(:repo, project: project) }
def expect_relative_link_to_resolve_to(relative_path, expected_path)
allow(repo).to receive(:submodule_url_for).and_return(relative_path)
result = submodule_links(submodule_item)
expect(result).to eq([expected_path, "#{expected_path}/tree/#{submodule_item.id}"])
end
it 'one level down' do it 'handles project under same group' do
result = relative_self_links('../test.git', commit_id, project) expect_relative_link_to_resolve_to('../test.git', "/#{group.path}/test")
expect(result).to eq(["/#{group.path}/test", "/#{group.path}/test/tree/#{commit_id}"])
end end
it 'with trailing whitespace' do it 'handles trailing whitespace' do
result = relative_self_links('../test.git ', commit_id, project) expect_relative_link_to_resolve_to('../test.git ', "/#{group.path}/test")
expect(result).to eq(["/#{group.path}/test", "/#{group.path}/test/tree/#{commit_id}"])
end end
it 'two levels down' do it 'handles project under another top group' do
result = relative_self_links('../../test.git', commit_id, project) expect_relative_link_to_resolve_to('../../baz/test.git ', "/baz/test")
expect(result).to eq(["/#{group.path}/test", "/#{group.path}/test/tree/#{commit_id}"]) end
context 'repo path resolves to be located at root (namespace absent)' do
it 'returns nil' do
allow(repo).to receive(:submodule_url_for).and_return('../../test.git')
result = submodule_links(submodule_item)
expect(result).to eq([nil, nil])
end
end end
it 'one level down with namespace and repo' do context 'repo path resolves to be located underneath current project path' do
result = relative_self_links('../foobar/test.git', commit_id, project) it 'returns nil because it is not possible to have repo nested under another repo' do
expect(result).to eq(["/foobar/test", "/foobar/test/tree/#{commit_id}"]) allow(repo).to receive(:submodule_url_for).and_return('./test.git')
result = submodule_links(submodule_item)
expect(result).to eq([nil, nil])
end
end end
it 'two levels down with namespace and repo' do context 'subgroup' do
result = relative_self_links('../foobar/baz/test.git', commit_id, project) let(:sub_group) { create(:group, parent: group, name: "sub group", path: "sub-group") }
expect(result).to eq(["/baz/test", "/baz/test/tree/#{commit_id}"]) let(:sub_project) { create(:project, group: sub_group) }
context 'project in sub group' do
let(:project) { sub_project }
it "handles referencing ancestor group's project" do
expect_relative_link_to_resolve_to('../../../top-group/test.git', "/#{group.path}/test")
end
end
it "handles referencing descendent group's project" do
expect_relative_link_to_resolve_to('../sub-group/test.git', "/top-group/sub-group/test")
end
it "handles referencing another top group's project" do
expect_relative_link_to_resolve_to('../../frontend/css/test.git', "/frontend/css/test")
end
end end
context 'personal project' do context 'personal project' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, namespace: user.namespace) } let(:project) { create(:project, namespace: user.namespace) }
it 'one level down with personal project' do it 'handles referencing another personal project' do
result = relative_self_links('../test.git', commit_id, project) expect_relative_link_to_resolve_to('../test.git', "/#{user.username}/test")
expect(result).to eq(["/#{user.username}/test", "/#{user.username}/test/tree/#{commit_id}"])
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