This is a better place that will benefit all submodule callers instead
of just resolve_gitlink_ref()

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 refs.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/refs.c b/refs.c
index 23e0a8eda..9c86c44b8 100644
--- a/refs.c
+++ b/refs.c
@@ -1321,25 +1321,10 @@ const char *resolve_ref_unsafe(const char *refname, int 
resolve_flags,
 int resolve_gitlink_ref(const char *submodule, const char *refname,
                        unsigned char *sha1)
 {
-       size_t len = strlen(submodule);
        struct ref_store *refs;
        int flags;
 
-       while (len && submodule[len - 1] == '/')
-               len--;
-
-       if (!len)
-               return -1;
-
-       if (submodule[len]) {
-               /* We need to strip off one or more trailing slashes */
-               char *stripped = xmemdupz(submodule, len);
-
-               refs = get_submodule_ref_store(stripped);
-               free(stripped);
-       } else {
-               refs = get_submodule_ref_store(submodule);
-       }
+       refs = get_submodule_ref_store(submodule);
 
        if (!refs)
                return -1;
@@ -1458,7 +1443,17 @@ struct ref_store *get_submodule_ref_store(const char 
*submodule)
 {
        struct strbuf submodule_sb = STRBUF_INIT;
        struct ref_store *refs;
+       char *to_free = NULL;
        int ret;
+       size_t len;
+
+       if (submodule) {
+               len = strlen(submodule);
+               while (len && submodule[len - 1] == '/')
+                       len--;
+               if (!len)
+                       submodule = NULL;
+       }
 
        if (!submodule || !*submodule) {
                /*
@@ -1468,15 +1463,19 @@ struct ref_store *get_submodule_ref_store(const char 
*submodule)
                return get_main_ref_store();
        }
 
+       if (submodule[len])
+               /* We need to strip off one or more trailing slashes */
+               submodule = to_free = xmemdupz(submodule, len);
+
        refs = lookup_submodule_ref_store(submodule);
        if (refs)
-               return refs;
+               goto done;
 
        strbuf_addstr(&submodule_sb, submodule);
        ret = is_nonbare_repository_dir(&submodule_sb);
        strbuf_release(&submodule_sb);
        if (!ret)
-               return refs;
+               goto done;
 
        ret = submodule_to_gitdir(&submodule_sb, submodule);
        if (!ret)
@@ -1485,6 +1484,9 @@ struct ref_store *get_submodule_ref_store(const char 
*submodule)
 
        if (refs)
                register_submodule_ref_store(refs, submodule);
+
+done:
+       free(to_free);
        return refs;
 }
 
-- 
2.11.0.157.gd943d85

Reply via email to