debian-mirror-gitlab/lib/gitlab/file_finder.rb

76 lines
2.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
# This class finds files in a repository by name and content
# the result is joined and sorted by file name
module Gitlab
class FileFinder
2019-01-03 12:48:30 +05:30
BATCH_SIZE = 100
2017-09-10 17:25:29 +05:30
attr_reader :project, :ref
2018-03-17 18:26:18 +05:30
delegate :repository, to: :project
2017-09-10 17:25:29 +05:30
def initialize(project, ref)
@project = project
@ref = ref
end
def find(query)
2019-01-03 12:48:30 +05:30
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 }
2018-11-08 19:23:39 +05:30
end
2019-01-03 12:48:30 +05:30
by_content = find_by_content(query.term)
already_found = Set.new(by_content.map(&:filename))
by_filename = find_by_filename(query.term, except: already_found)
2017-09-10 17:25:29 +05:30
2019-01-03 12:48:30 +05:30
files = (by_content + by_filename)
.sort_by(&:filename)
2018-11-08 19:23:39 +05:30
2019-01-03 12:48:30 +05:30
query.filter_results(files).map { |blob| [blob.filename, blob] }
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
private
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
def find_by_content(query)
2019-01-03 12:48:30 +05:30
results = repository.search_files_by_content(query, ref).first(BATCH_SIZE)
results.map { |result| Gitlab::ProjectSearchResults.parse_search_result(result, project) }
2017-09-10 17:25:29 +05:30
end
2018-11-08 19:23:39 +05:30
2019-01-03 12:48:30 +05:30
def find_by_filename(query, except: [])
filenames = search_filenames(query, except)
blobs(filenames).map do |blob|
Gitlab::SearchResults::FoundBlob.new(
id: blob.id,
filename: blob.path,
basename: File.basename(blob.path, File.extname(blob.path)),
ref: ref,
startline: 1,
data: blob.data,
project: project
)
2018-12-23 12:14:25 +05:30
end
2018-11-08 19:23:39 +05:30
end
2019-01-03 12:48:30 +05:30
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)
2018-11-08 19:23:39 +05:30
end
2017-09-10 17:25:29 +05:30
end
end