debian-mirror-gitlab/app/helpers/submodule_helper.rb

154 lines
4.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module SubmoduleHelper
2017-09-10 17:25:29 +05:30
extend self
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
VALID_SUBMODULE_PROTOCOLS = %w[http https git ssh].freeze
2014-09-02 18:07:02 +05:30
# links to files listing for submodule if submodule is a project on this server
2020-11-24 15:15:51 +05:30
def submodule_links(submodule_item, ref = nil, repository = @repository, diff_file = nil)
repository.submodule_links.for(submodule_item, ref, diff_file)
2019-09-30 21:07:59 +05:30
end
2020-11-24 15:15:51 +05:30
def submodule_links_for_url(submodule_item_id, url, repository, old_submodule_item_id = nil)
return [nil, nil, nil] unless url
2019-09-30 21:07:59 +05:30
2017-09-10 17:25:29 +05:30
if url == '.' || url == './'
2018-11-08 19:23:39 +05:30
url = File.join(Gitlab.config.gitlab.url, repository.project.full_path)
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
if url =~ %r{([^/:]+)/([^/]+(?:\.git)?)\Z}
2020-11-24 15:15:51 +05:30
namespace, project = Regexp.last_match(1), Regexp.last_match(2)
2017-09-10 17:25:29 +05:30
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-03-17 18:26:18 +05:30
namespace.sub!(%r{\A/}, '')
2017-08-17 22:00:37 +05:30
project.rstrip!
project.sub!(/\.git\z/, '')
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
if self_url?(url, namespace, project)
2020-11-24 15:15:51 +05:30
[
url_helpers.namespace_project_path(namespace, project),
url_helpers.namespace_project_tree_path(namespace, project, submodule_item_id),
(url_helpers.namespace_project_compare_path(namespace, project, to: submodule_item_id, from: old_submodule_item_id) if old_submodule_item_id)
]
2017-08-17 22:00:37 +05:30
elsif relative_self_url?(url)
2020-11-24 15:15:51 +05:30
relative_self_links(url, submodule_item_id, old_submodule_item_id, repository.project)
2020-04-08 14:13:33 +05:30
elsif gist_github_dot_com_url?(url)
gist_github_com_tree_links(namespace, project, submodule_item_id)
2017-08-17 22:00:37 +05:30
elsif github_dot_com_url?(url)
2020-11-24 15:15:51 +05:30
github_com_tree_links(namespace, project, submodule_item_id, old_submodule_item_id)
2017-08-17 22:00:37 +05:30
elsif gitlab_dot_com_url?(url)
2020-11-24 15:15:51 +05:30
gitlab_com_tree_links(namespace, project, submodule_item_id, old_submodule_item_id)
2017-08-17 22:00:37 +05:30
else
2020-11-24 15:15:51 +05:30
[sanitize_submodule_url(url), nil, nil]
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
else
2020-11-24 15:15:51 +05:30
[sanitize_submodule_url(url), nil, nil]
2014-09-02 18:07:02 +05:30
end
end
protected
2020-04-08 14:13:33 +05:30
def gist_github_dot_com_url?(url)
url =~ %r{gist\.github\.com[/:][^/]+/[^/]+\Z}
end
2014-09-02 18:07:02 +05:30
def github_dot_com_url?(url)
2018-03-17 18:26:18 +05:30
url =~ %r{github\.com[/:][^/]+/[^/]+\Z}
2014-09-02 18:07:02 +05:30
end
def gitlab_dot_com_url?(url)
2018-03-17 18:26:18 +05:30
url =~ %r{gitlab\.com[/:][^/]+/[^/]+\Z}
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def self_url?(url, namespace, project)
2017-08-17 22:00:37 +05:30
url_no_dotgit = url.chomp('.git')
return true if url_no_dotgit == [Gitlab.config.gitlab.url, '/', namespace, '/',
project].join('')
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
url_with_dotgit = url_no_dotgit + '.git'
2020-04-22 19:07:51 +05:30
url_with_dotgit == Gitlab::RepositoryUrlBuilder.build([namespace, '/', project].join(''))
2014-09-02 18:07:02 +05:30
end
def relative_self_url?(url)
2018-11-20 20:47:30 +05:30
url.start_with?('../', './')
2014-09-02 18:07:02 +05:30
end
2020-11-24 15:15:51 +05:30
def gitlab_com_tree_links(namespace, project, commit, old_commit)
2020-03-13 15:44:24 +05:30
base = ['https://gitlab.com/', namespace, '/', project].join('')
2020-11-24 15:15:51 +05:30
[
base,
[base, '/-/tree/', commit].join(''),
([base, '/-/compare/', old_commit, '...', commit].join('') if old_commit)
]
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
def gist_github_com_tree_links(namespace, project, commit)
base = ['https://gist.github.com/', namespace, '/', project].join('')
2020-11-24 15:15:51 +05:30
[base, [base, commit].join('/'), nil]
2020-04-08 14:13:33 +05:30
end
2020-11-24 15:15:51 +05:30
def github_com_tree_links(namespace, project, commit, old_commit)
2020-03-13 15:44:24 +05:30
base = ['https://github.com/', namespace, '/', project].join('')
2020-11-24 15:15:51 +05:30
[
base,
[base, '/tree/', commit].join(''),
([base, '/compare/', old_commit, '...', commit].join('') if old_commit)
]
2014-09-02 18:07:02 +05:30
end
2020-11-24 15:15:51 +05:30
def relative_self_links(relative_path, commit, old_commit, project)
2019-12-21 20:55:43 +05:30
relative_path = relative_path.rstrip
2018-11-20 20:47:30 +05:30
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)
2020-11-24 15:15:51 +05:30
return [nil, nil, nil]
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2018-11-20 20:47:30 +05:30
target_namespace_path.sub!(%r{^/}, '')
submodule_base = File.basename(submodule_project_path, '.git')
2018-03-17 18:26:18 +05:30
begin
[
2019-09-30 21:07:59 +05:30
url_helpers.namespace_project_path(target_namespace_path, submodule_base),
2020-11-24 15:15:51 +05:30
url_helpers.namespace_project_tree_path(target_namespace_path, submodule_base, commit),
(url_helpers.namespace_project_compare_path(target_namespace_path, submodule_base, to: commit, from: old_commit) if old_commit)
2018-03-17 18:26:18 +05:30
]
rescue ActionController::UrlGenerationError
2020-11-24 15:15:51 +05:30
[nil, nil, nil]
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
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-09-30 21:07:59 +05:30
def url_helpers
Gitlab::Routing.url_helpers
end
2014-09-02 18:07:02 +05:30
end