2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-12 03:16:33 -04:00
|
|
|
# This class finds files in a repository by name and content
|
|
|
|
# the result is joined and sorted by file name
|
|
|
|
module Gitlab
|
|
|
|
class FileFinder
|
|
|
|
attr_reader :project, :ref
|
|
|
|
|
2017-08-24 09:46:08 -04:00
|
|
|
delegate :repository, to: :project
|
|
|
|
|
2017-05-12 03:16:33 -04:00
|
|
|
def initialize(project, ref)
|
|
|
|
@project = project
|
|
|
|
@ref = ref
|
|
|
|
end
|
|
|
|
|
2020-02-16 16:08:53 -05:00
|
|
|
def find(query, content_match_cutoff: nil)
|
2018-12-02 16:47:33 -05:00
|
|
|
query = Gitlab::Search::Query.new(query, encode_binary: true) do
|
2019-10-23 20:07:18 -04:00
|
|
|
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 }
|
2018-06-06 20:14:10 -04:00
|
|
|
end
|
|
|
|
|
2020-02-16 16:08:53 -05:00
|
|
|
content_match_cutoff = nil if query.filters.any?
|
|
|
|
files = find_by_path(query.term) + find_by_content(query.term, { limit: content_match_cutoff })
|
2018-06-06 20:14:10 -04:00
|
|
|
|
2018-12-02 16:47:33 -05:00
|
|
|
files = query.filter_results(files) if query.filters.any?
|
2017-05-12 03:16:33 -04:00
|
|
|
|
2018-12-02 16:47:33 -05:00
|
|
|
files
|
2017-08-24 09:46:08 -04:00
|
|
|
end
|
2017-05-12 03:16:33 -04:00
|
|
|
|
2017-08-24 09:46:08 -04:00
|
|
|
private
|
2017-05-12 03:16:33 -04:00
|
|
|
|
2020-02-16 16:08:53 -05:00
|
|
|
def find_by_content(query, options)
|
|
|
|
repository.search_files_by_content(query, ref, options).map do |result|
|
2018-12-02 16:47:33 -05:00
|
|
|
Gitlab::Search::FoundBlob.new(content_match: result, project: project, ref: ref, repository: repository)
|
2017-08-24 09:46:08 -04:00
|
|
|
end
|
2017-05-12 03:16:33 -04:00
|
|
|
end
|
2018-06-04 07:41:37 -04:00
|
|
|
|
2019-10-23 20:07:18 -04:00
|
|
|
def find_by_path(query)
|
|
|
|
search_paths(query).map do |path|
|
2020-01-03 07:07:59 -05:00
|
|
|
Gitlab::Search::FoundBlob.new(blob_path: path, path: path, project: project, ref: ref, repository: repository)
|
2018-12-02 16:47:33 -05:00
|
|
|
end
|
2018-06-04 07:41:37 -04:00
|
|
|
end
|
|
|
|
|
2020-07-08 02:09:13 -04:00
|
|
|
# Overridden in Gitlab::WikiFileFinder
|
2019-10-23 20:07:18 -04:00
|
|
|
def search_paths(query)
|
2018-12-02 16:47:33 -05:00
|
|
|
repository.search_files_by_name(query, ref)
|
2018-06-04 07:41:37 -04:00
|
|
|
end
|
2017-05-12 03:16:33 -04:00
|
|
|
end
|
|
|
|
end
|