Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Stanislav Aleksandrov <lightofmysoul@gmail.com>
Cc: git@vger.kernel.org,  Patrick Steinhardt <ps@pks.im>,
	 Adrian Ratiu <adrian.ratiu@collabora.com>,
	 Jacob Keller <jacob.keller@gmail.com>, Jeff King <peff@peff.net>
Subject: Re: [RFC PATCH] submodule: make '^/' work like '../' but with an absolute path
Date: Mon, 23 Nov 2026 10:04:02 -0800	[thread overview]
Message-ID: <xmqqbj7f77cd.fsf@gitster.g> (raw)
In-Reply-To: <20260929185122.3127574-1-lightofmysoul@gmail.com> (Stanislav Aleksandrov's message of "Tue, 29 Sep 2026 18:51:22 +0000")

Stanislav Aleksandrov <lightofmysoul@gmail.com> writes:

> A submodule URL like "../../org/lib.git" breaks when the superproject
> is forked to a different depth, e.g. into a GitLab subgroup. An
> absolute URL doesn't break, but it forces one protocol on everyone.
>
> Resolve a URL starting with "^/" against the superproject's remote,
> like "../", but from the root of the server. Protocol, user, host and
> port stay the same. For "^/org/lib.git":
>
>     https://host/me/super.git      ->  https://host/org/lib.git
>     git@host:group/sub/super.git   ->  git@host:org/lib.git

So this thing is designed to handle scp-style URLs.


> diff --git a/Documentation/git-submodule.adoc b/Documentation/git-submodule.adoc
> index 722d827908..f512625eb8 100644
> --- a/Documentation/git-submodule.adoc
> +++ b/Documentation/git-submodule.adoc
> @@ -40,8 +40,9 @@ subcommands are available to perform operations on the submodules.
>  	project: the current project is termed the "superproject".
>  +
>  _<repository>_ is the URL of the new submodule's `origin` repository.
> -This may be either an absolute URL, or (if it begins with `./`
> -or `../`), the location relative to the superproject's default remote
> +This may be an absolute URL, (if it begins with `^/`) an absolute path
> +on the server of the superproject's default remote, or (if it begins with
> +`./` or `../`), the location relative to the superproject's default remote
>  repository (Please note that to specify a repository `foo.git`

Calling '^/' an "absolute path" (you have it in the commit title and
also here in the documentation) is a bit misleading for scp-style
URLs, like git@host:group/super.git -> git@host:org/lib.git, which
lack a leading '/'.  These scp-style URLs that lack the leading slash
'/' immediately after ':' are not absolute, and had better be called
by a different term, perhaps "paths relative to the server-root" or
something.

> diff --git a/remote.c b/remote.c
> index fe62068463..05ef35e3ab 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -3095,6 +3095,67 @@ char *relative_url(const char *remote_url, const char *url,
>  	return strbuf_detach(&sb, NULL);
>  }
>  
> +static const char *skip_bracketed_host(const char *host)
> +{
> +	const char *start = strstr(host, "@[");
> +	const char *end;
> +
> +	start = start ? start + 1 : host;
> +	if (*start != '[')
> +		return host;
> +	end = strchr(start + 1, ']');
> +	return end ? end : host;
> +}

The parsing might be a bit too loose against funny local path
components.  Wouldn't both of these

    test_submodule_relative_url \
        "(null)" "host:dir/@[x]/repo.git" "^/org/lib.git" "host:org/lib.git"
    test_submodule_relative_url \
        "(null)" "host:dir/@[x]:y/repo.git" "^/org/lib.git" "host:org/lib.git"

misbehave?

With

    host:dir/@[x]/repo.git

in root_relative_url() below, for such a string, is_url() is 0 which
means scp == true.  We call skip_bracketed_host().  It matches "@[" in
the path and strchr(start + 1, ']') advances past the first ':' to
']'.  'end' points at "/repo.git" and that is what is returned from
this function.

The caller in root_relative_url() then looks for ':' with strchr() and
fails, making end == NULL.  The caller would give a "cannot resolve"
error.

With

    host:dir/@[x]:y/repo.git

skip_bracketed_host() does the same, and advances past ']', making
'end' point at ":y/repo.git", and that is returned to the caller.

The caller in root_relative_url() then looks for ':' with strchr() and
sets 'end' to it ('host' is the original string), and has_host() would
return true.  It would return a corrupt URL "host:dir/@[x]:org/lib.git"
or something like that, instead of "host:org/lib.git" that is desired.

We'd probably need to at least make sure that we do not scan for "@[" or
a colon past the first slash (I didn't think through other details, so
there may be other kinds of care we need to take).

> +static int has_host(const char *start, const char *end)
> +{
> +	const char *p;
> +
> +	for (p = start; p < end; p++)
> +		if (*p == '@')
> +			start = p + 1;
> +	return start < end && *start != ':' && !starts_with(start, "[]");
> +}
> +
> +char *root_relative_url(const char *remote_url, const char *url)
> +{
> +	struct strbuf sb = STRBUF_INIT;
> +	const char *path, *host, *end;
> +	int scp;
> +
> +	if (!skip_prefix(url, "^/", &path))
> +		BUG("not a root-relative url: '%s'", url);
> +	if (*path == '/' || *path == ':')
> +		die(_("root-relative url '%s' must not start with '^//' or '^/:'"),
> +		    url);
> +
> +	for (end = remote_url; is_urlschemechar(end == remote_url, *end); end++)
> +		;
> +	if (starts_with(end, "::") || starts_with(remote_url, "file://") ||
> +	    url_is_local_not_ssh(remote_url))
> +		return NULL;
> +
> +	scp = !is_url(remote_url);
> +	if (scp) {
> +		host = remote_url;
> +		end = strchr(skip_bracketed_host(remote_url), ':');
> +	} else {
> +		host = strstr(remote_url, "://") + 3;
> +		end = strchrnul(host, '/');
> +	}
> +	if (!end || !has_host(host, end))
> +		return NULL;
> +
> +	strbuf_add(&sb, remote_url, end - remote_url);
> +	strbuf_addch(&sb, scp ? ':' : '/');
> +	if (scp && end[1] == '/')
> +		strbuf_addch(&sb, '/');
> +	strbuf_addstr(&sb, path);
> +	if (ends_with(path, "/"))
> +		strbuf_setlen(&sb, sb.len - 1);
> +	return strbuf_detach(&sb, NULL);
> +}
> +

      parent reply	other threads:[~2026-11-23 18:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-29 18:51 [RFC PATCH] submodule: make '^/' work like '../' but with an absolute path Stanislav Aleksandrov
2026-10-25 19:42 ` Jacob Keller
2026-10-25 21:25   ` Stanislav Aleksandrov
2026-10-25 23:11     ` Jacob Keller
2026-10-30 15:08       ` Stanislav Aleksandrov
2026-10-27 17:39 ` Junio C Hamano
2026-10-30 15:07   ` Stanislav Aleksandrov
2026-10-31 15:54     ` Junio C Hamano
2026-11-22 19:04       ` Junio C Hamano
2026-11-23  0:10 ` D. Ben Knoble
2026-11-23 18:04 ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqbj7f77cd.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=adrian.ratiu@collabora.com \
    --cc=git@vger.kernel.org \
    --cc=jacob.keller@gmail.com \
    --cc=lightofmysoul@gmail.com \
    --cc=peff@peff.net \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox