debian-mirror-gitlab/lib/gitlab/repo_path.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.4 KiB
Ruby
Raw Normal View History

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)
2021-02-22 17:27:13 +05:30
# Returns an array containing:
# - The repository container
# - The related project (if available)
# - The repository type
# - The original container path (if redirected)
#
# @returns [HasRepository, Project, String, String]
2020-03-13 15:44:24 +05:30
def self.parse(path)
2021-02-22 17:27:13 +05:30
repo_path = path.delete_prefix('/').delete_suffix('.git')
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-13 15:44:24 +05:30
next unless type.valid?(repo_path)
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
# Removing the suffix (.wiki, .design, ...) from the project path
full_path = repo_path.chomp(type.path_suffix)
2021-09-30 23:02:18 +05:30
container, project = find_container(type, full_path)
next unless container
2020-03-13 15:44:24 +05:30
2021-09-30 23:02:18 +05:30
redirected_path = repo_path if redirected?(container, repo_path)
return [container, project, type, redirected_path]
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.
2020-04-08 14:13:33 +05:30
[nil, nil, Gitlab::GlRepository.default_type, nil]
end
2021-02-22 17:27:13 +05:30
# Returns an array containing:
# - The repository container
# - The related project (if available)
#
# @returns [HasRepository, Project, String]
2020-04-08 14:13:33 +05:30
def self.find_container(type, full_path)
2021-09-30 23:02:18 +05:30
return [nil, nil] if full_path.blank?
2021-02-22 17:27:13 +05:30
2020-04-08 14:13:33 +05:30
if type.snippet?
2021-09-30 23:02:18 +05:30
snippet = find_snippet(full_path)
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
[snippet, snippet&.project]
2021-01-03 14:25:43 +05:30
elsif type.wiki?
2021-09-30 23:02:18 +05:30
wiki = find_wiki(full_path)
2021-01-03 14:25:43 +05:30
2021-09-30 23:02:18 +05:30
[wiki, wiki.try(:project)]
2020-04-08 14:13:33 +05:30
else
2021-09-30 23:02:18 +05:30
project = find_project(full_path)
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
[project, project]
2020-04-08 14:13:33 +05:30
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
def self.find_project(project_path)
2021-09-30 23:02:18 +05:30
Project.find_by_full_path(project_path, follow_redirects: true)
2020-03-13 15:44:24 +05:30
end
2021-02-22 17:27:13 +05:30
def self.redirected?(container, container_path)
container && container.full_path.casecmp(container_path) != 0
2017-09-10 17:25:29 +05:30
end
2020-04-08 14:13:33 +05:30
# Snippet_path can be either:
# - snippets/1
# - h5bp/html5-boilerplate/snippets/53
def self.find_snippet(snippet_path)
snippet_id, project_path = extract_snippet_info(snippet_path)
2021-09-30 23:02:18 +05:30
return unless snippet_id
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
project = find_project(project_path) if project_path
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
Snippet.find_by_id_and_project(id: snippet_id, project: project)
2020-04-08 14:13:33 +05:30
end
2021-01-03 14:25:43 +05:30
# Wiki path can be either:
# - namespace/project
# - group/subgroup/project
2021-02-22 17:27:13 +05:30
#
# And also in EE:
# - group
# - group/subgroup
def self.find_wiki(container_path)
container = Routable.find_by_full_path(container_path, follow_redirects: true)
# In CE, Group#wiki is not available so this will return nil for a group path.
2021-09-30 23:02:18 +05:30
container&.try(:wiki)
2021-01-03 14:25:43 +05:30
end
2020-04-08 14:13:33 +05:30
def self.extract_snippet_info(snippet_path)
path_segments = snippet_path.split('/')
snippet_id = path_segments.pop
2021-02-22 17:27:13 +05:30
path_segments.pop # Remove 'snippets' from path
project_path = File.join(path_segments).presence
2020-04-08 14:13:33 +05:30
[snippet_id, project_path]
end
2017-08-17 22:00:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::RepoPath.singleton_class.prepend_mod_with('Gitlab::RepoPath::ClassMethods')