upload-pack: split check_unreachable() in two, prep for get_reachable_list()

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jun 12, 2016 at 17:54 UTC 2997178ee63c76a4c449f35f299e20b32956795a
1 file changed +38 -18
upload-pack.c
+38 -18
@@ -452,21 +452,24 @@ static int is_our_ref(struct object *o)
452 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
453 }
454
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)
460 {
461 static const char *argv[] = {
462 "rev-list", "--stdin", NULL,
463 };
460 - static struct child_process cmd = CHILD_PROCESS_INIT;
464 struct object *o;
465 char namebuf[42]; /* ^ + SHA-1 + LF */
466 int i;
467
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;
473
474 /*
475 * If the next rev-list --stdin encounters an unknown commit,
@@ -475,7 +478,7 @@ static int has_unreachable(struct object_array *src)
478 */
479 sigchain_push(SIGPIPE, SIG_IGN);
480
478 - if (start_command(&cmd))
481 + if (start_command(cmd))
482 goto error;
483
484 namebuf[0] = '^';
@@ -487,7 +490,7 @@ static int has_unreachable(struct object_array *src)
490 if (!is_our_ref(o))
491 continue;
492 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)
494 goto error;
495 }
496 namebuf[40] = '\n';
@@ -496,17 +499,39 @@ static int has_unreachable(struct object_array *src)
499 if (is_our_ref(o))
500 continue;
501 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)
503 goto error;
504 }
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;
529
530 /*
531 * The commits out of the rev-list are not ancestors of
532 * our ref.
533 */
509 - i = read_in_full(cmd.out, namebuf, 1);
534 + i = read_in_full(cmd.out, buf, 1);
535 if (i)
536 goto error;
537 close(cmd.out);
@@ -520,16 +545,11 @@ static int has_unreachable(struct object_array *src)
545 if (finish_command(&cmd))
546 goto error;
547
523 - sigchain_pop(SIGPIPE);
524 -
548 /* All the non-tip ones are ancestors of what we advertised */
549 return 0;
550
551 error:
552 sigchain_pop(SIGPIPE);
530 -
531 - if (cmd.in >= 0)
532 - close(cmd.in);
553 if (cmd.out >= 0)
554 close(cmd.out);
555 return 1;