resolve_gitlink_ref(): avoid memory allocation in many cases

If we don't have to strip trailing '/' from the submodule path, then don't allocate and copy the submodule name. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 4, 2016 at 18:08 UTC 48a8475fd3f935d753cc2e0dd81562bc73f6d6d3
1 file changed +13 -6
refs.c
+13 -6
@@ -1301,19 +1301,26 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1301
1302 int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
1303 {
1304 - int len = strlen(path);
1305 - struct strbuf submodule = STRBUF_INIT;
1304 + size_t len = strlen(path);
1305 struct ref_store *refs;
1306 int flags;
1307
1309 - while (len && path[len-1] == '/')
1308 + while (len && path[len - 1] == '/')
1309 len--;
1310 +
1311 if (!len)
1312 return -1;
1313
1314 - strbuf_add(&submodule, path, len);
1315 - refs = get_ref_store(submodule.buf);
1316 - strbuf_release(&submodule);
1314 + if (path[len]) {
1315 + /* We need to strip off one or more trailing slashes */
1316 + char *stripped = xmemdupz(path, len);
1317 +
1318 + refs = get_ref_store(stripped);
1319 + free(stripped);
1320 + } else {
1321 + refs = get_ref_store(path);
1322 + }
1323 +
1324 if (!refs)
1325 return -1;
1326