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
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
attr_reader
:project
,
:ref
delegate
:repository
,
to: :project
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
by_content
=
find_by_content
(
query
)
results
=
blobs
.
map
do
|
blob
|
blob
=
Gitlab
::
ProjectSearchResults
.
parse_search_result
(
blob
)
found_file_names
<<
blob
.
filename
already_found
=
Set
.
new
(
by_content
.
map
(
&
:filename
))
by_filename
=
find_by_filename
(
query
,
except:
already_found
)
[
blob
.
filename
,
blob
]
end
(
by_content
+
by_filename
)
.
sort_by
(
&
:filename
)
.
map
{
|
blob
|
[
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
private
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
spec/lib/gitlab/file_finder_spec.rb
View file @
fa2915ec
...
...
@@ -7,15 +7,23 @@ describe Gitlab::FileFinder do
it
'finds by name'
do
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
it
'finds by content'
do
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
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