Skip to content

Commit 5545f05

Browse files
peffgitster
authored andcommitted
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>
1 parent a78c188 commit 5545f05

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

builtin/fetch-pack.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,24 @@ static const char fetch_pack_usage[] =
1010
"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
1111
"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
1212

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)
1515
{
16-
struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
16+
struct ref *ref;
1717
struct object_id oid;
18-
const int chunksz = GIT_SHA1_HEXSZ + 1;
1918

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

27-
memcpy(ref->name, name, namelen);
28-
ref->name[namelen] = '\0';
24+
ref = alloc_ref(name);
25+
oidcpy(&ref->old_oid, &oid);
2926
(*nr)++;
3027
ALLOC_GROW(*sought, *nr, *alloc);
3128
(*sought)[*nr - 1] = ref;
3229
}
3330

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-
4031
int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
4132
{
4233
int i, ret;

0 commit comments

Comments
 (0)