summaryrefslogtreecommitdiff
path: root/lib/gitlab/file_finder.rb
blob: 093d9ed809266112a7311030f43865056b5b34ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

    def initialize(project, ref)
      @project = project
      @ref = ref
    end

    def find(query)
      blobs = project.repository.search_files_by_content(query, ref).first(BATCH_SIZE)
      found_file_names = Set.new

      results = blobs.map do |blob|
        blob = Gitlab::ProjectSearchResults.parse_search_result(blob)
        found_file_names << blob.filename

        [blob.filename, blob]
      end

      project.repository.search_files_by_name(query, ref).first(BATCH_SIZE).each do |filename|
        results << [filename, OpenStruct.new(ref: ref)] unless found_file_names.include?(filename)
      end

      results.sort_by(&:first)
    end
  end
end