From: D. Ben Knoble Date: Fri, 27 Nov 2026 13:41:32 GMT Subject: Re: [PATCH] status: warn about stale submodule URLs Message-ID: In-Reply-To: Hi Alessio, On Fri, Nov 27, 2026 at 5:28 AM Alessio Attilio via GitGitGadget wrote: > > From: Alessio Attilio > > When a submodule URL changes in .gitmodules, the URL cached in > .git/config can keep pointing at the old remote. That leaves users > with no indication that later submodule operations may use a > repository different from the one the project declares. > > Warn in git status so users can find and sync stale configuration > before it causes a confusing fetch or update. Resolve relative URLs > like git submodule sync does, and leave customized submodule remotes > alone. This sounds like a good problem to solve, as it makes it more obvious when to run "git submodule sync"---especially if you don't trust one of the 2 URLs involved anymore! > diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c > -static char *get_default_remote(void) > -static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet) > -static char *get_up_path(const char *path) > diff --git a/submodule.c b/submodule.c > +char *resolve_submodule_url(struct repository *repo, const char *url, > + const char *up_path, int quiet) > +char *get_submodule_up_path(const char *path) I am somewhat surprised to see this refactoring: at a quick glance, it looks like some static functions needed to be exposed to another translation unit, but that was not explained in the proposed log message. (We also teach the URL resolver about a repository, all at the same time? And I can't see where get_default_remote ended up, but that's probably my skimming.) On top of that, our usual style is to first, in a preparatory patch, move code around (making it easy to see the movement), then in subsequent patches make the small needed changes for a fix or feature (making it easy to see the isolated changes). > diff --git a/t/t7508-status.sh b/t/t7508-status.sh > index e10c961268..bf28de1907 100755 > --- a/t/t7508-status.sh > +++ b/t/t7508-status.sh > @@ -921,6 +921,61 @@ 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_when_finished "rm -f actual err" && Removing some of these files is a bit unusual, I think. > + 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 >actual 2>err && > + test_must_be_empty err && > + git -C url-test/super config -f .gitmodules submodule.module.url \ > + "$PWD/url-test/updated-source" && > + git -C url-test/super status --porcelain >actual 2>err && > + test_must_be_empty err && > + git -C url-test/super status >actual 2>err && > + test_grep "module.*different URL" err && > + test_grep "git submodule sync" err && > + git -C url-test/super submodule sync && > + git -C url-test/super status >actual 2>err && > + test_must_be_empty 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 >actual 2>err && > + test_grep "module.*different URL" err && > + git -C url-test/super submodule sync && > + git -C url-test/super status >actual 2>err && > + test_must_be_empty err && > + git -C url-test/super config -f .gitmodules submodule.module.url \ > + ../another-relative-source && > + git -C url-test/super status >actual 2>err && > + test_grep "module.*different URL" err && > + git -C url-test/super submodule sync && > + git -C url-test/super status >actual 2>err && > + test_must_be_empty 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 >actual 2>err && > + test_must_be_empty 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 >actual 2>err && > + test_must_be_empty 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 >actual 2>err && > + test_grep "module.*different URL" err > +' I did not read the test script closely, but it seems we change some submodule URLs and assert on status output, which is roughly what I'd expect. > diff --git a/wt-status.c b/wt-status.c > index 238bb48643..e06c18d8e0 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,103 @@ 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 *url, > + const char *configured_url) > +{ > + struct repository subrepo = { 0 }; > + const struct string_list *remote_urls = NULL; > + const char *remote; > + const char *remote_url; > + char *key, *up_path, *old_url, *new_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; > + } I suppose in these cases we don't actually know the answer to the question "submodule_has_custom_remote_url"… but I'm not sure if the tri-state + behaving differently is worth it for a new warning yet, so I'm ok with falling back to the current state of affairs for now. > + 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); > + old_url = resolve_submodule_url(repo, configured_url, up_path, 1); > + new_url = resolve_submodule_url(repo, url, up_path, 1); > + ret = strcmp(remote_url, old_url) != 0 && > + strcmp(remote_url, new_url) != 0; I'm not sure I follow this strcmp logic: * remote_url is from the submodule's config, I assume from the .git/modules/…? * old_url is the resolved configured_url, which reading ahead tells me is the superproject's config? * new_url is the resolved url, which reading ahead tells me is also from a pre-resolved submodule's config (aka "expected_url")? So, I guess I'm actually confused about a few things: * the terminology between caller (expected/configured) and callee doesn't match; consistency might help * the double-resolution of new_url seems odd? * what are the 3 URLs involved? I suppose I was expecting a comparison of 2, since that's what the log message describes (.gitmodules + cached URL in .git/config). > + free(new_url); > + free(old_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 *configured_url; > + char *key, *expected_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, &configured_url)) { > + free(key); > + continue; > + } > + free(key); > + > + expected_url = resolve_submodule_url(s->repo, sub->url, NULL, 1); > + if (strcmp(configured_url, expected_url) && > + !submodule_has_custom_remote_url(s->repo, ce->name, > + expected_url, > + configured_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(expected_url); > + } > +} > + Thanks -- D. Ben Knoble