diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2018-01-16 11:03:34 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2018-01-16 11:03:34 +0000 |
commit | 480c01a9749ad1aa4ab033ae9e2270ab597c2cce (patch) | |
tree | 3562c2d5f60026f36b2accaf40dc11439b0d4227 | |
parent | 644f91552c2db87065796d3848dccc01ad939eb9 (diff) | |
parent | b1cf3225dbb6b897a8be405d599714b74cbfb547 (diff) | |
download | gitlab-ce-480c01a9749ad1aa4ab033ae9e2270ab597c2cce.tar.gz |
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
-rw-r--r-- | app/models/repository.rb | 6 | ||||
-rw-r--r-- | changelogs/unreleased/36571-ignore-root-in-repo.yml | 5 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 12 |
3 files changed, 21 insertions, 2 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 2ffd9558ebc..a67bb7294e6 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -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(/^\/*/, "")) + + 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) end diff --git a/changelogs/unreleased/36571-ignore-root-in-repo.yml b/changelogs/unreleased/36571-ignore-root-in-repo.yml new file mode 100644 index 00000000000..396e82be51b --- /dev/null +++ b/changelogs/unreleased/36571-ignore-root-in-repo.yml @@ -0,0 +1,5 @@ +--- +title: Ignore leading slashes when searching for files within context of repository. +merge_request: +author: Andrew McCallum +type: fixed diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index f3456e5b354..d9395ca61d7 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -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') |