Thread View: linux.kernel.git
1 messages
1 total messages
Started by "D. Ben Knoble"
Mon, 23 Nov 2026 00:10
Re: [RFC PATCH] submodule: make '^/' work like '../' but with an absolute path
Author: "D. Ben Knoble"
Date: Mon, 23 Nov 2026 00:10
Date: Mon, 23 Nov 2026 00:10
381 lines
26752 bytes
26752 bytes
I use submodules quite a bit, and I've had enough trouble deciding how
.gitmodules should record the URL that I might give this a proper
attempt in the wild once it's released. So let's see if I can
contribute anything useful?
On Tue, Sep 29, 2026 at 2:54 PM Stanislav Aleksandrov
<lightofmysoul@gmail.com> wrote:
>
> 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.
Not being familiar with GitLab subgroups, I don't totally follow this
description. I've read the test cases, though, and it sounds like the
issue is one where
- /org/repo.git has a submodule ../../org/lib.git
- it is forked to (say) /elsewhere/org/repo.git or
/org/elsewhere/repo.git: now the submodule resolves against
/elsewhere/org/lib.git (resp. /org/elsewhere/lib.git), which may not
be what you wanted.
I can see uses in both cases (and for absolute URLs), but not
requiring a "mirror fork" in such cases to use these URLs is
reasonable.
> 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
>
> Unlike "../", "^/" fails in a superproject cloned from a local path or
> without a remote, since there is no server. It also fails for
> "<transport>::<address>" remotes, whose address only the helper can
> parse.
So this privileges a few transports over others, but perhaps that's
reasonable. It might be nice to support local paths, but I don't see a
compelling need to off the top of my head (why anyone would replicate
the above scenario on a single machine, I'm just not sure)---if we
figured out how to support it later, does the current implementation
back us into a corner and prevent that, or would we just be lifting
the restriction and slotting in the new code?
> The syntax comes from svn:externals, where "^/" is the repository root.
Always good to see prior art.
> ---
> RFC: the syntax is new, and it changes the meaning of local paths
> that start with "^/".
>
> - Why not "/path" or "//host/path"? "/path" is already a local
> absolute path, "//host" is a network path on Windows, and neither
> can express "git@host:path" or SSH host aliases.
> - Why not url.<base>.insteadOf? It has to be set up on every clone;
> .gitmodules cannot carry it.
> - Compatibility: a local "^/..." path needs a "^" directory in the
> superproject and protocol.file.allow=always, and "git submodule
> add" refuses it.
> - Known costs: "^" is special in cmd.exe, Git Bash rewrites "^/..."
> arguments, and libgit2, JGit and forges would need to learn it.
>
> GitLab has an open request for the same thing:
> https://gitlab.com/gitlab-org/gitlab/-/issues/393295
As we move towards graduating this topic, I wonder if any of this
information ought to be present in the log message, or leaving folks
to discover it via the amlog notes -> mailing list is sufficient?
> 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`
> which is located right next to a superproject `bar.git`, you'll
> have to use `../foo.git` instead of `./foo.git` - as one might expect
In this hunk I think we are working too hard to minimize changes.
Presenting the conditional on "^/" *before* the explanation of what it
means is particularly awkward to me in this paragraph. Perhaps
(ignoring my unfamiliarity with AsciiDoc, ahem):
The URL may take the following forms:
* an absolute URL, using URI syntax;
* an absolute path on the server of the superproject's default
remote, using the syntax `^/<path>`;
* a location relative to the superproject's default remote, using
a path syntax starting with `./` or `../`.
? Perhaps there is yet a better way ;)
> diff --git a/Documentation/gitmodules.adoc b/Documentation/gitmodules.adoc
> index fd96639806..9f6cc9a4ef 100644
> --- a/Documentation/gitmodules.adoc
> +++ b/Documentation/gitmodules.adoc
> @@ -31,9 +31,10 @@ submodule.<name>.path::
>
> submodule.<name>.url::
> Defines a URL from which the submodule repository can be cloned.
> - This may be either an absolute URL ready to be passed to
> - linkgit:git-clone[1] or (if it begins with `./` or `../`) a location
> - relative to the superproject's origin repository.
> + This may be an absolute URL ready to be passed to
> + linkgit:git-clone[1], (if it begins with `./` or `../`) a location
> + relative to the superproject's origin repository, or (if it begins
> + with `^/`) an absolute path on the server of that repository.
And here, perhaps to avoid repeating ourselves, we should point back
at the other document?
Defines a URL (in the sense of linkgit:git-submodule[1]) from
which the submodule repository can be cloned.
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 5d3bcda334..16884ca6b6 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -50,7 +50,8 @@ static char *get_default_remote(void)
> return xstrdup(repo_default_remote(the_repository));
> }
>
> -static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
> +static char *resolve_relative_url_gently(const char *rel_url,
> + const char *up_path, int quiet)
> {
> char *remoteurl, *resolved_url;
> char *remote = get_default_remote();
> @@ -58,14 +59,17 @@ static char *resolve_relative_url(const char *rel_url, const char *up_path, int
>
> strbuf_addf(&remotesb, "remote.%s.url", remote);
> if (repo_config_get_string(the_repository, remotesb.buf, &remoteurl)) {
> - if (!quiet)
> + if (!quiet && !starts_with(rel_url, "^/"))
> warning(_("could not look up configuration '%s'. "
> "Assuming this repository is its own "
> "authoritative upstream."),
> remotesb.buf);
> remoteurl = xgetcwd();
I'm somewhat confused why we suppress this warning: the `^/` syntax
doesn't make sense if remoteurl=$PWD, right?
> }
> - resolved_url = relative_url(remoteurl, rel_url, up_path);
> + if (starts_with(rel_url, "^/"))
> + resolved_url = root_relative_url(remoteurl, rel_url);
> + else
> + resolved_url = relative_url(remoteurl, rel_url, up_path);
>
> free(remote);
> free(remoteurl);
> @@ -74,6 +78,16 @@ static char *resolve_relative_url(const char *rel_url, const char *up_path, int
> return resolved_url;
> }
> 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;
> +}
> +
> +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);
> +}
This is quite a bit more string-munging code to parse(?) a URL than
I'm comfortable reviewing. I wonder if we have something available in
one of our dependencies we can use to make this work without doing it
ourselves? Or if there's some way to make it easy to see which input
forms we handle (so that we can also think about those we don't)? I
saw the header comment (kept below), and it left me wondering if there
are URL formats *not* covered.
> --- a/remote.h
> +++ b/remote.h
> @@ -478,6 +478,21 @@ void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
> char *relative_url(const char *remote_url, const char *url,
> const char *up_path);
>
> +/*
> + * The `url` argument starts with "^/" and names a repository relative to
> + * the root of the server that `remote_url` points to: the path of
> + * `remote_url` is replaced with the rest of `url`, keeping its scheme, user,
> + * host and port. Returns NULL if `remote_url` has no host, and dies if
> + * `url` continues with '/' or ':', which could change the kind of URL.
> + *
> + * remote_url url outcome
> + * https://a.com/b/c ^/d/e https://a.com/d/e
> + * ssh://u@a.com:22/b/c ^/d/e ssh://u@a.com:22/d/e
> + * u@a.com:b/c ^/d/e u@a.com:d/e
> + * u@a.com:/b/c ^/d/e u@a.com:/d/e
> + */
> +char *root_relative_url(const char *remote_url, const char *url);
> diff --git a/submodule-config.c b/submodule-config.c
> index 37c3be377b..dfa819112c 100644
> --- a/submodule-config.c
> +++ b/submodule-config.c
> @@ -237,9 +237,10 @@ int check_submodule_name(const char *name)
> return 0;
> }
>
> -static int submodule_url_is_relative(const char *url)
> +int submodule_url_is_relative(const char *url)
> {
> - return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
> + return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url) ||
> + starts_with(url, "^/");
> }
Making this useful across more of the codebase could be a step [1/2],
if we were so inclined.
> @@ -342,6 +343,9 @@ int check_submodule_url(const char *url)
> if (count_leading_dotdots(url, &next) > 0 &&
> (*next == ':' || *next == '/'))
> return -1;
> + if (skip_prefix(url, "^/", &next) &&
> + (*next == ':' || *next == '/'))
> + return -1;
Are such "^/" URLs susceptible in the same way as the comment
describes the previous check? We probably don't want to *make* them
so…
What caught my eye was the repetition, wondering if we could collapse the 2 with
(count_leading_dotdots() || skip_prefix()) && …
, but now I'm also wondering if "^/" goes through the same resolver
(or a resolver with the same problems) as described in c44088ecc4
(credential: treat URL without scheme as invalid, 2020-04-18), and I'm
genuinely not sure.
> diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh
> index 56faf5fe73..c8618e8a30 100755
> --- a/t/t0060-path-utils.sh
> +++ b/t/t0060-path-utils.sh
> @@ -7,6 +7,11 @@ test_description='Test various path utilities'
> +test_expect_success 'root-relative submodule url cannot change the kind of url' '
> + for url in "^//evil.example.com/x.git" "^/:evil"
Ah, hm. Naïvely, I'd expect these to resolve to
"host/evil.example.com/x.git" and "host/:evil". I'm not sure how URL
parsing generally handles the latter, but the former doesn't seem like
it is actually an issue? Unless that's *not* how we resolve those.
> + do
> + test_must_fail test-tool submodule resolve-relative-url \
> + "(null)" host:/srv/super.git "$url" 2>err &&
> + test_grep "must not start with" err || return 1
> + done
> +'
> diff --git a/t/t7427-submodule-root-relative-url.sh b/t/t7427-submodule-root-relative-url.sh
> new file mode 100755
> index 0000000000..526dfec8d2
> --- /dev/null
> +++ b/t/t7427-submodule-root-relative-url.sh
> @@ -0,0 +1,190 @@
> +test_expect_success 'clone of the upstream in a subgroup' '
As I said earlier, I'm not sure about "subgroup"---that seems
potentially GitLab-specific. Is there a different and more clear way
to explain this test case?
> + git clone --recurse-submodules https://example.com/org/team/super.git upstream &&
> + test_cmp_config -C upstream https://example.com/org/lib.git submodule.lib.url &&
> + test_path_is_file upstream/lib/lib.t &&
> + test_path_is_file upstream/lib/dep/dep.t
> +'
[I suppose you all can decide if I succeeded; I didn't see much review
of the code prior to this, and I've omitted bits.]
--
D. Ben Knoble
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