BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
4e4866f2
Commit
4e4866f2
authored
Aug 14, 2015
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor pre/post receive commit services into one class
parent
bacad39e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
143 deletions
+65
-143
commit_service.rb
app/services/commit_service.rb
+16
-11
pre_commit_service.rb
app/services/pre_commit_service.rb
+0
-71
hook.rb
lib/gitlab/git/hook.rb
+48
-60
test_env.rb
spec/support/test_env.rb
+1
-1
No files found.
app/services/commit_service.rb
View file @
4e4866f2
...
@@ -4,48 +4,53 @@ class CommitService
...
@@ -4,48 +4,53 @@ class CommitService
class
PreReceiveError
<
StandardError
;
end
class
PreReceiveError
<
StandardError
;
end
class
CommitError
<
StandardError
;
end
class
CommitError
<
StandardError
;
end
def
self
.
transaction
(
project
,
current_user
,
ref
)
def
self
.
transaction
(
project
,
current_user
,
branch
)
repository
=
project
.
repository
repository
=
project
.
repository
path_to_repo
=
repository
.
path_to_repo
path_to_repo
=
repository
.
path_to_repo
empty_repo
=
repository
.
empty?
empty_repo
=
repository
.
empty?
oldrev
=
Gitlab
::
Git
::
BLANK_SHA
ref
=
Gitlab
::
Git
::
BRANCH_REF_PREFIX
+
branch
gl_id
=
Gitlab
::
ShellEnv
.
gl_id
(
current_user
)
# Create temporary ref
# Create temporary ref
random_string
=
SecureRandom
.
hex
random_string
=
SecureRandom
.
hex
tmp_ref
=
"refs/tmp/
#{
random_string
}
/head"
tmp_ref
=
"refs/tmp/
#{
random_string
}
/head"
unless
empty_repo
unless
empty_repo
target
=
repository
.
find_branch
(
ref
).
target
oldrev
=
repository
.
find_branch
(
branch
).
target
repository
.
rugged
.
references
.
create
(
tmp_ref
,
target
)
repository
.
rugged
.
references
.
create
(
tmp_ref
,
oldrev
)
end
end
# Make commit in tmp ref
# Make commit in tmp ref
sha
=
yield
(
tmp_ref
)
newrev
=
yield
(
tmp_ref
)
unless
sha
unless
newrev
raise
CommitError
.
new
(
'Failed to create commit'
)
raise
CommitError
.
new
(
'Failed to create commit'
)
end
end
# Run GitLab pre-receive hook
# Run GitLab pre-receive hook
status
=
PreCommitService
.
new
(
project
,
current_user
).
execute
(
sha
,
ref
)
pre_receive_hook
=
Gitlab
::
Git
::
Hook
.
new
(
'pre-receive'
,
path_to_repo
)
status
=
pre_receive_hook
.
trigger
(
gl_id
,
oldrev
,
newrev
,
ref
)
if
status
if
status
if
empty_repo
if
empty_repo
# Create branch
# Create branch
repository
.
rugged
.
references
.
create
(
Gitlab
::
Git
::
BRANCH_REF_PREFIX
+
ref
,
sha
)
repository
.
rugged
.
references
.
create
(
ref
,
newrev
)
else
else
# Update head
# Update head
current_
target
=
repository
.
find_branch
(
ref
).
target
current_
head
=
repository
.
find_branch
(
branch
).
target
# Make sure target branch was not changed during pre-receive hook
# Make sure target branch was not changed during pre-receive hook
if
current_
target
==
target
if
current_
head
==
oldrev
repository
.
rugged
.
references
.
update
(
Gitlab
::
Git
::
BRANCH_REF_PREFIX
+
ref
,
sha
)
repository
.
rugged
.
references
.
update
(
ref
,
newrev
)
else
else
raise
CommitError
.
new
(
'Commit was rejected because branch received new push'
)
raise
CommitError
.
new
(
'Commit was rejected because branch received new push'
)
end
end
end
end
# Run GitLab post receive hook
# Run GitLab post receive hook
PostCommitService
.
new
(
project
,
current_user
).
execute
(
sha
,
ref
)
post_receive_hook
=
Gitlab
::
Git
::
Hook
.
new
(
'post-receive'
,
path_to_repo
)
status
=
post_receive_hook
.
trigger
(
gl_id
,
oldrev
,
newrev
,
ref
)
else
else
# Remove tmp ref and return error to user
# Remove tmp ref and return error to user
repository
.
rugged
.
references
.
delete
(
tmp_ref
)
repository
.
rugged
.
references
.
delete
(
tmp_ref
)
...
...
app/services/pre_commit_service.rb
deleted
100644 → 0
View file @
bacad39e
class
PreCommitService
<
BaseService
include
Gitlab
::
Popen
attr_reader
:changes
,
:repo_path
def
execute
(
sha
,
branch
)
commit
=
repository
.
commit
(
sha
)
full_ref
=
Gitlab
::
Git
::
BRANCH_REF_PREFIX
+
branch
old_sha
=
commit
.
parent_id
||
Gitlab
::
Git
::
BLANK_SHA
@changes
=
"
#{
old_sha
}
#{
sha
}
#{
full_ref
}
"
@repo_path
=
repository
.
path_to_repo
pre_receive
end
private
def
pre_receive
hook
=
hook_file
(
'pre-receive'
,
repo_path
)
return
true
if
hook
.
nil?
call_receive_hook
(
hook
)
end
def
call_receive_hook
(
hook
)
# function will return true if succesful
exit_status
=
false
vars
=
{
'GL_ID'
=>
Gitlab
::
ShellEnv
.
gl_id
(
current_user
),
'PWD'
=>
repo_path
}
options
=
{
chdir:
repo_path
}
# we combine both stdout and stderr as we don't know what stream
# will be used by the custom hook
Open3
.
popen2e
(
vars
,
hook
,
options
)
do
|
stdin
,
stdout_stderr
,
wait_thr
|
exit_status
=
true
stdin
.
sync
=
true
# in git, pre- and post- receive hooks may just exit without
# reading stdin. We catch the exception to avoid a broken pipe
# warning
begin
# inject all the changes as stdin to the hook
changes
.
lines
do
|
line
|
stdin
.
puts
line
end
rescue
Errno
::
EPIPE
end
# need to close stdin before reading stdout
stdin
.
close
# only output stdut_stderr if scripts doesn't return 0
unless
wait_thr
.
value
==
0
exit_status
=
false
end
end
exit_status
end
def
hook_file
(
hook_type
,
repo_path
)
hook_path
=
File
.
join
(
repo_path
.
strip
,
'hooks'
)
hook_file
=
"
#{
hook_path
}
/
#{
hook_type
}
"
hook_file
if
File
.
exist?
(
hook_file
)
end
end
app/services/post_commit_service
.rb
→
lib/gitlab/git/hook
.rb
View file @
4e4866f2
class
PostCommitService
<
BaseService
module
Gitlab
include
Gitlab
::
Popen
module
Git
class
Hook
attr_reader
:changes
,
:repo_path
attr_reader
:name
,
:repo_path
,
:path
def
execute
(
sha
,
branch
)
def
initialize
(
name
,
repo_path
)
commit
=
repository
.
commit
(
sha
)
@name
=
name
full_ref
=
Gitlab
::
Git
::
BRANCH_REF_PREFIX
+
branch
@repo_path
=
repo_path
old_sha
=
commit
.
parent_id
||
Gitlab
::
Git
::
BLANK_SHA
@path
=
File
.
join
(
repo_path
.
strip
,
'hooks'
,
name
)
@changes
=
"
#{
old_sha
}
#{
sha
}
#{
full_ref
}
"
end
@repo_path
=
repository
.
path_to_repo
post_receive
end
private
def
post_receive
hook
=
hook_file
(
'post-receive'
,
repo_path
)
return
true
if
hook
.
nil?
call_receive_hook
(
hook
)
end
def
call_receive_hook
(
hook
)
def
exists?
# function will return true if succesful
File
.
exist?
(
path
)
exit_status
=
false
end
vars
=
{
def
trigger
(
gl_id
,
oldrev
,
newrev
,
ref
)
'GL_ID'
=>
Gitlab
::
ShellEnv
.
gl_id
(
current_user
),
return
true
unless
exists?
'PWD'
=>
repo_path
}
options
=
{
changes
=
[
oldrev
,
newrev
,
ref
].
join
(
" "
)
chdir:
repo_path
}
# we combine both stdout and stderr as we don't know what stream
# function will return true if succesful
# will be used by the custom hook
exit_status
=
false
Open3
.
popen2e
(
vars
,
hook
,
options
)
do
|
stdin
,
stdout_stderr
,
wait_thr
|
exit_status
=
true
stdin
.
sync
=
true
# in git, pre- and post- receive hooks may just exit without
vars
=
{
# reading stdin. We catch the exception to avoid a broken pipe
'GL_ID'
=>
gl_id
,
# warning
'PWD'
=>
repo_path
begin
}
# inject all the changes as stdin to the hook
changes
.
lines
do
|
line
|
options
=
{
stdin
.
puts
line
chdir:
repo_path
}
Open3
.
popen2
(
vars
,
path
,
options
)
do
|
stdin
,
_
,
wait_thr
|
exit_status
=
true
stdin
.
sync
=
true
# in git, pre- and post- receive hooks may just exit without
# reading stdin. We catch the exception to avoid a broken pipe
# warning
begin
# inject all the changes as stdin to the hook
changes
.
lines
do
|
line
|
stdin
.
puts
line
end
rescue
Errno
::
EPIPE
end
stdin
.
close
unless
wait_thr
.
value
==
0
exit_status
=
false
end
end
end
rescue
Errno
::
EPIPE
end
# need to close stdin before reading stdout
stdin
.
close
# only output stdut_stderr if scripts doesn't return 0
exit_status
unless
wait_thr
.
value
==
0
exit_status
=
false
end
end
end
end
exit_status
end
def
hook_file
(
hook_type
,
repo_path
)
hook_path
=
File
.
join
(
repo_path
.
strip
,
'hooks'
)
hook_file
=
"
#{
hook_path
}
/
#{
hook_type
}
"
hook_file
if
File
.
exist?
(
hook_file
)
end
end
end
end
spec/support/test_env.rb
View file @
4e4866f2
...
@@ -58,7 +58,7 @@ module TestEnv
...
@@ -58,7 +58,7 @@ module TestEnv
end
end
def
disable_pre_receive
def
disable_pre_receive
allow_any_instance_of
(
PreCommitService
).
to
receive
(
:execute
).
and_return
(
true
)
allow_any_instance_of
(
Gitlab
::
Git
::
Hook
).
to
receive
(
:trigger
).
and_return
(
true
)
end
end
# Clean /tmp/tests
# Clean /tmp/tests
...
...
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