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
Jérome Perrin
gitlab-ce
Commits
fa2915ec
Commit
fa2915ec
authored
Aug 24, 2017
by
Nick Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix searching for files by path
parent
eae604e6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
14 deletions
+48
-14
changelogs/unreleased/36939-fix-find-blobs-by-path.yml
changelogs/unreleased/36939-fix-find-blobs-by-path.yml
+5
-0
lib/gitlab/file_finder.rb
lib/gitlab/file_finder.rb
+32
-11
spec/lib/gitlab/file_finder_spec.rb
spec/lib/gitlab/file_finder_spec.rb
+11
-3
No files found.
changelogs/unreleased/36939-fix-find-blobs-by-path.yml
0 → 100644
View file @
fa2915ec
---
title
:
Fix searching for files by path
merge_request
:
13798
author
:
type
:
fixed
lib/gitlab/file_finder.rb
View file @
fa2915ec
...
@@ -6,27 +6,48 @@ module Gitlab
...
@@ -6,27 +6,48 @@ module Gitlab
attr_reader
:project
,
:ref
attr_reader
:project
,
:ref
delegate
:repository
,
to: :project
def
initialize
(
project
,
ref
)
def
initialize
(
project
,
ref
)
@project
=
project
@project
=
project
@ref
=
ref
@ref
=
ref
end
end
def
find
(
query
)
def
find
(
query
)
blobs
=
project
.
repository
.
search_files_by_content
(
query
,
ref
).
first
(
BATCH_SIZE
)
by_content
=
find_by_content
(
query
)
found_file_names
=
Set
.
new
results
=
blobs
.
map
do
|
blob
|
already_found
=
Set
.
new
(
by_content
.
map
(
&
:filename
))
blob
=
Gitlab
::
ProjectSearchResults
.
parse_search_result
(
blob
)
by_filename
=
find_by_filename
(
query
,
except:
already_found
)
found_file_names
<<
blob
.
filename
[
blob
.
filename
,
blob
]
(
by_content
+
by_filename
)
end
.
sort_by
(
&
:filename
)
.
map
{
|
blob
|
[
blob
.
filename
,
blob
]
}
end
project
.
repository
.
search_files_by_name
(
query
,
ref
).
first
(
BATCH_SIZE
).
each
do
|
filename
|
private
results
<<
[
filename
,
OpenStruct
.
new
(
ref:
ref
)]
unless
found_file_names
.
include?
(
filename
)
end
results
.
sort_by
(
&
:first
)
def
find_by_content
(
query
)
results
=
repository
.
search_files_by_content
(
query
,
ref
).
first
(
BATCH_SIZE
)
results
.
map
{
|
result
|
Gitlab
::
ProjectSearchResults
.
parse_search_result
(
result
)
}
end
def
find_by_filename
(
query
,
except:
[])
filenames
=
repository
.
search_files_by_name
(
query
,
ref
).
first
(
BATCH_SIZE
)
filenames
.
delete_if
{
|
filename
|
except
.
include?
(
filename
)
}
unless
except
.
empty?
blob_refs
=
filenames
.
map
{
|
filename
|
[
ref
,
filename
]
}
blobs
=
Gitlab
::
Git
::
Blob
.
batch
(
repository
,
blob_refs
,
blob_size_limit:
1024
)
blobs
.
map
do
|
blob
|
Gitlab
::
SearchResults
::
FoundBlob
.
new
(
id:
blob
.
id
,
filename:
blob
.
path
,
basename:
File
.
basename
(
blob
.
path
),
ref:
ref
,
startline:
1
,
data:
blob
.
data
)
end
end
end
end
end
end
end
spec/lib/gitlab/file_finder_spec.rb
View file @
fa2915ec
...
@@ -7,15 +7,23 @@ describe Gitlab::FileFinder do
...
@@ -7,15 +7,23 @@ describe Gitlab::FileFinder do
it
'finds by name'
do
it
'finds by name'
do
results
=
finder
.
find
(
'files'
)
results
=
finder
.
find
(
'files'
)
expect
(
results
.
map
(
&
:first
)).
to
include
(
'files/images/wm.svg'
)
filename
,
blob
=
results
.
find
{
|
_
,
blob
|
blob
.
filename
==
'files/images/wm.svg'
}
expect
(
filename
).
to
eq
(
'files/images/wm.svg'
)
expect
(
blob
).
to
be_a
(
Gitlab
::
SearchResults
::
FoundBlob
)
expect
(
blob
.
ref
).
to
eq
(
finder
.
ref
)
expect
(
blob
.
data
).
not_to
be_empty
end
end
it
'finds by content'
do
it
'finds by content'
do
results
=
finder
.
find
(
'files'
)
results
=
finder
.
find
(
'files'
)
blob
=
results
.
select
{
|
result
|
result
.
first
==
"CHANGELOG"
}.
flatten
.
last
filename
,
blob
=
results
.
find
{
|
_
,
blob
|
blob
.
filename
==
'CHANGELOG'
}
expect
(
blob
.
filename
).
to
eq
(
"CHANGELOG"
)
expect
(
filename
).
to
eq
(
'CHANGELOG'
)
expect
(
blob
).
to
be_a
(
Gitlab
::
SearchResults
::
FoundBlob
)
expect
(
blob
.
ref
).
to
eq
(
finder
.
ref
)
expect
(
blob
.
data
).
not_to
be_empty
end
end
end
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