gitlab-org--gitlab-foss/lib/gitlab/file_finder.rb

49 lines
1.4 KiB
Ruby
Raw Normal View History

# 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
def find(query)
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 }
end
files = find_by_filename(query.term) + find_by_content(query.term)
files = query.filter_results(files) if query.filters.any?
2017-05-12 03:16:33 -04: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
2017-08-24 09:46:08 -04:00
def find_by_content(query)
repository.search_files_by_content(query, ref).map do |result|
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
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-06-04 07:41:37 -04:00
end
def search_filenames(query)
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