1
1
require 'securerandom'
2
2
3
3
class Repository
4
- class PreReceiveError < StandardError ; end
5
4
class CommitError < StandardError ; end
6
5
7
6
include Gitlab ::ShellAdapter
@@ -108,10 +107,19 @@ def find_tag(name)
108
107
tags . find { |tag | tag . name == name }
109
108
end
110
109
111
- def add_branch ( branch_name , ref )
112
- expire_branches_cache
110
+ def add_branch ( user , branch_name , target )
111
+ oldrev = Gitlab ::Git ::BLANK_SHA
112
+ ref = Gitlab ::Git ::BRANCH_REF_PREFIX + branch_name
113
+ target = commit ( target ) . try ( :id )
114
+
115
+ return false unless target
116
+
117
+ GitHooksService . new . execute ( user , path_to_repo , oldrev , target , ref ) do
118
+ rugged . branches . create ( branch_name , target )
119
+ end
113
120
114
- gitlab_shell . add_branch ( path_with_namespace , branch_name , ref )
121
+ expire_branches_cache
122
+ find_branch ( branch_name )
115
123
end
116
124
117
125
def add_tag ( tag_name , ref , message = nil )
@@ -120,10 +128,20 @@ def add_tag(tag_name, ref, message = nil)
120
128
gitlab_shell . add_tag ( path_with_namespace , tag_name , ref , message )
121
129
end
122
130
123
- def rm_branch ( branch_name )
131
+ def rm_branch ( user , branch_name )
124
132
expire_branches_cache
125
133
126
- gitlab_shell . rm_branch ( path_with_namespace , branch_name )
134
+ branch = find_branch ( branch_name )
135
+ oldrev = branch . try ( :target )
136
+ newrev = Gitlab ::Git ::BLANK_SHA
137
+ ref = Gitlab ::Git ::BRANCH_REF_PREFIX + branch_name
138
+
139
+ GitHooksService . new . execute ( user , path_to_repo , oldrev , newrev , ref ) do
140
+ rugged . branches . delete ( branch_name )
141
+ end
142
+
143
+ expire_branches_cache
144
+ true
127
145
end
128
146
129
147
def rm_tag ( tag_name )
@@ -550,7 +568,6 @@ def fetch_ref(source_path, source_ref, target_ref)
550
568
def commit_with_hooks ( current_user , branch )
551
569
oldrev = Gitlab ::Git ::BLANK_SHA
552
570
ref = Gitlab ::Git ::BRANCH_REF_PREFIX + branch
553
- gl_id = Gitlab ::ShellEnv . gl_id ( current_user )
554
571
was_empty = empty?
555
572
556
573
# Create temporary ref
@@ -569,15 +586,7 @@ def commit_with_hooks(current_user, branch)
569
586
raise CommitError . new ( 'Failed to create commit' )
570
587
end
571
588
572
- # Run GitLab pre-receive hook
573
- pre_receive_hook = Gitlab ::Git ::Hook . new ( 'pre-receive' , path_to_repo )
574
- pre_receive_hook_status = pre_receive_hook . trigger ( gl_id , oldrev , newrev , ref )
575
-
576
- # Run GitLab update hook
577
- update_hook = Gitlab ::Git ::Hook . new ( 'update' , path_to_repo )
578
- update_hook_status = update_hook . trigger ( gl_id , oldrev , newrev , ref )
579
-
580
- if pre_receive_hook_status && update_hook_status
589
+ GitHooksService . new . execute ( current_user , path_to_repo , oldrev , newrev , ref ) do
581
590
if was_empty
582
591
# Create branch
583
592
rugged . references . create ( ref , newrev )
@@ -592,16 +601,11 @@ def commit_with_hooks(current_user, branch)
592
601
raise CommitError . new ( 'Commit was rejected because branch received new push' )
593
602
end
594
603
end
595
-
596
- # Run GitLab post receive hook
597
- post_receive_hook = Gitlab ::Git ::Hook . new ( 'post-receive' , path_to_repo )
598
- post_receive_hook . trigger ( gl_id , oldrev , newrev , ref )
599
- else
600
- # Remove tmp ref and return error to user
601
- rugged . references . delete ( tmp_ref )
602
-
603
- raise PreReceiveError . new ( 'Commit was rejected by git hook' )
604
604
end
605
+ rescue GitHooksService ::PreReceiveError
606
+ # Remove tmp ref and return error to user
607
+ rugged . references . delete ( tmp_ref )
608
+ raise
605
609
end
606
610
607
611
private
0 commit comments