BigW Consortium Gitlab

submodule_helper.rb 3.21 KB
Newer Older
1 2 3
module SubmoduleHelper
  include Gitlab::ShellAdapter

4 5
  VALID_SUBMODULE_PROTOCOLS = %w[http https git ssh].freeze

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

10 11 12 13
    if url == '.' || url == './'
      url = File.join(Gitlab.config.gitlab.url, @project.full_path)
    end

14 15
    if url =~ /([^\/:]+)\/([^\/]+(?:\.git)?)\Z/
      namespace, project = $1, $2
16 17 18 19 20 21 22 23 24 25 26
      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\//, '')
27
      project.rstrip!
28
      project.sub!(/\.git\z/, '')
29

30 31 32 33 34 35 36 37 38 39 40 41
      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
42
    else
43
      [sanitize_submodule_url(url), nil]
44 45
    end
  end
Dmitriy Zaporozhets committed
46

47 48 49 50 51 52 53 54 55
  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 committed
56

Vinnie Okada committed
57
  def self_url?(url, namespace, project)
58 59 60 61 62
    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'
    url_with_dotgit == gitlab_shell.url_to_repo([namespace, '/', project].join(''))
63
  end
Dmitriy Zaporozhets committed
64

65
  def relative_self_url?(url)
66
    # (./)?(../repo.git) || (./)?(../../project/repo.git) )
67
    url =~ /\A((\.\/)?(\.\.\/))(?!(\.\.)|(.*\/)).*(\.git)?\z/ || url =~ /\A((\.\/)?(\.\.\/){2})(?!(\.\.))([^\/]*)\/(?!(\.\.)|(.*\/)).*(\.git)?\z/
68 69
  end

Vinnie Okada committed
70
  def standard_links(host, namespace, project, commit)
71 72
    base = ['https://', host, '/', namespace, '/', project].join('')
    [base, [base, '/tree/', commit].join('')]
73
  end
74 75

  def relative_self_links(url, commit)
76
    url.rstrip!
77 78 79 80 81 82 83 84 85 86
    # 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?
87
      namespace = @project.namespace.full_path
88
    end
89 90

    [
91 92
      namespace_project_path(namespace, base),
      namespace_project_tree_path(namespace, base, commit)
93
    ]
94
  end
95 96 97 98 99 100 101 102 103 104 105 106

  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
Dmitriy Zaporozhets committed
107
end