Skip to content

Commit 446d5d9

Browse files
peffgitster
authored andcommitted
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 <[email protected]>
1 parent 903fc7d commit 446d5d9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

builtin/receive-pack.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,12 +1634,17 @@ static const char *parse_pack_header(struct pack_header *hdr)
16341634

16351635
static const char *pack_lockfile;
16361636

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+
16371643
static const char *unpack(int err_fd, struct shallow_info *si)
16381644
{
16391645
struct pack_header hdr;
16401646
const char *hdr_err;
16411647
int status;
1642-
char hdr_arg[38];
16431648
struct child_process child = CHILD_PROCESS_INIT;
16441649
int fsck_objects = (receive_fsck_objects >= 0
16451650
? receive_fsck_objects
@@ -1653,9 +1658,6 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16531658
close(err_fd);
16541659
return hdr_err;
16551660
}
1656-
snprintf(hdr_arg, sizeof(hdr_arg),
1657-
"--pack_header=%"PRIu32",%"PRIu32,
1658-
ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
16591661

16601662
if (si->nr_ours || si->nr_theirs) {
16611663
alt_shallow_file = setup_temporary_shallow(si->shallow);
@@ -1679,7 +1681,8 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16791681
tmp_objdir_add_as_alternate(tmp_objdir);
16801682

16811683
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);
16831686
if (quiet)
16841687
argv_array_push(&child.args, "-q");
16851688
if (fsck_objects)
@@ -1697,8 +1700,8 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16971700
} else {
16981701
char hostname[256];
16991702

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);
17021705

17031706
if (gethostname(hostname, sizeof(hostname)))
17041707
xsnprintf(hostname, sizeof(hostname), "localhost");

0 commit comments

Comments
 (0)