🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
1 messages
1 total messages Started by Junio C Hamano Mon, 23 Nov 2026 18:04
Re: [RFC PATCH] submodule: make '^/' work like '../' but with an absolute path
#3648
Author: Junio C Hamano
Date: Mon, 23 Nov 2026 18:04
149 lines
18073 bytes
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);
> +}
> +
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads