Merge branch '36571-ignore-root-in-repo' into 'master'

Resolve "Error while searching for text starting with slash "/""

Closes #36571

See merge request gitlab-org/gitlab-ce!16455
This commit is contained in:
Sean McGivern 2018-01-16 11:03:34 +00:00
commit 480c01a974
3 changed files with 21 additions and 2 deletions

View File

@ -938,9 +938,11 @@ class Repository
end
def search_files_by_name(query, ref)
return [] if empty? || query.blank?
safe_query = Regexp.escape(query.sub(/^\/*/, ""))
args = %W(ls-tree --full-tree -r #{ref || root_ref} --name-status | #{Regexp.escape(query)})
return [] if empty? || safe_query.blank?
args = %W(ls-tree --full-tree -r #{ref || root_ref} --name-status | #{safe_query})
run_git(args).first.lines.map(&:strip)
end

View File

@ -0,0 +1,5 @@
---
title: Ignore leading slashes when searching for files within context of repository.
merge_request:
author: Andrew McCallum
type: fixed

View File

@ -668,6 +668,18 @@ describe Repository do
expect(results.first).to eq('files/html/500.html')
end
it 'ignores leading slashes' do
results = repository.search_files_by_name('/files', 'master')
expect(results.first).to eq('files/html/500.html')
end
it 'properly handles when query is only slashes' do
results = repository.search_files_by_name('//', 'master')
expect(results).to match_array([])
end
it 'properly handles when query is not present' do
results = repository.search_files_by_name('', 'master')