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>
This commit is contained in:
Atharva Raykar 2022-03-04 16:13:53 -08:00 committed by Junio C Hamano
parent e441966596
commit a77c3fcb5e

View file

@ -31,11 +31,13 @@
typedef void (*each_submodule_fn)(const struct cache_entry *list_item, typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
void *cb_data); void *cb_data);
static char *get_default_remote(void) static char *repo_get_default_remote(struct repository *repo)
{ {
char *dest = NULL, *ret; char *dest = NULL, *ret;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); struct ref_store *store = get_main_ref_store(repo);
const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
NULL);
if (!refname) if (!refname)
die(_("No such ref: %s"), "HEAD"); die(_("No such ref: %s"), "HEAD");
@ -48,7 +50,7 @@ static char *get_default_remote(void)
die(_("Expecting a full ref name, got %s"), refname); die(_("Expecting a full ref name, got %s"), refname);
strbuf_addf(&sb, "branch.%s.remote", refname); strbuf_addf(&sb, "branch.%s.remote", refname);
if (git_config_get_string(sb.buf, &dest)) if (repo_config_get_string(repo, sb.buf, &dest))
ret = xstrdup("origin"); ret = xstrdup("origin");
else else
ret = dest; ret = dest;
@ -57,6 +59,19 @@ static char *get_default_remote(void)
return ret; return ret;
} }
static char *get_default_remote_submodule(const char *module_path)
{
struct repository subrepo;
repo_submodule_init(&subrepo, the_repository, module_path, null_oid());
return repo_get_default_remote(&subrepo);
}
static char *get_default_remote(void)
{
return repo_get_default_remote(the_repository);
}
static int print_default_remote(int argc, const char **argv, const char *prefix) static int print_default_remote(int argc, const char **argv, const char *prefix)
{ {
char *remote; char *remote;
@ -1343,9 +1358,8 @@ static void sync_submodule(const char *path, const char *prefix,
{ {
const struct submodule *sub; const struct submodule *sub;
char *remote_key = NULL; char *remote_key = NULL;
char *sub_origin_url, *super_config_url, *displaypath; char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct child_process cp = CHILD_PROCESS_INIT;
char *sub_config_path = NULL; char *sub_config_path = NULL;
if (!is_submodule_active(the_repository, path)) if (!is_submodule_active(the_repository, path))
@ -1384,21 +1398,15 @@ static void sync_submodule(const char *path, const char *prefix,
if (!is_submodule_populated_gently(path, NULL)) if (!is_submodule_populated_gently(path, NULL))
goto cleanup; goto cleanup;
prepare_submodule_repo_env(&cp.env_array);
cp.git_cmd = 1;
cp.dir = path;
strvec_pushl(&cp.args, "submodule--helper",
"print-default-remote", NULL);
strbuf_reset(&sb); strbuf_reset(&sb);
if (capture_command(&cp, &sb, 0)) default_remote = get_default_remote_submodule(path);
if (!default_remote)
die(_("failed to get the default remote for submodule '%s'"), die(_("failed to get the default remote for submodule '%s'"),
path); path);
strbuf_strip_suffix(&sb, "\n"); remote_key = xstrfmt("remote.%s.url", default_remote);
remote_key = xstrfmt("remote.%s.url", sb.buf); free(default_remote);
strbuf_reset(&sb);
submodule_to_gitdir(&sb, path); submodule_to_gitdir(&sb, path);
strbuf_addstr(&sb, "/config"); strbuf_addstr(&sb, "/config");