@@ -22,10 +22,22 @@ async function exec(req: any, action: Action): Promise<Action> {
22
22
action . addStep ( step ) ;
23
23
return action ;
24
24
}
25
- const messageParts = req . body . toString ( 'utf8' ) . split ( ' ' ) ;
25
+ const packetLines = parsePacketLines ( req . body ) ;
26
+ const refUpdates = packetLines . filter ( ( line ) => line . includes ( 'refs/heads/' ) ) ;
26
27
27
- action . branch = messageParts [ 2 ] . trim ( ) . replace ( '\u0000' , '' ) ;
28
- action . setCommit ( messageParts [ 0 ] . substr ( 4 ) , messageParts [ 1 ] ) ;
28
+ if ( refUpdates . length !== 1 ) {
29
+ step . log ( 'Invalid number of branch updates.' ) ;
30
+ step . log ( `Expected 1, but got ${ refUpdates . length } ` ) ;
31
+ step . setError ( 'Your push has been blocked. Please make sure you are pushing to a single branch.' ) ;
32
+ action . addStep ( step ) ;
33
+ return action ;
34
+ }
35
+
36
+ const parts = refUpdates [ 0 ] . split ( ' ' ) ;
37
+ const [ oldCommit , newCommit , ref ] = parts ;
38
+
39
+ action . branch = ref . replace ( / \0 .* / , '' ) . trim ( ) ;
40
+ action . setCommit ( oldCommit , newCommit ) ;
29
41
30
42
const index = req . body . lastIndexOf ( 'PACK' ) ;
31
43
const buf = req . body . slice ( index ) ;
@@ -257,6 +269,26 @@ const unpack = (buf: Buffer) => {
257
269
return [ inflated . toString ( 'utf8' ) , deflated . length ] ;
258
270
} ;
259
271
272
+ const parsePacketLines = ( buffer : Buffer ) : string [ ] => {
273
+ const lines : string [ ] = [ ] ;
274
+ let offset = 0 ;
275
+
276
+ while ( offset + 4 <= buffer . length ) {
277
+ const lengthHex = buffer . toString ( 'utf8' , offset , offset + 4 ) ;
278
+ const length = parseInt ( lengthHex , 16 ) ;
279
+
280
+ if ( length === 0 ) {
281
+ // Flush packet ("0000") marks the end of the ref section
282
+ break ;
283
+ }
284
+
285
+ const line = buffer . toString ( 'utf8' , offset + 4 , offset + length ) ;
286
+ lines . push ( line ) ;
287
+ offset += length ;
288
+ }
289
+ return lines ;
290
+ }
291
+
260
292
exec . displayName = 'parsePush.exec' ;
261
293
262
294
export {
0 commit comments