submodule--helper: get remote names from any repository

`get_default_remote()` retrieves the name of a remote by resolving the refs from of the current repository's ref store. Thus in order to use it for retrieving the remote name of a submodule, we have to start a new subprocess which runs from the submodule directory. Let's instead introduce a function called `repo_get_default_remote()` which takes any repository object and retrieves the remote accordingly. `get_default_remote()` is then defined as a call to `repo_get_default_remote()` with 'the_repository' passed to it. Now that we have `repo_get_default_remote()`, we no longer have to start a subprocess that called `submodule--helper get-default-remote` from within the submodule directory. So let's make a function called `get_default_remote_submodule()` which takes a submodule path, and returns the default remote for that submodule, all within the same process. We can now use this function to save an unnecessary subprocess spawn in `sync_submodule()`, and also in a subsequent patch, which will require this functionality. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Shourya Shukla <periperidip@gmail.com> Helped-by: Glen Choo <chooglen@google.com> Signed-off-by: Atharva Raykar <raykar.ath@gmail.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Atharva Raykar committed Mar 4, 2022 at 16:13 UTC a77c3fcb5ec535507e1e71da4f642151f65b4b17
1 file changed +23 -15
builtin/submodule--helper.c
+23 -15
@@ -31,11 +31,13 @@
31 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
32 void *cb_data);
33
34 -static char *get_default_remote(void)
34 +static char *repo_get_default_remote(struct repository *repo)
35 {
36 char *dest = NULL, *ret;
37 struct strbuf sb = STRBUF_INIT;
38 - const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
38 + struct ref_store *store = get_main_ref_store(repo);
39 + const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
40 + NULL);
41
42 if (!refname)
43 die(_("No such ref: %s"), "HEAD");
@@ -48,7 +50,7 @@ static char *get_default_remote(void)
50 die(_("Expecting a full ref name, got %s"), refname);
51
52 strbuf_addf(&sb, "branch.%s.remote", refname);
51 - if (git_config_get_string(sb.buf, &dest))
53 + if (repo_config_get_string(repo, sb.buf, &dest))
54 ret = xstrdup("origin");
55 else
56 ret = dest;
@@ -57,6 +59,19 @@ static char *get_default_remote(void)
59 return ret;
60 }
61
62 +static char *get_default_remote_submodule(const char *module_path)
63 +{
64 + struct repository subrepo;
65 +
66 + repo_submodule_init(&subrepo, the_repository, module_path, null_oid());
67 + return repo_get_default_remote(&subrepo);
68 +}
69 +
70 +static char *get_default_remote(void)
71 +{
72 + return repo_get_default_remote(the_repository);
73 +}
74 +
75 static int print_default_remote(int argc, const char **argv, const char *prefix)
76 {
77 char *remote;
@@ -1343,9 +1358,8 @@ static void sync_submodule(const char *path, const char *prefix,
1358 {
1359 const struct submodule *sub;
1360 char *remote_key = NULL;
1346 - char *sub_origin_url, *super_config_url, *displaypath;
1361 + char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1362 struct strbuf sb = STRBUF_INIT;
1348 - struct child_process cp = CHILD_PROCESS_INIT;
1363 char *sub_config_path = NULL;
1364
1365 if (!is_submodule_active(the_repository, path))
@@ -1384,21 +1398,15 @@ static void sync_submodule(const char *path, const char *prefix,
1398 if (!is_submodule_populated_gently(path, NULL))
1399 goto cleanup;
1400
1387 - prepare_submodule_repo_env(&cp.env_array);
1388 - cp.git_cmd = 1;
1389 - cp.dir = path;
1390 - strvec_pushl(&cp.args, "submodule--helper",
1391 - "print-default-remote", NULL);
1392 -
1401 strbuf_reset(&sb);
1394 - if (capture_command(&cp, &sb, 0))
1402 + default_remote = get_default_remote_submodule(path);
1403 + if (!default_remote)
1404 die(_("failed to get the default remote for submodule '%s'"),
1405 path);
1406
1398 - strbuf_strip_suffix(&sb, "\n");
1399 - remote_key = xstrfmt("remote.%s.url", sb.buf);
1407 + remote_key = xstrfmt("remote.%s.url", default_remote);
1408 + free(default_remote);
1409
1401 - strbuf_reset(&sb);
1410 submodule_to_gitdir(&sb, path);
1411 strbuf_addstr(&sb, "/config");
1412