add alloc_ref_with_prefix()

In three cases in remote.c, a "raw" ref is allocated using alloc_ref()
and then its is constructed using sprintf().  Clean it up by adding a
helper function, alloc_ref_with_prefix(), which creates a composite
name.  Use it in alloc_ref_from_str(), too, as it simplifies the code.

Open code alloc_ref() in alloc_ref_with_prefix(), as the former is
going to be removed in the patch after the next.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
René Scharfe 2008-10-18 10:37:40 +02:00 committed by Junio C Hamano
parent 46dc1b0e33
commit 8009768e89
1 changed files with 16 additions and 16 deletions

View File

@ -749,6 +749,16 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
return -1; return -1;
} }


static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
const char *name)
{
size_t len = strlen(name);
struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
memcpy(ref->name, prefix, prefixlen);
memcpy(ref->name + prefixlen, name, len);
return ref;
}

struct ref *alloc_ref(unsigned namelen) struct ref *alloc_ref(unsigned namelen)
{ {
struct ref *ret = xcalloc(1, sizeof(struct ref) + namelen); struct ref *ret = xcalloc(1, sizeof(struct ref) + namelen);
@ -757,9 +767,7 @@ struct ref *alloc_ref(unsigned namelen)


struct ref *alloc_ref_from_str(const char* str) struct ref *alloc_ref_from_str(const char* str)
{ {
struct ref *ret = alloc_ref(strlen(str) + 1); return alloc_ref_with_prefix("", 0, str);
strcpy(ret->name, str);
return ret;
} }


static struct ref *copy_ref(const struct ref *ref) static struct ref *copy_ref(const struct ref *ref)
@ -1152,10 +1160,8 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,
struct ref *cpy = copy_ref(ref); struct ref *cpy = copy_ref(ref);
match = ref->name + remote_prefix_len; match = ref->name + remote_prefix_len;


cpy->peer_ref = alloc_ref(local_prefix_len + cpy->peer_ref = alloc_ref_with_prefix(refspec->dst,
strlen(match) + 1); local_prefix_len, match);
sprintf(cpy->peer_ref->name, "%s%s",
refspec->dst, match);
if (refspec->force) if (refspec->force)
cpy->peer_ref->force = 1; cpy->peer_ref->force = 1;
*tail = cpy; *tail = cpy;
@ -1188,7 +1194,6 @@ struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)


static struct ref *get_local_ref(const char *name) static struct ref *get_local_ref(const char *name)
{ {
struct ref *ret;
if (!name) if (!name)
return NULL; return NULL;


@ -1198,15 +1203,10 @@ static struct ref *get_local_ref(const char *name)


if (!prefixcmp(name, "heads/") || if (!prefixcmp(name, "heads/") ||
!prefixcmp(name, "tags/") || !prefixcmp(name, "tags/") ||
!prefixcmp(name, "remotes/")) { !prefixcmp(name, "remotes/"))
ret = alloc_ref(strlen(name) + 6); return alloc_ref_with_prefix("refs/", 5, name);
sprintf(ret->name, "refs/%s", name);
return ret;
}


ret = alloc_ref(strlen(name) + 12); return alloc_ref_with_prefix("refs/heads/", 11, name);
sprintf(ret->name, "refs/heads/%s", name);
return ret;
} }


int get_fetch_map(const struct ref *remote_refs, int get_fetch_map(const struct ref *remote_refs,