@matheusbsbs / matheus-bagliano / commits / b0b44bc7b2
Fork 0

use alloc_ref_from_str() everywhere

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>

René Scharfe committed Oct 18, 2008 at 10:41 UTC b0b44bc7b26c8c4b4221a377ce6ba174b843cb8d
3 files changed +4 -8
connect.c
+1 -2
@@ -90,9 +90,8 @@ struct ref **get_remote_heads(int in, struct ref **list,
90 continue;
91 if (nr_match && !path_match(name, nr_match, match))
92 continue;
93 - ref = alloc_ref(name_len + 1);
93 + ref = alloc_ref_from_str(buffer + 41);
94 hashcpy(ref->old_sha1, old_sha1);
95 - memcpy(ref->name, buffer + 41, name_len + 1);
95 *list = ref;
96 list = &ref->next;
97 }
remote.c
+1 -2
@@ -878,8 +878,7 @@ static struct ref *try_explicit_object_name(const char *name)
878 struct ref *ref;
879
880 if (!*name) {
881 - ref = alloc_ref(20);
882 - strcpy(ref->name, "(delete)");
881 + ref = alloc_ref_from_str("(delete)");
882 hashclr(ref->new_sha1);
883 return ref;
884 }
transport.c
+2 -4
@@ -75,7 +75,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
75
76 if (fd < 0)
77 continue;
78 - next = alloc_ref(path->len - name_offset + 1);
78 + next = alloc_ref_from_str(path->buf + name_offset);
79 if (read_in_full(fd, buffer, 40) != 40 ||
80 get_sha1_hex(buffer, next->old_sha1)) {
81 close(fd);
@@ -83,7 +83,6 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
83 continue;
84 }
85 close(fd);
86 - strcpy(next->name, path->buf + name_offset);
86 (*tail)->next = next;
87 *tail = next;
88 }
@@ -127,14 +126,13 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
126 (*list)->next->name)) > 0)
127 list = &(*list)->next;
128 if (!(*list)->next || cmp < 0) {
130 - struct ref *next = alloc_ref(len - 40);
129 + struct ref *next = alloc_ref_from_str(buffer + 41);
130 buffer[40] = '\0';
131 if (get_sha1_hex(buffer, next->old_sha1)) {
132 warning ("invalid SHA-1: %s", buffer);
133 free(next);
134 continue;
135 }
137 - strcpy(next->name, buffer + 41);
136 next->next = (*list)->next;
137 (*list)->next = next;
138 list = &(*list)->next;