Commit 480c01a9 authored by Sean McGivern's avatar Sean McGivern

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
parents 644f9155 b1cf3225
...@@ -938,9 +938,11 @@ class Repository ...@@ -938,9 +938,11 @@ class Repository
end end
def search_files_by_name(query, ref) def search_files_by_name(query, ref)
return [] if empty? || query.blank? safe_query = Regexp.escape(query.sub(/^\/*/, ""))
return [] if empty? || safe_query.blank?
args = %W(ls-tree --full-tree -r #{ref || root_ref} --name-status | #{Regexp.escape(query)}) args = %W(ls-tree --full-tree -r #{ref || root_ref} --name-status | #{safe_query})
run_git(args).first.lines.map(&:strip) run_git(args).first.lines.map(&:strip)
end end
......
---
title: Ignore leading slashes when searching for files within context of repository.
merge_request:
author: Andrew McCallum
type: fixed
...@@ -668,6 +668,18 @@ describe Repository do ...@@ -668,6 +668,18 @@ describe Repository do
expect(results.first).to eq('files/html/500.html') expect(results.first).to eq('files/html/500.html')
end 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 it 'properly handles when query is not present' do
results = repository.search_files_by_name('', 'master') results = repository.search_files_by_name('', 'master')
......
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