@matheusbsbs / matheus-bagliano / commits / 8009768e89
Fork 0

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>

René Scharfe committed Oct 18, 2008 at 10:37 UTC 8009768e89dfe389327654cf9d6868f680ef1f40
1 file changed +16 -16
remote.c
+16 -16
@@ -749,6 +749,16 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
749 return -1;
750 }
751
752 +static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
753 + const char *name)
754 +{
755 + size_t len = strlen(name);
756 + struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
757 + memcpy(ref->name, prefix, prefixlen);
758 + memcpy(ref->name + prefixlen, name, len);
759 + return ref;
760 +}
761 +
762 struct ref *alloc_ref(unsigned namelen)
763 {
764 struct ref *ret = xcalloc(1, sizeof(struct ref) + namelen);
@@ -757,9 +767,7 @@ struct ref *alloc_ref(unsigned namelen)
767
768 struct ref *alloc_ref_from_str(const char* str)
769 {
760 - struct ref *ret = alloc_ref(strlen(str) + 1);
761 - strcpy(ret->name, str);
762 - return ret;
770 + return alloc_ref_with_prefix("", 0, str);
771 }
772
773 static struct ref *copy_ref(const struct ref *ref)
@@ -1152,10 +1160,8 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,
1160 struct ref *cpy = copy_ref(ref);
1161 match = ref->name + remote_prefix_len;
1162
1155 - cpy->peer_ref = alloc_ref(local_prefix_len +
1156 - strlen(match) + 1);
1157 - sprintf(cpy->peer_ref->name, "%s%s",
1158 - refspec->dst, match);
1163 + cpy->peer_ref = alloc_ref_with_prefix(refspec->dst,
1164 + local_prefix_len, match);
1165 if (refspec->force)
1166 cpy->peer_ref->force = 1;
1167 *tail = cpy;
@@ -1188,7 +1194,6 @@ struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1194
1195 static struct ref *get_local_ref(const char *name)
1196 {
1191 - struct ref *ret;
1197 if (!name)
1198 return NULL;
1199
@@ -1198,15 +1203,10 @@ static struct ref *get_local_ref(const char *name)
1203
1204 if (!prefixcmp(name, "heads/") ||
1205 !prefixcmp(name, "tags/") ||
1201 - !prefixcmp(name, "remotes/")) {
1202 - ret = alloc_ref(strlen(name) + 6);
1203 - sprintf(ret->name, "refs/%s", name);
1204 - return ret;
1205 - }
1206 + !prefixcmp(name, "remotes/"))
1207 + return alloc_ref_with_prefix("refs/", 5, name);
1208
1207 - ret = alloc_ref(strlen(name) + 12);
1208 - sprintf(ret->name, "refs/heads/%s", name);
1209 - return ret;
1209 + return alloc_ref_with_prefix("refs/heads/", 11, name);
1210 }
1211
1212 int get_fetch_map(const struct ref *remote_refs,