diff options
author | Mark Chao <mchao@gitlab.com> | 2018-08-13 12:01:59 +0800 |
---|---|---|
committer | Mark Chao <mchao@gitlab.com> | 2018-08-20 19:52:53 +0800 |
commit | f4f8270c9fe5906e54e70aafadc375f7a2c869b0 (patch) | |
tree | 46566b92c4bda7e75eb1880c8f2dcd5c7860c684 /app/helpers | |
parent | 3c80adf5c8486315fa84ac237177c38b9ae625c9 (diff) | |
download | gitlab-ce-f4f8270c9fe5906e54e70aafadc375f7a2c869b0.tar.gz |
Allow resolving git submodule url for subgroup projects using relative path37356-relative-submodule-link
Diffstat (limited to 'app/helpers')
-rw-r--r-- | app/helpers/submodule_helper.rb | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/app/helpers/submodule_helper.rb b/app/helpers/submodule_helper.rb index ebfde993456..ec2cf2b16c0 100644 --- a/app/helpers/submodule_helper.rb +++ b/app/helpers/submodule_helper.rb @@ -64,8 +64,7 @@ module SubmoduleHelper end def relative_self_url?(url) - # (./)?(../repo.git) || (./)?(../../project/repo.git) ) - url =~ %r{\A((\./)?(\.\./))(?!(\.\.)|(.*/)).*(\.git)?\z} || url =~ %r{\A((\./)?(\.\./){2})(?!(\.\.))([^/]*)/(?!(\.\.)|(.*/)).*(\.git)?\z} + url.start_with?('../', './') end def standard_links(host, namespace, project, commit) @@ -73,25 +72,29 @@ module SubmoduleHelper [base, [base, '/tree/', commit].join('')] end - def relative_self_links(url, commit, project) - url.rstrip! - # Map relative links to a namespace and project - # For example: - # ../bar.git -> same namespace, repo bar - # ../foo/bar.git -> namespace foo, repo bar - # ../../foo/bar/baz.git -> namespace bar, repo baz - components = url.split('/') - base = components.pop.gsub(/.git$/, '') - namespace = components.pop.gsub(/^\.\.$/, '') - - if namespace.empty? - namespace = project.namespace.full_path + def relative_self_links(relative_path, commit, project) + relative_path.rstrip! + absolute_project_path = "/" + project.full_path + + # Resolve `relative_path` to target path + # Assuming `absolute_project_path` is `/g1/p1`: + # ../p2.git -> /g1/p2 + # ../g2/p3.git -> /g1/g2/p3 + # ../../g3/g4/p4.git -> /g3/g4/p4 + submodule_project_path = File.absolute_path(relative_path, absolute_project_path) + target_namespace_path = File.dirname(submodule_project_path) + + if target_namespace_path == '/' || target_namespace_path.start_with?(absolute_project_path) + return [nil, nil] end + target_namespace_path.sub!(%r{^/}, '') + submodule_base = File.basename(submodule_project_path, '.git') + begin [ - namespace_project_path(namespace, base), - namespace_project_tree_path(namespace, base, commit) + namespace_project_path(target_namespace_path, submodule_base), + namespace_project_tree_path(target_namespace_path, submodule_base, commit) ] rescue ActionController::UrlGenerationError [nil, nil] |