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
56e53556
Commit
56e53556
authored
Oct 23, 2017
by
Ahmad Sherif
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate Gitlab::Git::Wiki#file to Gitaly
Closes gitaly#689
parent
01340794
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
157 additions
and
33 deletions
+157
-33
lib/gitlab/git/wiki.rb
lib/gitlab/git/wiki.rb
+22
-5
lib/gitlab/gitaly_client/wiki_file.rb
lib/gitlab/gitaly_client/wiki_file.rb
+17
-0
lib/gitlab/gitaly_client/wiki_service.rb
lib/gitlab/gitaly_client/wiki_service.rb
+26
-0
spec/features/projects/wiki/user_views_wiki_page_spec.rb
spec/features/projects/wiki/user_views_wiki_page_spec.rb
+9
-4
spec/models/project_wiki_spec.rb
spec/models/project_wiki_spec.rb
+23
-24
spec/support/bare_repo_operations.rb
spec/support/bare_repo_operations.rb
+60
-0
No files found.
lib/gitlab/git/wiki.rb
View file @
56e53556
...
...
@@ -68,11 +68,13 @@ module Gitlab
end
def
file
(
name
,
version
)
version
||=
self
.
class
.
default_ref
gollum_file
=
gollum_wiki
.
file
(
name
,
version
)
return
unless
gollum_file
Gitlab
::
Git
::
WikiFile
.
new
(
gollum_file
)
@repository
.
gitaly_migrate
(
:wiki_find_file
)
do
|
is_enabled
|
if
is_enabled
gitaly_find_file
(
name
,
version
)
else
gollum_find_file
(
name
,
version
)
end
end
end
def
page_versions
(
page_path
)
...
...
@@ -156,6 +158,14 @@ module Gitlab
new_page
(
gollum_page
)
end
def
gollum_find_file
(
name
,
version
)
version
||=
self
.
class
.
default_ref
gollum_file
=
gollum_wiki
.
file
(
name
,
version
)
return
unless
gollum_file
Gitlab
::
Git
::
WikiFile
.
new
(
gollum_file
)
end
def
gitaly_write_page
(
name
,
format
,
content
,
commit_details
)
gitaly_wiki_client
.
write_page
(
name
,
format
,
content
,
commit_details
)
end
...
...
@@ -170,6 +180,13 @@ module Gitlab
Gitlab
::
Git
::
WikiPage
.
new
(
wiki_page
,
version
)
end
def
gitaly_find_file
(
name
,
version
)
wiki_file
=
gitaly_wiki_client
.
find_file
(
name
,
version
)
return
unless
wiki_file
Gitlab
::
Git
::
WikiFile
.
new
(
wiki_file
)
end
end
end
end
lib/gitlab/gitaly_client/wiki_file.rb
0 → 100644
View file @
56e53556
module
Gitlab
module
GitalyClient
class
WikiFile
FIELDS
=
%i(name mime_type path raw_data)
.
freeze
attr_accessor
(
*
FIELDS
)
def
initialize
(
params
)
params
=
params
.
with_indifferent_access
FIELDS
.
each
do
|
field
|
instance_variable_set
(
"@
#{
field
}
"
,
params
[
field
])
end
end
end
end
end
lib/gitlab/gitaly_client/wiki_service.rb
View file @
56e53556
...
...
@@ -80,6 +80,32 @@ module Gitlab
[
wiki_page
,
version
]
end
def
find_file
(
name
,
revision
)
request
=
Gitaly
::
WikiFindFileRequest
.
new
(
repository:
@gitaly_repo
,
name:
GitalyClient
.
encode
(
name
),
revision:
GitalyClient
.
encode
(
revision
)
)
response
=
GitalyClient
.
call
(
@repository
.
storage
,
:wiki_service
,
:wiki_find_file
,
request
)
wiki_file
=
nil
response
.
each
do
|
message
|
next
unless
message
.
name
.
present?
if
wiki_file
wiki_file
.
raw_data
<<
message
.
raw_data
else
wiki_file
=
GitalyClient
::
WikiFile
.
new
(
message
.
to_h
)
# All gRPC strings in a response are frozen, so we get
# an unfrozen version here so appending in the else clause below doesn't blow up.
wiki_file
.
raw_data
=
wiki_file
.
raw_data
.
dup
end
end
wiki_file
end
private
def
gitaly_commit_details
(
commit_details
)
...
...
spec/features/projects/wiki/user_views_wiki_page_spec.rb
View file @
56e53556
...
...
@@ -81,10 +81,15 @@ describe 'User views a wiki page' do
end
it
'shows a file stored in a page'
do
file
=
Gollum
::
File
.
new
(
project
.
wiki
)
allow_any_instance_of
(
Gollum
::
Wiki
).
to
receive
(
:file
).
with
(
'image.jpg'
,
'master'
).
and_return
(
file
)
allow_any_instance_of
(
Gollum
::
File
).
to
receive
(
:mime_type
).
and_return
(
'image/jpeg'
)
gollum_file_double
=
double
(
'Gollum::File'
,
mime_type:
'image/jpeg'
,
name:
'images/image.jpg'
,
path:
'images/image.jpg'
,
raw_data:
''
)
wiki_file
=
Gitlab
::
Git
::
WikiFile
.
new
(
gollum_file_double
)
allow
(
wiki_file
).
to
receive
(
:mime_type
).
and_return
(
'image/jpeg'
)
allow_any_instance_of
(
ProjectWiki
).
to
receive
(
:find_file
).
with
(
'image.jpg'
,
nil
).
and_return
(
wiki_file
)
expect
(
page
).
to
have_xpath
(
'//img[@data-src="image.jpg"]'
)
expect
(
page
).
to
have_link
(
'image'
,
href:
"
#{
project
.
wiki
.
wiki_base_path
}
/image.jpg"
)
...
...
spec/models/project_wiki_spec.rb
View file @
56e53556
...
...
@@ -156,27 +156,17 @@ describe ProjectWiki do
end
describe
'#find_file'
do
shared_examples
'finding a wiki file'
do
before
do
file
=
Gollum
::
File
.
new
(
subject
.
wiki
)
allow_any_instance_of
(
Gollum
::
Wiki
)
.
to
receive
(
:file
).
with
(
'image.jpg'
,
'master'
)
.
and_return
(
file
)
allow_any_instance_of
(
Gollum
::
File
)
.
to
receive
(
:mime_type
)
.
and_return
(
'image/jpeg'
)
allow_any_instance_of
(
Gollum
::
Wiki
)
.
to
receive
(
:file
).
with
(
'non-existant'
,
'master'
)
.
and_return
(
nil
)
end
file
=
File
.
open
(
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
'dk.png'
))
subject
.
wiki
# Make sure the wiki repo exists
after
do
allow_any_instance_of
(
Gollum
::
Wiki
).
to
receive
(
:file
).
and_call_original
allow_any_instance_of
(
Gollum
::
File
).
to
receive
(
:mime_type
).
and_call_original
BareRepoOperations
.
new
(
subject
.
repository
.
path_to_repo
).
commit_file
(
file
,
'image.png'
)
end
it
'returns the latest version of the file if it exists'
do
file
=
subject
.
find_file
(
'image.jp
g'
)
expect
(
file
.
mime_type
).
to
eq
(
'image/jpe
g'
)
file
=
subject
.
find_file
(
'image.pn
g'
)
expect
(
file
.
mime_type
).
to
eq
(
'image/pn
g'
)
end
it
'returns nil if the page does not exist'
do
...
...
@@ -184,11 +174,20 @@ describe ProjectWiki do
end
it
'returns a Gitlab::Git::WikiFile instance'
do
file
=
subject
.
find_file
(
'image.jp
g'
)
file
=
subject
.
find_file
(
'image.pn
g'
)
expect
(
file
).
to
be_a
Gitlab
::
Git
::
WikiFile
end
end
context
'when Gitaly wiki_find_file is enabled'
do
it_behaves_like
'finding a wiki file'
end
context
'when Gitaly wiki_find_file is disabled'
,
:skip_gitaly_mock
do
it_behaves_like
'finding a wiki file'
end
end
describe
"#create_page"
do
shared_examples
'creating a wiki page'
do
after
do
...
...
spec/support/bare_repo_operations.rb
0 → 100644
View file @
56e53556
require
'zlib'
class
BareRepoOperations
# The ID of empty tree.
# See http://stackoverflow.com/a/40884093/1856239 and https://github.com/git/git/blob/3ad8b5bf26362ac67c9020bf8c30eee54a84f56d/cache.h#L1011-L1012
EMPTY_TREE_ID
=
'4b825dc642cb6eb9a060e54bf8d69288fbee4904'
.
freeze
include
Gitlab
::
Popen
def
initialize
(
path_to_repo
)
@path_to_repo
=
path_to_repo
end
# Based on https://stackoverflow.com/a/25556917/1856239
def
commit_file
(
file
,
dst_path
,
branch
=
'master'
)
head_id
=
execute
([
'show'
,
'--format=format:%H'
,
'--no-patch'
,
branch
],
allow_failure:
true
)[
0
]
||
EMPTY_TREE_ID
execute
([
'read-tree'
,
'--empty'
])
execute
([
'read-tree'
,
head_id
])
blob_id
=
execute
([
'hash-object'
,
'--stdin'
,
'-w'
])
do
|
stdin
|
stdin
.
write
(
file
.
read
)
end
execute
([
'update-index'
,
'--add'
,
'--cacheinfo'
,
'100644'
,
blob_id
[
0
],
dst_path
])
tree_id
=
execute
([
'write-tree'
])
commit_tree_args
=
[
'commit-tree'
,
tree_id
[
0
],
'-m'
,
"Add
#{
dst_path
}
"
]
commit_tree_args
+=
[
'-p'
,
head_id
]
unless
head_id
==
EMPTY_TREE_ID
commit_id
=
execute
(
commit_tree_args
)
execute
([
'update-ref'
,
"refs/heads/
#{
branch
}
"
,
commit_id
[
0
]])
end
private
def
execute
(
args
,
allow_failure:
false
)
output
,
status
=
popen
(
base_args
+
args
,
nil
)
do
|
stdin
|
yield
stdin
if
block_given?
end
unless
status
.
zero?
if
allow_failure
return
[]
else
raise
"Got a non-zero exit code while calling out `
#{
args
.
join
(
' '
)
}
`:
#{
output
}
"
end
end
output
.
split
(
"
\n
"
)
end
def
base_args
[
Gitlab
.
config
.
git
.
bin_path
,
"--git-dir=
#{
@path_to_repo
}
"
]
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