2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-24 11:54:22 -04:00
|
|
|
module Gitlab
|
|
|
|
module RepoPath
|
|
|
|
NotFoundError = Class.new(StandardError)
|
|
|
|
|
2017-04-28 12:52:09 -04:00
|
|
|
def self.parse(repo_path)
|
2018-05-11 07:49:52 -04:00
|
|
|
project_path = repo_path.sub(/\.git\z/, '').sub(%r{\A/}, '')
|
|
|
|
|
2019-03-18 12:51:11 -04:00
|
|
|
# Detect the repo type based on the path, the first one tried is the project
|
|
|
|
# type, which does not have a suffix.
|
|
|
|
Gitlab::GlRepository.types.each do |_name, type|
|
|
|
|
# If the project path does not end with the defined suffix, try the next
|
|
|
|
# type.
|
|
|
|
# We'll always try to find a project with an empty suffix (for the
|
|
|
|
# `Gitlab::GlRepository::PROJECT` type.
|
|
|
|
next unless project_path.end_with?(type.path_suffix)
|
|
|
|
|
|
|
|
project, was_redirected = find_project(project_path.chomp(type.path_suffix))
|
|
|
|
redirected_path = project_path if was_redirected
|
|
|
|
|
|
|
|
# If we found a matching project, then the type was matched, no need to
|
|
|
|
# continue looking.
|
|
|
|
return [project, type, redirected_path] if project
|
2017-04-28 12:52:09 -04:00
|
|
|
end
|
|
|
|
|
2019-04-05 06:51:42 -04:00
|
|
|
# When a project did not exist, the parsed repo_type would be empty.
|
|
|
|
# In that case, we want to continue with a regular project repository. As we
|
|
|
|
# could create the project if the user pushing is allowed to do so.
|
|
|
|
[nil, Gitlab::GlRepository.default_type, nil]
|
2017-04-28 12:52:09 -04:00
|
|
|
end
|
|
|
|
|
2017-06-15 20:03:54 -04:00
|
|
|
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
|
2017-03-24 11:54:22 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Gitlab::RepoPath.singleton_class.prepend_if_ee('EE::Gitlab::RepoPath::ClassMethods')
|