2
2
#include "pack.h"
3
3
#include "refs.h"
4
4
#include "pkt-line.h"
5
+ #include "sideband.h"
5
6
#include "run-command.h"
6
7
#include "exec_cmd.h"
7
8
#include "commit.h"
@@ -27,11 +28,12 @@ static int receive_unpack_limit = -1;
27
28
static int transfer_unpack_limit = -1 ;
28
29
static int unpack_limit = 100 ;
29
30
static int report_status ;
31
+ static int use_sideband ;
30
32
static int prefer_ofs_delta = 1 ;
31
33
static int auto_update_server_info ;
32
34
static int auto_gc = 1 ;
33
35
static const char * head_name ;
34
- static char * capabilities_to_send ;
36
+ static int sent_capabilities ;
35
37
36
38
static enum deny_action parse_deny_action (const char * var , const char * value )
37
39
{
@@ -105,19 +107,21 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
105
107
106
108
static int show_ref (const char * path , const unsigned char * sha1 , int flag , void * cb_data )
107
109
{
108
- if (! capabilities_to_send )
110
+ if (sent_capabilities )
109
111
packet_write (1 , "%s %s\n" , sha1_to_hex (sha1 ), path );
110
112
else
111
- packet_write (1 , "%s %s%c%s\n" ,
112
- sha1_to_hex (sha1 ), path , 0 , capabilities_to_send );
113
- capabilities_to_send = NULL ;
113
+ packet_write (1 , "%s %s%c%s%s\n" ,
114
+ sha1_to_hex (sha1 ), path , 0 ,
115
+ " report-status delete-refs side-band-64k" ,
116
+ prefer_ofs_delta ? " ofs-delta" : "" );
117
+ sent_capabilities = 1 ;
114
118
return 0 ;
115
119
}
116
120
117
121
static void write_head_info (void )
118
122
{
119
123
for_each_ref (show_ref , NULL );
120
- if (capabilities_to_send )
124
+ if (! sent_capabilities )
121
125
show_ref ("capabilities^{}" , null_sha1 , 0 , NULL );
122
126
123
127
}
@@ -135,11 +139,25 @@ static struct command *commands;
135
139
static const char pre_receive_hook [] = "hooks/pre-receive" ;
136
140
static const char post_receive_hook [] = "hooks/post-receive" ;
137
141
142
+ static int copy_to_sideband (int in , int out , void * arg )
143
+ {
144
+ char data [128 ];
145
+ while (1 ) {
146
+ ssize_t sz = xread (in , data , sizeof (data ));
147
+ if (sz <= 0 )
148
+ break ;
149
+ send_sideband (1 , 2 , data , sz , use_sideband );
150
+ }
151
+ close (in );
152
+ return 0 ;
153
+ }
154
+
138
155
static int run_receive_hook (const char * hook_name )
139
156
{
140
157
static char buf [sizeof (commands -> old_sha1 ) * 2 + PATH_MAX + 4 ];
141
158
struct command * cmd ;
142
159
struct child_process proc ;
160
+ struct async muxer ;
143
161
const char * argv [2 ];
144
162
int have_input = 0 , code ;
145
163
@@ -159,9 +177,23 @@ static int run_receive_hook(const char *hook_name)
159
177
proc .in = -1 ;
160
178
proc .stdout_to_stderr = 1 ;
161
179
180
+ if (use_sideband ) {
181
+ memset (& muxer , 0 , sizeof (muxer ));
182
+ muxer .proc = copy_to_sideband ;
183
+ muxer .in = -1 ;
184
+ code = start_async (& muxer );
185
+ if (code )
186
+ return code ;
187
+ proc .err = muxer .in ;
188
+ }
189
+
162
190
code = start_command (& proc );
163
- if (code )
191
+ if (code ) {
192
+ if (use_sideband )
193
+ finish_async (& muxer );
164
194
return code ;
195
+ }
196
+
165
197
for (cmd = commands ; cmd ; cmd = cmd -> next ) {
166
198
if (!cmd -> error_string ) {
167
199
size_t n = snprintf (buf , sizeof (buf ), "%s %s %s\n" ,
@@ -173,13 +205,17 @@ static int run_receive_hook(const char *hook_name)
173
205
}
174
206
}
175
207
close (proc .in );
208
+ if (use_sideband )
209
+ finish_async (& muxer );
176
210
return finish_command (& proc );
177
211
}
178
212
179
213
static int run_update_hook (struct command * cmd )
180
214
{
181
215
static const char update_hook [] = "hooks/update" ;
182
216
const char * argv [5 ];
217
+ struct child_process proc ;
218
+ int code ;
183
219
184
220
if (access (update_hook , X_OK ) < 0 )
185
221
return 0 ;
@@ -190,8 +226,18 @@ static int run_update_hook(struct command *cmd)
190
226
argv [3 ] = sha1_to_hex (cmd -> new_sha1 );
191
227
argv [4 ] = NULL ;
192
228
193
- return run_command_v_opt (argv , RUN_COMMAND_NO_STDIN |
194
- RUN_COMMAND_STDOUT_TO_STDERR );
229
+ memset (& proc , 0 , sizeof (proc ));
230
+ proc .no_stdin = 1 ;
231
+ proc .stdout_to_stderr = 1 ;
232
+ proc .err = use_sideband ? -1 : 0 ;
233
+ proc .argv = argv ;
234
+
235
+ code = start_command (& proc );
236
+ if (code )
237
+ return code ;
238
+ if (use_sideband )
239
+ copy_to_sideband (proc .err , -1 , NULL );
240
+ return finish_command (& proc );
195
241
}
196
242
197
243
static int is_ref_checked_out (const char * ref )
@@ -368,8 +414,9 @@ static char update_post_hook[] = "hooks/post-update";
368
414
static void run_update_post_hook (struct command * cmd )
369
415
{
370
416
struct command * cmd_p ;
371
- int argc , status ;
417
+ int argc ;
372
418
const char * * argv ;
419
+ struct child_process proc ;
373
420
374
421
for (argc = 0 , cmd_p = cmd ; cmd_p ; cmd_p = cmd_p -> next ) {
375
422
if (cmd_p -> error_string )
@@ -391,8 +438,18 @@ static void run_update_post_hook(struct command *cmd)
391
438
argc ++ ;
392
439
}
393
440
argv [argc ] = NULL ;
394
- status = run_command_v_opt (argv , RUN_COMMAND_NO_STDIN
395
- | RUN_COMMAND_STDOUT_TO_STDERR );
441
+
442
+ memset (& proc , 0 , sizeof (proc ));
443
+ proc .no_stdin = 1 ;
444
+ proc .stdout_to_stderr = 1 ;
445
+ proc .err = use_sideband ? -1 : 0 ;
446
+ proc .argv = argv ;
447
+
448
+ if (!start_command (& proc )) {
449
+ if (use_sideband )
450
+ copy_to_sideband (proc .err , -1 , NULL );
451
+ finish_command (& proc );
452
+ }
396
453
}
397
454
398
455
static void execute_commands (const char * unpacker_error )
@@ -452,6 +509,8 @@ static void read_head_info(void)
452
509
if (reflen + 82 < len ) {
453
510
if (strstr (refname + reflen + 1 , "report-status" ))
454
511
report_status = 1 ;
512
+ if (strstr (refname + reflen + 1 , "side-band-64k" ))
513
+ use_sideband = LARGE_PACKET_MAX ;
455
514
}
456
515
cmd = xmalloc (sizeof (struct command ) + len - 80 );
457
516
hashcpy (cmd -> old_sha1 , old_sha1 );
@@ -551,17 +610,25 @@ static const char *unpack(void)
551
610
static void report (const char * unpack_status )
552
611
{
553
612
struct command * cmd ;
554
- packet_write (1 , "unpack %s\n" ,
555
- unpack_status ? unpack_status : "ok" );
613
+ struct strbuf buf = STRBUF_INIT ;
614
+
615
+ packet_buf_write (& buf , "unpack %s\n" ,
616
+ unpack_status ? unpack_status : "ok" );
556
617
for (cmd = commands ; cmd ; cmd = cmd -> next ) {
557
618
if (!cmd -> error_string )
558
- packet_write ( 1 , "ok %s\n" ,
559
- cmd -> ref_name );
619
+ packet_buf_write ( & buf , "ok %s\n" ,
620
+ cmd -> ref_name );
560
621
else
561
- packet_write ( 1 , "ng %s %s\n" ,
562
- cmd -> ref_name , cmd -> error_string );
622
+ packet_buf_write ( & buf , "ng %s %s\n" ,
623
+ cmd -> ref_name , cmd -> error_string );
563
624
}
564
- packet_flush (1 );
625
+ packet_buf_flush (& buf );
626
+
627
+ if (use_sideband )
628
+ send_sideband (1 , 1 , buf .buf , buf .len , use_sideband );
629
+ else
630
+ safe_write (1 , buf .buf , buf .len );
631
+ strbuf_release (& buf );
565
632
}
566
633
567
634
static int delete_only (struct command * cmd )
@@ -658,10 +725,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
658
725
else if (0 <= receive_unpack_limit )
659
726
unpack_limit = receive_unpack_limit ;
660
727
661
- capabilities_to_send = (prefer_ofs_delta ) ?
662
- " report-status delete-refs ofs-delta " :
663
- " report-status delete-refs " ;
664
-
665
728
if (advertise_refs || !stateless_rpc ) {
666
729
add_alternate_refs ();
667
730
write_head_info ();
@@ -695,5 +758,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
695
758
if (auto_update_server_info )
696
759
update_server_info (0 );
697
760
}
761
+ if (use_sideband )
762
+ packet_flush (1 );
698
763
return 0 ;
699
764
}
0 commit comments