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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
f6c4ccd1
Commit
f6c4ccd1
authored
May 12, 2017
by
Valery Sizov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backport FileFinder from EE
parent
180ec711
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
18 deletions
+55
-18
lib/gitlab/file_finder.rb
lib/gitlab/file_finder.rb
+32
-0
lib/gitlab/project_search_results.rb
lib/gitlab/project_search_results.rb
+1
-17
spec/lib/gitlab/file_finder_spec.rb
spec/lib/gitlab/file_finder_spec.rb
+21
-0
spec/lib/gitlab/project_search_results_spec.rb
spec/lib/gitlab/project_search_results_spec.rb
+1
-1
No files found.
lib/gitlab/file_finder.rb
0 → 100644
View file @
f6c4ccd1
# This class finds files in a repository by name and content
# the result is joined and sorted by file name
module
Gitlab
class
FileFinder
BATCH_SIZE
=
100
attr_reader
:project
,
:ref
def
initialize
(
project
,
ref
)
@project
=
project
@ref
=
ref
end
def
find
(
query
)
blobs
=
project
.
repository
.
search_files_by_content
(
query
,
ref
).
first
(
BATCH_SIZE
)
found_file_names
=
Set
.
new
results
=
blobs
.
map
do
|
blob
|
blob
=
Gitlab
::
ProjectSearchResults
.
parse_search_result
(
blob
)
found_file_names
<<
blob
.
filename
[
blob
.
filename
,
blob
]
end
project
.
repository
.
search_files_by_name
(
query
,
ref
).
first
(
BATCH_SIZE
).
each
do
|
filename
|
results
<<
[
filename
,
OpenStruct
.
new
(
ref:
ref
)]
unless
found_file_names
.
include?
(
filename
)
end
results
.
sort_by
(
&
:first
)
end
end
end
lib/gitlab/project_search_results.rb
View file @
f6c4ccd1
...
@@ -84,23 +84,7 @@ module Gitlab
...
@@ -84,23 +84,7 @@ module Gitlab
def
blobs
def
blobs
return
[]
unless
Ability
.
allowed?
(
@current_user
,
:download_code
,
@project
)
return
[]
unless
Ability
.
allowed?
(
@current_user
,
:download_code
,
@project
)
@blobs
||=
begin
@blobs
||=
Gitlab
::
FileFinder
.
new
(
project
,
repository_ref
).
find
(
query
)
blobs
=
project
.
repository
.
search_files_by_content
(
query
,
repository_ref
).
first
(
100
)
found_file_names
=
Set
.
new
results
=
blobs
.
map
do
|
blob
|
blob
=
self
.
class
.
parse_search_result
(
blob
)
found_file_names
<<
blob
.
filename
[
blob
.
filename
,
blob
]
end
project
.
repository
.
search_files_by_name
(
query
,
repository_ref
).
first
(
100
).
each
do
|
filename
|
results
<<
[
filename
,
nil
]
unless
found_file_names
.
include?
(
filename
)
end
results
.
sort_by
(
&
:first
)
end
end
end
def
wiki_blobs
def
wiki_blobs
...
...
spec/lib/gitlab/file_finder_spec.rb
0 → 100644
View file @
f6c4ccd1
require
'spec_helper'
describe
Gitlab
::
FileFinder
,
lib:
true
do
describe
'#find'
do
let
(
:project
)
{
create
(
:project
,
:public
,
:repository
)
}
let
(
:finder
)
{
described_class
.
new
(
project
,
project
.
default_branch
)
}
it
'finds by name'
do
results
=
finder
.
find
(
'files'
)
expect
(
results
.
map
(
&
:first
)).
to
include
(
'files/images/wm.svg'
)
end
it
'finds by content'
do
results
=
finder
.
find
(
'files'
)
blob
=
results
.
select
{
|
result
|
result
.
first
==
"CHANGELOG"
}.
flatten
.
last
expect
(
blob
.
filename
).
to
eq
(
"CHANGELOG"
)
end
end
end
spec/lib/gitlab/project_search_results_spec.rb
View file @
f6c4ccd1
...
@@ -55,7 +55,7 @@ describe Gitlab::ProjectSearchResults, lib: true do
...
@@ -55,7 +55,7 @@ describe Gitlab::ProjectSearchResults, lib: true do
end
end
it
'finds by name'
do
it
'finds by name'
do
expect
(
results
).
to
include
([
"files/images/wm.svg"
,
nil
]
)
expect
(
results
.
map
(
&
:first
)).
to
include
(
'files/images/wm.svg'
)
end
end
it
'finds by content'
do
it
'finds by content'
do
...
...
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