All of lore.kernel.org
 help / color / mirror / Atom feed
From: Glen Choo <chooglen@google.com>
To: git@vger.kernel.org
Cc: "Glen Choo" <chooglen@google.com>,
	"Junio C Hamano" <gitster@pobox.com>,
	"Atharva Raykar" <raykar.ath@gmail.com>,
	"Emily Shaffer" <emilyshaffer@google.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Josh Steadmon" <steadmon@google.com>,
	"Christian Couder" <christian.couder@gmail.com>,
	"Shourya Shukla" <periperidip@gmail.com>
Subject: [PATCH v4 05/13] submodule--helper: get remote names from any repository
Date: Fri,  4 Mar 2022 16:13:53 -0800	[thread overview]
Message-ID: <20220305001401.20888-6-chooglen@google.com> (raw)
In-Reply-To: <20220305001401.20888-1-chooglen@google.com>

From: Atharva Raykar <raykar.ath@gmail.com>

`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>
---
 builtin/submodule--helper.c | 38 ++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 77ca4270f4..b5a2d83029 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -31,11 +31,13 @@
 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
 				  void *cb_data);
 
-static char *get_default_remote(void)
+static char *repo_get_default_remote(struct repository *repo)
 {
 	char *dest = NULL, *ret;
 	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)
 		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);
 
 	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");
 	else
 		ret = dest;
@@ -57,6 +59,19 @@ static char *get_default_remote(void)
 	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)
 {
 	char *remote;
@@ -1343,9 +1358,8 @@ static void sync_submodule(const char *path, const char *prefix,
 {
 	const struct submodule *sub;
 	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 child_process cp = CHILD_PROCESS_INIT;
 	char *sub_config_path = NULL;
 
 	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))
 		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);
-	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'"),
 		      path);
 
-	strbuf_strip_suffix(&sb, "\n");
-	remote_key = xstrfmt("remote.%s.url", sb.buf);
+	remote_key = xstrfmt("remote.%s.url", default_remote);
+	free(default_remote);
 
-	strbuf_reset(&sb);
 	submodule_to_gitdir(&sb, path);
 	strbuf_addstr(&sb, "/config");
 
-- 
2.33.GIT


  parent reply	other threads:[~2022-03-05  0:15 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-01  0:08 [PATCH 00/13] submodule: convert parts of 'update' to C Glen Choo
2022-03-01  0:08 ` [PATCH 01/13] submodule tests: test for init and update failure output Glen Choo
2022-03-01  0:08 ` [PATCH 02/13] submodule--helper: remove update-module-mode Glen Choo
2022-03-01  0:08 ` [PATCH 03/13] submodule--helper: reorganize code for sh to C conversion Glen Choo
2022-03-01  0:08 ` [PATCH 04/13] submodule--helper run-update-procedure: remove --suboid Glen Choo
2022-03-01  0:08 ` [PATCH 05/13] submodule--helper: remove ensure-core-worktree Glen Choo
2022-03-01  0:08 ` [PATCH 06/13] submodule--helper: get remote names from any repository Glen Choo
2022-03-01  2:46   ` Junio C Hamano
2022-03-01  4:26     ` Glen Choo
2022-03-01  0:08 ` [PATCH 07/13] submodule--helper: don't use bitfield indirection for parse_options() Glen Choo
2022-03-01  0:08 ` [PATCH 08/13] submodule--helper run-update-procedure: learn --remote Glen Choo
2022-03-01  0:08 ` [PATCH 09/13] submodule--helper: refactor get_submodule_displaypath() Glen Choo
2022-03-01  0:08 ` [PATCH 10/13] submodule--helper: allow setting superprefix for init_submodule() Glen Choo
2022-03-01  0:08 ` [PATCH 11/13] submodule--helper update-clone: learn --init Glen Choo
2022-03-01  0:08 ` [PATCH 12/13] submodule update: add tests for --filter Glen Choo
2022-03-01  0:08 ` [PATCH 13/13] submodule--helper update-clone: check for --filter and --init Glen Choo
2022-03-01  1:29 ` [PATCH 00/13] submodule: convert parts of 'update' to C Glen Choo
2022-03-01  4:41 ` [PATCH v2 " Glen Choo
2022-03-01  4:41   ` [PATCH v2 01/13] submodule tests: test for init and update failure output Glen Choo
2022-03-01  4:41   ` [PATCH v2 02/13] submodule--helper: remove update-module-mode Glen Choo
2022-03-01  4:41   ` [PATCH v2 03/13] submodule--helper: reorganize code for sh to C conversion Glen Choo
2022-03-01  4:41   ` [PATCH v2 04/13] submodule--helper run-update-procedure: remove --suboid Glen Choo
2022-03-01  4:41   ` [PATCH v2 05/13] submodule--helper: remove ensure-core-worktree Glen Choo
2022-03-01  4:41   ` [PATCH v2 06/13] submodule--helper: get remote names from any repository Glen Choo
2022-03-01  8:01     ` Ævar Arnfjörð Bjarmason
2022-03-01  4:41   ` [PATCH v2 07/13] submodule--helper: don't use bitfield indirection for parse_options() Glen Choo
2022-03-01  4:41   ` [PATCH v2 08/13] submodule--helper run-update-procedure: learn --remote Glen Choo
2022-03-01  4:41   ` [PATCH v2 09/13] submodule--helper: refactor get_submodule_displaypath() Glen Choo
2022-03-01  8:05     ` Ævar Arnfjörð Bjarmason
2022-03-01  4:41   ` [PATCH v2 10/13] submodule--helper: allow setting superprefix for init_submodule() Glen Choo
2022-03-01  4:41   ` [PATCH v2 11/13] submodule--helper update-clone: learn --init Glen Choo
2022-03-01  4:41   ` [PATCH v2 12/13] submodule update: add tests for --filter Glen Choo
2022-03-01  8:07     ` Ævar Arnfjörð Bjarmason
2022-03-01 18:30       ` Glen Choo
2022-03-01  4:41   ` [PATCH v2 13/13] submodule--helper update-clone: check for --filter and --init Glen Choo
2022-03-01  7:21     ` Ævar Arnfjörð Bjarmason
2022-03-01  7:34       ` Junio C Hamano
2022-03-01 18:34         ` Glen Choo
2022-03-03 10:06           ` Ævar Arnfjörð Bjarmason
2022-03-03  0:57   ` [PATCH v3 00/13] submodule: convert parts of 'update' to C Glen Choo
2022-03-03  0:57     ` [PATCH v3 01/13] submodule tests: test for init and update failure output Glen Choo
2022-03-03  0:57     ` [PATCH v3 02/13] submodule--helper: remove update-module-mode Glen Choo
2022-03-03  0:57     ` [PATCH v3 03/13] submodule--helper: reorganize code for sh to C conversion Glen Choo
2022-03-03  0:57     ` [PATCH v3 04/13] submodule--helper run-update-procedure: remove --suboid Glen Choo
2022-03-03 21:09       ` Junio C Hamano
2022-03-03  0:57     ` [PATCH v3 05/13] submodule--helper: remove ensure-core-worktree Glen Choo
2022-03-03 21:25       ` Junio C Hamano
2022-03-04 21:27         ` Glen Choo
2022-03-03  0:57     ` [PATCH v3 06/13] submodule--helper: get remote names from any repository Glen Choo
2022-03-03  0:57     ` [PATCH v3 07/13] submodule--helper: don't use bitfield indirection for parse_options() Glen Choo
2022-03-03  0:57     ` [PATCH v3 08/13] submodule--helper run-update-procedure: learn --remote Glen Choo
2022-03-03 21:35       ` Junio C Hamano
2022-03-04 21:29         ` Glen Choo
2022-03-03  0:57     ` [PATCH v3 09/13] submodule--helper: refactor get_submodule_displaypath() Glen Choo
2022-03-03  0:57     ` [PATCH v3 10/13] submodule--helper: allow setting superprefix for init_submodule() Glen Choo
2022-03-03  0:57     ` [PATCH v3 11/13] submodule--helper update-clone: learn --init Glen Choo
2022-03-03  0:57     ` [PATCH v3 12/13] submodule update: add tests for --filter Glen Choo
2022-03-03  0:57     ` [PATCH v3 13/13] submodule--helper update-clone: check for --filter and --init Glen Choo
2022-03-03  9:58     ` [PATCH v3 00/13] submodule: convert parts of 'update' to C Ævar Arnfjörð Bjarmason
2022-03-03 21:41       ` Junio C Hamano
2022-03-05  0:13     ` [PATCH v4 " Glen Choo
2022-03-05  0:13       ` [PATCH v4 01/13] submodule tests: test for init and update failure output Glen Choo
2022-03-05  0:13       ` [PATCH v4 02/13] submodule--helper: remove update-module-mode Glen Choo
2022-03-05  0:13       ` [PATCH v4 03/13] submodule--helper: reorganize code for sh to C conversion Glen Choo
2022-03-05  0:13       ` [PATCH v4 04/13] submodule--helper run-update-procedure: remove --suboid Glen Choo
2022-03-05  0:13       ` Glen Choo [this message]
2022-03-05  0:13       ` [PATCH v4 06/13] submodule--helper: don't use bitfield indirection for parse_options() Glen Choo
2022-03-05  0:13       ` [PATCH v4 07/13] submodule--helper run-update-procedure: learn --remote Glen Choo
2022-03-05  0:13       ` [PATCH v4 08/13] submodule--helper: refactor get_submodule_displaypath() Glen Choo
2022-03-05  0:13       ` [PATCH v4 09/13] submodule--helper: allow setting superprefix for init_submodule() Glen Choo
2022-03-05  0:13       ` [PATCH v4 10/13] submodule--helper update-clone: learn --init Glen Choo
2022-03-05  0:13       ` [PATCH v4 11/13] submodule--helper: remove ensure-core-worktree Glen Choo
2022-03-05  0:14       ` [PATCH v4 12/13] submodule update: add tests for --filter Glen Choo
2022-03-05  0:14       ` [PATCH v4 13/13] submodule--helper update-clone: check for --filter and --init Glen Choo
2022-03-05  0:40       ` [PATCH v4 00/13] submodule: convert parts of 'update' to C Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220305001401.20888-6-chooglen@google.com \
    --to=chooglen@google.com \
    --cc=avarab@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=periperidip@gmail.com \
    --cc=raykar.ath@gmail.com \
    --cc=steadmon@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.