Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
9fb4d3ac
Commit
9fb4d3ac
authored
Feb 01, 2021
by
Changzheng Liu
Committed by
Matthias Käppler
Feb 01, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve search filter by taking space in file path into account
parent
d3f092da
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
3 deletions
+46
-3
changelogs/unreleased/281677_space_in_filename.yml
changelogs/unreleased/281677_space_in_filename.yml
+5
-0
lib/gitlab/search/query.rb
lib/gitlab/search/query.rb
+15
-3
spec/lib/gitlab/file_finder_spec.rb
spec/lib/gitlab/file_finder_spec.rb
+8
-0
spec/lib/gitlab/search/query_spec.rb
spec/lib/gitlab/search/query_spec.rb
+18
-0
No files found.
changelogs/unreleased/281677_space_in_filename.yml
0 → 100644
View file @
9fb4d3ac
---
title
:
Improve search filter by taking space in file path into account
merge_request
:
52392
author
:
type
:
fixed
lib/gitlab/search/query.rb
View file @
9fb4d3ac
...
...
@@ -5,6 +5,9 @@ module Gitlab
class
Query
<
SimpleDelegator
include
EncodingHelper
QUOTES_REGEXP
=
%r{
\A
"|"
\Z
}
.
freeze
TOKEN_WITH_QUOTES_REGEXP
=
%r{
\s
(?=(?:[^"]|"[^"]*")*$)}
.
freeze
def
initialize
(
query
,
filter_opts
=
{},
&
block
)
@raw_query
=
query
.
dup
@filters
=
[]
...
...
@@ -35,22 +38,24 @@ module Gitlab
def
extract_filters
fragments
=
[]
query_tokens
=
parse_raw_query
filters
=
@filters
.
each_with_object
([])
do
|
filter
,
parsed_filters
|
match
=
@raw_query
.
split
.
find
{
|
part
|
part
=~
/\A-?
#{
filter
[
:name
]
}
:/
}
match
=
query_tokens
.
find
{
|
part
|
part
=~
/\A-?
#{
filter
[
:name
]
}
:/
}
next
unless
match
input
=
match
.
split
(
':'
)[
1
..-
1
].
join
next
if
input
.
empty?
filter
[
:negated
]
=
match
.
start_with?
(
"-"
)
filter
[
:value
]
=
parse_filter
(
filter
,
input
)
filter
[
:value
]
=
parse_filter
(
filter
,
input
.
gsub
(
QUOTES_REGEXP
,
''
)
)
filter
[
:regex_value
]
=
Regexp
.
escape
(
filter
[
:value
]).
gsub
(
'\*'
,
'.*?'
)
fragments
<<
match
parsed_filters
<<
filter
end
query
=
(
@raw_query
.
split
-
fragments
).
join
(
' '
)
query
=
(
query_tokens
-
fragments
).
join
(
' '
)
query
=
'*'
if
query
.
empty?
[
query
,
filters
]
...
...
@@ -61,6 +66,13 @@ module Gitlab
@filter_options
[
:encode_binary
]
?
encode_binary
(
result
)
:
result
end
def
parse_raw_query
# Positive lookahead for any non-quote char or even number of quotes
# for example '"search term" path:"foo bar.txt"' would break into
# ["search term", "path:\"foo bar.txt\""]
@raw_query
.
split
(
TOKEN_WITH_QUOTES_REGEXP
).
reject
(
&
:empty?
)
end
end
end
end
spec/lib/gitlab/file_finder_spec.rb
View file @
9fb4d3ac
...
...
@@ -53,6 +53,14 @@ RSpec.describe Gitlab::FileFinder do
end
end
context
'with white space in the path'
do
it
'filters by path correctly'
do
results
=
subject
.
find
(
'directory path:"with space/README.md"'
)
expect
(
results
.
count
).
to
eq
(
1
)
end
end
it
'does not cause N+1 query'
do
expect
(
Gitlab
::
GitalyClient
).
to
receive
(
:call
).
at_most
(
10
).
times
.
and_call_original
...
...
spec/lib/gitlab/search/query_spec.rb
View file @
9fb4d3ac
...
...
@@ -46,4 +46,22 @@ RSpec.describe Gitlab::Search::Query do
expect
(
subject
.
filters
).
to
all
(
include
(
negated:
true
))
end
end
context
'with filter value in quotes'
do
let
(
:query
)
{
'"foo bar" name:"my test script.txt"'
}
it
'does not break the filter value in quotes'
do
expect
(
subject
.
term
).
to
eq
(
'"foo bar"'
)
expect
(
subject
.
filters
[
0
]).
to
include
(
name: :name
,
negated:
false
,
value:
"MY TEST SCRIPT.TXT"
)
end
end
context
'with extra white spaces between the query words'
do
let
(
:query
)
{
' foo = bar name:"my test.txt"'
}
it
'removes the extra whitespace between tokens'
do
expect
(
subject
.
term
).
to
eq
(
'foo = bar'
)
expect
(
subject
.
filters
[
0
]).
to
include
(
name: :name
,
negated:
false
,
value:
"MY TEST.TXT"
)
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment