@@ -6,7 +6,6 @@ package repository
6
6
7
7
import (
8
8
"container/list"
9
- "encoding/json"
10
9
"fmt"
11
10
"time"
12
11
@@ -90,7 +89,6 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
90
89
91
90
addTags := make ([]string , 0 , len (optsList ))
92
91
delTags := make ([]string , 0 , len (optsList ))
93
- actions := make ([]* commitRepoActionOptions , 0 , len (optsList ))
94
92
var pusher * models.User
95
93
96
94
for _ , opts := range optsList {
@@ -102,8 +100,10 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
102
100
tagName := opts .TagName ()
103
101
if opts .IsDelRef () {
104
102
delTags = append (delTags , tagName )
103
+ notification .NotifyDeleteRef (pusher , repo , "tag" , opts .RefFullName )
105
104
} else { // is new tag
106
105
addTags = append (addTags , tagName )
106
+ notification .NotifyCreateRef (pusher , repo , "tag" , opts .RefFullName )
107
107
}
108
108
} else if opts .IsBranch () { // If is branch reference
109
109
if pusher == nil || pusher .ID != opts .PusherID {
@@ -123,13 +123,32 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
123
123
return fmt .Errorf ("gitRepo.GetCommit: %v" , err )
124
124
}
125
125
126
+ refName := opts .RefName ()
127
+
126
128
// Push new branch.
127
129
var l * list.List
128
130
if opts .IsNewRef () {
131
+ if repo .IsEmpty { // Change default branch and empty status only if pushed ref is non-empty branch.
132
+ repo .DefaultBranch = refName
133
+ repo .IsEmpty = false
134
+ if repo .DefaultBranch != setting .Repository .DefaultBranch {
135
+ if err := gitRepo .SetDefaultBranch (repo .DefaultBranch ); err != nil {
136
+ if ! git .IsErrUnsupportedVersion (err ) {
137
+ return err
138
+ }
139
+ }
140
+ }
141
+ // Update the is empty and default_branch columns
142
+ if err := models .UpdateRepositoryCols (repo , "default_branch" , "is_empty" ); err != nil {
143
+ return fmt .Errorf ("UpdateRepositoryCols: %v" , err )
144
+ }
145
+ }
146
+
129
147
l , err = newCommit .CommitsBeforeLimit (10 )
130
148
if err != nil {
131
149
return fmt .Errorf ("newCommit.CommitsBeforeLimit: %v" , err )
132
150
}
151
+ notification .NotifyCreateRef (pusher , repo , "branch" , opts .RefFullName )
133
152
} else {
134
153
l , err = newCommit .CommitsBeforeUntil (opts .OldCommitID )
135
154
if err != nil {
@@ -152,6 +171,15 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
152
171
}
153
172
154
173
commits = repo_module .ListToPushCommits (l )
174
+ if len (commits .Commits ) > setting .UI .FeedMaxCommitNum {
175
+ commits .Commits = commits .Commits [:setting .UI .FeedMaxCommitNum ]
176
+ }
177
+ commits .CompareURL = repo .ComposeCompareURL (opts .OldCommitID , opts .NewCommitID )
178
+ notification .NotifyPushCommits (pusher , repo , opts , commits )
179
+
180
+ if err := repofiles .UpdateIssuesCommit (pusher , repo , commits .Commits , refName ); err != nil {
181
+ log .Error ("updateIssuesCommit: %v" , err )
182
+ }
155
183
156
184
if err = models .RemoveDeletedBranch (repo .ID , branch ); err != nil {
157
185
log .Error ("models.RemoveDeletedBranch %s/%s failed: %v" , repo .ID , branch , err )
@@ -161,149 +189,30 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
161
189
if err := repo_module .CacheRef (repo , gitRepo , opts .RefFullName ); err != nil {
162
190
log .Error ("repo_module.CacheRef %s/%s failed: %v" , repo .ID , branch , err )
163
191
}
164
- } else if err = pull_service .CloseBranchPulls (pusher , repo .ID , branch ); err != nil {
165
- // close all related pulls
166
- log .Error ("close related pull request failed: %v" , err )
192
+ } else {
193
+ notification .NotifyDeleteRef (pusher , repo , "branch" , opts .RefFullName )
194
+ if err = pull_service .CloseBranchPulls (pusher , repo .ID , branch ); err != nil {
195
+ // close all related pulls
196
+ log .Error ("close related pull request failed: %v" , err )
197
+ }
167
198
}
168
199
169
200
// Even if user delete a branch on a repository which he didn't watch, he will be watch that.
170
201
if err = models .WatchIfAuto (opts .PusherID , repo .ID , true ); err != nil {
171
202
log .Warn ("Fail to perform auto watch on user %v for repo %v: %v" , opts .PusherID , repo .ID , err )
172
203
}
204
+ } else {
205
+ log .Trace ("Non-tag and non-branch commits pushed." )
173
206
}
174
- actions = append (actions , & commitRepoActionOptions {
175
- PushUpdateOptions : * opts ,
176
- Pusher : pusher ,
177
- RepoOwnerID : repo .OwnerID ,
178
- Commits : commits ,
179
- })
180
207
}
181
208
if err := repo_module .PushUpdateAddDeleteTags (repo , gitRepo , addTags , delTags ); err != nil {
182
209
return fmt .Errorf ("PushUpdateAddDeleteTags: %v" , err )
183
210
}
184
211
185
- if err := commitRepoAction (repo , gitRepo , actions ... ); err != nil {
186
- return fmt .Errorf ("commitRepoAction: %v" , err )
187
- }
188
-
189
- return nil
190
- }
191
-
192
- // commitRepoActionOptions represent options of a new commit action.
193
- type commitRepoActionOptions struct {
194
- repo_module.PushUpdateOptions
195
-
196
- Pusher * models.User
197
- RepoOwnerID int64
198
- Commits * repo_module.PushCommits
199
- }
200
-
201
- // commitRepoAction adds new commit action to the repository, and prepare
202
- // corresponding webhooks.
203
- func commitRepoAction (repo * models.Repository , gitRepo * git.Repository , optsList ... * commitRepoActionOptions ) error {
204
- actions := make ([]* models.Action , len (optsList ))
205
-
206
- for i , opts := range optsList {
207
- if opts .Pusher == nil || opts .Pusher .Name != opts .PusherName {
208
- var err error
209
- opts .Pusher , err = models .GetUserByName (opts .PusherName )
210
- if err != nil {
211
- return fmt .Errorf ("GetUserByName [%s]: %v" , opts .PusherName , err )
212
- }
213
- }
214
-
215
- refName := git .RefEndName (opts .RefFullName )
216
-
217
- // Change default branch and empty status only if pushed ref is non-empty branch.
218
- if repo .IsEmpty && opts .IsBranch () && ! opts .IsDelRef () {
219
- repo .DefaultBranch = refName
220
- repo .IsEmpty = false
221
- if refName != "master" {
222
- if err := gitRepo .SetDefaultBranch (repo .DefaultBranch ); err != nil {
223
- if ! git .IsErrUnsupportedVersion (err ) {
224
- return err
225
- }
226
- }
227
- }
228
- // Update the is empty and default_branch columns
229
- if err := models .UpdateRepositoryCols (repo , "default_branch" , "is_empty" ); err != nil {
230
- return fmt .Errorf ("UpdateRepositoryCols: %v" , err )
231
- }
232
- }
233
-
234
- opType := models .ActionCommitRepo
235
-
236
- // Check it's tag push or branch.
237
- if opts .IsTag () {
238
- opType = models .ActionPushTag
239
- if opts .IsDelRef () {
240
- opType = models .ActionDeleteTag
241
- }
242
- opts .Commits = & repo_module.PushCommits {}
243
- } else if opts .IsDelRef () {
244
- opType = models .ActionDeleteBranch
245
- opts .Commits = & repo_module.PushCommits {}
246
- } else {
247
- // if not the first commit, set the compare URL.
248
- if ! opts .IsNewRef () {
249
- opts .Commits .CompareURL = repo .ComposeCompareURL (opts .OldCommitID , opts .NewCommitID )
250
- }
251
-
252
- if err := repofiles .UpdateIssuesCommit (opts .Pusher , repo , opts .Commits .Commits , refName ); err != nil {
253
- log .Error ("updateIssuesCommit: %v" , err )
254
- }
255
- }
256
-
257
- if len (opts .Commits .Commits ) > setting .UI .FeedMaxCommitNum {
258
- opts .Commits .Commits = opts .Commits .Commits [:setting .UI .FeedMaxCommitNum ]
259
- }
260
-
261
- data , err := json .Marshal (opts .Commits )
262
- if err != nil {
263
- return fmt .Errorf ("Marshal: %v" , err )
264
- }
265
-
266
- actions [i ] = & models.Action {
267
- ActUserID : opts .Pusher .ID ,
268
- ActUser : opts .Pusher ,
269
- OpType : opType ,
270
- Content : string (data ),
271
- RepoID : repo .ID ,
272
- Repo : repo ,
273
- RefName : refName ,
274
- IsPrivate : repo .IsPrivate ,
275
- }
276
-
277
- var isHookEventPush = true
278
- switch opType {
279
- case models .ActionCommitRepo : // Push
280
- if opts .IsNewBranch () {
281
- notification .NotifyCreateRef (opts .Pusher , repo , "branch" , opts .RefFullName )
282
- }
283
- case models .ActionDeleteBranch : // Delete Branch
284
- notification .NotifyDeleteRef (opts .Pusher , repo , "branch" , opts .RefFullName )
285
-
286
- case models .ActionPushTag : // Create
287
- notification .NotifyCreateRef (opts .Pusher , repo , "tag" , opts .RefFullName )
288
-
289
- case models .ActionDeleteTag : // Delete Tag
290
- notification .NotifyDeleteRef (opts .Pusher , repo , "tag" , opts .RefFullName )
291
- default :
292
- isHookEventPush = false
293
- }
294
-
295
- if isHookEventPush {
296
- notification .NotifyPushCommits (opts .Pusher , repo , & opts .PushUpdateOptions , opts .Commits )
297
- }
298
- }
299
-
300
212
// Change repository last updated time.
301
213
if err := models .UpdateRepositoryUpdatedTime (repo .ID , time .Now ()); err != nil {
302
214
return fmt .Errorf ("UpdateRepositoryUpdatedTime: %v" , err )
303
215
}
304
216
305
- if err := models .NotifyWatchers (actions ... ); err != nil {
306
- return fmt .Errorf ("NotifyWatchers: %v" , err )
307
- }
308
217
return nil
309
218
}
0 commit comments