upload-pack: add 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 079aa97e24daff1329f041e00ba621b0afd59163
2 files changed +50 -4
object.h
+1 -1
@@ -31,7 +31,7 @@ struct object_array {
31 * revision.h: 0---------10 26
32 * fetch-pack.c: 0---4
33 * walker.c: 0-2
34 - * upload-pack.c: 11----------------19
34 + * upload-pack.c: 4 11----------------19
35 * builtin/blame.c: 12-13
36 * bisect.c: 16
37 * bundle.c: 16
upload-pack.c
+49 -3
@@ -456,7 +456,8 @@ static int is_our_ref(struct object *o)
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)
459 + struct object_array *src,
460 + struct object_array *reachable)
461 {
462 static const char *argv[] = {
463 "rev-list", "--stdin", NULL,
@@ -487,6 +488,8 @@ static int do_reachable_revlist(struct child_process *cmd,
488 o = get_indexed_object(--i);
489 if (!o)
490 continue;
491 + if (reachable && o->type == OBJ_COMMIT)
492 + o->flags &= ~TMP_MARK;
493 if (!is_our_ref(o))
494 continue;
495 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
@@ -496,8 +499,13 @@ static int do_reachable_revlist(struct child_process *cmd,
499 namebuf[40] = '\n';
500 for (i = 0; i < src->nr; i++) {
501 o = src->objects[i].item;
499 - if (is_our_ref(o))
502 + if (is_our_ref(o)) {
503 + if (reachable)
504 + add_object_array(o, NULL, reachable);
505 continue;
506 + }
507 + if (reachable && o->type == OBJ_COMMIT)
508 + o->flags |= TMP_MARK;
509 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
510 if (write_in_full(cmd->in, namebuf, 41) < 0)
511 goto error;
@@ -518,13 +526,51 @@ error:
526 return -1;
527 }
528
529 +static int get_reachable_list(struct object_array *src,
530 + struct object_array *reachable)
531 +{
532 + struct child_process cmd = CHILD_PROCESS_INIT;
533 + int i;
534 + struct object *o;
535 + char namebuf[42]; /* ^ + SHA-1 + LF */
536 +
537 + if (do_reachable_revlist(&cmd, src, reachable) < 0)
538 + return -1;
539 +
540 + while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
541 + struct object_id sha1;
542 +
543 + if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
544 + break;
545 +
546 + o = lookup_object(sha1.hash);
547 + if (o && o->type == OBJ_COMMIT) {
548 + o->flags &= ~TMP_MARK;
549 + }
550 + }
551 + for (i = get_max_object_index(); 0 < i; i--) {
552 + o = get_indexed_object(i - 1);
553 + if (o && o->type == OBJ_COMMIT &&
554 + (o->flags & TMP_MARK)) {
555 + add_object_array(o, NULL, reachable);
556 + o->flags &= ~TMP_MARK;
557 + }
558 + }
559 + close(cmd.out);
560 +
561 + if (finish_command(&cmd))
562 + return -1;
563 +
564 + return 0;
565 +}
566 +
567 static int has_unreachable(struct object_array *src)
568 {
569 struct child_process cmd = CHILD_PROCESS_INIT;
570 char buf[1];
571 int i;
572
527 - if (do_reachable_revlist(&cmd, src) < 0)
573 + if (do_reachable_revlist(&cmd, src, NULL) < 0)
574 return 1;
575
576 /*