@@ -165,6 +165,24 @@ enum ack_type {
165
165
ACK_ready
166
166
};
167
167
168
+ static void consume_shallow_list (int fd )
169
+ {
170
+ if (args .stateless_rpc && args .depth > 0 ) {
171
+ /* If we sent a depth we will get back "duplicate"
172
+ * shallow and unshallow commands every time there
173
+ * is a block of have lines exchanged.
174
+ */
175
+ char line [1000 ];
176
+ while (packet_read_line (fd , line , sizeof (line ))) {
177
+ if (!prefixcmp (line , "shallow " ))
178
+ continue ;
179
+ if (!prefixcmp (line , "unshallow " ))
180
+ continue ;
181
+ die ("git fetch-pack: expected shallow list" );
182
+ }
183
+ }
184
+ }
185
+
168
186
static enum ack_type get_ack (int fd , unsigned char * result_sha1 )
169
187
{
170
188
static char line [1000 ];
@@ -190,6 +208,15 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1)
190
208
die ("git fetch_pack: expected ACK/NAK, got '%s'" , line );
191
209
}
192
210
211
+ static void send_request (int fd , struct strbuf * buf )
212
+ {
213
+ if (args .stateless_rpc ) {
214
+ send_sideband (fd , -1 , buf -> buf , buf -> len , LARGE_PACKET_MAX );
215
+ packet_flush (fd );
216
+ } else
217
+ safe_write (fd , buf -> buf , buf -> len );
218
+ }
219
+
193
220
static int find_common (int fd [2 ], unsigned char * result_sha1 ,
194
221
struct ref * refs )
195
222
{
@@ -199,7 +226,10 @@ static int find_common(int fd[2], unsigned char *result_sha1,
199
226
unsigned in_vain = 0 ;
200
227
int got_continue = 0 ;
201
228
struct strbuf req_buf = STRBUF_INIT ;
229
+ size_t state_len = 0 ;
202
230
231
+ if (args .stateless_rpc && multi_ack == 1 )
232
+ die ("--stateless-rpc requires multi_ack_detailed" );
203
233
if (marked )
204
234
for_each_ref (clear_marks , NULL );
205
235
marked = 1 ;
@@ -256,13 +286,13 @@ static int find_common(int fd[2], unsigned char *result_sha1,
256
286
if (args .depth > 0 )
257
287
packet_buf_write (& req_buf , "deepen %d" , args .depth );
258
288
packet_buf_flush (& req_buf );
259
-
260
- safe_write (fd [1 ], req_buf .buf , req_buf .len );
289
+ state_len = req_buf .len ;
261
290
262
291
if (args .depth > 0 ) {
263
292
char line [1024 ];
264
293
unsigned char sha1 [20 ];
265
294
295
+ send_request (fd [1 ], & req_buf );
266
296
while (packet_read_line (fd [0 ], line , sizeof (line ))) {
267
297
if (!prefixcmp (line , "shallow " )) {
268
298
if (get_sha1_hex (line + 8 , sha1 ))
@@ -284,28 +314,40 @@ static int find_common(int fd[2], unsigned char *result_sha1,
284
314
}
285
315
die ("expected shallow/unshallow, got %s" , line );
286
316
}
317
+ } else if (!args .stateless_rpc )
318
+ send_request (fd [1 ], & req_buf );
319
+
320
+ if (!args .stateless_rpc ) {
321
+ /* If we aren't using the stateless-rpc interface
322
+ * we don't need to retain the headers.
323
+ */
324
+ strbuf_setlen (& req_buf , 0 );
325
+ state_len = 0 ;
287
326
}
288
327
289
328
flushes = 0 ;
290
329
retval = -1 ;
291
330
while ((sha1 = get_rev ())) {
292
- packet_write ( fd [ 1 ] , "have %s\n" , sha1_to_hex (sha1 ));
331
+ packet_buf_write ( & req_buf , "have %s\n" , sha1_to_hex (sha1 ));
293
332
if (args .verbose )
294
333
fprintf (stderr , "have %s\n" , sha1_to_hex (sha1 ));
295
334
in_vain ++ ;
296
335
if (!(31 & ++ count )) {
297
336
int ack ;
298
337
299
- packet_flush (fd [1 ]);
338
+ packet_buf_flush (& req_buf );
339
+ send_request (fd [1 ], & req_buf );
340
+ strbuf_setlen (& req_buf , state_len );
300
341
flushes ++ ;
301
342
302
343
/*
303
344
* We keep one window "ahead" of the other side, and
304
345
* will wait for an ACK only on the next one
305
346
*/
306
- if (count == 32 )
347
+ if (! args . stateless_rpc && count == 32 )
307
348
continue ;
308
349
350
+ consume_shallow_list (fd [0 ]);
309
351
do {
310
352
ack = get_ack (fd [0 ], result_sha1 );
311
353
if (args .verbose && ack )
@@ -322,6 +364,17 @@ static int find_common(int fd[2], unsigned char *result_sha1,
322
364
case ACK_continue : {
323
365
struct commit * commit =
324
366
lookup_commit (result_sha1 );
367
+ if (args .stateless_rpc
368
+ && ack == ACK_common
369
+ && !(commit -> object .flags & COMMON )) {
370
+ /* We need to replay the have for this object
371
+ * on the next RPC request so the peer knows
372
+ * it is in common with us.
373
+ */
374
+ const char * hex = sha1_to_hex (result_sha1 );
375
+ packet_buf_write (& req_buf , "have %s\n" , hex );
376
+ state_len = req_buf .len ;
377
+ }
325
378
mark_common (commit , 0 , 1 );
326
379
retval = 0 ;
327
380
in_vain = 0 ;
@@ -339,7 +392,8 @@ static int find_common(int fd[2], unsigned char *result_sha1,
339
392
}
340
393
}
341
394
done :
342
- packet_write (fd [1 ], "done\n" );
395
+ packet_buf_write (& req_buf , "done\n" );
396
+ send_request (fd [1 ], & req_buf );
343
397
if (args .verbose )
344
398
fprintf (stderr , "done\n" );
345
399
if (retval != 0 ) {
@@ -348,6 +402,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
348
402
}
349
403
strbuf_release (& req_buf );
350
404
405
+ consume_shallow_list (fd [0 ]);
351
406
while (flushes || multi_ack ) {
352
407
int ack = get_ack (fd [0 ], result_sha1 );
353
408
if (ack ) {
@@ -672,6 +727,8 @@ static struct ref *do_fetch_pack(int fd[2],
672
727
*/
673
728
warning ("no common commits" );
674
729
730
+ if (args .stateless_rpc )
731
+ packet_flush (fd [1 ]);
675
732
if (get_pack (fd , pack_lockfile ))
676
733
die ("git fetch-pack: fetch failed." );
677
734
@@ -742,6 +799,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
742
799
struct ref * ref = NULL ;
743
800
char * dest = NULL , * * heads ;
744
801
int fd [2 ];
802
+ char * pack_lockfile = NULL ;
803
+ char * * pack_lockfile_ptr = NULL ;
745
804
struct child_process * conn ;
746
805
747
806
nr_heads = 0 ;
@@ -791,6 +850,15 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
791
850
args .no_progress = 1 ;
792
851
continue ;
793
852
}
853
+ if (!strcmp ("--stateless-rpc" , arg )) {
854
+ args .stateless_rpc = 1 ;
855
+ continue ;
856
+ }
857
+ if (!strcmp ("--lock-pack" , arg )) {
858
+ args .lock_pack = 1 ;
859
+ pack_lockfile_ptr = & pack_lockfile ;
860
+ continue ;
861
+ }
794
862
usage (fetch_pack_usage );
795
863
}
796
864
dest = (char * )arg ;
@@ -801,19 +869,27 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
801
869
if (!dest )
802
870
usage (fetch_pack_usage );
803
871
804
- conn = git_connect (fd , (char * )dest , args .uploadpack ,
805
- args .verbose ? CONNECT_VERBOSE : 0 );
806
- if (conn ) {
807
- get_remote_heads (fd [0 ], & ref , 0 , NULL , 0 , NULL );
808
-
809
- ref = fetch_pack (& args , fd , conn , ref , dest , nr_heads , heads , NULL );
810
- close (fd [0 ]);
811
- close (fd [1 ]);
812
- if (finish_connect (conn ))
813
- ref = NULL ;
872
+ if (args .stateless_rpc ) {
873
+ conn = NULL ;
874
+ fd [0 ] = 0 ;
875
+ fd [1 ] = 1 ;
814
876
} else {
815
- ref = NULL ;
877
+ conn = git_connect (fd , (char * )dest , args .uploadpack ,
878
+ args .verbose ? CONNECT_VERBOSE : 0 );
879
+ }
880
+
881
+ get_remote_heads (fd [0 ], & ref , 0 , NULL , 0 , NULL );
882
+
883
+ ref = fetch_pack (& args , fd , conn , ref , dest ,
884
+ nr_heads , heads , pack_lockfile_ptr );
885
+ if (pack_lockfile ) {
886
+ printf ("lock %s\n" , pack_lockfile );
887
+ fflush (stdout );
816
888
}
889
+ close (fd [0 ]);
890
+ close (fd [1 ]);
891
+ if (finish_connect (conn ))
892
+ ref = NULL ;
817
893
ret = !ref ;
818
894
819
895
if (!ret && nr_heads ) {
0 commit comments