Class: Gitlab::Search::FoundBlob
- Inherits:
-
Object
- Object
- Gitlab::Search::FoundBlob
- 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
-
#blob_path ⇒ Object
readonly
Returns the value of attribute blob_path.
-
#content_match ⇒ Object
readonly
Returns the value of attribute content_match.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#group_level_blob ⇒ Object
readonly
Returns the value of attribute group_level_blob.
-
#highlight_line ⇒ Object
readonly
Returns the value of attribute highlight_line.
-
#matched_lines_count ⇒ Object
readonly
Returns the value of attribute matched_lines_count.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Class Method Summary collapse
Instance Method Summary collapse
- #basename ⇒ Object
- #binary? ⇒ Boolean
-
#binary_path ⇒ Object
binary_path is used for running filters on all matches.
- #data ⇒ Object
- #fetch_blob ⇒ Object
- #id ⇒ Object
-
#initialize(opts = {}) ⇒ FoundBlob
constructor
A new instance of FoundBlob.
- #path ⇒ Object
- #present ⇒ Object
- #project_id ⇒ Object
- #ref ⇒ Object
- #startline ⇒ Object
Methods included from BlobActiveModel
Methods included from BlobLanguageFromGitAttributes
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, #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 45 |
# 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) @matched_lines_count = opts.fetch(:matched_lines_count, 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_path ⇒ Object (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_match ⇒ Object (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 |
#group ⇒ Object (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_blob ⇒ Object (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_line ⇒ Object (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 |
#matched_lines_count ⇒ Object (readonly)
Returns the value of attribute matched_lines_count.
12 13 14 |
# File 'lib/gitlab/search/found_blob.rb', line 12 def matched_lines_count @matched_lines_count end |
#project ⇒ Object (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
#basename ⇒ Object
71 72 73 |
# File 'lib/gitlab/search/found_blob.rb', line 71 def basename @basename ||= encode_utf8(@binary_basename || parsed_content[:binary_basename]) end |
#binary? ⇒ Boolean
87 88 89 |
# File 'lib/gitlab/search/found_blob.rb', line 87 def binary? false end |
#binary_path ⇒ Object
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
63 64 65 |
# File 'lib/gitlab/search/found_blob.rb', line 63 def binary_path @binary_path ||= content_match ? search_result_path : parsed_content[:binary_path] end |
#data ⇒ Object
75 76 77 |
# File 'lib/gitlab/search/found_blob.rb', line 75 def data @data ||= encode_utf8(@binary_data || parsed_content[:binary_data]) end |
#fetch_blob ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gitlab/search/found_blob.rb', line 91 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 |
#id ⇒ Object
47 48 49 |
# File 'lib/gitlab/search/found_blob.rb', line 47 def id @id ||= parsed_content[:id] end |
#path ⇒ Object
67 68 69 |
# File 'lib/gitlab/search/found_blob.rb', line 67 def path @path ||= encode_utf8(@binary_path || parsed_content[:binary_path]) end |
#present ⇒ Object
83 84 85 |
# File 'lib/gitlab/search/found_blob.rb', line 83 def present super(presenter_class: BlobPresenter) end |
#project_id ⇒ Object
79 80 81 |
# File 'lib/gitlab/search/found_blob.rb', line 79 def project_id @project_id || @project&.id end |
#ref ⇒ Object
51 52 53 |
# File 'lib/gitlab/search/found_blob.rb', line 51 def ref @ref ||= parsed_content[:ref] end |
#startline ⇒ Object
55 56 57 |
# File 'lib/gitlab/search/found_blob.rb', line 55 def startline @startline ||= parsed_content[:startline] end |