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

113 lines
3.3 KiB
Ruby
Raw Normal View History

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
2015-09-11 14:41:01 +05:30
def submodule_links(submodule_item, ref = nil, repository = @repository)
url = repository.submodule_url_for(ref, submodule_item.path)
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
if url == '.' || url == './'
url = File.join(Gitlab.config.gitlab.url, @project.full_path)
end
2018-03-17 18:26:18 +05:30
if url =~ %r{([^/:]+)/([^/]+(?:\.git)?)\Z}
2017-08-17 22:00:37 +05:30
namespace, project = $1, $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)
[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-09-02 18:07:02 +05:30
else
2017-08-17 22:00:37 +05:30
[sanitize_submodule_url(url), nil]
2014-09-02 18:07:02 +05:30
end
end
protected
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'
2017-09-10 17:25:29 +05:30
url_with_dotgit == Gitlab::Shell.new.url_to_repo([namespace, '/', project].join(''))
2014-09-02 18:07:02 +05:30
end
def relative_self_url?(url)
# (./)?(../repo.git) || (./)?(../../project/repo.git) )
2018-03-17 18:26:18 +05:30
url =~ %r{\A((\./)?(\.\./))(?!(\.\.)|(.*/)).*(\.git)?\z} || url =~ %r{\A((\./)?(\.\./){2})(?!(\.\.))([^/]*)/(?!(\.\.)|(.*/)).*(\.git)?\z}
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def standard_links(host, namespace, project, commit)
2017-08-17 22:00:37 +05:30
base = ['https://', host, '/', namespace, '/', project].join('')
[base, [base, '/tree/', commit].join('')]
2014-09-02 18:07:02 +05:30
end
def relative_self_links(url, commit)
2017-09-10 17:25:29 +05:30
url.rstrip!
2015-04-26 12:48:37 +05:30
# 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-08-17 22:00:37 +05:30
namespace = @project.namespace.full_path
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
begin
[
namespace_project_path(namespace, base),
namespace_project_tree_path(namespace, base, commit)
]
rescue ActionController::UrlGenerationError
[nil, nil]
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
2014-09-02 18:07:02 +05:30
end