2014-02-03 21:35:48 -05:00
|
|
|
module SubmoduleHelper
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
|
|
|
# links to files listing for submodule if submodule is a project on this server
|
2015-01-19 08:07:37 -05:00
|
|
|
def submodule_links(submodule_item, ref = nil)
|
|
|
|
url = @repository.submodule_url_for(ref, submodule_item.path)
|
2014-02-10 02:55:38 -05:00
|
|
|
|
2015-01-24 13:02:58 -05:00
|
|
|
return url, nil unless url =~ /([^\/:]+)\/([^\/]+\.git)\Z/
|
2014-02-03 21:35:48 -05:00
|
|
|
|
2015-01-24 13:02:58 -05:00
|
|
|
namespace = $1
|
|
|
|
project = $2
|
2014-02-03 21:35:48 -05:00
|
|
|
project.chomp!('.git')
|
|
|
|
|
2015-01-24 13:02:58 -05:00
|
|
|
if self_url?(url, namespace, project)
|
|
|
|
return namespace_project_path(namespace, project),
|
|
|
|
namespace_project_tree_path(namespace, project,
|
|
|
|
submodule_item.id)
|
2014-02-25 16:42:45 -05:00
|
|
|
elsif relative_self_url?(url)
|
|
|
|
relative_self_links(url, submodule_item.id)
|
2014-02-03 21:35:48 -05:00
|
|
|
elsif github_dot_com_url?(url)
|
2015-01-24 13:02:58 -05:00
|
|
|
standard_links('github.com', namespace, project, submodule_item.id)
|
2014-02-03 21:35:48 -05:00
|
|
|
elsif gitlab_dot_com_url?(url)
|
2015-01-24 13:02:58 -05:00
|
|
|
standard_links('gitlab.com', namespace, project, submodule_item.id)
|
2014-02-03 21:35:48 -05:00
|
|
|
else
|
|
|
|
return url, nil
|
|
|
|
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)
|
|
|
|
return true if url == [ Gitlab.config.gitlab.url, '/', namespace, '/',
|
|
|
|
project, '.git' ].join('')
|
|
|
|
url == gitlab_shell.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) )
|
|
|
|
url =~ /^((\.\/)?(\.\.\/))(?!(\.\.)|(.*\/)).*\.git\Z/ || url =~ /^((\.\/)?(\.\.\/){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)
|
|
|
|
base = [ 'https://', host, '/', namespace, '/', project ].join('')
|
2014-02-03 21:35:48 -05:00
|
|
|
return base, [ base, '/tree/', commit ].join('')
|
|
|
|
end
|
2014-02-25 16:42:45 -05:00
|
|
|
|
|
|
|
def relative_self_links(url, commit)
|
2014-02-26 17:33:04 -05:00
|
|
|
if url.scan(/(\.\.\/)/).size == 2
|
2014-02-28 11:22:07 -05:00
|
|
|
base = url[/([^\/]*\/[^\/]*)\.git/, 1]
|
2014-02-25 16:42:45 -05:00
|
|
|
else
|
2014-02-28 11:22:07 -05:00
|
|
|
base = [ @project.group.path, '/', url[/([^\/]*)\.git/, 1] ].join('')
|
2014-02-25 16:42:45 -05:00
|
|
|
end
|
2015-01-24 13:02:58 -05:00
|
|
|
return namespace_project_path(base.namespace, base),
|
|
|
|
namespace_project_tree_path(base.namespace, base, commit)
|
2014-02-25 16:42:45 -05:00
|
|
|
end
|
2014-02-10 02:55:38 -05:00
|
|
|
end
|