submodule--helper: add options for compute_submodule_clone_url()

Let's modify the interface to `compute_submodule_clone_url()` function by adding two more arguments, so that we can reuse this in various parts of `submodule--helper.c` that follow a common pattern, which is--read the remote url configuration of the superproject and then call `relative_url()`. This function is nearly identical to `resolve_relative_url()`, the only difference being the extra warning message. We can add a quiet flag to it, to suppress that warning when not needed, and then refactor `resolve_relative_url()` by using this function, something we will do in the next patch. We also rename the local variable 'relurl' to avoid potential confusion with the 'rel_url' parameter while we are at it. Having this functionality factored out will be useful for converting the rest of `submodule add` in subsequent patches. Signed-off-by: Atharva Raykar <raykar.ath@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Shourya Shukla <periperidip@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Atharva Raykar committed Aug 10, 2021 at 17:16 UTC 6baf4e4da44e8412d61118c5f35f383b679462ca
1 file changed +11 -7
builtin/submodule--helper.c
+11 -7
@@ -590,24 +590,28 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
590 return 0;
591 }
592
593 -static char *compute_submodule_clone_url(const char *rel_url)
593 +static char *compute_submodule_clone_url(const char *rel_url, const char *up_path, int quiet)
594 {
595 - char *remoteurl, *relurl;
595 + char *remoteurl, *resolved_url;
596 char *remote = get_default_remote();
597 struct strbuf remotesb = STRBUF_INIT;
598
599 strbuf_addf(&remotesb, "remote.%s.url", remote);
600 if (git_config_get_string(remotesb.buf, &remoteurl)) {
601 - warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
601 + if (!quiet)
602 + warning(_("could not look up configuration '%s'. "
603 + "Assuming this repository is its own "
604 + "authoritative upstream."),
605 + remotesb.buf);
606 remoteurl = xgetcwd();
607 }
604 - relurl = relative_url(remoteurl, rel_url, NULL);
608 + resolved_url = relative_url(remoteurl, rel_url, up_path);
609
610 free(remote);
611 free(remoteurl);
612 strbuf_release(&remotesb);
613
610 - return relurl;
614 + return resolved_url;
615 }
616
617 struct init_cb {
@@ -660,7 +664,7 @@ static void init_submodule(const char *path, const char *prefix,
664 if (starts_with_dot_dot_slash(url) ||
665 starts_with_dot_slash(url)) {
666 char *oldurl = url;
663 - url = compute_submodule_clone_url(oldurl);
667 + url = compute_submodule_clone_url(oldurl, NULL, 0);
668 free(oldurl);
669 }
670
@@ -2134,7 +2138,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
2138 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2139 if (starts_with_dot_slash(sub->url) ||
2140 starts_with_dot_dot_slash(sub->url)) {
2137 - url = compute_submodule_clone_url(sub->url);
2141 + url = compute_submodule_clone_url(sub->url, NULL, 0);
2142 need_free_url = 1;
2143 } else
2144 url = sub->url;