BigW Consortium Gitlab

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

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

Vinnie Okada committed
8
    return url, nil unless url =~ /([^\/:]+)\/([^\/]+\.git)\Z/
9

Vinnie Okada committed
10 11
    namespace = $1
    project = $2
12 13
    project.chomp!('.git')

Vinnie Okada committed
14 15 16 17
    if self_url?(url, namespace, project)
      return namespace_project_path(namespace, project),
        namespace_project_tree_path(namespace, project,
                                    submodule_item.id)
18 19
    elsif relative_self_url?(url)
      relative_self_links(url, submodule_item.id)
20
    elsif github_dot_com_url?(url)
Vinnie Okada committed
21
      standard_links('github.com', namespace, project, submodule_item.id)
22
    elsif gitlab_dot_com_url?(url)
Vinnie Okada committed
23
      standard_links('gitlab.com', namespace, project, submodule_item.id)
24 25 26 27
    else
      return url, nil
    end
  end
Dmitriy Zaporozhets committed
28

29 30 31 32 33 34 35 36 37
  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
38

Vinnie Okada committed
39 40 41 42
  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(''))
43
  end
Dmitriy Zaporozhets committed
44

45
  def relative_self_url?(url)
46
    # (./)?(../repo.git) || (./)?(../../project/repo.git) )
47
    url =~ /\A((\.\/)?(\.\.\/))(?!(\.\.)|(.*\/)).*\.git\z/ || url =~ /\A((\.\/)?(\.\.\/){2})(?!(\.\.))([^\/]*)\/(?!(\.\.)|(.*\/)).*\.git\z/
48 49
  end

Vinnie Okada committed
50 51
  def standard_links(host, namespace, project, commit)
    base = [ 'https://', host, '/', namespace, '/', project ].join('')
52
    [base, [ base, '/tree/', commit ].join('')]
53
  end
54 55

  def relative_self_links(url, commit)
56 57 58 59 60 61 62 63 64 65
    # 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?
66
      namespace = @project.namespace.path
67
    end
68 69

    [
70 71
      namespace_project_path(namespace, base),
      namespace_project_tree_path(namespace, base, commit)
72
    ]
73
  end
Dmitriy Zaporozhets committed
74
end