receive-pack: print --pack-header directly into argv array

After receive-pack reads the pack header from the client, it feeds the already-read part to index-pack and unpack-objects via their --pack-header command-line options. To do so, we format it into a fixed buffer, then duplicate it into the child's argv_array. Our buffer is long enough to handle any possible input, so this isn't wrong. But it's more complicated than it needs to be; we can just argv_array_pushf() the final value and avoid the intermediate copy. This drops the magic number and is more efficient, too. Note that we need to push to the argv_array in order, which means we can't do the push until we are in the "unpack-objects versus index-pack" conditional. Rather than duplicate the slightly complicated format specifier, I pushed it into a helper function. Signed-off-by: Jeff King <peff@peff.net>

Jeff King committed Mar 28, 2017 at 15:46 UTC 446d5d911214fd3d61921478c98d4a88f84e410c
1 file changed +10 -7
builtin/receive-pack.c
+10 -7
@@ -1634,12 +1634,17 @@ static const char *parse_pack_header(struct pack_header *hdr)
1634
1635 static const char *pack_lockfile;
1636
1637 +static void push_header_arg(struct argv_array *args, struct pack_header *hdr)
1638 +{
1639 + argv_array_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
1640 + ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
1641 +}
1642 +
1643 static const char *unpack(int err_fd, struct shallow_info *si)
1644 {
1645 struct pack_header hdr;
1646 const char *hdr_err;
1647 int status;
1642 - char hdr_arg[38];
1648 struct child_process child = CHILD_PROCESS_INIT;
1649 int fsck_objects = (receive_fsck_objects >= 0
1650 ? receive_fsck_objects
@@ -1653,9 +1658,6 @@ static const char *unpack(int err_fd, struct shallow_info *si)
1658 close(err_fd);
1659 return hdr_err;
1660 }
1656 - snprintf(hdr_arg, sizeof(hdr_arg),
1657 - "--pack_header=%"PRIu32",%"PRIu32,
1658 - ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
1661
1662 if (si->nr_ours || si->nr_theirs) {
1663 alt_shallow_file = setup_temporary_shallow(si->shallow);
@@ -1679,7 +1681,8 @@ static const char *unpack(int err_fd, struct shallow_info *si)
1681 tmp_objdir_add_as_alternate(tmp_objdir);
1682
1683 if (ntohl(hdr.hdr_entries) < unpack_limit) {
1682 - argv_array_pushl(&child.args, "unpack-objects", hdr_arg, NULL);
1684 + argv_array_push(&child.args, "unpack-objects");
1685 + push_header_arg(&child.args, &hdr);
1686 if (quiet)
1687 argv_array_push(&child.args, "-q");
1688 if (fsck_objects)
@@ -1697,8 +1700,8 @@ static const char *unpack(int err_fd, struct shallow_info *si)
1700 } else {
1701 char hostname[256];
1702
1700 - argv_array_pushl(&child.args, "index-pack",
1701 - "--stdin", hdr_arg, NULL);
1703 + argv_array_pushl(&child.args, "index-pack", "--stdin", NULL);
1704 + push_header_arg(&child.args, &hdr);
1705
1706 if (gethostname(hostname, sizeof(hostname)))
1707 xsnprintf(hostname, sizeof(hostname), "localhost");