diff options
author | Michael Haggerty <mhagger@alum.mit.edu> | 2016-09-04 18:08:23 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-09-09 15:28:13 -0700 |
commit | 48a8475fd3f935d753cc2e0dd81562bc73f6d6d3 (patch) | |
tree | 378c216572c8fee7fb2e371a3e039aa9d1de963a /refs.c | |
parent | 424dcc7683a37d1f14aa0dd485300001cb854f6c (diff) | |
download | git-48a8475fd3f935d753cc2e0dd81562bc73f6d6d3.tar.gz |
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>
Diffstat (limited to 'refs.c')
-rw-r--r-- | refs.c | 19 |
1 files changed, 13 insertions, 6 deletions
@@ -1301,19 +1301,26 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1) { - int len = strlen(path); - struct strbuf submodule = STRBUF_INIT; + size_t len = strlen(path); struct ref_store *refs; int flags; - while (len && path[len-1] == '/') + while (len && path[len - 1] == '/') len--; + if (!len) return -1; - strbuf_add(&submodule, path, len); - refs = get_ref_store(submodule.buf); - strbuf_release(&submodule); + if (path[len]) { + /* We need to strip off one or more trailing slashes */ + char *stripped = xmemdupz(path, len); + + refs = get_ref_store(stripped); + free(stripped); + } else { + refs = get_ref_store(path); + } + if (!refs) return -1; |