From patchwork Tue Mar 1 00:08:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12763924 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B5BEC433F5 for ; Tue, 1 Mar 2022 00:08:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231189AbiCAAJW (ORCPT ); Mon, 28 Feb 2022 19:09:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231254AbiCAAJR (ORCPT ); Mon, 28 Feb 2022 19:09:17 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E172B9BB84 for ; Mon, 28 Feb 2022 16:08:33 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id z10-20020a634c0a000000b0036c5eb39076so7421121pga.18 for ; Mon, 28 Feb 2022 16:08:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=t0gS2GJJJ+/nEbuiqRGr4cv3+fBqX6X1h/kU524+UoY=; b=rOu7Rj3cXK+0HH8xZEvtI1Kl/OjqaAcfAznzTYAIZ2YWQUubFRmNxWDweRDq5OEbvb UFJcZtViLLS5MoN9DFNnUWLuMkCFB4gktqk7oKOVrpo0Cds35F1FFEiYArY2vbGR7JW2 bU3dxik72Ij+S9l0vsp6TD6l8ehB/a9xNFtin6zkukiuQtj2Zz5Gyim6R92EHLZXM+SI +TL9oNyHrhkeSHcEvpFypPPOaf4f7Le1bdjg+N2CuRz+RVDzRCAaWFMEgdpHOOHXNO5T vE/BImmunAS3gg9JuEjoKQjXUR2kPDoq6GWyYa7V++gyasgIARBStW0+wZTYo82yPyVl 8qWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=t0gS2GJJJ+/nEbuiqRGr4cv3+fBqX6X1h/kU524+UoY=; b=psXfy2sTNC7E1M+rtzOV22gQPfZyBpE6gyFfZvCZYkemvKB0XNQXUF+H4IZCE/Z86y tSCUTW5xk9AGRzRYlkS1Akq1iupeC2M5kBFVHfMWRwl8Vrrp6Na8rSGPl1JXErs+mxdS JpNc4MQ3Uw5VONir+T82Q+wgyJpYqhH2o2qmW/RubCkiocVb+NRn00t+kdzeMBHh6aJh EDKab1MOCf3DtpS1b2KgrVNiJbRckADcVLPEqZzesIghm+m6hLVWMe1G0QZpodUPTxpQ KHBA4JQQ4Y3+sS5q8Pci5HkEHW9W4epXhhzpspz+zKd6tRmt0BpR5iQEzM+JYJbC1xzA 9JTg== X-Gm-Message-State: AOAM532F0SP8dMDvzlCw1TwkfDXsZSlRw1tDI1yeiIWp1MMgZire/fF7 LXN+ijgL+u/PkiC5/saz5ekICHbhSGgEDy8ACffd66gZBBNtv60AEVtjnXk9RXMT4FMRWG5hkaT wCRHNSSTZ2SV6UouhcbqqUZBZAGyoDeh0l3l7GlwOPFknQO6jfNZA279wkmlB3Ac= X-Google-Smtp-Source: ABdhPJxoAoIRycQIxdCFIwBD1Lmpkm6QFyniqVApjHS6ijwfX1m8K8GID8hmYLwcb40uhQ7ekOQxscVU4CYukg== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:90b:c8e:b0:1bc:3481:b2b7 with SMTP id o14-20020a17090b0c8e00b001bc3481b2b7mr19166558pjz.187.1646093312916; Mon, 28 Feb 2022 16:08:32 -0800 (PST) Date: Mon, 28 Feb 2022 16:08:09 -0800 In-Reply-To: <20220301000816.56177-1-chooglen@google.com> Message-Id: <20220301000816.56177-7-chooglen@google.com> Mime-Version: 1.0 References: <20220301000816.56177-1-chooglen@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH 06/13] submodule--helper: get remote names from any repository From: Glen Choo To: git@vger.kernel.org Cc: Glen Choo , Junio C Hamano , Atharva Raykar , Emily Shaffer , " =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7AgQmphcm1hc29u?= " , Josh Steadmon , Christian Couder , Shourya Shukla Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Atharva Raykar `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 the next patch, which will require this functionality. Mentored-by: Christian Couder Mentored-by: Shourya Shukla Helped-by: Glen Choo Signed-off-by: Atharva Raykar Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano Signed-off-by: Glen Choo --- builtin/submodule--helper.c | 39 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 6b473fc0d2..f934e33c7e 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -31,11 +31,14 @@ 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); + int ignore_errno; + const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL, + NULL, &ignore_errno); if (!refname) die(_("No such ref: %s"), "HEAD"); @@ -48,7 +51,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 +60,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 +1359,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 +1399,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");