diff options
author | Douwe Maan <douwe@gitlab.com> | 2018-08-31 15:31:44 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-08-31 15:31:44 +0300 |
commit | c691e07a7bafe51322e6a1ea5ec78fdc426d384c (patch) | |
tree | e9eabca02e8aa7bbe6eba78fb45d7e2be5d6e798 /app | |
parent | bdae30d8fb91d5df1f19a12a51c9566c7c196cdf (diff) | |
parent | f4f8270c9fe5906e54e70aafadc375f7a2c869b0 (diff) |
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
Diffstat (limited to 'app')
-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] |