@@ -11,7 +11,6 @@ static const char send_pack_usage[] =
1111" --all and explicit <ref> specification are mutually exclusive." ;
1212
1313static struct send_pack_args args = {
14- /* .receivepack = */ "git-receive-pack" ,
1514};
1615
1716static int feed_object (const unsigned char * sha1 , int fd , int negative )
@@ -31,7 +30,7 @@ static int feed_object(const unsigned char *sha1, int fd, int negative)
3130/*
3231 * Make a pack stream and spit it out into file descriptor fd
3332 */
34- static int pack_objects (int fd , struct ref * refs , struct extra_have_objects * extra )
33+ static int pack_objects (int fd , struct ref * refs , struct extra_have_objects * extra , struct send_pack_args * args )
3534{
3635 /*
3736 * The child becomes pack-objects --revs; we feed
@@ -49,7 +48,7 @@ static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *ext
4948 struct child_process po ;
5049 int i ;
5150
52- if (args . use_thin_pack )
51+ if (args -> use_thin_pack )
5352 argv [4 ] = "--thin" ;
5453 memset (& po , 0 , sizeof (po ));
5554 po .argv = argv ;
@@ -83,8 +82,6 @@ static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *ext
8382 return 0 ;
8483}
8584
86- static struct ref * remote_refs , * * remote_tail ;
87-
8885static int receive_status (int in , struct ref * refs )
8986{
9087 struct ref * hint ;
@@ -172,16 +169,6 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
172169 }
173170}
174171
175- static const char * prettify_ref (const struct ref * ref )
176- {
177- const char * name = ref -> name ;
178- return name + (
179- !prefixcmp (name , "refs/heads/" ) ? 11 :
180- !prefixcmp (name , "refs/tags/" ) ? 10 :
181- !prefixcmp (name , "refs/remotes/" ) ? 13 :
182- 0 );
183- }
184-
185172#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
186173
187174static void print_ref_status (char flag , const char * summary , struct ref * to , struct ref * from , const char * msg )
@@ -310,47 +297,29 @@ static int refs_pushed(struct ref *ref)
310297 return 0 ;
311298}
312299
313- static int do_send_pack (int in , int out , struct remote * remote , const char * dest , int nr_refspec , const char * * refspec )
300+ int send_pack (struct send_pack_args * args ,
301+ int fd [], struct child_process * conn ,
302+ struct ref * remote_refs ,
303+ struct extra_have_objects * extra_have )
314304{
315- struct ref * ref , * local_refs ;
305+ int in = fd [0 ];
306+ int out = fd [1 ];
307+ struct ref * ref ;
316308 int new_refs ;
317309 int ask_for_status_report = 0 ;
318310 int allow_deleting_refs = 0 ;
319311 int expect_status_report = 0 ;
320- int flags = MATCH_REFS_NONE ;
321312 int ret ;
322- struct extra_have_objects extra_have ;
323-
324- memset (& extra_have , 0 , sizeof (extra_have ));
325- if (args .send_all )
326- flags |= MATCH_REFS_ALL ;
327- if (args .send_mirror )
328- flags |= MATCH_REFS_MIRROR ;
329-
330- /* No funny business with the matcher */
331- remote_tail = get_remote_heads (in , & remote_refs , 0 , NULL , REF_NORMAL ,
332- & extra_have );
333- local_refs = get_local_heads ();
334313
335314 /* Does the other end support the reporting? */
336315 if (server_supports ("report-status" ))
337316 ask_for_status_report = 1 ;
338317 if (server_supports ("delete-refs" ))
339318 allow_deleting_refs = 1 ;
340319
341- /* match them up */
342- if (!remote_tail )
343- remote_tail = & remote_refs ;
344- if (match_refs (local_refs , remote_refs , & remote_tail ,
345- nr_refspec , refspec , flags )) {
346- close (out );
347- return -1 ;
348- }
349-
350320 if (!remote_refs ) {
351321 fprintf (stderr , "No refs in common and none specified; doing nothing.\n"
352322 "Perhaps you should specify a branch such as 'master'.\n" );
353- close (out );
354323 return 0 ;
355324 }
356325
@@ -362,7 +331,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
362331
363332 if (ref -> peer_ref )
364333 hashcpy (ref -> new_sha1 , ref -> peer_ref -> new_sha1 );
365- else if (!args . send_mirror )
334+ else if (!args -> send_mirror )
366335 continue ;
367336
368337 ref -> deletion = is_null_sha1 (ref -> new_sha1 );
@@ -401,15 +370,15 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
401370 (!has_sha1_file (ref -> old_sha1 )
402371 || !ref_newer (ref -> new_sha1 , ref -> old_sha1 ));
403372
404- if (ref -> nonfastforward && !ref -> force && !args . force_update ) {
373+ if (ref -> nonfastforward && !ref -> force && !args -> force_update ) {
405374 ref -> status = REF_STATUS_REJECT_NONFASTFORWARD ;
406375 continue ;
407376 }
408377
409378 if (!ref -> deletion )
410379 new_refs ++ ;
411380
412- if (!args . dry_run ) {
381+ if (!args -> dry_run ) {
413382 char * old_hex = sha1_to_hex (ref -> old_sha1 );
414383 char * new_hex = sha1_to_hex (ref -> new_sha1 );
415384
@@ -430,27 +399,19 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
430399 }
431400
432401 packet_flush (out );
433- if (new_refs && !args .dry_run ) {
434- if (pack_objects (out , remote_refs , & extra_have ) < 0 )
402+ if (new_refs && !args -> dry_run ) {
403+ if (pack_objects (out , remote_refs , extra_have , args ) < 0 ) {
404+ for (ref = remote_refs ; ref ; ref = ref -> next )
405+ ref -> status = REF_STATUS_NONE ;
435406 return -1 ;
407+ }
436408 }
437- else
438- close (out );
439409
440410 if (expect_status_report )
441411 ret = receive_status (in , remote_refs );
442412 else
443413 ret = 0 ;
444414
445- print_push_status (dest , remote_refs );
446-
447- if (!args .dry_run && remote ) {
448- for (ref = remote_refs ; ref ; ref = ref -> next )
449- update_tracking_ref (remote , ref );
450- }
451-
452- if (!refs_pushed (remote_refs ))
453- fprintf (stderr , "Everything up-to-date\n" );
454415 if (ret < 0 )
455416 return ret ;
456417 for (ref = remote_refs ; ref ; ref = ref -> next ) {
@@ -499,31 +460,39 @@ static void verify_remote_names(int nr_heads, const char **heads)
499460
500461int cmd_send_pack (int argc , const char * * argv , const char * prefix )
501462{
502- int i , nr_heads = 0 ;
503- const char * * heads = NULL ;
463+ int i , nr_refspecs = 0 ;
464+ const char * * refspecs = NULL ;
504465 const char * remote_name = NULL ;
505466 struct remote * remote = NULL ;
506467 const char * dest = NULL ;
468+ int fd [2 ];
469+ struct child_process * conn ;
470+ struct extra_have_objects extra_have ;
471+ struct ref * remote_refs , * * remote_tail , * local_refs ;
472+ int ret ;
473+ int send_all = 0 ;
474+ const char * receivepack = "git-receive-pack" ;
475+ int flags ;
507476
508477 argv ++ ;
509478 for (i = 1 ; i < argc ; i ++ , argv ++ ) {
510479 const char * arg = * argv ;
511480
512481 if (* arg == '-' ) {
513482 if (!prefixcmp (arg , "--receive-pack=" )) {
514- args . receivepack = arg + 15 ;
483+ receivepack = arg + 15 ;
515484 continue ;
516485 }
517486 if (!prefixcmp (arg , "--exec=" )) {
518- args . receivepack = arg + 7 ;
487+ receivepack = arg + 7 ;
519488 continue ;
520489 }
521490 if (!prefixcmp (arg , "--remote=" )) {
522491 remote_name = arg + 9 ;
523492 continue ;
524493 }
525494 if (!strcmp (arg , "--all" )) {
526- args . send_all = 1 ;
495+ send_all = 1 ;
527496 continue ;
528497 }
529498 if (!strcmp (arg , "--dry-run" )) {
@@ -552,8 +521,8 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
552521 dest = arg ;
553522 continue ;
554523 }
555- heads = (const char * * ) argv ;
556- nr_heads = argc - i ;
524+ refspecs = (const char * * ) argv ;
525+ nr_refspecs = argc - i ;
557526 break ;
558527 }
559528 if (!dest )
@@ -562,8 +531,8 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
562531 * --all and --mirror are incompatible; neither makes sense
563532 * with any refspecs.
564533 */
565- if ((heads && (args . send_all || args .send_mirror )) ||
566- ( args . send_all && args .send_mirror ))
534+ if ((refspecs && (send_all || args .send_mirror )) ||
535+ ( send_all && args .send_mirror ))
567536 usage (send_pack_usage );
568537
569538 if (remote_name ) {
@@ -574,24 +543,50 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
574543 }
575544 }
576545
577- return send_pack (& args , dest , remote , nr_heads , heads );
578- }
546+ conn = git_connect (fd , dest , receivepack , args .verbose ? CONNECT_VERBOSE : 0 );
579547
580- int send_pack (struct send_pack_args * my_args ,
581- const char * dest , struct remote * remote ,
582- int nr_heads , const char * * heads )
583- {
584- int fd [2 ], ret ;
585- struct child_process * conn ;
548+ memset (& extra_have , 0 , sizeof (extra_have ));
549+
550+ get_remote_heads (fd [0 ], & remote_refs , 0 , NULL , REF_NORMAL ,
551+ & extra_have );
586552
587- memcpy (& args , my_args , sizeof (args ));
553+ verify_remote_names (nr_refspecs , refspecs );
554+
555+ local_refs = get_local_heads ();
556+
557+ flags = MATCH_REFS_NONE ;
558+
559+ if (send_all )
560+ flags |= MATCH_REFS_ALL ;
561+ if (args .send_mirror )
562+ flags |= MATCH_REFS_MIRROR ;
563+
564+ /* match them up */
565+ remote_tail = & remote_refs ;
566+ while (* remote_tail )
567+ remote_tail = & ((* remote_tail )-> next );
568+ if (match_refs (local_refs , remote_refs , & remote_tail ,
569+ nr_refspecs , refspecs , flags )) {
570+ return -1 ;
571+ }
588572
589- verify_remote_names ( nr_heads , heads );
573+ ret = send_pack ( & args , fd , conn , remote_refs , & extra_have );
590574
591- conn = git_connect (fd , dest , args .receivepack , args .verbose ? CONNECT_VERBOSE : 0 );
592- ret = do_send_pack (fd [0 ], fd [1 ], remote , dest , nr_heads , heads );
575+ close (fd [1 ]);
593576 close (fd [0 ]);
594- /* do_send_pack always closes fd[1] */
577+
595578 ret |= finish_connect (conn );
596- return !!ret ;
579+
580+ print_push_status (dest , remote_refs );
581+
582+ if (!args .dry_run && remote ) {
583+ struct ref * ref ;
584+ for (ref = remote_refs ; ref ; ref = ref -> next )
585+ update_tracking_ref (remote , ref );
586+ }
587+
588+ if (!ret && !refs_pushed (remote_refs ))
589+ fprintf (stderr , "Everything up-to-date\n" );
590+
591+ return ret ;
597592}
0 commit comments