Merge branch 'backport_file_filder_from_ee' into 'master'
Backport FileFinder from EE Closes #32188 See merge request !11319
This commit is contained in:
commit
e012427cf3
4 changed files with 55 additions and 18 deletions
32
lib/gitlab/file_finder.rb
Normal file
32
lib/gitlab/file_finder.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
21
spec/lib/gitlab/file_finder_spec.rb
Normal file
21
spec/lib/gitlab/file_finder_spec.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
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
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
it 'finds by content' do
|
||||
|
|
Loading…
Reference in a new issue