From: "Alessio Attilio via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "D. Ben Knoble" <ben.knoble@gmail.com>,
Alessio Attilio <attilio.alessio@protonmail.com>,
Alessio Attilio <attilio.alessio@protonmail.com>
Subject: [PATCH v5 1/4] submodule: share repository-aware URL resolution
Date: Tue, 01 Dec 2026 21:08:55 +0000 [thread overview]
Message-ID: <35d9d09ebea6fa065b8266dcd87bf71f44594458.1796159338.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2509.v5.git.git.1796159338.gitgitgadget@gmail.com>
From: Alessio Attilio <attilio.alessio@protonmail.com>
Status must resolve relative submodule URLs against the same remote as
git submodule sync. Move the resolver into common submodule code and
pass the repository explicitly, preserving existing helper behavior.
Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
---
builtin/submodule--helper.c | 60 +++++--------------------------------
submodule.c | 45 ++++++++++++++++++++++++++++
submodule.h | 12 ++++++++
3 files changed, 64 insertions(+), 53 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 461b682d0d..a05372bae8 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -46,35 +46,6 @@
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
void *cb_data);
-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)
-{
- char *remoteurl, *resolved_url;
- char *remote = get_default_remote();
- struct strbuf remotesb = STRBUF_INIT;
-
- strbuf_addf(&remotesb, "remote.%s.url", remote);
- if (repo_config_get_string(the_repository, remotesb.buf, &remoteurl)) {
- if (!quiet)
- warning(_("could not look up configuration '%s'. "
- "Assuming this repository is its own "
- "authoritative upstream."),
- remotesb.buf);
- remoteurl = xgetcwd();
- }
- resolved_url = relative_url(remoteurl, rel_url, up_path);
-
- free(remote);
- free(remoteurl);
- strbuf_release(&remotesb);
-
- return resolved_url;
-}
-
static int get_default_remote_submodule(const char *module_path, char **default_remote)
{
const struct submodule *sub;
@@ -92,7 +63,7 @@ static int get_default_remote_submodule(const char *module_path, char **default_
starts_with_dot_slash(url)) {
char *oldurl = url;
- url = resolve_relative_url(oldurl, NULL, 1);
+ url = resolve_submodule_url(the_repository, oldurl, NULL, 1);
free(oldurl);
}
}
@@ -290,23 +261,6 @@ static void module_list_active(struct module_list *list)
*list = active_modules;
}
-static char *get_up_path(const char *path)
-{
- struct strbuf sb = STRBUF_INIT;
-
- strbuf_addstrings(&sb, "../", count_slashes(path));
-
- /*
- * Check if 'path' ends with slash or not
- * for having the same output for dir/sub_dir
- * and dir/sub_dir/
- */
- if (!is_dir_sep(path[strlen(path) - 1]))
- strbuf_addstr(&sb, "../");
-
- return strbuf_detach(&sb, NULL);
-}
-
static void for_each_listed_submodule(const struct module_list *list,
each_submodule_fn fn, void *cb_data)
{
@@ -623,7 +577,7 @@ static void init_submodule(const char *path, const char *prefix,
starts_with_dot_slash(url)) {
char *oldurl = url;
- url = resolve_relative_url(oldurl, NULL, 0);
+ url = resolve_submodule_url(the_repository, oldurl, NULL, 0);
free(oldurl);
}
@@ -1453,10 +1407,10 @@ static void sync_submodule(const char *path, const char *prefix,
if (sub && sub->url) {
if (starts_with_dot_dot_slash(sub->url) ||
starts_with_dot_slash(sub->url)) {
- char *up_path = get_up_path(path);
+ char *up_path = get_submodule_up_path(path);
- sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
- super_config_url = resolve_relative_url(sub->url, NULL, 1);
+ sub_origin_url = resolve_submodule_url(the_repository, sub->url, up_path, 1);
+ super_config_url = resolve_submodule_url(the_repository, sub->url, NULL, 1);
free(up_path);
} else {
sub_origin_url = xstrdup(sub->url);
@@ -2334,7 +2288,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
if (sub->url && (starts_with_dot_slash(sub->url) ||
starts_with_dot_dot_slash(sub->url))) {
- url = resolve_relative_url(sub->url, NULL, 0);
+ url = resolve_submodule_url(the_repository, sub->url, NULL, 0);
need_free_url = 1;
} else
url = sub->url;
@@ -3761,7 +3715,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
"of the working tree"));
/* dereference source url relative to parent's url */
- to_free = resolve_relative_url(add_data.repo, NULL, 1);
+ to_free = resolve_submodule_url(the_repository, add_data.repo, NULL, 1);
add_data.realrepo = to_free;
} else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
add_data.realrepo = add_data.repo;
diff --git a/submodule.c b/submodule.c
index a74d1925aa..c8d95538d0 100644
--- a/submodule.c
+++ b/submodule.c
@@ -292,6 +292,51 @@ int is_submodule_active(struct repository *repo, const char *path)
return is_tree_submodule_active(repo, null_oid(the_hash_algo), path);
}
+char *resolve_submodule_url(struct repository *repo, const char *url,
+ const char *up_path, int quiet)
+{
+ char *remoteurl, *resolved_url;
+ const char *remote;
+ struct strbuf remotesb = STRBUF_INIT;
+
+ if (!starts_with_dot_dot_slash(url) && !starts_with_dot_slash(url))
+ return xstrdup(url);
+
+ remote = repo_default_remote(repo);
+ strbuf_addf(&remotesb, "remote.%s.url", remote);
+ if (repo_config_get_string(repo, remotesb.buf, &remoteurl)) {
+ if (!quiet)
+ warning(_("could not look up configuration '%s'. "
+ "Assuming this repository is its own "
+ "authoritative upstream."),
+ remotesb.buf);
+ remoteurl = repo->worktree ? xstrdup(repo->worktree) : xgetcwd();
+ }
+ resolved_url = relative_url(remoteurl, url, up_path);
+
+ free(remoteurl);
+ strbuf_release(&remotesb);
+
+ return resolved_url;
+}
+
+char *get_submodule_up_path(const char *path)
+{
+ struct strbuf sb = STRBUF_INIT;
+
+ strbuf_addstrings(&sb, "../", count_slashes(path));
+
+ /*
+ * Check if 'path' ends with slash or not
+ * for having the same output for dir/sub_dir
+ * and dir/sub_dir/
+ */
+ if (!is_dir_sep(path[strlen(path) - 1]))
+ strbuf_addstr(&sb, "../");
+
+ return strbuf_detach(&sb, NULL);
+}
+
int is_submodule_populated_gently(const char *path, int *return_error_code)
{
int ret = 0;
diff --git a/submodule.h b/submodule.h
index 1a465a1208..477091cd3c 100644
--- a/submodule.h
+++ b/submodule.h
@@ -62,6 +62,18 @@ int is_tree_submodule_active(struct repository *repo,
const struct object_id *treeish_name,
const char *path);
int is_submodule_active(struct repository *repo, const char *path);
+/*
+ * Resolve a relative submodule URL against the superproject's default remote.
+ * 'url' must be non-NULL; 'up_path' may be NULL. The caller must free the
+ * returned string.
+ */
+char *resolve_submodule_url(struct repository *repo, const char *url,
+ const char *up_path, int quiet);
+/*
+ * Return the path prefix from a submodule back to its superproject. 'path'
+ * must be non-NULL and non-empty. The caller must free the returned string.
+ */
+char *get_submodule_up_path(const char *path);
/*
* Determine if a submodule has been populated at a given 'path' by checking if
* the <path>/.git resolves to a valid git repository.
--
gitgitgadget
next prev parent reply other threads:[~2026-12-01 21:09 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 10:21 [PATCH] status: warn about stale submodule URLs Alessio Attilio via GitGitGadget
2026-11-27 13:41 ` D. Ben Knoble
2026-11-27 16:16 ` [PATCH v2 0/2] status: report when submodule URLs are out of sync Alessio Attilio via GitGitGadget
2026-11-27 16:16 ` [PATCH v2 1/2] submodule: prepare URL helpers for reuse Alessio Attilio via GitGitGadget
2026-11-27 16:16 ` [PATCH v2 2/2] status: warn about stale submodule URLs Alessio Attilio via GitGitGadget
2026-11-27 18:11 ` Junio C Hamano
2026-11-27 20:35 ` ATTILIO ALESSIO
2026-11-30 15:46 ` D. Ben Knoble
2026-11-27 21:27 ` [PATCH v3 0/2] status: report when submodule URLs are out of sync Alessio Attilio via GitGitGadget
2026-11-27 21:27 ` [PATCH v3 1/2] submodule: prepare URL helpers for reuse Alessio Attilio via GitGitGadget
2026-11-28 3:38 ` Junio C Hamano
2026-11-27 21:27 ` [PATCH v3 2/2] status: warn about stale submodule URLs Alessio Attilio via GitGitGadget
2026-11-28 4:08 ` Junio C Hamano
2026-11-28 4:19 ` [PATCH v3 0/2] status: report when submodule URLs are out of sync Junio C Hamano
2026-11-28 16:53 ` [PATCH v4 0/3] " Alessio Attilio via GitGitGadget
2026-11-28 16:53 ` [PATCH v4 1/3] submodule: move URL helpers into submodule.c Alessio Attilio via GitGitGadget
2026-11-30 15:54 ` D. Ben Knoble
2026-11-28 16:53 ` [PATCH v4 2/3] submodule: make URL resolution repository-aware Alessio Attilio via GitGitGadget
2026-11-30 15:56 ` D. Ben Knoble
2026-11-28 16:53 ` [PATCH v4 3/3] status: report when submodule URLs are out of sync Alessio Attilio via GitGitGadget
2026-11-29 15:42 ` Junio C Hamano
2026-12-01 21:08 ` [PATCH v5 0/4] " Alessio Attilio via GitGitGadget
2026-12-01 21:08 ` Alessio Attilio via GitGitGadget [this message]
2026-12-01 21:08 ` [PATCH v5 2/4] status: warn about submodule URL mismatches Alessio Attilio via GitGitGadget
2026-12-01 21:08 ` [PATCH v5 3/4] status: preserve customized submodule remotes Alessio Attilio via GitGitGadget
2026-12-01 21:08 ` [PATCH v5 4/4] status: test submodule URL warnings Alessio Attilio via GitGitGadget
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=35d9d09ebea6fa065b8266dcd87bf71f44594458.1796159338.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=attilio.alessio@protonmail.com \
--cc=ben.knoble@gmail.com \
--cc=git@vger.kernel.org \
/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