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/
CONTENT_REGEXP =
/^(?<ref>[^:]*):(?<path>[^\x00]*)\x00(?<startline>\d+)\x00/

Constants included from EncodingHelper

EncodingHelper::BOM_UTF8, EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, EncodingHelper::ESCAPED_CHARS, EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BlobActiveModel

#to_ability_name

Methods included from BlobLanguageFromGitAttributes

#language_from_gitattributes

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_encoding, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8, #encode_utf8_no_detect, #encode_utf8_with_escaping!, #encode_utf8_with_replacement_character, #force_encode_utf8, #strip_bom, #unquote_path

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
41
42
43
44
# 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)
  @highlight_line = opts.fetch(:highlight_line, nil)
  @binary_data = opts.fetch(:data, nil)
  @per_page = opts.fetch(:per_page, 20)
  @project = opts.fetch(:project, nil)
  @group = opts.fetch(:group, 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)
  @group_id = opts.fetch(:group_id, nil)
  @content_match = opts.fetch(:content_match, nil)
  @blob_path = opts.fetch(:blob_path, nil)
  @repository = opts.fetch(:repository, nil)
  @group_level_blob = opts.fetch(:group_level_blob, false)
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

#groupObject (readonly)

Returns the value of attribute group.



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

def group
  @group
end

#group_level_blobObject (readonly)

Returns the value of attribute group_level_blob.



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

def group_level_blob
  @group_level_blob
end

#highlight_lineObject (readonly)

Returns the value of attribute highlight_line.



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

def highlight_line
  @highlight_line
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



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

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

#binary?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/gitlab/search/found_blob.rb', line 86

def binary?
  false
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



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

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

#dataObject



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

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

#fetch_blobObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gitlab/search/found_blob.rb', line 90

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



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

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

#pathObject



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

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

#presentObject



82
83
84
# File 'lib/gitlab/search/found_blob.rb', line 82

def present
  super(presenter_class: BlobPresenter)
end

#project_idObject



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

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

#refObject



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

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

#startlineObject



54
55
56
# File 'lib/gitlab/search/found_blob.rb', line 54

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