2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module RepoPath
|
|
|
|
NotFoundError = Class.new(StandardError)
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def self.parse(path)
|
|
|
|
repo_path = path.sub(/\.git\z/, '').sub(%r{\A/}, '')
|
|
|
|
redirected_path = nil
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
# 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.
|
2020-03-09 13:42:32 +05:30
|
|
|
next unless type.valid?(repo_path)
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
# Removing the suffix (.wiki, .design, ...) from the project path
|
|
|
|
full_path = repo_path.chomp(type.path_suffix)
|
|
|
|
|
|
|
|
project, was_redirected = find_project(full_path)
|
|
|
|
redirected_path = repo_path if was_redirected
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
# If we found a matching project, then the type was matched, no need to
|
|
|
|
# continue looking.
|
|
|
|
return [project, type, redirected_path] if project
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
# 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-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def self.find_project(project_path)
|
|
|
|
project = Project.find_by_full_path(project_path, follow_redirects: true)
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
[project, redirected?(project, project_path)]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.redirected?(project, project_path)
|
|
|
|
project && project.full_path.casecmp(project_path) != 0
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
Gitlab::RepoPath.singleton_class.prepend_if_ee('EE::Gitlab::RepoPath::ClassMethods')
|