Skip to content

Commit 0e3ae59

Browse files
peffdscho
authored andcommitted
packfile: factor out --pack_header argument parsing
Both index-pack and unpack-objects accept a --pack_header argument. This is an undocumented internal argument used by receive-pack and fetch to pass along information about the header of the pack, which they've already read from the incoming stream. In preparation for a bugfix, let's factor the duplicated code into a common helper. The callers are still responsible for identifying the option. While this could likewise be factored out, it is more flexible this way (e.g., if they ever started using parse-options and wanted to handle both the stuck and unstuck forms). Likewise, the callers are responsible for reporting errors, though they both just call die(). I've tweaked unpack-objects to match index-pack in marking the error for translation. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6b442e6 commit 0e3ae59

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

builtin/index-pack.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,18 +1955,10 @@ int cmd_index_pack(int argc,
19551955
nr_threads = 1;
19561956
}
19571957
} else if (starts_with(arg, "--pack_header=")) {
1958-
struct pack_header *hdr;
1959-
char *c;
1960-
1961-
hdr = (struct pack_header *)input_buffer;
1962-
hdr->hdr_signature = htonl(PACK_SIGNATURE);
1963-
hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
1964-
if (*c != ',')
1965-
die(_("bad %s"), arg);
1966-
hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
1967-
if (*c)
1958+
if (parse_pack_header_option(arg + 14,
1959+
input_buffer,
1960+
&input_len) < 0)
19681961
die(_("bad %s"), arg);
1969-
input_len = sizeof(*hdr);
19701962
} else if (!strcmp(arg, "-v")) {
19711963
verbose = 1;
19721964
} else if (!strcmp(arg, "--progress-title")) {

builtin/unpack-objects.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "progress.h"
1919
#include "decorate.h"
2020
#include "fsck.h"
21+
#include "packfile.h"
2122

2223
static int dry_run, quiet, recover, has_errors, strict;
2324
static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
@@ -645,18 +646,9 @@ int cmd_unpack_objects(int argc,
645646
continue;
646647
}
647648
if (starts_with(arg, "--pack_header=")) {
648-
struct pack_header *hdr;
649-
char *c;
650-
651-
hdr = (struct pack_header *)buffer;
652-
hdr->hdr_signature = htonl(PACK_SIGNATURE);
653-
hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
654-
if (*c != ',')
655-
die("bad %s", arg);
656-
hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
657-
if (*c)
658-
die("bad %s", arg);
659-
len = sizeof(*hdr);
649+
if (parse_pack_header_option(arg + 14,
650+
buffer, &len) < 0)
651+
die(_("bad %s"), arg);
660652
continue;
661653
}
662654
if (skip_prefix(arg, "--max-input-size=", &arg)) {

packfile.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,3 +2315,20 @@ int is_promisor_object(struct repository *r, const struct object_id *oid)
23152315
}
23162316
return oidset_contains(&promisor_objects, oid);
23172317
}
2318+
2319+
int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len)
2320+
{
2321+
struct pack_header *hdr;
2322+
char *c;
2323+
2324+
hdr = (struct pack_header *)out;
2325+
hdr->hdr_signature = htonl(PACK_SIGNATURE);
2326+
hdr->hdr_version = htonl(strtoul(in, &c, 10));
2327+
if (*c != ',')
2328+
return -1;
2329+
hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
2330+
if (*c)
2331+
return -1;
2332+
*len = sizeof(*hdr);
2333+
return 0;
2334+
}

packfile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,10 @@ int is_promisor_object(struct repository *r, const struct object_id *oid);
216216
int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
217217
size_t idx_size, struct packed_git *p);
218218

219+
/*
220+
* Parse a --pack_header option as accepted by index-pack and unpack-objects,
221+
* turning it into the matching bytes we'd find in a pack.
222+
*/
223+
int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len);
224+
219225
#endif

0 commit comments

Comments
 (0)