Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
f4149bcd
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f4149bcd
编写于
8月 14, 2015
作者:
D
Dmitriy Zaporozhets
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor how repository makes commit with pre/post receive hooks
上级
4e4866f2
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
94 addition
and
95 deletion
+94
-95
app/models/repository.rb
app/models/repository.rb
+90
-29
app/services/commit_service.rb
app/services/commit_service.rb
+0
-56
app/services/files/base_service.rb
app/services/files/base_service.rb
+1
-1
app/services/files/create_service.rb
app/services/files/create_service.rb
+1
-3
app/services/files/delete_service.rb
app/services/files/delete_service.rb
+1
-3
app/services/files/update_service.rb
app/services/files/update_service.rb
+1
-3
未找到文件。
app/models/repository.rb
浏览文件 @
f4149bcd
require
'securerandom'
class
Repository
class
PreReceiveError
<
StandardError
;
end
class
CommitError
<
StandardError
;
end
include
Gitlab
::
ShellAdapter
attr_accessor
:raw_repository
,
:path_with_namespace
,
:project
...
...
@@ -364,43 +369,47 @@ class Repository
@root_ref
||=
raw_repository
.
root_ref
end
def
commit_file
(
user
,
path
,
content
,
message
,
ref
)
path
[
0
]
=
''
if
path
[
0
]
==
'/'
def
commit_file
(
user
,
path
,
content
,
message
,
branch
)
commit_with_hooks
(
user
,
branch
)
do
|
ref
|
path
[
0
]
=
''
if
path
[
0
]
==
'/'
committer
=
user_to_comitter
(
user
)
options
=
{}
options
[
:committer
]
=
committer
options
[
:author
]
=
committer
options
[
:commit
]
=
{
message:
message
,
branch:
ref
,
}
committer
=
user_to_comitter
(
user
)
options
=
{}
options
[
:committer
]
=
committer
options
[
:author
]
=
committer
options
[
:commit
]
=
{
message:
message
,
branch:
ref
,
}
options
[
:file
]
=
{
content:
content
,
path:
path
}
options
[
:file
]
=
{
content:
content
,
path:
path
}
Gitlab
::
Git
::
Blob
.
commit
(
raw_repository
,
options
)
Gitlab
::
Git
::
Blob
.
commit
(
raw_repository
,
options
)
end
end
def
remove_file
(
user
,
path
,
message
,
ref
)
path
[
0
]
=
''
if
path
[
0
]
==
'/'
def
remove_file
(
user
,
path
,
message
,
branch
)
commit_with_hooks
(
user
,
branch
)
do
|
branch
|
path
[
0
]
=
''
if
path
[
0
]
==
'/'
committer
=
user_to_comitter
(
user
)
options
=
{}
options
[
:committer
]
=
committer
options
[
:author
]
=
committer
options
[
:commit
]
=
{
message:
message
,
branch:
ref
}
committer
=
user_to_comitter
(
user
)
options
=
{}
options
[
:committer
]
=
committer
options
[
:author
]
=
committer
options
[
:commit
]
=
{
message:
message
,
branch:
ref
}
options
[
:file
]
=
{
path:
path
}
options
[
:file
]
=
{
path:
path
}
Gitlab
::
Git
::
Blob
.
remove
(
raw_repository
,
options
)
Gitlab
::
Git
::
Blob
.
remove
(
raw_repository
,
options
)
end
end
def
user_to_comitter
(
user
)
...
...
@@ -479,6 +488,58 @@ class Repository
Gitlab
::
Popen
.
popen
(
args
,
path_to_repo
)
end
def
commit_with_hooks
(
current_user
,
branch
)
oldrev
=
Gitlab
::
Git
::
BLANK_SHA
ref
=
Gitlab
::
Git
::
BRANCH_REF_PREFIX
+
branch
gl_id
=
Gitlab
::
ShellEnv
.
gl_id
(
current_user
)
# Create temporary ref
random_string
=
SecureRandom
.
hex
tmp_ref
=
"refs/tmp/
#{
random_string
}
/head"
unless
empty?
oldrev
=
find_branch
(
branch
).
target
rugged
.
references
.
create
(
tmp_ref
,
oldrev
)
end
# Make commit in tmp ref
newrev
=
yield
(
tmp_ref
)
unless
newrev
raise
CommitError
.
new
(
'Failed to create commit'
)
end
# Run GitLab pre-receive hook
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
empty?
# Create branch
rugged
.
references
.
create
(
ref
,
newrev
)
else
# Update head
current_head
=
find_branch
(
branch
).
target
# Make sure target branch was not changed during pre-receive hook
if
current_head
==
oldrev
rugged
.
references
.
update
(
ref
,
newrev
)
else
raise
CommitError
.
new
(
'Commit was rejected because branch received new push'
)
end
end
# Run GitLab post receive hook
post_receive_hook
=
Gitlab
::
Git
::
Hook
.
new
(
'post-receive'
,
path_to_repo
)
status
=
post_receive_hook
.
trigger
(
gl_id
,
oldrev
,
newrev
,
ref
)
else
# Remove tmp ref and return error to user
rugged
.
references
.
delete
(
tmp_ref
)
raise
PreReceiveError
.
new
(
'Commit was rejected by pre-reveive hook'
)
end
end
private
def
cache
...
...
app/services/commit_service.rb
浏览文件 @
f4149bcd
require
'securerandom'
class
CommitService
class
PreReceiveError
<
StandardError
;
end
class
CommitError
<
StandardError
;
end
def
self
.
transaction
(
project
,
current_user
,
branch
)
repository
=
project
.
repository
path_to_repo
=
repository
.
path_to_repo
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
random_string
=
SecureRandom
.
hex
tmp_ref
=
"refs/tmp/
#{
random_string
}
/head"
unless
empty_repo
oldrev
=
repository
.
find_branch
(
branch
).
target
repository
.
rugged
.
references
.
create
(
tmp_ref
,
oldrev
)
end
# Make commit in tmp ref
newrev
=
yield
(
tmp_ref
)
unless
newrev
raise
CommitError
.
new
(
'Failed to create commit'
)
end
# Run GitLab pre-receive hook
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
empty_repo
# Create branch
repository
.
rugged
.
references
.
create
(
ref
,
newrev
)
else
# Update head
current_head
=
repository
.
find_branch
(
branch
).
target
# Make sure target branch was not changed during pre-receive hook
if
current_head
==
oldrev
repository
.
rugged
.
references
.
update
(
ref
,
newrev
)
else
raise
CommitError
.
new
(
'Commit was rejected because branch received new push'
)
end
end
# Run GitLab post receive hook
post_receive_hook
=
Gitlab
::
Git
::
Hook
.
new
(
'post-receive'
,
path_to_repo
)
status
=
post_receive_hook
.
trigger
(
gl_id
,
oldrev
,
newrev
,
ref
)
else
# Remove tmp ref and return error to user
repository
.
rugged
.
references
.
delete
(
tmp_ref
)
raise
PreReceiveError
.
new
(
'Commit was rejected by pre-reveive hook'
)
end
end
end
app/services/files/base_service.rb
浏览文件 @
f4149bcd
...
...
@@ -26,7 +26,7 @@ module Files
else
error
(
"Something went wrong. Your changes were not committed"
)
end
rescue
CommitService
::
CommitError
,
CommitService
::
PreReceiveError
,
ValidationError
=>
ex
rescue
Repository
::
CommitError
,
Repository
::
PreReceiveError
,
ValidationError
=>
ex
error
(
ex
.
message
)
end
...
...
app/services/files/create_service.rb
浏览文件 @
f4149bcd
...
...
@@ -3,9 +3,7 @@ require_relative "base_service"
module
Files
class
CreateService
<
Files
::
BaseService
def
commit
CommitService
.
transaction
(
project
,
current_user
,
@target_branch
)
do
|
tmp_ref
|
repository
.
commit_file
(
current_user
,
@file_path
,
@file_content
,
@commit_message
,
tmp_ref
)
end
repository
.
commit_file
(
current_user
,
@file_path
,
@file_content
,
@commit_message
,
@target_branch
)
end
def
validate
...
...
app/services/files/delete_service.rb
浏览文件 @
f4149bcd
...
...
@@ -3,9 +3,7 @@ require_relative "base_service"
module
Files
class
DeleteService
<
Files
::
BaseService
def
commit
CommitService
.
transaction
(
project
,
current_user
,
@target_branch
)
do
|
tmp_ref
|
repository
.
remove_file
(
current_user
,
@file_path
,
@commit_message
,
tmp_ref
)
end
repository
.
remove_file
(
current_user
,
@file_path
,
@commit_message
,
@target_branch
)
end
end
end
app/services/files/update_service.rb
浏览文件 @
f4149bcd
...
...
@@ -3,9 +3,7 @@ require_relative "base_service"
module
Files
class
UpdateService
<
Files
::
BaseService
def
commit
CommitService
.
transaction
(
project
,
current_user
,
@target_branch
)
do
|
tmp_ref
|
repository
.
commit_file
(
current_user
,
@file_path
,
@file_content
,
@commit_message
,
tmp_ref
)
end
repository
.
commit_file
(
current_user
,
@file_path
,
@file_content
,
@commit_message
,
@target_branch
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录