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
|
|
|
|
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)
|
2018-12-23 12:14:25 +05:30
|
|
|
query = Gitlab::Search::Query.new(query, encode_binary: true) do
|
|
|
|
filter :filename, matcher: ->(filter, blob) { blob.binary_filename =~ /#{filter[:regex_value]}$/i }
|
|
|
|
filter :path, matcher: ->(filter, blob) { blob.binary_filename =~ /#{filter[:regex_value]}/i }
|
|
|
|
filter :extension, matcher: ->(filter, blob) { blob.binary_filename =~ /\.#{filter[:regex_value]}$/i }
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
files = find_by_filename(query.term) + find_by_content(query.term)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
files = query.filter_results(files) if query.filters.any?
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
files
|
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)
|
2018-12-23 12:14:25 +05:30
|
|
|
repository.search_files_by_content(query, ref).map do |result|
|
|
|
|
Gitlab::Search::FoundBlob.new(content_match: result, project: project, ref: ref, repository: repository)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
def find_by_filename(query)
|
|
|
|
search_filenames(query).map do |filename|
|
|
|
|
Gitlab::Search::FoundBlob.new(blob_filename: filename, project: project, ref: ref, repository: repository)
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
def search_filenames(query)
|
|
|
|
repository.search_files_by_name(query, ref)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|