Class: Gitlab::Search::FoundBlob

Inherits:
Object
  • Object
show all
Includes:
BlobActiveModel, BlobLanguageFromGitAttributes, EncodingHelper, Utils::StrongMemoize, Presentable
Defined in:
lib/gitlab/search/found_blob.rb

Constant Summary collapse

PATH_REGEXP =
/\A(?<ref>[^:]*):(?<path>[^\x00]*)\x00/.freeze
CONTENT_REGEXP =
/^(?<ref>[^:]*):(?<path>[^\x00]*)\x00(?<startline>\d+)\x00/.freeze

Constants included from EncodingHelper

EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BlobActiveModel

#to_ability_name

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from BlobLanguageFromGitAttributes

#language_from_gitattributes

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8

Constructor Details

#initialize(opts = {}) ⇒ FoundBlob

Returns a new instance of FoundBlob.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/search/found_blob.rb', line 23

def initialize(opts = {})
  @id = opts.fetch(:id, nil)
  @binary_path = opts.fetch(:path, nil)
  @binary_basename = opts.fetch(:basename, nil)
  @ref = opts.fetch(:ref, nil)
  @startline = opts.fetch(:startline, nil)
  @binary_data = opts.fetch(:data, nil)
  @per_page = opts.fetch(:per_page, 20)
  @project = opts.fetch(:project, nil)
  # Some callers (e.g. Elasticsearch) do not have the Project object,
  # yet they can trigger many calls in one go,
  # causing duplicated queries.
  # Allow those to just pass project_id instead.
  @project_id = opts.fetch(:project_id, nil)
  @content_match = opts.fetch(:content_match, nil)
  @blob_path = opts.fetch(:blob_path, nil)
  @repository = opts.fetch(:repository, nil)
end

Instance Attribute Details

#blob_pathObject (readonly)

Returns the value of attribute blob_path


12
13
14
# File 'lib/gitlab/search/found_blob.rb', line 12

def blob_path
  @blob_path
end

#content_matchObject (readonly)

Returns the value of attribute content_match


12
13
14
# File 'lib/gitlab/search/found_blob.rb', line 12

def content_match
  @content_match
end

#projectObject (readonly)

Returns the value of attribute project


12
13
14
# File 'lib/gitlab/search/found_blob.rb', line 12

def project
  @project
end

Class Method Details

.preload_blobs(blobs) ⇒ Object


17
18
19
20
21
# File 'lib/gitlab/search/found_blob.rb', line 17

def self.preload_blobs(blobs)
  to_fetch = blobs.select { |blob| blob.is_a?(self) && blob.blob_path }

  to_fetch.each { |blob| blob.fetch_blob }
end

Instance Method Details

#basenameObject


66
67
68
# File 'lib/gitlab/search/found_blob.rb', line 66

def basename
  @basename ||= encode_utf8(@binary_basename || parsed_content[:binary_basename])
end

#binary_pathObject

binary_path is used for running filters on all matches. For grepped results (which use content_match), we get the path from the beginning of the grepped result which is faster than parsing the whole snippet


58
59
60
# File 'lib/gitlab/search/found_blob.rb', line 58

def binary_path
  @binary_path ||= content_match ? search_result_path : parsed_content[:binary_path]
end

#dataObject


70
71
72
# File 'lib/gitlab/search/found_blob.rb', line 70

def data
  @data ||= encode_utf8(@binary_data || parsed_content[:binary_data])
end

#fetch_blobObject


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gitlab/search/found_blob.rb', line 82

def fetch_blob
  path = [ref, blob_path]
  missing_blob = { binary_path: blob_path }

  BatchLoader.for(path).batch(default_value: missing_blob) do |refs, loader|
    Gitlab::Git::Blob.batch(repository, refs, blob_size_limit: 1024).each do |blob|
      # if the blob couldn't be fetched for some reason,
      # show at least the blob path
      data = {
        id: blob.id,
        binary_path: blob.path,
        binary_basename: path_without_extension(blob.path),
        ref: ref,
        startline: 1,
        binary_data: blob.data,
        project: project
      }

      loader.call([ref, blob.path], data)
    end
  end
end

#idObject


42
43
44
# File 'lib/gitlab/search/found_blob.rb', line 42

def id
  @id ||= parsed_content[:id]
end

#pathObject


62
63
64
# File 'lib/gitlab/search/found_blob.rb', line 62

def path
  @path ||= encode_utf8(@binary_path || parsed_content[:binary_path])
end

#presentObject


78
79
80
# File 'lib/gitlab/search/found_blob.rb', line 78

def present
  super(presenter_class: BlobPresenter)
end

#project_idObject


74
75
76
# File 'lib/gitlab/search/found_blob.rb', line 74

def project_id
  @project_id || @project&.id
end

#refObject


46
47
48
# File 'lib/gitlab/search/found_blob.rb', line 46

def ref
  @ref ||= parsed_content[:ref]
end

#startlineObject


50
51
52
# File 'lib/gitlab/search/found_blob.rb', line 50

def startline
  @startline ||= parsed_content[:startline]
end