Code

use alloc_ref_from_str() everywhere
authorRené Scharfe <rene.scharfe@lsrfire.ath.cx>
Sat, 18 Oct 2008 08:41:33 +0000 (10:41 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sat, 18 Oct 2008 13:53:47 +0000 (06:53 -0700)
Replace pairs of alloc_ref() and strcpy() with alloc_ref_from_str(),
simplifying the code.

In connect.c, also a pair of alloc_ref() and memcpy() is replaced --
the additional cost of a strlen() call should not have too much of an
impact.  Consistency and simplicity are more important.

In remote.c, the code was allocating 11 bytes more than needed for
the name part, but I couldn't see them being used for anything.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
connect.c
remote.c
transport.c

index 67d2cd86a89a92f8f75baa7ae5e820ac3150f3b4..b69060bca0917cf84612f861dbc8be5d1c6f775c 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -90,9 +90,8 @@ struct ref **get_remote_heads(int in, struct ref **list,
                        continue;
                if (nr_match && !path_match(name, nr_match, match))
                        continue;
-               ref = alloc_ref(name_len + 1);
+               ref = alloc_ref_from_str(buffer + 41);
                hashcpy(ref->old_sha1, old_sha1);
-               memcpy(ref->name, buffer + 41, name_len + 1);
                *list = ref;
                list = &ref->next;
        }
index 98cbcf94c3535404c4138a6e5b80da97cb3294f2..44d681da08f954f1c6680e71c022ac607b07db9c 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -878,8 +878,7 @@ static struct ref *try_explicit_object_name(const char *name)
        struct ref *ref;
 
        if (!*name) {
-               ref = alloc_ref(20);
-               strcpy(ref->name, "(delete)");
+               ref = alloc_ref_from_str("(delete)");
                hashclr(ref->new_sha1);
                return ref;
        }
index 5110c56c4e80dc224ff3308e643fad6d5ed77ffb..3d034759bf78baefcb902aafd106788604fd32bb 100644 (file)
@@ -75,7 +75,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
 
                        if (fd < 0)
                                continue;
-                       next = alloc_ref(path->len - name_offset + 1);
+                       next = alloc_ref_from_str(path->buf + name_offset);
                        if (read_in_full(fd, buffer, 40) != 40 ||
                                        get_sha1_hex(buffer, next->old_sha1)) {
                                close(fd);
@@ -83,7 +83,6 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
                                continue;
                        }
                        close(fd);
-                       strcpy(next->name, path->buf + name_offset);
                        (*tail)->next = next;
                        *tail = next;
                }
@@ -127,14 +126,13 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
                                      (*list)->next->name)) > 0)
                        list = &(*list)->next;
                if (!(*list)->next || cmp < 0) {
-                       struct ref *next = alloc_ref(len - 40);
+                       struct ref *next = alloc_ref_from_str(buffer + 41);
                        buffer[40] = '\0';
                        if (get_sha1_hex(buffer, next->old_sha1)) {
                                warning ("invalid SHA-1: %s", buffer);
                                free(next);
                                continue;
                        }
-                       strcpy(next->name, buffer + 41);
                        next->next = (*list)->next;
                        (*list)->next = next;
                        list = &(*list)->next;