@@ -452,21 +452,24 @@ static int is_our_ref(struct object *o)
452452 return o -> flags & ((allow_hidden_ref ? HIDDEN_REF : 0 ) | OUR_REF );
453453}
454454
455- static int has_unreachable (struct object_array * src )
455+ /*
456+ * on successful case, it's up to the caller to close cmd->out
457+ */
458+ static int do_reachable_revlist (struct child_process * cmd ,
459+ struct object_array * src )
456460{
457461 static const char * argv [] = {
458462 "rev-list" , "--stdin" , NULL ,
459463 };
460- static struct child_process cmd = CHILD_PROCESS_INIT ;
461464 struct object * o ;
462465 char namebuf [42 ]; /* ^ + SHA-1 + LF */
463466 int i ;
464467
465- cmd . argv = argv ;
466- cmd . git_cmd = 1 ;
467- cmd . no_stderr = 1 ;
468- cmd . in = -1 ;
469- cmd . out = -1 ;
468+ cmd -> argv = argv ;
469+ cmd -> git_cmd = 1 ;
470+ cmd -> no_stderr = 1 ;
471+ cmd -> in = -1 ;
472+ cmd -> out = -1 ;
470473
471474 /*
472475 * If the next rev-list --stdin encounters an unknown commit,
@@ -475,7 +478,7 @@ static int has_unreachable(struct object_array *src)
475478 */
476479 sigchain_push (SIGPIPE , SIG_IGN );
477480
478- if (start_command (& cmd ))
481+ if (start_command (cmd ))
479482 goto error ;
480483
481484 namebuf [0 ] = '^' ;
@@ -487,7 +490,7 @@ static int has_unreachable(struct object_array *src)
487490 if (!is_our_ref (o ))
488491 continue ;
489492 memcpy (namebuf + 1 , oid_to_hex (& o -> oid ), GIT_SHA1_HEXSZ );
490- if (write_in_full (cmd . in , namebuf , 42 ) < 0 )
493+ if (write_in_full (cmd -> in , namebuf , 42 ) < 0 )
491494 goto error ;
492495 }
493496 namebuf [40 ] = '\n' ;
@@ -496,17 +499,39 @@ static int has_unreachable(struct object_array *src)
496499 if (is_our_ref (o ))
497500 continue ;
498501 memcpy (namebuf , oid_to_hex (& o -> oid ), GIT_SHA1_HEXSZ );
499- if (write_in_full (cmd . in , namebuf , 41 ) < 0 )
502+ if (write_in_full (cmd -> in , namebuf , 41 ) < 0 )
500503 goto error ;
501504 }
502- close (cmd .in );
503- cmd .in = -1 ;
505+ close (cmd -> in );
506+ cmd -> in = -1 ;
507+ sigchain_pop (SIGPIPE );
508+
509+ return 0 ;
510+
511+ error :
512+ sigchain_pop (SIGPIPE );
513+
514+ if (cmd -> in >= 0 )
515+ close (cmd -> in );
516+ if (cmd -> out >= 0 )
517+ close (cmd -> out );
518+ return -1 ;
519+ }
520+
521+ static int has_unreachable (struct object_array * src )
522+ {
523+ struct child_process cmd = CHILD_PROCESS_INIT ;
524+ char buf [1 ];
525+ int i ;
526+
527+ if (do_reachable_revlist (& cmd , src ) < 0 )
528+ return 1 ;
504529
505530 /*
506531 * The commits out of the rev-list are not ancestors of
507532 * our ref.
508533 */
509- i = read_in_full (cmd .out , namebuf , 1 );
534+ i = read_in_full (cmd .out , buf , 1 );
510535 if (i )
511536 goto error ;
512537 close (cmd .out );
@@ -520,16 +545,11 @@ static int has_unreachable(struct object_array *src)
520545 if (finish_command (& cmd ))
521546 goto error ;
522547
523- sigchain_pop (SIGPIPE );
524-
525548 /* All the non-tip ones are ancestors of what we advertised */
526549 return 0 ;
527550
528551error :
529552 sigchain_pop (SIGPIPE );
530-
531- if (cmd .in >= 0 )
532- close (cmd .in );
533553 if (cmd .out >= 0 )
534554 close (cmd .out );
535555 return 1 ;
0 commit comments