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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 24, 2015 at 17:07 UTC 984a43b902f005520e27268fecd10b9b517c0b17
1 file changed +27 -29
fetch-pack.c
+27 -29
@@ -681,11 +681,10 @@ static int get_pack(struct fetch_pack_args *args,
681 int xd[2], char **pack_lockfile)
682 {
683 struct async demux;
684 - const char *argv[22];
685 - char keep_arg[256];
686 - char hdr_arg[256];
687 - const char **av, *cmd_name;
684 int do_keep = args->keep_pack;
685 + const char *cmd_name;
686 + struct pack_header header;
687 + int pass_header = 0;
688 struct child_process cmd = CHILD_PROCESS_INIT;
689 int ret;
690
@@ -705,17 +704,11 @@ static int get_pack(struct fetch_pack_args *args,
704 else
705 demux.out = xd[0];
706
708 - cmd.argv = argv;
709 - av = argv;
710 - *hdr_arg = 0;
707 if (!args->keep_pack && unpack_limit) {
712 - struct pack_header header;
708
709 if (read_pack_header(demux.out, &header))
710 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;
712 if (ntohl(header.hdr_entries) < unpack_limit)
713 do_keep = 0;
714 else
@@ -723,44 +716,49 @@ static int get_pack(struct fetch_pack_args *args,
716 }
717
718 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);
721 }
722
723 if (do_keep) {
724 if (pack_lockfile)
725 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");
729 if (!args->quiet && !args->no_progress)
736 - *av++ = "-v";
730 + argv_array_push(&cmd.args, "-v");
731 if (args->use_thin_pack)
738 - *av++ = "--fix-thin";
732 + argv_array_push(&cmd.args, "--fix-thin");
733 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);
740 }
741 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");
743 }
744 else {
750 - *av++ = cmd_name = "unpack-objects";
745 + cmd_name = "unpack-objects";
746 + argv_array_push(&cmd.args, cmd_name);
747 if (args->quiet || args->no_progress)
752 - *av++ = "-q";
748 + argv_array_push(&cmd.args, "-q");
749 args->check_self_contained_and_connected = 0;
750 }
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));
756 if (fetch_fsck_objects >= 0
757 ? fetch_fsck_objects
758 : transfer_fsck_objects >= 0
759 ? transfer_fsck_objects
760 : 0)
762 - *av++ = "--strict";
763 - *av++ = NULL;
761 + argv_array_push(&cmd.args, "--strict");
762
763 cmd.in = demux.out;
764 cmd.git_cmd = 1;