@@ -8,10 +8,12 @@ import (
8
8
"bufio"
9
9
"bytes"
10
10
"fmt"
11
+ "io"
11
12
"net/http"
12
13
"os"
13
14
"strconv"
14
15
"strings"
16
+ "time"
15
17
16
18
"code.gitea.io/gitea/models"
17
19
"code.gitea.io/gitea/modules/git"
58
60
}
59
61
)
60
62
63
+ type delayWriter struct {
64
+ internal io.Writer
65
+ buf * bytes.Buffer
66
+ timer * time.Timer
67
+ }
68
+
69
+ func newDelayWriter (internal io.Writer , delay time.Duration ) * delayWriter {
70
+ timer := time .NewTimer (delay )
71
+ return & delayWriter {
72
+ internal : internal ,
73
+ buf : & bytes.Buffer {},
74
+ timer : timer ,
75
+ }
76
+ }
77
+
78
+ func (d * delayWriter ) Write (p []byte ) (n int , err error ) {
79
+ if d .buf != nil {
80
+ select {
81
+ case <- d .timer .C :
82
+ _ , err := d .internal .Write (d .buf .Bytes ())
83
+ if err != nil {
84
+ return 0 , err
85
+ }
86
+ d .buf = nil
87
+ return d .internal .Write (p )
88
+ default :
89
+ return d .buf .Write (p )
90
+ }
91
+ }
92
+ return d .internal .Write (p )
93
+ }
94
+
95
+ func (d * delayWriter ) WriteString (s string ) (n int , err error ) {
96
+ if d .buf != nil {
97
+ select {
98
+ case <- d .timer .C :
99
+ _ , err := d .internal .Write (d .buf .Bytes ())
100
+ if err != nil {
101
+ return 0 , err
102
+ }
103
+ d .buf = nil
104
+ return d .internal .Write ([]byte (s ))
105
+ default :
106
+ return d .buf .WriteString (s )
107
+ }
108
+ }
109
+ return d .internal .Write ([]byte (s ))
110
+ }
111
+
112
+ func (d * delayWriter ) Close () error {
113
+ if d == nil {
114
+ return nil
115
+ }
116
+ stopped := d .timer .Stop ()
117
+ if stopped {
118
+ return nil
119
+ }
120
+ select {
121
+ case <- d .timer .C :
122
+ default :
123
+ }
124
+ if d .buf == nil {
125
+ return nil
126
+ }
127
+ _ , err := d .internal .Write (d .buf .Bytes ())
128
+ d .buf = nil
129
+ return err
130
+ }
131
+
132
+ type nilWriter struct {}
133
+
134
+ func (n * nilWriter ) Write (p []byte ) (int , error ) {
135
+ return len (p ), nil
136
+ }
137
+
138
+ func (n * nilWriter ) WriteString (s string ) (int , error ) {
139
+ return len (s ), nil
140
+ }
141
+
61
142
func runHookPreReceive (c * cli.Context ) error {
62
143
if os .Getenv (models .EnvIsInternal ) == "true" {
63
144
return nil
@@ -101,6 +182,18 @@ Gitea or set your environment appropriately.`, "")
101
182
total := 0
102
183
lastline := 0
103
184
185
+ var out io.Writer
186
+ out = & nilWriter {}
187
+ if setting .Git .VerbosePush {
188
+ if setting .Git .VerbosePushDelay > 0 {
189
+ dWriter := newDelayWriter (os .Stdout , setting .Git .VerbosePushDelay )
190
+ defer dWriter .Close ()
191
+ out = dWriter
192
+ } else {
193
+ out = os .Stdout
194
+ }
195
+ }
196
+
104
197
for scanner .Scan () {
105
198
// TODO: support news feeds for wiki
106
199
if isWiki {
@@ -124,12 +217,10 @@ Gitea or set your environment appropriately.`, "")
124
217
newCommitIDs [count ] = newCommitID
125
218
refFullNames [count ] = refFullName
126
219
count ++
127
- fmt .Fprintf (os .Stdout , "*" )
128
- os .Stdout .Sync ()
220
+ fmt .Fprintf (out , "*" )
129
221
130
222
if count >= hookBatchSize {
131
- fmt .Fprintf (os .Stdout , " Checking %d branches\n " , count )
132
- os .Stdout .Sync ()
223
+ fmt .Fprintf (out , " Checking %d branches\n " , count )
133
224
134
225
hookOptions .OldCommitIDs = oldCommitIDs
135
226
hookOptions .NewCommitIDs = newCommitIDs
@@ -147,12 +238,10 @@ Gitea or set your environment appropriately.`, "")
147
238
lastline = 0
148
239
}
149
240
} else {
150
- fmt .Fprintf (os .Stdout , "." )
151
- os .Stdout .Sync ()
241
+ fmt .Fprintf (out , "." )
152
242
}
153
243
if lastline >= hookBatchSize {
154
- fmt .Fprintf (os .Stdout , "\n " )
155
- os .Stdout .Sync ()
244
+ fmt .Fprintf (out , "\n " )
156
245
lastline = 0
157
246
}
158
247
}
@@ -162,8 +251,7 @@ Gitea or set your environment appropriately.`, "")
162
251
hookOptions .NewCommitIDs = newCommitIDs [:count ]
163
252
hookOptions .RefFullNames = refFullNames [:count ]
164
253
165
- fmt .Fprintf (os .Stdout , " Checking %d branches\n " , count )
166
- os .Stdout .Sync ()
254
+ fmt .Fprintf (out , " Checking %d branches\n " , count )
167
255
168
256
statusCode , msg := private .HookPreReceive (username , reponame , hookOptions )
169
257
switch statusCode {
@@ -173,14 +261,11 @@ Gitea or set your environment appropriately.`, "")
173
261
fail (msg , "" )
174
262
}
175
263
} else if lastline > 0 {
176
- fmt .Fprintf (os .Stdout , "\n " )
177
- os .Stdout .Sync ()
264
+ fmt .Fprintf (out , "\n " )
178
265
lastline = 0
179
266
}
180
267
181
- fmt .Fprintf (os .Stdout , "Checked %d references in total\n " , total )
182
- os .Stdout .Sync ()
183
-
268
+ fmt .Fprintf (out , "Checked %d references in total\n " , total )
184
269
return nil
185
270
}
186
271
@@ -206,6 +291,19 @@ Gitea or set your environment appropriately.`, "")
206
291
}
207
292
}
208
293
294
+ var out io.Writer
295
+ var dWriter * delayWriter
296
+ out = & nilWriter {}
297
+ if setting .Git .VerbosePush {
298
+ if setting .Git .VerbosePushDelay > 0 {
299
+ dWriter = newDelayWriter (os .Stdout , setting .Git .VerbosePushDelay )
300
+ defer dWriter .Close ()
301
+ out = dWriter
302
+ } else {
303
+ out = os .Stdout
304
+ }
305
+ }
306
+
209
307
// the environment setted on serv command
210
308
repoUser := os .Getenv (models .EnvRepoUsername )
211
309
isWiki := (os .Getenv (models .EnvRepoIsWiki ) == "true" )
@@ -241,7 +339,7 @@ Gitea or set your environment appropriately.`, "")
241
339
continue
242
340
}
243
341
244
- fmt .Fprintf (os . Stdout , "." )
342
+ fmt .Fprintf (out , "." )
245
343
oldCommitIDs [count ] = string (fields [0 ])
246
344
newCommitIDs [count ] = string (fields [1 ])
247
345
refFullNames [count ] = string (fields [2 ])
@@ -250,16 +348,15 @@ Gitea or set your environment appropriately.`, "")
250
348
}
251
349
count ++
252
350
total ++
253
- os .Stdout .Sync ()
254
351
255
352
if count >= hookBatchSize {
256
- fmt .Fprintf (os .Stdout , " Processing %d references\n " , count )
257
- os .Stdout .Sync ()
353
+ fmt .Fprintf (out , " Processing %d references\n " , count )
258
354
hookOptions .OldCommitIDs = oldCommitIDs
259
355
hookOptions .NewCommitIDs = newCommitIDs
260
356
hookOptions .RefFullNames = refFullNames
261
357
resp , err := private .HookPostReceive (repoUser , repoName , hookOptions )
262
358
if resp == nil {
359
+ _ = dWriter .Close ()
263
360
hookPrintResults (results )
264
361
fail ("Internal Server Error" , err )
265
362
}
@@ -277,9 +374,9 @@ Gitea or set your environment appropriately.`, "")
277
374
fail ("Internal Server Error" , "SetDefaultBranch failed with Error: %v" , err )
278
375
}
279
376
}
280
- fmt .Fprintf (os .Stdout , "Processed %d references in total\n " , total )
281
- os .Stdout .Sync ()
377
+ fmt .Fprintf (out , "Processed %d references in total\n " , total )
282
378
379
+ _ = dWriter .Close ()
283
380
hookPrintResults (results )
284
381
return nil
285
382
}
@@ -288,19 +385,18 @@ Gitea or set your environment appropriately.`, "")
288
385
hookOptions .NewCommitIDs = newCommitIDs [:count ]
289
386
hookOptions .RefFullNames = refFullNames [:count ]
290
387
291
- fmt .Fprintf (os .Stdout , " Processing %d references\n " , count )
292
- os .Stdout .Sync ()
388
+ fmt .Fprintf (out , " Processing %d references\n " , count )
293
389
294
390
resp , err := private .HookPostReceive (repoUser , repoName , hookOptions )
295
391
if resp == nil {
392
+ _ = dWriter .Close ()
296
393
hookPrintResults (results )
297
394
fail ("Internal Server Error" , err )
298
395
}
299
396
wasEmpty = wasEmpty || resp .RepoWasEmpty
300
397
results = append (results , resp .Results ... )
301
398
302
- fmt .Fprintf (os .Stdout , "Processed %d references in total\n " , total )
303
- os .Stdout .Sync ()
399
+ fmt .Fprintf (out , "Processed %d references in total\n " , total )
304
400
305
401
if wasEmpty && masterPushed {
306
402
// We need to tell the repo to reset the default branch to master
@@ -309,7 +405,7 @@ Gitea or set your environment appropriately.`, "")
309
405
fail ("Internal Server Error" , "SetDefaultBranch failed with Error: %v" , err )
310
406
}
311
407
}
312
-
408
+ _ = dWriter . Close ()
313
409
hookPrintResults (results )
314
410
315
411
return nil
0 commit comments