alloc_ref_from_str(): factor out a common pattern of alloc_ref from string
Also fix an underallocation in walker.c::interpret_target(). Signed-off-by: Krzysztof Kowalczyk <kkowalczyk@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
1f8115b113
commit
737922aa64
@ -508,10 +508,8 @@ static void find_non_local_tags(struct transport *transport,
|
|||||||
will_fetch(head, ref->old_sha1))) {
|
will_fetch(head, ref->old_sha1))) {
|
||||||
path_list_insert(ref_name, &new_refs);
|
path_list_insert(ref_name, &new_refs);
|
||||||
|
|
||||||
rm = alloc_ref(strlen(ref_name) + 1);
|
rm = alloc_ref_from_str(ref_name);
|
||||||
strcpy(rm->name, ref_name);
|
rm->peer_ref = alloc_ref_from_str(ref_name);
|
||||||
rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
|
|
||||||
strcpy(rm->peer_ref->name, ref_name);
|
|
||||||
hashcpy(rm->old_sha1, ref_sha1);
|
hashcpy(rm->old_sha1, ref_sha1);
|
||||||
|
|
||||||
**tail = rm;
|
**tail = rm;
|
||||||
|
@ -1761,8 +1761,7 @@ static void one_remote_ref(char *refname)
|
|||||||
struct ref *ref;
|
struct ref *ref;
|
||||||
struct object *obj;
|
struct object *obj;
|
||||||
|
|
||||||
ref = alloc_ref(strlen(refname) + 1);
|
ref = alloc_ref_from_str(refname);
|
||||||
strcpy(ref->name, refname);
|
|
||||||
|
|
||||||
if (http_fetch_ref(remote->url, ref) != 0) {
|
if (http_fetch_ref(remote->url, ref) != 0) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
@ -1894,8 +1893,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
|
|||||||
char *ref_info;
|
char *ref_info;
|
||||||
struct ref *ref;
|
struct ref *ref;
|
||||||
|
|
||||||
ref = alloc_ref(strlen(ls->dentry_name) + 1);
|
ref = alloc_ref_from_str(ls->dentry_name);
|
||||||
strcpy(ref->name, ls->dentry_name);
|
|
||||||
|
|
||||||
if (http_fetch_ref(remote->url, ref) != 0) {
|
if (http_fetch_ref(remote->url, ref) != 0) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
|
23
remote.c
23
remote.c
@ -691,6 +691,13 @@ struct ref *alloc_ref(unsigned namelen)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ref *alloc_ref_from_str(const char* str)
|
||||||
|
{
|
||||||
|
struct ref *ret = alloc_ref(strlen(str) + 1);
|
||||||
|
strcpy(ret->name, str);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static struct ref *copy_ref(const struct ref *ref)
|
static struct ref *copy_ref(const struct ref *ref)
|
||||||
{
|
{
|
||||||
struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
|
struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
|
||||||
@ -797,7 +804,6 @@ static struct ref *try_explicit_object_name(const char *name)
|
|||||||
{
|
{
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
struct ref *ref;
|
struct ref *ref;
|
||||||
int len;
|
|
||||||
|
|
||||||
if (!*name) {
|
if (!*name) {
|
||||||
ref = alloc_ref(20);
|
ref = alloc_ref(20);
|
||||||
@ -807,21 +813,14 @@ static struct ref *try_explicit_object_name(const char *name)
|
|||||||
}
|
}
|
||||||
if (get_sha1(name, sha1))
|
if (get_sha1(name, sha1))
|
||||||
return NULL;
|
return NULL;
|
||||||
len = strlen(name) + 1;
|
ref = alloc_ref_from_str(name);
|
||||||
ref = alloc_ref(len);
|
|
||||||
memcpy(ref->name, name, len);
|
|
||||||
hashcpy(ref->new_sha1, sha1);
|
hashcpy(ref->new_sha1, sha1);
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct ref *make_linked_ref(const char *name, struct ref ***tail)
|
static struct ref *make_linked_ref(const char *name, struct ref ***tail)
|
||||||
{
|
{
|
||||||
struct ref *ret;
|
struct ref *ret = alloc_ref_from_str(name);
|
||||||
size_t len;
|
|
||||||
|
|
||||||
len = strlen(name) + 1;
|
|
||||||
ret = alloc_ref(len);
|
|
||||||
memcpy(ret->name, name, len);
|
|
||||||
tail_link_ref(ret, tail);
|
tail_link_ref(ret, tail);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1125,9 +1124,7 @@ static struct ref *get_local_ref(const char *name)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!prefixcmp(name, "refs/")) {
|
if (!prefixcmp(name, "refs/")) {
|
||||||
ret = alloc_ref(strlen(name) + 1);
|
return alloc_ref_from_str(name);
|
||||||
strcpy(ret->name, name);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!prefixcmp(name, "heads/") ||
|
if (!prefixcmp(name, "heads/") ||
|
||||||
|
2
remote.h
2
remote.h
@ -54,6 +54,8 @@ struct refspec {
|
|||||||
|
|
||||||
struct ref *alloc_ref(unsigned namelen);
|
struct ref *alloc_ref(unsigned namelen);
|
||||||
|
|
||||||
|
struct ref *alloc_ref_from_str(const char* str);
|
||||||
|
|
||||||
struct ref *copy_ref_list(const struct ref *ref);
|
struct ref *copy_ref_list(const struct ref *ref);
|
||||||
|
|
||||||
int check_ref_type(const struct ref *ref, int flags);
|
int check_ref_type(const struct ref *ref, int flags);
|
||||||
|
@ -504,8 +504,7 @@ static struct ref *get_refs_via_curl(struct transport *transport)
|
|||||||
|
|
||||||
strbuf_release(&buffer);
|
strbuf_release(&buffer);
|
||||||
|
|
||||||
ref = alloc_ref(strlen("HEAD") + 1);
|
ref = alloc_ref_from_str("HEAD");
|
||||||
strcpy(ref->name, "HEAD");
|
|
||||||
if (!walker->fetch_ref(walker, ref) &&
|
if (!walker->fetch_ref(walker, ref) &&
|
||||||
!resolve_remote_symref(ref, refs)) {
|
!resolve_remote_symref(ref, refs)) {
|
||||||
ref->next = refs;
|
ref->next = refs;
|
||||||
@ -546,9 +545,8 @@ static struct ref *get_refs_from_bundle(struct transport *transport)
|
|||||||
die ("Could not read bundle '%s'.", transport->url);
|
die ("Could not read bundle '%s'.", transport->url);
|
||||||
for (i = 0; i < data->header.references.nr; i++) {
|
for (i = 0; i < data->header.references.nr; i++) {
|
||||||
struct ref_list_entry *e = data->header.references.list + i;
|
struct ref_list_entry *e = data->header.references.list + i;
|
||||||
struct ref *ref = alloc_ref(strlen(e->name) + 1);
|
struct ref *ref = alloc_ref_from_str(e->name);
|
||||||
hashcpy(ref->old_sha1, e->sha1);
|
hashcpy(ref->old_sha1, e->sha1);
|
||||||
strcpy(ref->name, e->name);
|
|
||||||
ref->next = result;
|
ref->next = result;
|
||||||
result = ref;
|
result = ref;
|
||||||
}
|
}
|
||||||
|
3
walker.c
3
walker.c
@ -190,8 +190,7 @@ static int interpret_target(struct walker *walker, char *target, unsigned char *
|
|||||||
if (!get_sha1_hex(target, sha1))
|
if (!get_sha1_hex(target, sha1))
|
||||||
return 0;
|
return 0;
|
||||||
if (!check_ref_format(target)) {
|
if (!check_ref_format(target)) {
|
||||||
struct ref *ref = alloc_ref(strlen(target));
|
struct ref *ref = alloc_ref_from_str(target);
|
||||||
strcpy(ref->name, target);
|
|
||||||
if (!walker->fetch_ref(walker, ref)) {
|
if (!walker->fetch_ref(walker, ref)) {
|
||||||
hashcpy(sha1, ref->old_sha1);
|
hashcpy(sha1, ref->old_sha1);
|
||||||
free(ref);
|
free(ref);
|
||||||
|
Loading…
Reference in New Issue
Block a user