Backport FileFinder from EE

This commit is contained in:
Valery Sizov 2017-05-12 10:16:33 +03:00
parent 180ec7113e
commit f6c4ccd1f2
4 changed files with 55 additions and 18 deletions

32
lib/gitlab/file_finder.rb Normal file
View 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

View File

@ -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

View 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

View File

@ -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