1
0
mirror of https://github.com/git/git.git synced 2025-03-16 19:55:17 +00:00

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>
This commit is contained in:
Jeff King 2016-02-22 17:44:50 -05:00 committed by Junio C Hamano
parent a78c188a32
commit 5545f057d4

View File

@ -10,33 +10,24 @@ static const char fetch_pack_usage[] =
"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] " "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]"; "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc, static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
const char *name, int namelen) const char *name)
{ {
struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1); struct ref *ref;
struct object_id oid; struct object_id oid;
const int chunksz = GIT_SHA1_HEXSZ + 1;
if (namelen > chunksz && name[chunksz - 1] == ' ' && if (!get_oid_hex(name, &oid) && name[GIT_SHA1_HEXSZ] == ' ')
!get_oid_hex(name, &oid)) { name += GIT_SHA1_HEXSZ + 1;
oidcpy(&ref->old_oid, &oid); else
name += chunksz; oidclr(&oid);
namelen -= chunksz;
}
memcpy(ref->name, name, namelen); ref = alloc_ref(name);
ref->name[namelen] = '\0'; oidcpy(&ref->old_oid, &oid);
(*nr)++; (*nr)++;
ALLOC_GROW(*sought, *nr, *alloc); ALLOC_GROW(*sought, *nr, *alloc);
(*sought)[*nr - 1] = ref; (*sought)[*nr - 1] = ref;
} }
static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
const char *string)
{
add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
}
int cmd_fetch_pack(int argc, const char **argv, const char *prefix) int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
{ {
int i, ret; int i, ret;