Commit 4a2cc381 authored by Sean McGivern's avatar Sean McGivern

Merge branch '36939-fix-find-blobs-by-path' into 'master'

Fix searching for files by path

Closes #36939

See merge request !13798
parents 48bf8a2e fa2915ec
---
title: Fix searching for files by path
merge_request: 13798
author:
type: fixed
...@@ -6,27 +6,48 @@ module Gitlab ...@@ -6,27 +6,48 @@ module Gitlab
attr_reader :project, :ref attr_reader :project, :ref
delegate :repository, to: :project
def initialize(project, ref) def initialize(project, ref)
@project = project @project = project
@ref = ref @ref = ref
end end
def find(query) def find(query)
blobs = project.repository.search_files_by_content(query, ref).first(BATCH_SIZE) by_content = find_by_content(query)
found_file_names = Set.new
results = blobs.map do |blob| already_found = Set.new(by_content.map(&:filename))
blob = Gitlab::ProjectSearchResults.parse_search_result(blob) by_filename = find_by_filename(query, except: already_found)
found_file_names << blob.filename
[blob.filename, blob] (by_content + by_filename)
.sort_by(&:filename)
.map { |blob| [blob.filename, blob] }
end end
project.repository.search_files_by_name(query, ref).first(BATCH_SIZE).each do |filename| private
results << [filename, OpenStruct.new(ref: ref)] unless found_file_names.include?(filename)
def find_by_content(query)
results = repository.search_files_by_content(query, ref).first(BATCH_SIZE)
results.map { |result| Gitlab::ProjectSearchResults.parse_search_result(result) }
end end
results.sort_by(&:first) def find_by_filename(query, except: [])
filenames = repository.search_files_by_name(query, ref).first(BATCH_SIZE)
filenames.delete_if { |filename| except.include?(filename) } unless except.empty?
blob_refs = filenames.map { |filename| [ref, filename] }
blobs = Gitlab::Git::Blob.batch(repository, blob_refs, blob_size_limit: 1024)
blobs.map do |blob|
Gitlab::SearchResults::FoundBlob.new(
id: blob.id,
filename: blob.path,
basename: File.basename(blob.path),
ref: ref,
startline: 1,
data: blob.data
)
end
end end
end end
end end
...@@ -7,15 +7,23 @@ describe Gitlab::FileFinder do ...@@ -7,15 +7,23 @@ describe Gitlab::FileFinder do
it 'finds by name' do it 'finds by name' do
results = finder.find('files') results = finder.find('files')
expect(results.map(&:first)).to include('files/images/wm.svg')
filename, blob = results.find { |_, blob| blob.filename == 'files/images/wm.svg' }
expect(filename).to eq('files/images/wm.svg')
expect(blob).to be_a(Gitlab::SearchResults::FoundBlob)
expect(blob.ref).to eq(finder.ref)
expect(blob.data).not_to be_empty
end end
it 'finds by content' do it 'finds by content' do
results = finder.find('files') results = finder.find('files')
blob = results.select { |result| result.first == "CHANGELOG" }.flatten.last filename, blob = results.find { |_, blob| blob.filename == 'CHANGELOG' }
expect(blob.filename).to eq("CHANGELOG") expect(filename).to eq('CHANGELOG')
expect(blob).to be_a(Gitlab::SearchResults::FoundBlob)
expect(blob.ref).to eq(finder.ref)
expect(blob.data).not_to be_empty
end end
end end
end end
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