Skip to content

Commit 079aa97

Browse files
pcloudsgitster
authored andcommitted
upload-pack: add get_reachable_list()
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2997178 commit 079aa97

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct object_array {
3131
* revision.h: 0---------10 26
3232
* fetch-pack.c: 0---4
3333
* walker.c: 0-2
34-
* upload-pack.c: 11----------------19
34+
* upload-pack.c: 4 11----------------19
3535
* builtin/blame.c: 12-13
3636
* bisect.c: 16
3737
* bundle.c: 16

upload-pack.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ static int is_our_ref(struct object *o)
456456
* on successful case, it's up to the caller to close cmd->out
457457
*/
458458
static int do_reachable_revlist(struct child_process *cmd,
459-
struct object_array *src)
459+
struct object_array *src,
460+
struct object_array *reachable)
460461
{
461462
static const char *argv[] = {
462463
"rev-list", "--stdin", NULL,
@@ -487,6 +488,8 @@ static int do_reachable_revlist(struct child_process *cmd,
487488
o = get_indexed_object(--i);
488489
if (!o)
489490
continue;
491+
if (reachable && o->type == OBJ_COMMIT)
492+
o->flags &= ~TMP_MARK;
490493
if (!is_our_ref(o))
491494
continue;
492495
memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
@@ -496,8 +499,13 @@ static int do_reachable_revlist(struct child_process *cmd,
496499
namebuf[40] = '\n';
497500
for (i = 0; i < src->nr; i++) {
498501
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);
500505
continue;
506+
}
507+
if (reachable && o->type == OBJ_COMMIT)
508+
o->flags |= TMP_MARK;
501509
memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
502510
if (write_in_full(cmd->in, namebuf, 41) < 0)
503511
goto error;
@@ -518,13 +526,51 @@ static int do_reachable_revlist(struct child_process *cmd,
518526
return -1;
519527
}
520528

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+
521567
static int has_unreachable(struct object_array *src)
522568
{
523569
struct child_process cmd = CHILD_PROCESS_INIT;
524570
char buf[1];
525571
int i;
526572

527-
if (do_reachable_revlist(&cmd, src) < 0)
573+
if (do_reachable_revlist(&cmd, src, NULL) < 0)
528574
return 1;
529575

530576
/*

0 commit comments

Comments
 (0)