@@ -21,6 +21,10 @@ import (
21
21
"github.com/urfave/cli"
22
22
)
23
23
24
+ const (
25
+ hookBatchSize = 200
26
+ )
27
+
24
28
var (
25
29
// CmdHook represents the available hooks sub-command.
26
30
CmdHook = cli.Command {
@@ -75,12 +79,23 @@ Gitea or set your environment appropriately.`, "")
75
79
prID , _ := strconv .ParseInt (os .Getenv (models .ProtectedBranchPRID ), 10 , 64 )
76
80
isDeployKey , _ := strconv .ParseBool (os .Getenv (models .EnvIsDeployKey ))
77
81
78
- buf := bytes .NewBuffer (nil )
82
+ hookOptions := private.HookOptions {
83
+ UserID : userID ,
84
+ GitAlternativeObjectDirectories : os .Getenv (private .GitAlternativeObjectDirectories ),
85
+ GitObjectDirectory : os .Getenv (private .GitObjectDirectory ),
86
+ GitQuarantinePath : os .Getenv (private .GitQuarantinePath ),
87
+ ProtectedBranchID : prID ,
88
+ IsDeployKey : isDeployKey ,
89
+ }
90
+
79
91
scanner := bufio .NewScanner (os .Stdin )
80
- for scanner .Scan () {
81
- buf .Write (scanner .Bytes ())
82
- buf .WriteByte ('\n' )
83
92
93
+ oldCommitIDs := make ([]string , hookBatchSize )
94
+ newCommitIDs := make ([]string , hookBatchSize )
95
+ refFullNames := make ([]string , hookBatchSize )
96
+ count := 0
97
+
98
+ for scanner .Scan () {
84
99
// TODO: support news feeds for wiki
85
100
if isWiki {
86
101
continue
@@ -97,26 +112,40 @@ Gitea or set your environment appropriately.`, "")
97
112
98
113
// If the ref is a branch, check if it's protected
99
114
if strings .HasPrefix (refFullName , git .BranchPrefix ) {
100
- statusCode , msg := private . HookPreReceive ( username , reponame , private. HookOptions {
101
- OldCommitID : oldCommitID ,
102
- NewCommitID : newCommitID ,
103
- RefFullName : refFullName ,
104
- UserID : userID ,
105
- GitAlternativeObjectDirectories : os . Getenv ( private . GitAlternativeObjectDirectories ),
106
- GitObjectDirectory : os . Getenv ( private . GitObjectDirectory ),
107
- GitQuarantinePath : os . Getenv ( private . GitQuarantinePath ),
108
- ProtectedBranchID : prID ,
109
- IsDeployKey : isDeployKey ,
110
- })
111
- switch statusCode {
112
- case http .StatusInternalServerError :
113
- fail ("Internal Server Error" , msg )
114
- case http . StatusForbidden :
115
- fail ( msg , "" )
115
+ oldCommitIDs [ count ] = oldCommitID
116
+ newCommitIDs [ count ] = newCommitID
117
+ refFullNames [ count ] = refFullName
118
+ count ++
119
+ if count >= hookBatchSize {
120
+ hookOptions . OldCommitIDs = oldCommitIDs
121
+ hookOptions . NewCommitIDs = newCommitIDs
122
+ hookOptions . RefFullNames = refFullNames
123
+ statusCode , msg := private . HookPreReceive ( username , reponame , hookOptions )
124
+ switch statusCode {
125
+ case http . StatusInternalServerError :
126
+ fail ( "Internal Server Error" , msg )
127
+ case http .StatusForbidden :
128
+ fail (msg , "" )
129
+ }
130
+ count = 0
116
131
}
117
132
}
118
133
}
119
134
135
+ if count > 0 {
136
+ hookOptions .OldCommitIDs = oldCommitIDs [:count ]
137
+ hookOptions .NewCommitIDs = newCommitIDs [:count ]
138
+ hookOptions .RefFullNames = refFullNames [:count ]
139
+
140
+ statusCode , msg := private .HookPreReceive (username , reponame , hookOptions )
141
+ switch statusCode {
142
+ case http .StatusInternalServerError :
143
+ fail ("Internal Server Error" , msg )
144
+ case http .StatusForbidden :
145
+ fail (msg , "" )
146
+ }
147
+ }
148
+
120
149
return nil
121
150
}
122
151
@@ -156,6 +185,18 @@ Gitea or set your environment appropriately.`, "")
156
185
pusherID , _ := strconv .ParseInt (os .Getenv (models .EnvPusherID ), 10 , 64 )
157
186
pusherName := os .Getenv (models .EnvPusherName )
158
187
188
+ hookOptions := private.HookOptions {
189
+ UserName : pusherName ,
190
+ UserID : pusherID ,
191
+ GitAlternativeObjectDirectories : os .Getenv (private .GitAlternativeObjectDirectories ),
192
+ GitObjectDirectory : os .Getenv (private .GitObjectDirectory ),
193
+ GitQuarantinePath : os .Getenv (private .GitQuarantinePath ),
194
+ }
195
+ oldCommitIDs := make ([]string , hookBatchSize )
196
+ newCommitIDs := make ([]string , hookBatchSize )
197
+ refFullNames := make ([]string , hookBatchSize )
198
+ count := 0
199
+
159
200
buf := bytes .NewBuffer (nil )
160
201
scanner := bufio .NewScanner (os .Stdin )
161
202
for scanner .Scan () {
@@ -172,33 +213,61 @@ Gitea or set your environment appropriately.`, "")
172
213
continue
173
214
}
174
215
175
- oldCommitID := string (fields [0 ])
176
- newCommitID := string (fields [1 ])
177
- refFullName := string (fields [2 ])
216
+ oldCommitIDs [count ] = string (fields [0 ])
217
+ newCommitIDs [count ] = string (fields [1 ])
218
+ refFullNames [count ] = string (fields [2 ])
219
+ count ++
178
220
179
- res , err := private .HookPostReceive (repoUser , repoName , private.HookOptions {
180
- OldCommitID : oldCommitID ,
181
- NewCommitID : newCommitID ,
182
- RefFullName : refFullName ,
183
- UserID : pusherID ,
184
- UserName : pusherName ,
185
- })
221
+ if count >= hookBatchSize {
222
+ hookOptions .OldCommitIDs = oldCommitIDs
223
+ hookOptions .NewCommitIDs = newCommitIDs
224
+ hookOptions .RefFullNames = refFullNames
225
+ resps , err := private .HookPostReceive (repoUser , repoName , hookOptions )
226
+ if resps == nil {
227
+ fail ("Internal Server Error" , err )
228
+ }
229
+ for _ , res := range resps {
230
+ if ! res .Message {
231
+ continue
232
+ }
186
233
187
- if res == nil {
188
- fail ("Internal Server Error" , err )
234
+ fmt .Fprintln (os .Stderr , "" )
235
+ if res .Create {
236
+ fmt .Fprintf (os .Stderr , "Create a new pull request for '%s':\n " , res .Branch )
237
+ fmt .Fprintf (os .Stderr , " %s\n " , res .URL )
238
+ } else {
239
+ fmt .Fprint (os .Stderr , "Visit the existing pull request:\n " )
240
+ fmt .Fprintf (os .Stderr , " %s\n " , res .URL )
241
+ }
242
+ fmt .Fprintln (os .Stderr , "" )
243
+ }
244
+ count = 0
189
245
}
246
+ }
190
247
191
- if res ["message" ] == false {
248
+ if count == 0 {
249
+ return nil
250
+ }
251
+
252
+ hookOptions .OldCommitIDs = oldCommitIDs [:count ]
253
+ hookOptions .NewCommitIDs = newCommitIDs [:count ]
254
+ hookOptions .RefFullNames = refFullNames [:count ]
255
+ resps , err := private .HookPostReceive (repoUser , repoName , hookOptions )
256
+ if resps == nil {
257
+ fail ("Internal Server Error" , err )
258
+ }
259
+ for _ , res := range resps {
260
+ if ! res .Message {
192
261
continue
193
262
}
194
263
195
264
fmt .Fprintln (os .Stderr , "" )
196
- if res [ "create" ] == true {
197
- fmt .Fprintf (os .Stderr , "Create a new pull request for '%s':\n " , res [ "branch" ] )
198
- fmt .Fprintf (os .Stderr , " %s\n " , res [ "url" ] )
265
+ if res . Create {
266
+ fmt .Fprintf (os .Stderr , "Create a new pull request for '%s':\n " , res . Branch )
267
+ fmt .Fprintf (os .Stderr , " %s\n " , res . URL )
199
268
} else {
200
269
fmt .Fprint (os .Stderr , "Visit the existing pull request:\n " )
201
- fmt .Fprintf (os .Stderr , " %s\n " , res [ "url" ] )
270
+ fmt .Fprintf (os .Stderr , " %s\n " , res . URL )
202
271
}
203
272
fmt .Fprintln (os .Stderr , "" )
204
273
}
0 commit comments