git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 2/3] submodule: make URL resolution repository-aware

From
AGAlessio Attilio via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 28, 2026, 16:53 UTC
Message-ID
<ad4dafd82bb3759ab77805bb2fbc27ffd33c4e52.1795884822.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2509.v4.git.git.1795884822.gitgitgadget@gmail.com>
From: Alessio Attilio <attilio.alessio@protonmail.com>

Pass the repository explicitly to resolve_submodule_url() instead of relying on the_repository, and update existing callers. Resolve relative URLs against the repository worktree when no default remote URL is configured.

Keep this API change separate from moving the helpers. The non-relative URL fast path is established in the preparatory patch and remains independent of repository lookup.

Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
---
 builtin/submodule--helper.c | 12 ++++++------
 submodule.c                 | 23 +++++++++--------------
 submodule.h                 | 12 +++++++++++-
 3 files changed, 26 insertions(+), 21 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index cc84968bd3..a05372bae8 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -63,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_submodule_url(oldurl, NULL, 1);
+			url = resolve_submodule_url(the_repository, oldurl, NULL, 1);
 			free(oldurl);
 		}
 	}
@@ -577,7 +577,7 @@ static void init_submodule(const char *path, const char *prefix,
 		    starts_with_dot_slash(url)) {
 			char *oldurl = url;
 
-			url = resolve_submodule_url(oldurl, NULL, 0);
+			url = resolve_submodule_url(the_repository, oldurl, NULL, 0);
 			free(oldurl);
 		}
 
@@ -1409,8 +1409,8 @@ static void sync_submodule(const char *path, const char *prefix,
 		    starts_with_dot_slash(sub->url)) {
 			char *up_path = get_submodule_up_path(path);
 
-			sub_origin_url = resolve_submodule_url(sub->url, up_path, 1);
-			super_config_url = resolve_submodule_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);
@@ -2288,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_submodule_url(sub->url, NULL, 0);
+			url = resolve_submodule_url(the_repository, sub->url, NULL, 0);
 			need_free_url = 1;
 		} else
 			url = sub->url;
@@ -3715,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_submodule_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 9b7bb5c2f3..c8d95538d0 100644
--- a/submodule.c
+++ b/submodule.c
@@ -292,33 +292,28 @@ int is_submodule_active(struct repository *repo, const char *path)
 	return is_tree_submodule_active(repo, null_oid(the_hash_algo), path);
 }
 
-static char *get_default_remote(void)
-{
-	return xstrdup(repo_default_remote(the_repository));
-}
-
-char *resolve_submodule_url(const char *rel_url, const char *up_path, int quiet)
+char *resolve_submodule_url(struct repository *repo, const char *url,
+			    const char *up_path, int quiet)
 {
 	char *remoteurl, *resolved_url;
-	char *remote;
+	const char *remote;
 	struct strbuf remotesb = STRBUF_INIT;
 
-	if (!starts_with_dot_dot_slash(rel_url) && !starts_with_dot_slash(rel_url))
-		return xstrdup(rel_url);
+	if (!starts_with_dot_dot_slash(url) && !starts_with_dot_slash(url))
+		return xstrdup(url);
 
-	remote = get_default_remote();
+	remote = repo_default_remote(repo);
 	strbuf_addf(&remotesb, "remote.%s.url", remote);
-	if (repo_config_get_string(the_repository, remotesb.buf, &remoteurl)) {
+	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 = xgetcwd();
+		remoteurl = repo->worktree ? xstrdup(repo->worktree) : xgetcwd();
 	}
-	resolved_url = relative_url(remoteurl, rel_url, up_path);
+	resolved_url = relative_url(remoteurl, url, up_path);
 
-	free(remote);
 	free(remoteurl);
 	strbuf_release(&remotesb);
 
diff --git a/submodule.h b/submodule.h
index 82972781b4..477091cd3c 100644
--- a/submodule.h
+++ b/submodule.h
@@ -62,7 +62,17 @@ 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);
-char *resolve_submodule_url(const char *url, const char *up_path, int quiet);
+/*
+ * 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
-- 
gitgitgadget
Previous: D. Ben KnobleNext: D. Ben Knoble
Message 18 of 26 in “status: warn about stale submodule URLs”
  1. status: warn about stale submodule URLsAlessio Attilio via GitGitGadget, Nov 27, 2026
  2. D. Ben KnobleNov 27, 2026
  3. 0/2 status: report when submodule URLs are out of syncAlessio Attilio via GitGitGadget, Nov 27, 2026
  4. 1/2 submodule: prepare URL helpers for reuseAlessio Attilio via GitGitGadget, Nov 27, 2026
  5. 2/2 status: warn about stale submodule URLsAlessio Attilio via GitGitGadget, Nov 27, 2026
  6. Junio C HamanoNov 27, 2026
  7. ATTILIO ALESSIONov 27, 2026
  8. D. Ben KnobleNov 30, 2026
  9. 0/2 status: report when submodule URLs are out of syncAlessio Attilio via GitGitGadget, Nov 27, 2026
  10. 1/2 submodule: prepare URL helpers for reuseAlessio Attilio via GitGitGadget, Nov 27, 2026
  11. Junio C HamanoNov 28, 2026
  12. 2/2 status: warn about stale submodule URLsAlessio Attilio via GitGitGadget, Nov 27, 2026
  13. Junio C HamanoNov 28, 2026
  14. Junio C HamanoNov 28, 2026
  15. 0/3 status: report when submodule URLs are out of syncAlessio Attilio via GitGitGadget, Nov 28, 2026
  16. 1/3 submodule: move URL helpers into submodule.cAlessio Attilio via GitGitGadget, Nov 28, 2026
  17. D. Ben KnobleNov 30, 2026
  18. 2/3 submodule: make URL resolution repository-awareAlessio Attilio via GitGitGadget, Nov 28, 2026
  19. D. Ben KnobleNov 30, 2026
  20. 3/3 status: report when submodule URLs are out of syncAlessio Attilio via GitGitGadget, Nov 28, 2026
  21. Junio C HamanoNov 29, 2026
  22. 0/4 status: report when submodule URLs are out of syncAlessio Attilio via GitGitGadget, Dec 1, 2026
  23. 1/4 submodule: share repository-aware URL resolutionAlessio Attilio via GitGitGadget, Dec 1, 2026
  24. 2/4 status: warn about submodule URL mismatchesAlessio Attilio via GitGitGadget, Dec 1, 2026
  25. 3/4 status: preserve customized submodule remotesAlessio Attilio via GitGitGadget, Dec 1, 2026
  26. 4/4 status: test submodule URL warningsAlessio Attilio via GitGitGadget, Dec 1, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.