Merge branch 'sb/submodule-ignore-trailing-slash'
[gitweb.git] / builtin / submodule--helper.c
index 444ec06c2a31d1ec4981ba7475147c3154cd1b18..4beeda5f9f49d6c2e2f552308b50bacf70ebd421 100644 (file)
@@ -95,6 +95,8 @@ static int chop_last_dir(char **remoteurl, int is_relative)
  * NEEDSWORK: This works incorrectly on the domain and protocol part.
  * remote_url      url              outcome          expectation
  * http://a.com/b  ../c             http://a.com/c   as is
+ * http://a.com/b/ ../c             http://a.com/c   same as previous line, but
+ *                                                   ignore trailing slash in url
  * http://a.com/b  ../../c          http://c         error out
  * http://a.com/b  ../../../c       http:/c          error out
  * http://a.com/b  ../../../../c    http:c           error out
@@ -113,8 +115,8 @@ static char *relative_url(const char *remote_url,
        struct strbuf sb = STRBUF_INIT;
        size_t len = strlen(remoteurl);
 
-       if (is_dir_sep(remoteurl[len]))
-               remoteurl[len] = '\0';
+       if (is_dir_sep(remoteurl[len-1]))
+               remoteurl[len-1] = '\0';
 
        if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
                is_relative = 0;
@@ -147,6 +149,8 @@ static char *relative_url(const char *remote_url,
        }
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
+       if (ends_with(url, "/"))
+               strbuf_setlen(&sb, sb.len - 1);
        free(remoteurl);
 
        if (starts_with_dot_slash(sb.buf))
@@ -492,20 +496,16 @@ static int add_possible_reference_from_superproject(
 {
        struct submodule_alternate_setup *sas = sas_cb;
 
-       /* directory name, minus trailing slash */
-       size_t namelen = alt->name - alt->base - 1;
-       struct strbuf name = STRBUF_INIT;
-       strbuf_add(&name, alt->base, namelen);
-
        /*
         * If the alternate object store is another repository, try the
         * standard layout with .git/modules/<name>/objects
         */
-       if (ends_with(name.buf, ".git/objects")) {
+       if (ends_with(alt->path, ".git/objects")) {
                char *sm_alternate;
                struct strbuf sb = STRBUF_INIT;
                struct strbuf err = STRBUF_INIT;
-               strbuf_add(&sb, name.buf, name.len - strlen("objects"));
+               strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
+
                /*
                 * We need to end the new path with '/' to mark it as a dir,
                 * otherwise a submodule name containing '/' will be broken
@@ -533,7 +533,6 @@ static int add_possible_reference_from_superproject(
                strbuf_release(&sb);
        }
 
-       strbuf_release(&name);
        return 0;
 }