BigW Consortium Gitlab

repo_path.rb 1.25 KB
Newer Older
1 2 3 4
module Gitlab
  module RepoPath
    NotFoundError = Class.new(StandardError)

5
    def self.parse(repo_path)
6
      wiki = false
7
      project_path = strip_storage_path(repo_path.sub(/\.git\z/, ''), fail_on_not_found: false)
8 9 10 11
      project, was_redirected = find_project(project_path)

      if project_path.end_with?('.wiki') && project.nil?
        project, was_redirected = find_project(project_path.chomp('.wiki'))
12 13 14
        wiki = true
      end

15 16 17
      redirected_path = project_path if was_redirected

      [project, wiki, redirected_path]
18 19 20 21
    end

    def self.strip_storage_path(repo_path, fail_on_not_found: true)
      result = repo_path
22

23
      storage = Gitlab.config.repositories.storages.values.find do |params|
24
        repo_path.start_with?(params.legacy_disk_path)
25 26
      end

27
      if storage
28
        result = result.sub(storage.legacy_disk_path, '')
29
      elsif fail_on_not_found
30 31 32
        raise NotFoundError.new("No known storage path matches #{repo_path.inspect}")
      end

33
      result.sub(%r{\A/*}, '')
34
    end
35 36 37 38 39 40 41

    def self.find_project(project_path)
      project = Project.find_by_full_path(project_path, follow_redirects: true)
      was_redirected = project && project.full_path.casecmp(project_path) != 0

      [project, was_redirected]
    end
42 43
  end
end