Class: Gitlab::FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/file_finder.rb

Direct Known Subclasses

WikiFileFinder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, ref) ⇒ FileFinder

Returns a new instance of FileFinder.


11
12
13
14
# File 'lib/gitlab/file_finder.rb', line 11

def initialize(project, ref)
  @project = project
  @ref = ref
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project


7
8
9
# File 'lib/gitlab/file_finder.rb', line 7

def project
  @project
end

#refObject (readonly)

Returns the value of attribute ref


7
8
9
# File 'lib/gitlab/file_finder.rb', line 7

def ref
  @ref
end

Instance Method Details

#find(query, content_match_cutoff: nil) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/file_finder.rb', line 16

def find(query, content_match_cutoff: nil)
  query = Gitlab::Search::Query.new(query, encode_binary: true) do
    filter :filename, matcher: ->(filter, blob) { blob.binary_path =~ /#{filter[:regex_value]}$/i }
    filter :path, matcher: ->(filter, blob) { blob.binary_path =~ /#{filter[:regex_value]}/i }
    filter :extension, matcher: ->(filter, blob) { blob.binary_path =~ /\.#{filter[:regex_value]}$/i }
  end

  content_match_cutoff = nil if query.filters.any?
  files = find_by_path(query.term) + find_by_content(query.term, { limit: content_match_cutoff })

  files = query.filter_results(files) if query.filters.any?

  files
end