2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-03 21:35:48 -05:00
|
|
|
module SubmoduleHelper
|
2017-05-26 14:51:24 -04:00
|
|
|
extend self
|
2014-02-03 21:35:48 -05:00
|
|
|
|
2017-04-10 12:55:31 -04:00
|
|
|
VALID_SUBMODULE_PROTOCOLS = %w[http https git ssh].freeze
|
|
|
|
|
2014-02-03 21:35:48 -05:00
|
|
|
# links to files listing for submodule if submodule is a project on this server
|
2015-04-13 01:27:45 -04:00
|
|
|
def submodule_links(submodule_item, ref = nil, repository = @repository)
|
2020-01-20 16:08:46 -05:00
|
|
|
repository.submodule_links.for(submodule_item, ref)
|
2019-07-17 09:06:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def submodule_links_for_url(submodule_item_id, url, repository)
|
2019-07-25 18:01:17 -04:00
|
|
|
return [nil, nil] unless url
|
|
|
|
|
2017-04-18 01:44:32 -04:00
|
|
|
if url == '.' || url == './'
|
2018-07-13 13:04:29 -04:00
|
|
|
url = File.join(Gitlab.config.gitlab.url, repository.project.full_path)
|
2017-04-18 01:44:32 -04:00
|
|
|
end
|
|
|
|
|
2018-01-27 00:35:53 -05:00
|
|
|
if url =~ %r{([^/:]+)/([^/]+(?:\.git)?)\Z}
|
2020-09-03 05:08:20 -04:00
|
|
|
namespace, project = Regexp.last_match(1), Regexp.last_match(2)
|
2017-06-05 02:26:20 -04:00
|
|
|
gitlab_hosts = [Gitlab.config.gitlab.url,
|
|
|
|
Gitlab.config.gitlab_shell.ssh_path_prefix]
|
|
|
|
|
|
|
|
gitlab_hosts.each do |host|
|
|
|
|
if url.start_with?(host)
|
|
|
|
namespace, _, project = url.sub(host, '').rpartition('/')
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-27 00:35:53 -05:00
|
|
|
namespace.sub!(%r{\A/}, '')
|
2017-05-28 17:18:18 -04:00
|
|
|
project.rstrip!
|
2017-04-10 12:55:31 -04:00
|
|
|
project.sub!(/\.git\z/, '')
|
2014-02-03 21:35:48 -05:00
|
|
|
|
2017-04-10 12:55:31 -04:00
|
|
|
if self_url?(url, namespace, project)
|
2019-07-22 08:25:56 -04:00
|
|
|
[url_helpers.namespace_project_path(namespace, project),
|
|
|
|
url_helpers.namespace_project_tree_path(namespace, project, submodule_item_id)]
|
2017-04-10 12:55:31 -04:00
|
|
|
elsif relative_self_url?(url)
|
2019-07-17 09:06:19 -04:00
|
|
|
relative_self_links(url, submodule_item_id, repository.project)
|
2020-03-16 17:09:21 -04:00
|
|
|
elsif gist_github_dot_com_url?(url)
|
|
|
|
gist_github_com_tree_links(namespace, project, submodule_item_id)
|
2017-04-10 12:55:31 -04:00
|
|
|
elsif github_dot_com_url?(url)
|
2020-01-27 07:08:35 -05:00
|
|
|
github_com_tree_links(namespace, project, submodule_item_id)
|
2017-04-10 12:55:31 -04:00
|
|
|
elsif gitlab_dot_com_url?(url)
|
2020-01-27 07:08:35 -05:00
|
|
|
gitlab_com_tree_links(namespace, project, submodule_item_id)
|
2017-04-10 12:55:31 -04:00
|
|
|
else
|
|
|
|
[sanitize_submodule_url(url), nil]
|
|
|
|
end
|
2014-02-03 21:35:48 -05:00
|
|
|
else
|
2017-04-10 12:55:31 -04:00
|
|
|
[sanitize_submodule_url(url), nil]
|
2014-02-03 21:35:48 -05:00
|
|
|
end
|
|
|
|
end
|
2014-02-10 02:55:38 -05:00
|
|
|
|
2014-02-03 21:35:48 -05:00
|
|
|
protected
|
|
|
|
|
2020-03-16 17:09:21 -04:00
|
|
|
def gist_github_dot_com_url?(url)
|
|
|
|
url =~ %r{gist\.github\.com[/:][^/]+/[^/]+\Z}
|
|
|
|
end
|
|
|
|
|
2014-02-03 21:35:48 -05:00
|
|
|
def github_dot_com_url?(url)
|
2018-01-27 00:35:53 -05:00
|
|
|
url =~ %r{github\.com[/:][^/]+/[^/]+\Z}
|
2014-02-03 21:35:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_dot_com_url?(url)
|
2018-01-27 00:35:53 -05:00
|
|
|
url =~ %r{gitlab\.com[/:][^/]+/[^/]+\Z}
|
2014-02-03 21:35:48 -05:00
|
|
|
end
|
2014-02-10 02:55:38 -05:00
|
|
|
|
2015-01-24 13:02:58 -05:00
|
|
|
def self_url?(url, namespace, project)
|
2017-04-18 01:44:16 -04:00
|
|
|
url_no_dotgit = url.chomp('.git')
|
|
|
|
return true if url_no_dotgit == [Gitlab.config.gitlab.url, '/', namespace, '/',
|
|
|
|
project].join('')
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-04-18 01:44:16 -04:00
|
|
|
url_with_dotgit = url_no_dotgit + '.git'
|
2020-04-07 11:09:30 -04:00
|
|
|
url_with_dotgit == Gitlab::RepositoryUrlBuilder.build([namespace, '/', project].join(''))
|
2014-02-03 21:35:48 -05:00
|
|
|
end
|
2014-02-10 02:55:38 -05:00
|
|
|
|
2014-02-25 16:42:45 -05:00
|
|
|
def relative_self_url?(url)
|
2018-08-13 00:01:59 -04:00
|
|
|
url.start_with?('../', './')
|
2014-02-25 16:42:45 -05:00
|
|
|
end
|
|
|
|
|
2020-01-27 07:08:35 -05:00
|
|
|
def gitlab_com_tree_links(namespace, project, commit)
|
|
|
|
base = ['https://gitlab.com/', namespace, '/', project].join('')
|
|
|
|
[base, [base, '/-/tree/', commit].join('')]
|
|
|
|
end
|
|
|
|
|
2020-03-16 17:09:21 -04:00
|
|
|
def gist_github_com_tree_links(namespace, project, commit)
|
|
|
|
base = ['https://gist.github.com/', namespace, '/', project].join('')
|
|
|
|
[base, [base, commit].join('/')]
|
|
|
|
end
|
|
|
|
|
2020-01-27 07:08:35 -05:00
|
|
|
def github_com_tree_links(namespace, project, commit)
|
|
|
|
base = ['https://github.com/', namespace, '/', project].join('')
|
2017-02-21 18:33:53 -05:00
|
|
|
[base, [base, '/tree/', commit].join('')]
|
2014-02-03 21:35:48 -05:00
|
|
|
end
|
2014-02-25 16:42:45 -05:00
|
|
|
|
2018-08-13 00:01:59 -04:00
|
|
|
def relative_self_links(relative_path, commit, project)
|
2019-09-30 08:06:01 -04:00
|
|
|
relative_path = relative_path.rstrip
|
2018-08-13 00:01:59 -04:00
|
|
|
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]
|
2014-02-25 16:42:45 -05:00
|
|
|
end
|
2015-03-24 21:35:57 -04:00
|
|
|
|
2018-08-13 00:01:59 -04:00
|
|
|
target_namespace_path.sub!(%r{^/}, '')
|
|
|
|
submodule_base = File.basename(submodule_project_path, '.git')
|
|
|
|
|
2017-09-15 10:28:41 -04:00
|
|
|
begin
|
|
|
|
[
|
2019-07-22 08:25:56 -04:00
|
|
|
url_helpers.namespace_project_path(target_namespace_path, submodule_base),
|
|
|
|
url_helpers.namespace_project_tree_path(target_namespace_path, submodule_base, commit)
|
2017-09-15 10:28:41 -04:00
|
|
|
]
|
|
|
|
rescue ActionController::UrlGenerationError
|
|
|
|
[nil, nil]
|
|
|
|
end
|
2014-02-25 16:42:45 -05:00
|
|
|
end
|
2017-04-10 12:55:31 -04:00
|
|
|
|
|
|
|
def sanitize_submodule_url(url)
|
|
|
|
uri = URI.parse(url)
|
|
|
|
|
|
|
|
if uri.scheme.in?(VALID_SUBMODULE_PROTOCOLS)
|
|
|
|
uri.to_s
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
nil
|
|
|
|
end
|
2019-07-22 08:25:56 -04:00
|
|
|
|
|
|
|
def url_helpers
|
|
|
|
Gitlab::Routing.url_helpers
|
|
|
|
end
|
2014-02-10 02:55:38 -05:00
|
|
|
end
|