remote: allow relative_url() to return an absolute url

When the 'url' parameter was absolute, the previous implementation would concatenate 'remote_url' with 'url'. Instead, we want to return 'url' in this case. The documentation now discusses what happens when supplying two absolute URLs. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 16, 2022 at 20:11 UTC 834e3520ab6435073ef58922b24d732f96cdf461
2 files changed +11 -2
remote.c
+10 -2
@@ -2761,10 +2761,18 @@ char *relative_url(const char *remote_url, const char *url,
2761 int is_relative = 0;
2762 int colonsep = 0;
2763 char *out;
2764 - char *remoteurl = xstrdup(remote_url);
2764 + char *remoteurl;
2765 struct strbuf sb = STRBUF_INIT;
2766 - size_t len = strlen(remoteurl);
2766 + size_t len;
2767 +
2768 + if (!url_is_local_not_ssh(url) || is_absolute_path(url))
2769 + return xstrdup(url);
2770 +
2771 + len = strlen(remote_url);
2772 + if (!len)
2773 + BUG("invalid empty remote_url");
2774
2775 + remoteurl = xstrdup(remote_url);
2776 if (is_dir_sep(remoteurl[len-1]))
2777 remoteurl[len-1] = '\0';
2778
remote.h
+1
@@ -434,6 +434,7 @@ void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
434 * http://a.com/b ../../../c http:/c error out
435 * http://a.com/b ../../../../c http:c error out
436 * http://a.com/b ../../../../../c .:c error out
437 + * http://a.com/b http://d.org/e http://d.org/e as is
438 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
439 * when a local part has a colon in its path component, too.
440 */