From: Alessio Attilio <atti…@protonmail.com>
Users may point a populated submodule remote at a fork. Since git
submodule sync would overwrite custom or multi-URL remotes, suppress
the advice unless the configured remote matches the old or declared URL.
Signed-off-by: Alessio Attilio <atti…@protonmail.com>
---
wt-status.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 83 insertions(+), 1 deletion(-)
diff --git a/wt-status.c b/wt-status.c
index bbd0e082dc..69c05735ec 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -2696,6 +2696,85 @@ 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 *declared_url,
+ const char *cached_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;
+ char *declared_super_url;
+ char *worktree_path;
+ int error_code, populated, ret = 0;
+
+ worktree_path = repo_worktree_path(repo, "%s", path);
+ if (!worktree_path)
+ return 0;
+ populated = is_submodule_populated_gently(worktree_path, &error_code);
+ free(worktree_path);
+ if (!populated)
+ return 0;
+
+ if (repo_submodule_init(&subrepo, repo, path,
+ null_oid(repo->hash_algo)) < 0) {
+ repo_clear(&subrepo);
+ return 0;
+ }
+
+ declared_super_url = resolve_submodule_url(repo, declared_url, NULL, 1);
+ remote = repo_remote_from_url(&subrepo, declared_super_url);
+ if (!remote)
+ remote = repo_default_remote(&subrepo);
+ free(declared_super_url);
+
+ key = xstrfmt("remote.%s.url", remote);
+ if (!repo_config_get_string_multi(&subrepo, key, &remote_urls) &&
+ remote_urls->nr == 1 &&
+ (remote_url = remote_urls->items[0].string)) {
+ up_path = get_submodule_up_path(path);
+ /*
+ * sync_submodule() stores a relative URL in the superproject's
+ * config without the submodule path prefix, but adds that prefix
+ * when storing the URL in the submodule's remote configuration.
+ */
+ if (url_is_local_not_ssh(cached_url) &&
+ !is_absolute_path(cached_url)) {
+ struct strbuf sb = STRBUF_INIT;
+
+ strbuf_addstr(&sb, up_path);
+ strbuf_addstr(&sb, cached_url);
+ config_remote_url = strbuf_detach(&sb, NULL);
+ } else {
+ config_remote_url = xstrdup(cached_url);
+ }
+ gitmodules_remote_url = resolve_submodule_url(repo, declared_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;
@@ -2734,7 +2813,10 @@ static void wt_status_check_submodule_urls(struct wt_status *s)
free(key);
gitmodules_url = resolve_submodule_url(s->repo, sub->url, NULL, 1);
- if (strcmp(config_url, gitmodules_url))
+ 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);
--
gitgitgadget