Skip to content

Commit a698d67

Browse files
rscharfegitster
authored andcommitted
upload-pack: use buffered I/O to talk to rev-list
Like f0bca72 (send-pack: use buffered I/O to talk to pack-objects, 2016-06-08), significantly reduce the number of system calls and simplify the code for sending object IDs to rev-list by using stdio's buffering. Take care to handle errors immediately to get the correct error code, and to flush the buffer explicitly before closing the stream in order to catch any write errors for these last bytes. Helped-by: Chris Torek <[email protected]> Helped-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6af3b00 commit a698d67

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

upload-pack.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,8 @@ static int do_reachable_revlist(struct child_process *cmd,
595595
"rev-list", "--stdin", NULL,
596596
};
597597
struct object *o;
598-
char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
598+
FILE *cmd_in = NULL;
599599
int i;
600-
const unsigned hexsz = the_hash_algo->hexsz;
601600

602601
cmd->argv = argv;
603602
cmd->git_cmd = 1;
@@ -615,8 +614,8 @@ static int do_reachable_revlist(struct child_process *cmd,
615614
if (start_command(cmd))
616615
goto error;
617616

618-
namebuf[0] = '^';
619-
namebuf[hexsz + 1] = '\n';
617+
cmd_in = xfdopen(cmd->in, "w");
618+
620619
for (i = get_max_object_index(); 0 < i; ) {
621620
o = get_indexed_object(--i);
622621
if (!o)
@@ -625,11 +624,9 @@ static int do_reachable_revlist(struct child_process *cmd,
625624
o->flags &= ~TMP_MARK;
626625
if (!is_our_ref(o, allow_uor))
627626
continue;
628-
memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
629-
if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
627+
if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
630628
goto error;
631629
}
632-
namebuf[hexsz] = '\n';
633630
for (i = 0; i < src->nr; i++) {
634631
o = src->objects[i].item;
635632
if (is_our_ref(o, allow_uor)) {
@@ -639,11 +636,12 @@ static int do_reachable_revlist(struct child_process *cmd,
639636
}
640637
if (reachable && o->type == OBJ_COMMIT)
641638
o->flags |= TMP_MARK;
642-
memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
643-
if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
639+
if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
644640
goto error;
645641
}
646-
close(cmd->in);
642+
if (ferror(cmd_in) || fflush(cmd_in))
643+
goto error;
644+
fclose(cmd_in);
647645
cmd->in = -1;
648646
sigchain_pop(SIGPIPE);
649647

@@ -652,8 +650,8 @@ static int do_reachable_revlist(struct child_process *cmd,
652650
error:
653651
sigchain_pop(SIGPIPE);
654652

655-
if (cmd->in >= 0)
656-
close(cmd->in);
653+
if (cmd_in)
654+
fclose(cmd_in);
657655
if (cmd->out >= 0)
658656
close(cmd->out);
659657
return -1;

0 commit comments

Comments
 (0)