debian-mirror-gitlab/lib/extracts_path.rb

67 lines
2.4 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
# Module providing methods for dealing with separating a tree-ish string and a
# file path string when combined in a request parameter
module ExtractsPath
2020-06-23 00:09:42 +05:30
extend ::Gitlab::Utils::Override
include ExtractsRef
2014-09-02 18:07:02 +05:30
2016-11-03 12:29:30 +05:30
# If we have an ID of 'foo.atom', and the controller provides Atom and HTML
# formats, then we have to check if the request was for the Atom version of
# the ID without the '.atom' suffix, or the HTML version of the ID including
# the suffix. We only check this if the version including the suffix doesn't
# match, so it is possible to create a branch which has an unroutable Atom
# feed.
def extract_ref_without_atom(id)
id_without_atom = id.sub(/\.atom$/, '')
valid_refs = ref_names.select { |v| "#{id_without_atom}/".start_with?("#{v}/") }
valid_refs.max_by(&:length)
end
2020-06-23 00:09:42 +05:30
# Extends the method to handle if there is no path and the ref doesn't
# exist in the repo, try to resolve the ref without an '.atom' suffix.
# If _that_ ref is found, set the request's format to Atom manually.
2016-11-03 12:29:30 +05:30
#
2014-09-02 18:07:02 +05:30
# Automatically renders `not_found!` if a valid tree path could not be
# resolved (e.g., when a user inserts an invalid path or ref).
2020-06-23 00:09:42 +05:30
#
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2020-06-23 00:09:42 +05:30
override :assign_ref_vars
2014-09-02 18:07:02 +05:30
def assign_ref_vars
2020-06-23 00:09:42 +05:30
super
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
if @path.empty? && !@commit && @id.ends_with?('.atom')
@id = @ref = extract_ref_without_atom(@id)
@commit = @repo.commit(@ref)
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
request.format = :atom if @commit
2014-09-02 18:07:02 +05:30
end
raise InvalidPathError unless @commit
@hex_path = Digest::SHA1.hexdigest(@path)
2017-09-10 17:25:29 +05:30
@logs_path = logs_file_project_ref_path(@project, @ref, @path)
2014-09-02 18:07:02 +05:30
rescue RuntimeError, NoMethodError, InvalidPathError
2015-10-24 18:46:33 +05:30
render_404
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
def lfs_blob_ids
blob_ids = tree.blobs.map(&:id)
2018-11-18 11:00:15 +05:30
# When current endpoint is a Blob then `tree.blobs` will be empty, it means we need to analyze
# the current Blob in order to determine if it's a LFS object
blob_ids = Array.wrap(@repo.blob_at(@commit.id, @path)&.id) if blob_ids.empty? # rubocop:disable Gitlab/ModuleWithInstanceVariables
2020-06-23 00:09:42 +05:30
@lfs_blob_ids = Gitlab::Git::Blob.batch_lfs_pointers(repository_container.repository, blob_ids).map(&:id) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2014-09-02 18:07:02 +05:30
end
2016-09-29 09:46:39 +05:30
private
2020-06-23 00:09:42 +05:30
override :repository_container
def repository_container
@project
2016-11-03 12:29:30 +05:30
end
2014-09-02 18:07:02 +05:30
end