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
|
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def find(query)
|
2018-06-06 20:14:10 -04:00
|
|
|
query = Gitlab::Search::Query.new(query) do
|
|
|
|
filter :filename, matcher: ->(filter, blob) { blob.filename =~ /#{filter[:regex_value]}$/i }
|
|
|
|
filter :path, matcher: ->(filter, blob) { blob.filename =~ /#{filter[:regex_value]}/i }
|
|
|
|
filter :extension, matcher: ->(filter, blob) { blob.filename =~ /\.#{filter[:regex_value]}$/i }
|
|
|
|
end
|
|
|
|
|
|
|
|
by_content = find_by_content(query.term)
|
2017-05-12 03:16:33 -04:00
|
|
|
|
2017-08-24 09:46:08 -04:00
|
|
|
already_found = Set.new(by_content.map(&:filename))
|
2018-06-06 20:14:10 -04:00
|
|
|
by_filename = find_by_filename(query.term, except: already_found)
|
|
|
|
|
|
|
|
files = (by_content + by_filename)
|
|
|
|
.sort_by(&:filename)
|
2017-05-12 03:16:33 -04:00
|
|
|
|
2018-06-06 20:14:10 -04:00
|
|
|
query.filter_results(files).map { |blob| [blob.filename, blob] }
|
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
|
|
|
|
2017-08-24 09:46:08 -04:00
|
|
|
def find_by_content(query)
|
|
|
|
results = repository.search_files_by_content(query, ref).first(BATCH_SIZE)
|
2018-02-13 07:41:35 -05:00
|
|
|
results.map { |result| Gitlab::ProjectSearchResults.parse_search_result(result, project) }
|
2017-08-24 09:46:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_filename(query, except: [])
|
2018-06-04 07:41:37 -04:00
|
|
|
filenames = search_filenames(query, except)
|
2017-08-24 09:46:08 -04:00
|
|
|
|
2018-06-04 07:41:37 -04:00
|
|
|
blobs(filenames).map do |blob|
|
2017-08-24 09:46:08 -04:00
|
|
|
Gitlab::SearchResults::FoundBlob.new(
|
|
|
|
id: blob.id,
|
|
|
|
filename: blob.path,
|
2018-06-04 07:41:37 -04:00
|
|
|
basename: File.basename(blob.path, File.extname(blob.path)),
|
2017-08-24 09:46:08 -04:00
|
|
|
ref: ref,
|
|
|
|
startline: 1,
|
2018-02-13 07:41:35 -05:00
|
|
|
data: blob.data,
|
|
|
|
project: project
|
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
|
|
|
|
|
|
|
def search_filenames(query, except)
|
|
|
|
filenames = repository.search_files_by_name(query, ref).first(BATCH_SIZE)
|
|
|
|
|
|
|
|
filenames.delete_if { |filename| except.include?(filename) } unless except.empty?
|
|
|
|
|
|
|
|
filenames
|
|
|
|
end
|
|
|
|
|
|
|
|
def blob_refs(filenames)
|
|
|
|
filenames.map { |filename| [ref, filename] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def blobs(filenames)
|
|
|
|
Gitlab::Git::Blob.batch(repository, blob_refs(filenames), blob_size_limit: 1024)
|
|
|
|
end
|
2017-05-12 03:16:33 -04:00
|
|
|
end
|
|
|
|
end
|