[PATCH 17/38] resolve_gitlink_ref(): avoid memory allocation in many cases
- From
Michael Haggerty <mhagger@alum.mit.edu>
- Date
- Jun 3, 2016, 21:03 UTC
- Message-ID
- <c126a2bf5e1f48faf48b3b4ee7cc599313a76b99.1464983301.git.mhagger@alum.mit.edu>
- In-Reply-To
- <cover.1464983301.git.mhagger@alum.mit.edu>
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> --- refs.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/refs.c b/refs.c index 5e0777a..7a8ef6d 100644 --- a/refs.c +++ b/refs.c @@ -1301,19 +1301,25 @@ 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, orig_len = strlen(path); struct ref_store *refs; int flags; - while (len && path[len-1] == '/') - len--; + for (len = orig_len; len && path[len - 1] == '/'; len--) + ; + if (!len) return -1; - strbuf_add(&submodule, path, len); - refs = get_ref_store(submodule.buf); - strbuf_release(&submodule); + if (len == orig_len) { + refs = get_ref_store(path); + } else { + char *stripped = xmemdupz(path, len); + + refs = get_ref_store(stripped); + free(stripped); + } + if (!refs) return -1;
-- 2.8.1