Merge branch 'rs/alloc-ref'

* rs/alloc-ref:
  make alloc_ref_from_str() the new alloc_ref()
  use alloc_ref_from_str() everywhere
  add alloc_ref_with_prefix()
This commit is contained in:
Junio C Hamano 2008-10-21 17:58:01 -07:00
commit 6af50f7536
7 changed files with 30 additions and 43 deletions

View File

@ -521,8 +521,8 @@ static void find_non_local_tags(struct transport *transport,
will_fetch(head, ref->old_sha1))) { will_fetch(head, ref->old_sha1))) {
string_list_insert(ref_name, &new_refs); string_list_insert(ref_name, &new_refs);
rm = alloc_ref_from_str(ref_name); rm = alloc_ref(ref_name);
rm->peer_ref = alloc_ref_from_str(ref_name); rm->peer_ref = alloc_ref(ref_name);
hashcpy(rm->old_sha1, ref_sha1); hashcpy(rm->old_sha1, ref_sha1);
**tail = rm; **tail = rm;

View File

@ -90,9 +90,8 @@ struct ref **get_remote_heads(int in, struct ref **list,
continue; continue;
if (nr_match && !path_match(name, nr_match, match)) if (nr_match && !path_match(name, nr_match, match))
continue; continue;
ref = alloc_ref(name_len + 1); ref = alloc_ref(buffer + 41);
hashcpy(ref->old_sha1, old_sha1); hashcpy(ref->old_sha1, old_sha1);
memcpy(ref->name, buffer + 41, name_len + 1);
*list = ref; *list = ref;
list = &ref->next; list = &ref->next;
} }

View File

@ -1780,7 +1780,7 @@ static void one_remote_ref(char *refname)
struct ref *ref; struct ref *ref;
struct object *obj; struct object *obj;
ref = alloc_ref_from_str(refname); ref = alloc_ref(refname);
if (http_fetch_ref(remote->url, ref) != 0) { if (http_fetch_ref(remote->url, ref) != 0) {
fprintf(stderr, fprintf(stderr,
@ -1887,7 +1887,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_from_str(ls->dentry_name); ref = alloc_ref(ls->dentry_name);
if (http_fetch_ref(remote->url, ref) != 0) { if (http_fetch_ref(remote->url, ref) != 0) {
fprintf(stderr, fprintf(stderr,

View File

@ -749,17 +749,19 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
return -1; return -1;
} }
struct ref *alloc_ref(unsigned namelen) static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
const char *name)
{ {
struct ref *ret = xcalloc(1, sizeof(struct ref) + namelen); size_t len = strlen(name);
return ret; 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_from_str(const char* str) struct ref *alloc_ref(const char *name)
{ {
struct ref *ret = alloc_ref(strlen(str) + 1); return alloc_ref_with_prefix("", 0, name);
strcpy(ret->name, str);
return ret;
} }
static struct ref *copy_ref(const struct ref *ref) static struct ref *copy_ref(const struct ref *ref)
@ -870,21 +872,20 @@ static struct ref *try_explicit_object_name(const char *name)
struct ref *ref; struct ref *ref;
if (!*name) { if (!*name) {
ref = alloc_ref(20); ref = alloc_ref("(delete)");
strcpy(ref->name, "(delete)");
hashclr(ref->new_sha1); hashclr(ref->new_sha1);
return ref; return ref;
} }
if (get_sha1(name, sha1)) if (get_sha1(name, sha1))
return NULL; return NULL;
ref = alloc_ref_from_str(name); ref = alloc_ref(name);
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 = alloc_ref_from_str(name); struct ref *ret = alloc_ref(name);
tail_link_ref(ret, tail); tail_link_ref(ret, tail);
return ret; return ret;
} }
@ -1152,10 +1153,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,25 +1187,18 @@ 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;
if (!prefixcmp(name, "refs/")) { if (!prefixcmp(name, "refs/"))
return alloc_ref_from_str(name); return alloc_ref(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,

View File

@ -55,9 +55,7 @@ struct refspec {
extern const struct refspec *tag_refspec; extern const struct refspec *tag_refspec;
struct ref *alloc_ref(unsigned namelen); struct ref *alloc_ref(const char *name);
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);

View File

@ -75,7 +75,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
if (fd < 0) if (fd < 0)
continue; continue;
next = alloc_ref(path->len - name_offset + 1); next = alloc_ref(path->buf + name_offset);
if (read_in_full(fd, buffer, 40) != 40 || if (read_in_full(fd, buffer, 40) != 40 ||
get_sha1_hex(buffer, next->old_sha1)) { get_sha1_hex(buffer, next->old_sha1)) {
close(fd); close(fd);
@ -83,7 +83,6 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
continue; continue;
} }
close(fd); close(fd);
strcpy(next->name, path->buf + name_offset);
(*tail)->next = next; (*tail)->next = next;
*tail = next; *tail = next;
} }
@ -127,14 +126,13 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
(*list)->next->name)) > 0) (*list)->next->name)) > 0)
list = &(*list)->next; list = &(*list)->next;
if (!(*list)->next || cmp < 0) { if (!(*list)->next || cmp < 0) {
struct ref *next = alloc_ref(len - 40); struct ref *next = alloc_ref(buffer + 41);
buffer[40] = '\0'; buffer[40] = '\0';
if (get_sha1_hex(buffer, next->old_sha1)) { if (get_sha1_hex(buffer, next->old_sha1)) {
warning ("invalid SHA-1: %s", buffer); warning ("invalid SHA-1: %s", buffer);
free(next); free(next);
continue; continue;
} }
strcpy(next->name, buffer + 41);
next->next = (*list)->next; next->next = (*list)->next;
(*list)->next = next; (*list)->next = next;
list = &(*list)->next; list = &(*list)->next;
@ -501,7 +499,7 @@ static struct ref *get_refs_via_curl(struct transport *transport)
strbuf_release(&buffer); strbuf_release(&buffer);
ref = alloc_ref_from_str("HEAD"); ref = alloc_ref("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;
@ -542,7 +540,7 @@ 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_from_str(e->name); struct ref *ref = alloc_ref(e->name);
hashcpy(ref->old_sha1, e->sha1); hashcpy(ref->old_sha1, e->sha1);
ref->next = result; ref->next = result;
result = ref; result = ref;

View File

@ -191,7 +191,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_from_str(target); struct ref *ref = alloc_ref(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);