diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-05-12 14:32:43 +0300 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-05-12 14:32:43 +0300 |
commit | e012427cf32f0fb36aa80c89442e4731d0939e65 (patch) | |
tree | f2ee2ce77303316aad3e603aadbad0c71a57c626 /lib | |
parent | 9bef3f6d3d2a5916248450d5505d0de1e13615df (diff) | |
parent | f6c4ccd1f234e1b5bab8f7ffbd98b3d1092b4873 (diff) |
Merge branch 'backport_file_filder_from_ee' into 'master'
Backport FileFinder from EE
Closes #32188
See merge request !11319
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/file_finder.rb | 32 | ||||
-rw-r--r-- | lib/gitlab/project_search_results.rb | 18 |
2 files changed, 33 insertions, 17 deletions
diff --git a/lib/gitlab/file_finder.rb b/lib/gitlab/file_finder.rb new file mode 100644 index 00000000000..093d9ed8092 --- /dev/null +++ b/lib/gitlab/file_finder.rb @@ -0,0 +1,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 diff --git a/lib/gitlab/project_search_results.rb b/lib/gitlab/project_search_results.rb index 47cfe412715..561aa9e162c 100644 --- a/lib/gitlab/project_search_results.rb +++ b/lib/gitlab/project_search_results.rb @@ -84,23 +84,7 @@ module Gitlab def blobs return [] unless Ability.allowed?(@current_user, :download_code, @project) - @blobs ||= begin - blobs = project.repository.search_files_by_content(query, repository_ref).first(100) - found_file_names = Set.new - - results = blobs.map do |blob| - blob = self.class.parse_search_result(blob) - found_file_names << blob.filename - - [blob.filename, blob] - end - - project.repository.search_files_by_name(query, repository_ref).first(100).each do |filename| - results << [filename, nil] unless found_file_names.include?(filename) - end - - results.sort_by(&:first) - end + @blobs ||= Gitlab::FileFinder.new(project, repository_ref).find(query) end def wiki_blobs |