submodule_helper.rb 1.78 KB
Newer Older
1 2 3 4 5
module SubmoduleHelper
  include Gitlab::ShellAdapter

  # links to files listing for submodule if submodule is a project on this server
  def submodule_links(submodule_item)
6
    url = @repository.submodule_url_for(@ref, submodule_item.path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7

8 9 10 11 12 13 14
    return url, nil unless url =~ /([^\/:]+\/[^\/]+\.git)\Z/

    project = $1
    project.chomp!('.git')

    if self_url?(url, project)
      return project_path(project), project_tree_path(project, submodule_item.id)
15 16
    elsif relative_self_url?(url)
      relative_self_links(url, submodule_item.id)
17 18 19 20 21 22 23 24
    elsif github_dot_com_url?(url)
      standard_links('github.com', project, submodule_item.id)
    elsif gitlab_dot_com_url?(url)
      standard_links('gitlab.com', project, submodule_item.id)
    else
      return url, nil
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
25

26 27 28 29 30 31 32 33 34
  protected

  def github_dot_com_url?(url)
    url =~ /github\.com[\/:][^\/]+\/[^\/]+\Z/
  end

  def gitlab_dot_com_url?(url)
    url =~ /gitlab\.com[\/:][^\/]+\/[^\/]+\Z/
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
35

36 37 38 39
  def self_url?(url, project)
    return true if url == [ Gitlab.config.gitlab.url, '/', project, '.git' ].join('')
    url == gitlab_shell.url_to_repo(project)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
40

41
  def relative_self_url?(url)
42 43
    # (./)?(../repo.git) || (./)?(../../project/repo.git) )
    url =~ /^((\.\/)?(\.\.\/))(?!(\.\.)|(.*\/)).*\.git\Z/ || url =~ /^((\.\/)?(\.\.\/){2})(?!(\.\.))([^\/]*)\/(?!(\.\.)|(.*\/)).*\.git\Z/
44 45
  end

46 47 48 49
  def standard_links(host, project, commit)
    base = [ 'https://', host, '/', project ].join('')
    return base, [ base, '/tree/', commit ].join('')
  end
50 51

  def relative_self_links(url, commit)
52
    if url.scan(/(\.\.\/)/).size == 2
abubadabu's avatar
abubadabu committed
53
      base = url[/([^\/]*\/[^\/]*)\.git/, 1]
54
    else
abubadabu's avatar
abubadabu committed
55
      base = [ @project.group.path, '/', url[/([^\/]*)\.git/, 1] ].join('')
56
    end
abubadabu's avatar
abubadabu committed
57
    return project_path(base), project_tree_path(base, commit)
58
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59
end