[PATCH v2 2/2] status: warn about stale submodule URLs
- From
- Alessio Attilio via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Nov 27, 2026, 16:16 UTC
- Message-ID
- <b0dd21100ccfe4411ccc1237fdb7ddedb66075e8.1795796162.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2509.v2.git.git.1795796162.gitgitgadget@gmail.com>
From: Alessio Attilio <attilio.alessio@protonmail.com>
When a submodule URL changes in .gitmodules, the cached submodule.<name>.url in the superproject config can stay stale. Warn in git status so users can run git submodule sync before a later submodule operation uses the outdated URL.
Resolve relative URLs using the same paths as git submodule sync. Avoid warning when the submodule remote indicates a local customization that sync would overwrite.
Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com> --- submodule.c | 3 ++ t/t7508-status.sh | 54 ++++++++++++++++++++++++ wt-status.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+)
diff --git a/submodule.c b/submodule.c index f9ca99c5c8..5d0018d032 100644 --- a/submodule.c +++ b/submodule.c @@ -299,6 +299,9 @@ char *resolve_submodule_url(struct repository *repo, const char *url, char *remoteurl, *resolved_url; struct strbuf remotesb = STRBUF_INIT; + if (!starts_with_dot_dot_slash(url) && !starts_with_dot_slash(url)) + return xstrdup(url); + strbuf_addf(&remotesb, "remote.%s.url", remote); if (repo_config_get_string(repo, remotesb.buf, &remoteurl)) { if (!quiet) diff --git a/t/t7508-status.sh b/t/t7508-status.sh index e10c961268..76a2e96dd7 100755 --- a/t/t7508-status.sh +++ b/t/t7508-status.sh @@ -921,6 +921,60 @@ test_expect_success 'setup status submodule summary' ' git add sm ' +test_expect_success 'status reports submodule URL mismatch' ' + test_when_finished "rm -rf url-test" && + test_create_repo url-test/source && + test_commit -C url-test/source initial && + test_create_repo url-test/super && + git -C url-test/super -c protocol.file.allow=always \ + submodule add "$PWD/url-test/source" module && + git -C url-test/super commit -m "add submodule" && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_must_be_empty url-test/err && + git -C url-test/super config -f .gitmodules submodule.module.url \ + "$PWD/url-test/updated-source" && + git -C url-test/super status --porcelain >url-test/actual 2>url-test/err && + test_must_be_empty url-test/err && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_grep "module.*different URL" url-test/err && + test_grep "git submodule sync" url-test/err && + git -C url-test/super submodule sync && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_must_be_empty url-test/err && + git -C url-test/super remote add origin ../source-parent && + git -C url-test/super config -f .gitmodules submodule.module.url \ + ../relative-source && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_grep "module.*different URL" url-test/err && + git -C url-test/super submodule sync && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_must_be_empty url-test/err && + git -C url-test/super config -f .gitmodules submodule.module.url \ + ../another-relative-source && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_grep "module.*different URL" url-test/err && + git -C url-test/super submodule sync && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_must_be_empty url-test/err && + git -C url-test/super/module config remote.origin.url \ + "$PWD/url-test/fork" && + git -C url-test/super config -f .gitmodules submodule.module.url \ + ../fork-source && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_must_be_empty url-test/err && + git -C url-test/super/module config --add remote.origin.url \ + "$PWD/url-test/another-fork" && + git -C url-test/super config -f .gitmodules submodule.module.url \ + ../another-fork-source && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_must_be_empty url-test/err && + git -C url-test/super/module config --unset-all remote.origin.url && + git -C url-test/super config --add submodule.module.url \ + "$PWD/url-test/another-configured-source" && + git -C url-test/super status >url-test/actual 2>url-test/err && + test_grep "module.*different URL" url-test/err +' + test_expect_success 'status submodule summary is disabled by default' ' cat >expect <<EOF && On branch main diff --git a/wt-status.c b/wt-status.c index 238bb48643..be6902c9dd 100644 --- a/wt-status.c +++ b/wt-status.c @@ -3,6 +3,7 @@ #include "git-compat-util.h" #include "advice.h" +#include "config.h" #include "wt-status.h" #include "object.h" #include "dir.h" @@ -23,6 +24,7 @@ #include "remote.h" #include "refs.h" #include "submodule.h" +#include "submodule-config.h" #include "column.h" #include "read-cache.h" #include "setup.h" @@ -2692,8 +2694,110 @@ static void wt_porcelain_v2_print(struct wt_status *s) } } +static int submodule_has_custom_remote_url(struct repository *repo, + const char *path, + const char *gitmodules_url, + const char *config_url) +{ + struct repository subrepo = { 0 }; + const struct string_list *remote_urls = NULL; + const char *remote; + const char *remote_url; + char *key, *up_path, *config_remote_url, *gitmodules_remote_url; + int error_code, ret = 0; + + if (!is_submodule_populated_gently(path, &error_code)) + return 0; + + if (repo_submodule_init(&subrepo, repo, path, + null_oid(repo->hash_algo)) < 0) { + repo_clear(&subrepo); + return 0; + } + + remote = repo_default_remote(&subrepo); + + key = xstrfmt("remote.%s.url", remote); + if (!repo_config_get_value_multi(&subrepo, key, &remote_urls) && + remote_urls->nr == 1) { + remote_url = remote_urls->items[0].string; + up_path = get_submodule_up_path(path); + config_remote_url = resolve_submodule_url(repo, config_url, + up_path, 1); + gitmodules_remote_url = resolve_submodule_url(repo, gitmodules_url, + up_path, 1); + /* + * The submodule's remote URL may be a local fork. If it matches + * neither the old cached URL nor the new .gitmodules URL, leave it + * alone; syncing would overwrite that customization. + */ + ret = strcmp(remote_url, config_remote_url) != 0 && + strcmp(remote_url, gitmodules_remote_url) != 0; + free(gitmodules_remote_url); + free(config_remote_url); + free(up_path); + } else { + /* + * Multiple URLs are a local customization; sync would replace + * them all with a single URL. + */ + ret = remote_urls && remote_urls->nr > 1; + } + free(key); + repo_clear(&subrepo); + + return ret; +} + +static void wt_status_check_submodule_urls(struct wt_status *s) +{ + int i; + + if (s->status_format != STATUS_FORMAT_LONG && + s->status_format != STATUS_FORMAT_NONE) + return; + + if (is_gitmodules_unmerged(s->repo->index)) + return; + + for (i = 0; i < s->repo->index->cache_nr; i++) { + struct cache_entry *ce = s->repo->index->cache[i]; + const struct submodule *sub; + const char *config_url; + char *key, *gitmodules_url; + + if (ce_stage(ce) || !S_ISGITLINK(ce->ce_mode)) + continue; + + sub = submodule_from_path(s->repo, null_oid(s->repo->hash_algo), + ce->name); + if (!sub || !sub->url || !is_submodule_active(s->repo, ce->name)) + continue; + + key = xstrfmt("submodule.%s.url", sub->name); + if (repo_config_get_value(s->repo, key, &config_url)) { + free(key); + continue; + } + free(key); + + gitmodules_url = resolve_submodule_url(s->repo, sub->url, NULL, 1); + if (strcmp(config_url, gitmodules_url) && + !submodule_has_custom_remote_url(s->repo, ce->name, + sub->url, + config_url)) + warning(_("submodule '%s' has a different URL in .git/config " + "than in .gitmodules\n" + " run 'git submodule sync' to update it"), ce->name); + + free(gitmodules_url); + } +} + void wt_status_print(struct wt_status *s) { + wt_status_check_submodule_urls(s); + trace2_data_intmax("status", s->repo, "count/changed", s->change.nr); trace2_data_intmax("status", s->repo, "count/untracked", s->untracked.nr);
-- gitgitgadget