fetch-pack: simplify add_sought_entry

We have two variants of this function, one that takes a string and one that takes a ptr/len combo. But we only call the latter with the length of a NUL-terminated string, so our first simplification is to drop it in favor of the string variant. Since we know we have a string, we can also replace the manual memory computation with a call to alloc_ref(). Furthermore, we can rely on get_oid_hex() to complain if it hits the end of the string. That means we can simplify the check for "<sha1> <ref>" versus just "<ref>". Rather than manage the ptr/len pair, we can just bump the start of our string forward. The original code over-allocated based on the original "namelen" (which wasn't _wrong_, but was simply wasteful and confusing). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 22, 2016 at 17:44 UTC 5545f057d4684de99feca0ca480297e3d71fb812
1 file changed +9 -18
builtin/fetch-pack.c
+9 -18
@@ -10,33 +10,24 @@ static const char fetch_pack_usage[] =
10 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
11 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
12
13 -static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc,
14 - const char *name, int namelen)
13 +static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
14 + const char *name)
15 {
16 - struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
16 + struct ref *ref;
17 struct object_id oid;
18 - const int chunksz = GIT_SHA1_HEXSZ + 1;
18
20 - if (namelen > chunksz && name[chunksz - 1] == ' ' &&
21 - !get_oid_hex(name, &oid)) {
22 - oidcpy(&ref->old_oid, &oid);
23 - name += chunksz;
24 - namelen -= chunksz;
25 - }
19 + if (!get_oid_hex(name, &oid) && name[GIT_SHA1_HEXSZ] == ' ')
20 + name += GIT_SHA1_HEXSZ + 1;
21 + else
22 + oidclr(&oid);
23
27 - memcpy(ref->name, name, namelen);
28 - ref->name[namelen] = '\0';
24 + ref = alloc_ref(name);
25 + oidcpy(&ref->old_oid, &oid);
26 (*nr)++;
27 ALLOC_GROW(*sought, *nr, *alloc);
28 (*sought)[*nr - 1] = ref;
29 }
30
34 -static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
35 - const char *string)
36 -{
37 - add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
38 -}
39 -
31 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
32 {
33 int i, ret;