Skip to content

Commit a77c3fc

Browse files
tfidfwastakengitster
authored andcommitted
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 <[email protected]> Mentored-by: Shourya Shukla <[email protected]> Helped-by: Glen Choo <[email protected]> Signed-off-by: Atharva Raykar <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e441966 commit a77c3fc

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

builtin/submodule--helper.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
3232
void *cb_data);
3333

34-
static char *get_default_remote(void)
34+
static char *repo_get_default_remote(struct repository *repo)
3535
{
3636
char *dest = NULL, *ret;
3737
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);
3941

4042
if (!refname)
4143
die(_("No such ref: %s"), "HEAD");
@@ -48,7 +50,7 @@ static char *get_default_remote(void)
4850
die(_("Expecting a full ref name, got %s"), refname);
4951

5052
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))
5254
ret = xstrdup("origin");
5355
else
5456
ret = dest;
@@ -57,6 +59,19 @@ static char *get_default_remote(void)
5759
return ret;
5860
}
5961

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+
6075
static int print_default_remote(int argc, const char **argv, const char *prefix)
6176
{
6277
char *remote;
@@ -1343,9 +1358,8 @@ static void sync_submodule(const char *path, const char *prefix,
13431358
{
13441359
const struct submodule *sub;
13451360
char *remote_key = NULL;
1346-
char *sub_origin_url, *super_config_url, *displaypath;
1361+
char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
13471362
struct strbuf sb = STRBUF_INIT;
1348-
struct child_process cp = CHILD_PROCESS_INIT;
13491363
char *sub_config_path = NULL;
13501364

13511365
if (!is_submodule_active(the_repository, path))
@@ -1384,21 +1398,15 @@ static void sync_submodule(const char *path, const char *prefix,
13841398
if (!is_submodule_populated_gently(path, NULL))
13851399
goto cleanup;
13861400

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-
13931401
strbuf_reset(&sb);
1394-
if (capture_command(&cp, &sb, 0))
1402+
default_remote = get_default_remote_submodule(path);
1403+
if (!default_remote)
13951404
die(_("failed to get the default remote for submodule '%s'"),
13961405
path);
13971406

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);
14001409

1401-
strbuf_reset(&sb);
14021410
submodule_to_gitdir(&sb, path);
14031411
strbuf_addstr(&sb, "/config");
14041412

0 commit comments

Comments
 (0)