Module: ExtractsPath

Overview

Module providing methods for dealing with separating a tree-ish string and a file path string when combined in a request parameter

Constant Summary

Constants included from ExtractsRef

ExtractsRef::InvalidPathError

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from ExtractsRef

#extract_ref, #tree

Instance Method Details

#assign_ref_varsObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/extracts_path.rb', line 31

def assign_ref_vars
  super

  if @path.empty? && !@commit && @id.ends_with?('.atom')
    @id = @ref = extract_ref_without_atom(@id)
    @commit = @repo.commit(@ref)

    request.format = :atom if @commit
  end

  raise InvalidPathError unless @commit

  @hex_path = Digest::SHA1.hexdigest(@path)
  @logs_path = logs_file_project_ref_path(@project, @ref, @path)
rescue RuntimeError, NoMethodError, InvalidPathError
  render_404
end

#extract_ref_without_atom(id) ⇒ Object

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.


15
16
17
18
19
20
# File 'lib/extracts_path.rb', line 15

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

#lfs_blob_idsObject

rubocop:enable Gitlab/ModuleWithInstanceVariables


50
51
52
53
54
55
56
57
58
# File 'lib/extracts_path.rb', line 50

def lfs_blob_ids
  blob_ids = tree.blobs.map(&:id)

  # 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

  @lfs_blob_ids = Gitlab::Git::Blob.batch_lfs_pointers(repository_container.repository, blob_ids).map(&:id) # rubocop:disable Gitlab/ModuleWithInstanceVariables
end