Skip to content

Commit 984a43b

Browse files
peffgitster
authored andcommitted
fetch-pack: use argv_array for index-pack / unpack-objects
This cleans up a magic number that must be kept in sync with the rest of the code (the number of argv slots). It also lets us drop some fixed buffers and an sprintf (since we can now use argv_array_pushf). We do still have to keep one fixed buffer for calling gethostname, but at least now the size computations for it are much simpler. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43bb66a commit 984a43b

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

fetch-pack.c

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,10 @@ static int get_pack(struct fetch_pack_args *args,
681681
int xd[2], char **pack_lockfile)
682682
{
683683
struct async demux;
684-
const char *argv[22];
685-
char keep_arg[256];
686-
char hdr_arg[256];
687-
const char **av, *cmd_name;
688684
int do_keep = args->keep_pack;
685+
const char *cmd_name;
686+
struct pack_header header;
687+
int pass_header = 0;
689688
struct child_process cmd = CHILD_PROCESS_INIT;
690689
int ret;
691690

@@ -705,62 +704,61 @@ static int get_pack(struct fetch_pack_args *args,
705704
else
706705
demux.out = xd[0];
707706

708-
cmd.argv = argv;
709-
av = argv;
710-
*hdr_arg = 0;
711707
if (!args->keep_pack && unpack_limit) {
712-
struct pack_header header;
713708

714709
if (read_pack_header(demux.out, &header))
715710
die("protocol error: bad pack header");
716-
snprintf(hdr_arg, sizeof(hdr_arg),
717-
"--pack_header=%"PRIu32",%"PRIu32,
718-
ntohl(header.hdr_version), ntohl(header.hdr_entries));
711+
pass_header = 1;
719712
if (ntohl(header.hdr_entries) < unpack_limit)
720713
do_keep = 0;
721714
else
722715
do_keep = 1;
723716
}
724717

725718
if (alternate_shallow_file) {
726-
*av++ = "--shallow-file";
727-
*av++ = alternate_shallow_file;
719+
argv_array_push(&cmd.args, "--shallow-file");
720+
argv_array_push(&cmd.args, alternate_shallow_file);
728721
}
729722

730723
if (do_keep) {
731724
if (pack_lockfile)
732725
cmd.out = -1;
733-
*av++ = cmd_name = "index-pack";
734-
*av++ = "--stdin";
726+
cmd_name = "index-pack";
727+
argv_array_push(&cmd.args, cmd_name);
728+
argv_array_push(&cmd.args, "--stdin");
735729
if (!args->quiet && !args->no_progress)
736-
*av++ = "-v";
730+
argv_array_push(&cmd.args, "-v");
737731
if (args->use_thin_pack)
738-
*av++ = "--fix-thin";
732+
argv_array_push(&cmd.args, "--fix-thin");
739733
if (args->lock_pack || unpack_limit) {
740-
int s = sprintf(keep_arg,
741-
"--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid());
742-
if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
743-
strcpy(keep_arg + s, "localhost");
744-
*av++ = keep_arg;
734+
char hostname[256];
735+
if (gethostname(hostname, sizeof(hostname)))
736+
xsnprintf(hostname, sizeof(hostname), "localhost");
737+
argv_array_pushf(&cmd.args,
738+
"--keep=fetch-pack %"PRIuMAX " on %s",
739+
(uintmax_t)getpid(), hostname);
745740
}
746741
if (args->check_self_contained_and_connected)
747-
*av++ = "--check-self-contained-and-connected";
742+
argv_array_push(&cmd.args, "--check-self-contained-and-connected");
748743
}
749744
else {
750-
*av++ = cmd_name = "unpack-objects";
745+
cmd_name = "unpack-objects";
746+
argv_array_push(&cmd.args, cmd_name);
751747
if (args->quiet || args->no_progress)
752-
*av++ = "-q";
748+
argv_array_push(&cmd.args, "-q");
753749
args->check_self_contained_and_connected = 0;
754750
}
755-
if (*hdr_arg)
756-
*av++ = hdr_arg;
751+
752+
if (pass_header)
753+
argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
754+
ntohl(header.hdr_version),
755+
ntohl(header.hdr_entries));
757756
if (fetch_fsck_objects >= 0
758757
? fetch_fsck_objects
759758
: transfer_fsck_objects >= 0
760759
? transfer_fsck_objects
761760
: 0)
762-
*av++ = "--strict";
763-
*av++ = NULL;
761+
argv_array_push(&cmd.args, "--strict");
764762

765763
cmd.in = demux.out;
766764
cmd.git_cmd = 1;

0 commit comments

Comments
 (0)