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)
|
|
|
|
url = repository.submodule_url_for(ref, submodule_item.path)
|
2014-02-10 02:55:38 -05:00
|
|
|
|
2017-04-18 01:44:32 -04:00
|
|
|
if url == '.' || url == './'
|
|
|
|
url = File.join(Gitlab.config.gitlab.url, @project.full_path)
|
|
|
|
end
|
|
|
|
|
2017-04-10 12:55:31 -04:00
|
|
|
if url =~ /([^\/:]+)\/([^\/]+(?:\.git)?)\Z/
|
|
|
|
namespace, project = $1, $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
|
|
|
|
|
|
|
|
namespace.sub!(/\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)
|
|
|
|
[namespace_project_path(namespace, project),
|
|
|
|
namespace_project_tree_path(namespace, project, submodule_item.id)]
|
|
|
|
elsif relative_self_url?(url)
|
|
|
|
relative_self_links(url, submodule_item.id)
|
|
|
|
elsif github_dot_com_url?(url)
|
|
|
|
standard_links('github.com', namespace, project, submodule_item.id)
|
|
|
|
elsif gitlab_dot_com_url?(url)
|
|
|
|
standard_links('gitlab.com', namespace, project, submodule_item.id)
|
|
|
|
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
|
|
|
|
|
|
|
|
def github_dot_com_url?(url)
|
|
|
|
url =~ /github\.com[\/:][^\/]+\/[^\/]+\Z/
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_dot_com_url?(url)
|
|
|
|
url =~ /gitlab\.com[\/:][^\/]+\/[^\/]+\Z/
|
|
|
|
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('')
|
|
|
|
url_with_dotgit = url_no_dotgit + '.git'
|
2017-05-26 14:51:24 -04:00
|
|
|
url_with_dotgit == Gitlab::Shell.new.url_to_repo([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)
|
2014-02-26 17:33:04 -05:00
|
|
|
# (./)?(../repo.git) || (./)?(../../project/repo.git) )
|
2017-04-18 01:44:16 -04:00
|
|
|
url =~ /\A((\.\/)?(\.\.\/))(?!(\.\.)|(.*\/)).*(\.git)?\z/ || url =~ /\A((\.\/)?(\.\.\/){2})(?!(\.\.))([^\/]*)\/(?!(\.\.)|(.*\/)).*(\.git)?\z/
|
2014-02-25 16:42:45 -05:00
|
|
|
end
|
|
|
|
|
2015-01-24 13:02:58 -05:00
|
|
|
def standard_links(host, namespace, project, commit)
|
2017-02-21 18:33:53 -05:00
|
|
|
base = ['https://', host, '/', namespace, '/', project].join('')
|
|
|
|
[base, [base, '/tree/', commit].join('')]
|
2014-02-03 21:35:48 -05:00
|
|
|
end
|
2014-02-25 16:42:45 -05:00
|
|
|
|
|
|
|
def relative_self_links(url, commit)
|
2017-06-28 15:01:39 -04:00
|
|
|
url.rstrip!
|
2015-04-10 01:07:32 -04:00
|
|
|
# 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?
|
2017-02-14 09:03:24 -05:00
|
|
|
namespace = @project.namespace.full_path
|
2014-02-25 16:42:45 -05:00
|
|
|
end
|
2015-03-24 21:35:57 -04:00
|
|
|
|
2017-09-15 10:28:41 -04:00
|
|
|
begin
|
|
|
|
[
|
|
|
|
namespace_project_path(namespace, base),
|
|
|
|
namespace_project_tree_path(namespace, base, commit)
|
|
|
|
]
|
|
|
|
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
|
2014-02-10 02:55:38 -05:00
|
|
|
end
|