Commit e012427c authored by Sean McGivern's avatar Sean McGivern

Merge branch 'backport_file_filder_from_ee' into 'master'

Backport FileFinder from EE

Closes #32188

See merge request !11319
parents 9bef3f6d f6c4ccd1
# 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
...@@ -84,23 +84,7 @@ module Gitlab ...@@ -84,23 +84,7 @@ module Gitlab
def blobs def blobs
return [] unless Ability.allowed?(@current_user, :download_code, @project) return [] unless Ability.allowed?(@current_user, :download_code, @project)
@blobs ||= begin @blobs ||= Gitlab::FileFinder.new(project, repository_ref).find(query)
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
end end
def wiki_blobs def wiki_blobs
......
require 'spec_helper'
describe Gitlab::FileFinder, lib: true do
describe '#find' do
let(:project) { create(:project, :public, :repository) }
let(:finder) { described_class.new(project, project.default_branch) }
it 'finds by name' do
results = finder.find('files')
expect(results.map(&:first)).to include('files/images/wm.svg')
end
it 'finds by content' do
results = finder.find('files')
blob = results.select { |result| result.first == "CHANGELOG" }.flatten.last
expect(blob.filename).to eq("CHANGELOG")
end
end
end
...@@ -55,7 +55,7 @@ describe Gitlab::ProjectSearchResults, lib: true do ...@@ -55,7 +55,7 @@ describe Gitlab::ProjectSearchResults, lib: true do
end end
it 'finds by name' do it 'finds by name' do
expect(results).to include(["files/images/wm.svg", nil]) expect(results.map(&:first)).to include('files/images/wm.svg')
end end
it 'finds by content' do it 'finds by content' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment