From patchwork Tue Jun 15 14:57:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atharva Raykar X-Patchwork-Id: 12322093 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65E3EC48BE5 for ; Tue, 15 Jun 2021 14:58:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40C1A61580 for ; Tue, 15 Jun 2021 14:58:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231495AbhFOPAN (ORCPT ); Tue, 15 Jun 2021 11:00:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231250AbhFOPAN (ORCPT ); Tue, 15 Jun 2021 11:00:13 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C104C061574 for ; Tue, 15 Jun 2021 07:58:09 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id s17-20020a17090a8811b029016e89654f93so1989185pjn.1 for ; Tue, 15 Jun 2021 07:58:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4+1ty/JKR461TmzuKCw11kCdrLmEaKGs01YA6uEwmxM=; b=c1lAbpjIVmNw257Mt5MmuqRSe6Wx+cwt0fVM4cOZch2ECPhbxPySbWyrbEYcpeVul7 JZ4R434VPU1zsMSwbAef5DL72ZoHkwfDe39HiP+AoHerngcPonGOWOIff+AOg+W1usmV OBLH9ByHy3dnE7tYPId8faIQHCsLt28MxL+fyhKbmWMhxdpGkkaXKoo/YWCmRgHJXyDg sQXoyREzN8kOq7ihha1T6GbehtG23tiGS+MT/q7ehnvAL7jPZW3VvEcmZTb1mr7FE1n1 RHRn5i0Smvp9pi5dNbHlRFwMYUvKQa0AxwE3LQxP1XPq3MjDqyV0bMMeUckh7chIWW6f FnEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4+1ty/JKR461TmzuKCw11kCdrLmEaKGs01YA6uEwmxM=; b=L1b5G9/um7OtyfSu3aK6zst98UEGOOj2ZHdefo4nZ27XbjathUfCKVJ8Ejqur4hE+4 wpI8UCUvN6ALItkHk2j141SDPHkSejnw9BYwgOykmHNnhqRb7C6nlP7A+uVTtaoWvqYe 1LCkGmiO/KFOzSldlrcxnER42mOnqD1CcyP2LgakNEZXQsm+ij7w4Zk//r0WkKc2Yyfz ddGD+VPR3HazeCdf/WC6S8XHQdxTl8clxW19BgJvNRunkaYaKvNLZuZOFDM29PYBMb2+ xjlemS1fhgMJ0pMLgkJv+BquKe3KnEyYuMZ5EOAQphpEhjHtpZgg2PnaXOO/XNoJcZYY 9Dag== X-Gm-Message-State: AOAM532r1/klyE+IQN40UWL+8Ca7ryfeht/rOvX6xFwTG0gmEU98yW4f AcGlzvsshHScGxdkpFLXLw/XsVlUQvPAze0bFxg= X-Google-Smtp-Source: ABdhPJz96CcEUs+3MuyrIQxrDgHW3knAXW64/7+sua1tqRoJv488HwWV82XK6QEpdvKIsO0xNp0U3Q== X-Received: by 2002:a17:902:ced1:b029:115:3e22:1eb4 with SMTP id d17-20020a170902ced1b02901153e221eb4mr4264672plg.19.1623769088400; Tue, 15 Jun 2021 07:58:08 -0700 (PDT) Received: from atharva-on-air.Dlink ([119.82.107.60]) by smtp.gmail.com with ESMTPSA id u1sm16340120pgh.80.2021.06.15.07.58.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Jun 2021 07:58:07 -0700 (PDT) From: Atharva Raykar To: git@vger.kernel.org Cc: Atharva Raykar , Christian Couder , Shourya Shukla , Junio C Hamano Subject: [PATCH v6 1/3] submodule--helper: refactor module_clone() Date: Tue, 15 Jun 2021 20:27:43 +0530 Message-Id: <20210615145745.33382-2-raykar.ath@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210615145745.33382-1-raykar.ath@gmail.com> References: <20210615093810.27930-1-raykar.ath@gmail.com> <20210615145745.33382-1-raykar.ath@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Separate out the core logic of module_clone() from the flag parsing---this way we can call the equivalent of the `submodule--helper clone` subcommand directly within C, without needing to push arguments in a strvec. Signed-off-by: Atharva Raykar Mentored-by: Christian Couder Mentored-by: Shourya Shukla Suggested-by: Junio C Hamano --- builtin/submodule--helper.c | 241 +++++++++++++++++++----------------- 1 file changed, 128 insertions(+), 113 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index d55f6262e9..ae246a35f9 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1658,45 +1658,20 @@ static int module_deinit(int argc, const char **argv, const char *prefix) return 0; } -static int clone_submodule(const char *path, const char *gitdir, const char *url, - const char *depth, struct string_list *reference, int dissociate, - int quiet, int progress, int single_branch) -{ - struct child_process cp = CHILD_PROCESS_INIT; - - strvec_push(&cp.args, "clone"); - strvec_push(&cp.args, "--no-checkout"); - if (quiet) - strvec_push(&cp.args, "--quiet"); - if (progress) - strvec_push(&cp.args, "--progress"); - if (depth && *depth) - strvec_pushl(&cp.args, "--depth", depth, NULL); - if (reference->nr) { - struct string_list_item *item; - for_each_string_list_item(item, reference) - strvec_pushl(&cp.args, "--reference", - item->string, NULL); - } - if (dissociate) - strvec_push(&cp.args, "--dissociate"); - if (gitdir && *gitdir) - strvec_pushl(&cp.args, "--separate-git-dir", gitdir, NULL); - if (single_branch >= 0) - strvec_push(&cp.args, single_branch ? - "--single-branch" : - "--no-single-branch"); - - strvec_push(&cp.args, "--"); - strvec_push(&cp.args, url); - strvec_push(&cp.args, path); - - cp.git_cmd = 1; - prepare_submodule_repo_env(&cp.env_array); - cp.no_stdin = 1; - - return run_command(&cp); -} +struct module_clone_data { + const char *prefix; + const char *path; + const char *name; + const char *url; + const char *depth; + struct string_list reference; + unsigned int quiet: 1; + unsigned int progress: 1; + unsigned int dissociate: 1; + unsigned int require_init: 1; + int single_branch; +}; +#define MODULE_CLONE_DATA_INIT { .reference = STRING_LIST_INIT_NODUP, .single_branch = -1 } struct submodule_alternate_setup { const char *submodule_name; @@ -1802,37 +1777,128 @@ static void prepare_possible_alternates(const char *sm_name, free(error_strategy); } +static int clone_submodule(struct module_clone_data *clone_data) +{ + char *p, *sm_gitdir; + char *sm_alternate = NULL, *error_strategy = NULL; + struct strbuf sb = STRBUF_INIT; + struct child_process cp = CHILD_PROCESS_INIT; + + strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), clone_data->name); + sm_gitdir = absolute_pathdup(sb.buf); + strbuf_reset(&sb); + + if (!is_absolute_path(clone_data->path)) { + strbuf_addf(&sb, "%s/%s", get_git_work_tree(), clone_data->path); + clone_data->path = strbuf_detach(&sb, NULL); + } else { + clone_data->path = xstrdup(clone_data->path); + } + + if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) + die(_("refusing to create/use '%s' in another submodule's " + "git dir"), sm_gitdir); + + if (!file_exists(sm_gitdir)) { + if (safe_create_leading_directories_const(sm_gitdir) < 0) + die(_("could not create directory '%s'"), sm_gitdir); + + prepare_possible_alternates(clone_data->name, &clone_data->reference); + + strvec_push(&cp.args, "clone"); + strvec_push(&cp.args, "--no-checkout"); + if (clone_data->quiet) + strvec_push(&cp.args, "--quiet"); + if (clone_data->progress) + strvec_push(&cp.args, "--progress"); + if (clone_data->depth && *(clone_data->depth)) + strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL); + if (clone_data->reference.nr) { + struct string_list_item *item; + for_each_string_list_item(item, &clone_data->reference) + strvec_pushl(&cp.args, "--reference", + item->string, NULL); + } + if (clone_data->dissociate) + strvec_push(&cp.args, "--dissociate"); + if (sm_gitdir && *sm_gitdir) + strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL); + if (clone_data->single_branch >= 0) + strvec_push(&cp.args, clone_data->single_branch ? + "--single-branch" : + "--no-single-branch"); + + strvec_push(&cp.args, "--"); + strvec_push(&cp.args, clone_data->url); + strvec_push(&cp.args, clone_data->path); + + cp.git_cmd = 1; + prepare_submodule_repo_env(&cp.env_array); + cp.no_stdin = 1; + + if(run_command(&cp)) + die(_("clone of '%s' into submodule path '%s' failed"), + clone_data->url, clone_data->path); + } else { + if (clone_data->require_init && !access(clone_data->path, X_OK) && + !is_empty_dir(clone_data->path)) + die(_("directory not empty: '%s'"), clone_data->path); + if (safe_create_leading_directories_const(clone_data->path) < 0) + die(_("could not create directory '%s'"), clone_data->path); + strbuf_addf(&sb, "%s/index", sm_gitdir); + unlink_or_warn(sb.buf); + strbuf_reset(&sb); + } + + connect_work_tree_and_git_dir(clone_data->path, sm_gitdir, 0); + + p = git_pathdup_submodule(clone_data->path, "config"); + if (!p) + die(_("could not get submodule directory for '%s'"), clone_data->path); + + /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */ + git_config_get_string("submodule.alternateLocation", &sm_alternate); + if (sm_alternate) + git_config_set_in_file(p, "submodule.alternateLocation", + sm_alternate); + git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); + if (error_strategy) + git_config_set_in_file(p, "submodule.alternateErrorStrategy", + error_strategy); + + free(sm_alternate); + free(error_strategy); + + strbuf_release(&sb); + free(sm_gitdir); + free(p); + return 0; +} + static int module_clone(int argc, const char **argv, const char *prefix) { - const char *name = NULL, *url = NULL, *depth = NULL; - int quiet = 0; - int progress = 0; - char *p, *path = NULL, *sm_gitdir; - struct strbuf sb = STRBUF_INIT; - struct string_list reference = STRING_LIST_INIT_NODUP; - int dissociate = 0, require_init = 0; - char *sm_alternate = NULL, *error_strategy = NULL; - int single_branch = -1; + int dissociate = 0, quiet = 0, progress = 0, require_init = 0; + struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; struct option module_clone_options[] = { - OPT_STRING(0, "prefix", &prefix, + OPT_STRING(0, "prefix", &clone_data.prefix, N_("path"), N_("alternative anchor for relative paths")), - OPT_STRING(0, "path", &path, + OPT_STRING(0, "path", &clone_data.path, N_("path"), N_("where the new submodule will be cloned to")), - OPT_STRING(0, "name", &name, + OPT_STRING(0, "name", &clone_data.name, N_("string"), N_("name of the new submodule")), - OPT_STRING(0, "url", &url, + OPT_STRING(0, "url", &clone_data.url, N_("string"), N_("url where to clone the submodule from")), - OPT_STRING_LIST(0, "reference", &reference, + OPT_STRING_LIST(0, "reference", &clone_data.reference, N_("repo"), N_("reference repository")), OPT_BOOL(0, "dissociate", &dissociate, N_("use --reference only while cloning")), - OPT_STRING(0, "depth", &depth, + OPT_STRING(0, "depth", &clone_data.depth, N_("string"), N_("depth for shallow clones")), OPT__QUIET(&quiet, "Suppress output for cloning a submodule"), @@ -1840,7 +1906,7 @@ static int module_clone(int argc, const char **argv, const char *prefix) N_("force cloning progress")), OPT_BOOL(0, "require-init", &require_init, N_("disallow cloning into non-empty directory")), - OPT_BOOL(0, "single-branch", &single_branch, + OPT_BOOL(0, "single-branch", &clone_data.single_branch, N_("clone only one branch, HEAD or --branch")), OPT_END() }; @@ -1856,67 +1922,16 @@ static int module_clone(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, prefix, module_clone_options, git_submodule_helper_usage, 0); - if (argc || !url || !path || !*path) + clone_data.dissociate = !!dissociate; + clone_data.quiet = !!quiet; + clone_data.progress = !!progress; + clone_data.require_init = !!require_init; + + if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path)) usage_with_options(git_submodule_helper_usage, module_clone_options); - strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name); - sm_gitdir = absolute_pathdup(sb.buf); - strbuf_reset(&sb); - - if (!is_absolute_path(path)) { - strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path); - path = strbuf_detach(&sb, NULL); - } else - path = xstrdup(path); - - if (validate_submodule_git_dir(sm_gitdir, name) < 0) - die(_("refusing to create/use '%s' in another submodule's " - "git dir"), sm_gitdir); - - if (!file_exists(sm_gitdir)) { - if (safe_create_leading_directories_const(sm_gitdir) < 0) - die(_("could not create directory '%s'"), sm_gitdir); - - prepare_possible_alternates(name, &reference); - - if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate, - quiet, progress, single_branch)) - die(_("clone of '%s' into submodule path '%s' failed"), - url, path); - } else { - if (require_init && !access(path, X_OK) && !is_empty_dir(path)) - die(_("directory not empty: '%s'"), path); - if (safe_create_leading_directories_const(path) < 0) - die(_("could not create directory '%s'"), path); - strbuf_addf(&sb, "%s/index", sm_gitdir); - unlink_or_warn(sb.buf); - strbuf_reset(&sb); - } - - connect_work_tree_and_git_dir(path, sm_gitdir, 0); - - p = git_pathdup_submodule(path, "config"); - if (!p) - die(_("could not get submodule directory for '%s'"), path); - - /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */ - git_config_get_string("submodule.alternateLocation", &sm_alternate); - if (sm_alternate) - git_config_set_in_file(p, "submodule.alternateLocation", - sm_alternate); - git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); - if (error_strategy) - git_config_set_in_file(p, "submodule.alternateErrorStrategy", - error_strategy); - - free(sm_alternate); - free(error_strategy); - - strbuf_release(&sb); - free(sm_gitdir); - free(path); - free(p); + clone_submodule(&clone_data); return 0; } From patchwork Tue Jun 15 14:57:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Atharva Raykar X-Patchwork-Id: 12322095 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 300C5C48BDF for ; Tue, 15 Jun 2021 14:58:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 197616145D for ; Tue, 15 Jun 2021 14:58:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231520AbhFOPAW (ORCPT ); Tue, 15 Jun 2021 11:00:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230462AbhFOPAW (ORCPT ); Tue, 15 Jun 2021 11:00:22 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D331CC061574 for ; Tue, 15 Jun 2021 07:58:17 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id z26so13456039pfj.5 for ; Tue, 15 Jun 2021 07:58:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OkgSEeH9ULVrrmSUGnpLpQj8nOgggoVDNaLNK/5lGBA=; b=pCXh6qSEsV4afVGKotP1Z3iNpn2AB158aBX15Ei+9ruOho8edNfrC8mgiI6ooARSGX qq0jNRsomNGFwHY/9VZrxall3ypgD8ajWAhB0BtCL4z+VG3GK1yv1eVQ37m2lqcnN2OP F3XaEVoXcEjQVv3KYkvavtjRGfse/CkCjTr3XXePO1dfktcNIj6p25Zi64YWl64YKooL ABufN84N68fKH+94PwebrZ3+uewpnsJNEdMusM6l1rHapnOtQC5ZAPyo8nn5aMxI/srO Q3Vg+o3nw3JCg5JKnAUUAdtF+BO7V92gKb0UCx4N81OnVKVOuT/xHp6K15g7eoxMjsDF OFcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OkgSEeH9ULVrrmSUGnpLpQj8nOgggoVDNaLNK/5lGBA=; b=nRUYKrCBsizS+typ+Qyiysaf2a1zv03HwNavqeZBqw3e2wn1K7wS88L+MSbq5THbUu GsB4ylfUzL2HjTiTLG+421KAWCaTVnKeFqMEej04xns5s+kuZeH9zDaMXGBrTnG5nGLl NLvlX9nQdox3INVokKG3WFSz3NI/RQ5d4KUcbg6h1KGDWrvaLusNhMzpa1kWUctIrrKh vuPyam19sftkRlBwxz9R2h0ikH037gt9yhqjE0DsjwHMEDjdYBOkQlo89SJ5PbciEk/+ UV4J1/DntKee7DAxuw14MHbOFvdHNZxfTtny++YKoGGNa17gdOQDcbS2OVT5G2YS0veW DXiA== X-Gm-Message-State: AOAM5332+e0+TV3+Seb7LBmfdN9D2ReHCqZSeU1cMa0Hvebzn80XPaa0 Ybgkjf5mcRglfKd4Au55DWeYus6QY0Z/8801kcg= X-Google-Smtp-Source: ABdhPJzOzMgCPhkD1fc5F/ccY+PmH4iR89IYdGfJr8yWh84+SGX8Q4zVsikHb1vadW1iIa0C9l840w== X-Received: by 2002:a05:6a00:1794:b029:2ee:c59c:56bf with SMTP id s20-20020a056a001794b02902eec59c56bfmr4798887pfg.2.1623769096969; Tue, 15 Jun 2021 07:58:16 -0700 (PDT) Received: from atharva-on-air.Dlink ([119.82.107.60]) by smtp.gmail.com with ESMTPSA id u1sm16340120pgh.80.2021.06.15.07.58.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Jun 2021 07:58:16 -0700 (PDT) From: Atharva Raykar To: git@vger.kernel.org Cc: Atharva Raykar , Christian Couder , Shourya Shukla , Prathamesh Chavan , =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7Ru?= =?utf-8?b?ZyBEYW5o?= Subject: [PATCH v6 2/3] submodule--helper: introduce add-clone subcommand Date: Tue, 15 Jun 2021 20:27:44 +0530 Message-Id: <20210615145745.33382-3-raykar.ath@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210615145745.33382-1-raykar.ath@gmail.com> References: <20210615093810.27930-1-raykar.ath@gmail.com> <20210615145745.33382-1-raykar.ath@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Let's add a new "add-clone" subcommand to `git submodule--helper` with the goal of converting part of the shell code in git-submodule.sh related to `git submodule add` into C code. This new subcommand clones the repository that is to be added, and checks out to the appropriate branch. This is meant to be a faithful conversion that leaves the behaviour of 'submodule add' unchanged. The only minor change is that if a submodule name has been supplied with a name that clashes with a local submodule, the message shown to the user ("A git directory for 'foo' is found locally...") is prepended with "error" for clarity. This is part of a series of changes that will result in all of 'submodule add' being converted to C. Signed-off-by: Atharva Raykar Mentored-by: Christian Couder Mentored-by: Shourya Shukla Based-on-patch-by: Shourya Shukla Based-on-patch-by: Prathamesh Chavan Helped-by: Đoàn Trần Công Danh --- builtin/submodule--helper.c | 177 ++++++++++++++++++++++++++++++++++++ git-submodule.sh | 38 +------- 2 files changed, 178 insertions(+), 37 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index ae246a35f9..6d52a73a57 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2760,6 +2760,182 @@ static int module_set_branch(int argc, const char **argv, const char *prefix) return !!ret; } +struct add_data { + const char *prefix; + const char *branch; + const char *reference_path; + const char *sm_path; + const char *sm_name; + const char *repo; + const char *realrepo; + int depth; + unsigned int force: 1; + unsigned int quiet: 1; + unsigned int progress: 1; + unsigned int dissociate: 1; +}; +#define ADD_DATA_INIT { .depth = -1 } + +static void show_fetch_remotes(FILE *output, const char *sm_name, const char *git_dir_path) +{ + struct child_process cp_remote = CHILD_PROCESS_INIT; + struct strbuf sb_remote_out = STRBUF_INIT; + + cp_remote.git_cmd = 1; + strvec_pushf(&cp_remote.env_array, + "GIT_DIR=%s", git_dir_path); + strvec_push(&cp_remote.env_array, "GIT_WORK_TREE=."); + strvec_pushl(&cp_remote.args, "remote", "-v", NULL); + if (!capture_command(&cp_remote, &sb_remote_out, 0)) { + char *next_line; + char *line = sb_remote_out.buf; + while ((next_line = strchr(line, '\n')) != NULL) { + size_t len = next_line - line; + if (strip_suffix_mem(line, &len, " (fetch)")) + fprintf(output, " %.*s\n", (int)len, line); + line = next_line + 1; + } + } + + strbuf_release(&sb_remote_out); +} + +static int add_submodule(const struct add_data *add_data) +{ + char *submod_gitdir_path; + struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; + + /* perhaps the path already exists and is already a git repo, else clone it */ + if (is_directory(add_data->sm_path)) { + struct strbuf sm_path = STRBUF_INIT; + strbuf_addstr(&sm_path, add_data->sm_path); + submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path); + if (is_nonbare_repository_dir(&sm_path)) + printf(_("Adding existing repo at '%s' to the index\n"), + add_data->sm_path); + else + die(_("'%s' already exists and is not a valid git repo"), + add_data->sm_path); + strbuf_release(&sm_path); + free(submod_gitdir_path); + } else { + struct child_process cp = CHILD_PROCESS_INIT; + submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name); + + if (is_directory(submod_gitdir_path)) { + if (!add_data->force) { + fprintf(stderr, _("A git directory for '%s' is found " + "locally with remote(s):"), + add_data->sm_name); + show_fetch_remotes(stderr, add_data->sm_name, + submod_gitdir_path); + free(submod_gitdir_path); + die(_("If you want to reuse this local git " + "directory instead of cloning again from\n" + " %s\n" + "use the '--force' option. If the local git " + "directory is not the correct repo\n" + "or if you are unsure what this means, choose " + "another name with the '--name' option.\n"), + add_data->realrepo); + } else { + printf(_("Reactivating local git directory for " + "submodule '%s'\n"), add_data->sm_name); + } + } + free(submod_gitdir_path); + + clone_data.prefix = add_data->prefix; + clone_data.path = add_data->sm_path; + clone_data.name = add_data->sm_name; + clone_data.url = add_data->realrepo; + clone_data.quiet = add_data->quiet; + clone_data.progress = add_data->progress; + if (add_data->reference_path) + string_list_append(&clone_data.reference, + xstrdup(add_data->reference_path)); + clone_data.dissociate = add_data->dissociate; + if (add_data->depth >= 0) + clone_data.depth = xstrfmt("%d", add_data->depth); + + if (clone_submodule(&clone_data)) + return -1; + + prepare_submodule_repo_env(&cp.env_array); + cp.git_cmd = 1; + cp.dir = add_data->sm_path; + strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL); + + if (add_data->branch) { + strvec_pushl(&cp.args, "-B", add_data->branch, NULL); + strvec_pushf(&cp.args, "origin/%s", add_data->branch); + } + + if (run_command(&cp)) + die(_("unable to checkout submodule '%s'"), add_data->sm_path); + } + return 0; +} + +static int add_clone(int argc, const char **argv, const char *prefix) +{ + int force = 0, quiet = 0, dissociate = 0, progress = 0; + struct add_data add_data = ADD_DATA_INIT; + + struct option options[] = { + OPT_STRING('b', "branch", &add_data.branch, + N_("branch"), + N_("branch of repository to checkout on cloning")), + OPT_STRING(0, "prefix", &prefix, + N_("path"), + N_("alternative anchor for relative paths")), + OPT_STRING(0, "path", &add_data.sm_path, + N_("path"), + N_("where the new submodule will be cloned to")), + OPT_STRING(0, "name", &add_data.sm_name, + N_("string"), + N_("name of the new submodule")), + OPT_STRING(0, "url", &add_data.realrepo, + N_("string"), + N_("url where to clone the submodule from")), + OPT_STRING(0, "reference", &add_data.reference_path, + N_("repo"), + N_("reference repository")), + OPT_BOOL(0, "dissociate", &dissociate, + N_("use --reference only while cloning")), + OPT_INTEGER(0, "depth", &add_data.depth, + N_("depth for shallow clones")), + OPT_BOOL(0, "progress", &progress, + N_("force cloning progress")), + OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"), + PARSE_OPT_NOCOMPLETE), + OPT__QUIET(&quiet, "suppress output for cloning a submodule"), + OPT_END() + }; + + const char *const usage[] = { + N_("git submodule--helper add-clone [...] " + "--url --path --name "), + NULL + }; + + argc = parse_options(argc, argv, prefix, options, usage, 0); + + if (argc != 0) + usage_with_options(usage, options); + + add_data.prefix = prefix; + add_data.progress = !!progress; + add_data.dissociate = !!dissociate; + add_data.force = !!force; + add_data.quiet = !!quiet; + + if (add_submodule(&add_data)) + return 1; + + return 0; +} + #define SUPPORT_SUPER_PREFIX (1<<0) struct cmd_struct { @@ -2772,6 +2948,7 @@ static struct cmd_struct commands[] = { {"list", module_list, 0}, {"name", module_name, 0}, {"clone", module_clone, 0}, + {"add-clone", add_clone, 0}, {"update-module-mode", module_update_module_mode, 0}, {"update-clone", update_clone, 0}, {"ensure-core-worktree", ensure_core_worktree, 0}, diff --git a/git-submodule.sh b/git-submodule.sh index 4678378424..f71e1e5495 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -241,43 +241,7 @@ cmd_add() die "$(eval_gettext "'$sm_name' is not a valid submodule name")" fi - # perhaps the path exists and is already a git repo, else clone it - if test -e "$sm_path" - then - if test -d "$sm_path"/.git || test -f "$sm_path"/.git - then - eval_gettextln "Adding existing repo at '\$sm_path' to the index" - else - die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")" - fi - - else - if test -d ".git/modules/$sm_name" - then - if test -z "$force" - then - eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):" - GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2 - die "$(eval_gettextln "\ -If you want to reuse this local git directory instead of cloning again from - \$realrepo -use the '--force' option. If the local git directory is not the correct repo -or you are unsure what this means choose another name with the '--name' option.")" - else - eval_gettextln "Reactivating local git directory for submodule '\$sm_name'." - fi - fi - git submodule--helper clone ${GIT_QUIET:+--quiet} ${progress:+"--progress"} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${dissociate:+"--dissociate"} ${depth:+"$depth"} || exit - ( - sanitize_submodule_env - cd "$sm_path" && - # ash fails to wordsplit ${branch:+-b "$branch"...} - case "$branch" in - '') git checkout -f -q ;; - ?*) git checkout -f -q -B "$branch" "origin/$branch" ;; - esac - ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")" - fi + git submodule--helper add-clone ${GIT_QUIET:+--quiet} ${force:+"--force"} ${progress:+"--progress"} ${branch:+--branch "$branch"} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${dissociate:+"--dissociate"} ${depth:+"$depth"} || exit git config submodule."$sm_name".url "$realrepo" git add --no-warn-embedded-repo $force "$sm_path" || From patchwork Tue Jun 15 14:57:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atharva Raykar X-Patchwork-Id: 12322097 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA6E4C48BE5 for ; Tue, 15 Jun 2021 14:58:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B42CF6145D for ; Tue, 15 Jun 2021 14:58:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231309AbhFOPA2 (ORCPT ); Tue, 15 Jun 2021 11:00:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231522AbhFOPA1 (ORCPT ); Tue, 15 Jun 2021 11:00:27 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF559C06175F for ; Tue, 15 Jun 2021 07:58:22 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id h1so8611568plt.1 for ; Tue, 15 Jun 2021 07:58:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=im+sA4IbBl6ueIrbDtu6Z4cceKK7PM6NGSb1lMHZs4Q=; b=nwKiewnkA9Sa/08ax4hjLznlpUDWtFLH5b7Iuu+NjEUEPUybZDThpwZntgQDUyYPOm AnCopMWUXADaNKvT9yHhBnJ42frhy0InMCNfazyYbVNAiZzfXgOzNkx0CHb/Ol9sqW1x fhlDqUewJvqXz5mm9qua9PkKSyDHEw5FfStZ4xsXdp4JzlWjgihhslVCiufB+Dx43JHM SjGOppz9NXi4+8kF9VWvxGLy4XMvnvRSuDy/wzAj9xSLd8k9N2E6AZRiJ4NCYWjH7Z0l FZRxkjU3oZbn0zVQZgoqHVUYIix9xgFjiyFY8RQzK2vVnPR1CjbGYVIkObLpCuBueAce I1lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=im+sA4IbBl6ueIrbDtu6Z4cceKK7PM6NGSb1lMHZs4Q=; b=no8ERgaAsJOYPKbXsGZHtv2wRPVI+j/yJdYnNQpy/C0wfHjxMQuRlRwsuu4VPeA3zo YL/JF6WwZ0sfOF8TBtPJTBc6ANHHNoN2G8R/GnUbcLvuOomsiy14NO7zdLO9HhdRzxhI U4otTTpWypSZJIRCmJoNQaEcnzGOCUY+n84kWDAnj/hlCgFPN2QET12S8neTx/Kzm5XF EkToKfC34dDEfGEOWUxkvKHfu4oxT+G7X9IeaOnytaXajl0axU8AtFfJeu+aL1zkHSLw KSNcBpmfF4HvqP8oYj5+UvBmoL3ZdjKtuM3KnCO636x9iPTyVA5LGixlbe7T3SwTA5X2 ByFA== X-Gm-Message-State: AOAM533Kim3ZKFE1p/zCZ60Tr0JIlu1nU/lpUO/DEKuPhG93Or5bqoRm jaVXtUUTvy6OBkIVpeVMsb/LVRYEeK0oihpptg4= X-Google-Smtp-Source: ABdhPJzpr1qWo4P2vF0TVx06okwCMLL4AKKdrEEFi50vz4qjGS7tva6Ym6MZWH3MMcegaPadxJZoig== X-Received: by 2002:a17:903:2c3:b029:11b:cbb9:edfb with SMTP id s3-20020a17090302c3b029011bcbb9edfbmr4651291plk.12.1623769102214; Tue, 15 Jun 2021 07:58:22 -0700 (PDT) Received: from atharva-on-air.Dlink ([119.82.107.60]) by smtp.gmail.com with ESMTPSA id u1sm16340120pgh.80.2021.06.15.07.58.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Jun 2021 07:58:21 -0700 (PDT) From: Atharva Raykar To: git@vger.kernel.org Cc: Atharva Raykar , Christian Couder , Shourya Shukla , Prathamesh Chavan Subject: [PATCH v6 3/3] submodule--helper: introduce add-config subcommand Date: Tue, 15 Jun 2021 20:27:45 +0530 Message-Id: <20210615145745.33382-4-raykar.ath@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210615145745.33382-1-raykar.ath@gmail.com> References: <20210615093810.27930-1-raykar.ath@gmail.com> <20210615145745.33382-1-raykar.ath@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a new "add-config" subcommand to `git submodule--helper` with the goal of converting part of the shell code in git-submodule.sh related to `git submodule add` into C code. This new subcommand sets the configuration variables of a newly added submodule, by registering the url in local git config, as well as the submodule name and path in the .gitmodules file. It also sets 'submodule..active' to "true" if the submodule path has not already been covered by any pathspec specified in 'submodule.active'. This is meant to be a faithful conversion from shell to C, with only one minor change: A warning is emitted if no value is specified in 'submodule.active', ie, the config looks like: "[submodule] active\n", because it is an invalid configuration. It would be helpful to let the user know that the pathspec is unset, and the value of 'submodule..active' might be set to 'true' so that they can rectify their configuration and prevent future surprises (especially given that the latter variable has a higher priority than the former). The structure of the conditional to check if we need to set the 'active' toggle looks different from the shell version -- but behaves the same. The change was made to decrease code duplication. A comment has been added to explain that only one value of 'submodule.active' is obtained to check if we need to call is_submodule_active() at all. This is part of a series of changes that will result in all of 'submodule add' being converted to C. Signed-off-by: Atharva Raykar Mentored-by: Christian Couder Mentored-by: Shourya Shukla Based-on-patch-by: Shourya Shukla Based-on-patch-by: Prathamesh Chavan --- builtin/submodule--helper.c | 125 ++++++++++++++++++++++++++++++++++++ git-submodule.sh | 28 +------- 2 files changed, 126 insertions(+), 27 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 6d52a73a57..bce1e06d74 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2936,6 +2936,130 @@ static int add_clone(int argc, const char **argv, const char *prefix) return 0; } +static void config_submodule_in_gitmodules(const char *name, const char *var, const char *value) +{ + char *key; + + if (!is_writing_gitmodules_ok()) + die(_("please make sure that the .gitmodules file is in the working tree")); + + key = xstrfmt("submodule.%s.%s", name, var); + config_set_in_gitmodules_file_gently(key, value); + free(key); +} + +static void configure_added_submodule(struct add_data *add_data) +{ + char *key, *submod_pathspec = NULL; + struct child_process add_submod = CHILD_PROCESS_INIT; + struct child_process add_gitmodules = CHILD_PROCESS_INIT; + int pathspec_key_exists, activate = 0; + + key = xstrfmt("submodule.%s.url", add_data->sm_name); + git_config_set_gently(key, add_data->realrepo); + free(key); + + add_submod.git_cmd = 1; + strvec_pushl(&add_submod.args, "add", + "--no-warn-embedded-repo", NULL); + if (add_data->force) + strvec_push(&add_submod.args, "--force"); + strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL); + + if (run_command(&add_submod)) + die(_("Failed to add submodule '%s'"), add_data->sm_path); + + config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path); + config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo); + if (add_data->branch) + config_submodule_in_gitmodules(add_data->sm_name, + "branch", add_data->branch); + + add_gitmodules.git_cmd = 1; + strvec_pushl(&add_gitmodules.args, + "add", "--force", "--", ".gitmodules", NULL); + + if (run_command(&add_gitmodules)) + die(_("Failed to register submodule '%s'"), add_data->sm_path); + + /* + * NEEDSWORK: In a multi-working-tree world this needs to be + * set in the per-worktree config. + */ + pathspec_key_exists = !git_config_get_string("submodule.active", + &submod_pathspec); + if (pathspec_key_exists && !submod_pathspec) { + warning(_("The submodule.active configuration exists, but the " + "pathspec was unset. If the submodule is not already " + "active, the value of submodule.%s.active will be " + "be set to 'true'."), add_data->sm_name); + activate = 1; + } + + /* + * If submodule.active does not exist, or if the pathspec was unset, + * we will activate this module unconditionally. + * + * Otherwise, we ask is_submodule_active(), which iterates + * through all the values of 'submodule.active' to determine + * if this module is already active. + */ + if (!pathspec_key_exists || activate || + !is_submodule_active(the_repository, add_data->sm_path)) { + key = xstrfmt("submodule.%s.active", add_data->sm_name); + git_config_set_gently(key, "true"); + free(key); + } +} + +static int add_config(int argc, const char **argv, const char *prefix) +{ + int force = 0; + struct add_data add_data = ADD_DATA_INIT; + + struct option options[] = { + OPT_STRING('b', "branch", &add_data.branch, + N_("branch"), + N_("branch of repository to store in " + "the submodule configuration")), + OPT_STRING(0, "url", &add_data.repo, + N_("string"), + N_("url to clone submodule from")), + OPT_STRING(0, "resolved-url", &add_data.realrepo, + N_("string"), + N_("url to clone the submodule from, after it has " + "been dereferenced relative to parent's url, " + "in the case where is a relative url")), + OPT_STRING(0, "path", &add_data.sm_path, + N_("path"), + N_("where the new submodule will be cloned to")), + OPT_STRING(0, "name", &add_data.sm_name, + N_("string"), + N_("name of the new submodule")), + OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"), + PARSE_OPT_NOCOMPLETE), + OPT_END() + }; + + const char *const usage[] = { + N_("git submodule--helper add-config " + "[--force|-f] [--branch|-b ] " + "--url --resolved-url " + "--path --name "), + NULL + }; + + argc = parse_options(argc, argv, prefix, options, usage, 0); + + if (argc != 0) + usage_with_options(usage, options); + + add_data.force = !!force; + configure_added_submodule(&add_data); + + return 0; +} + #define SUPPORT_SUPER_PREFIX (1<<0) struct cmd_struct { @@ -2949,6 +3073,7 @@ static struct cmd_struct commands[] = { {"name", module_name, 0}, {"clone", module_clone, 0}, {"add-clone", add_clone, 0}, + {"add-config", add_config, 0}, {"update-module-mode", module_update_module_mode, 0}, {"update-clone", update_clone, 0}, {"ensure-core-worktree", ensure_core_worktree, 0}, diff --git a/git-submodule.sh b/git-submodule.sh index f71e1e5495..9826378fa6 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -242,33 +242,7 @@ cmd_add() fi git submodule--helper add-clone ${GIT_QUIET:+--quiet} ${force:+"--force"} ${progress:+"--progress"} ${branch:+--branch "$branch"} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${dissociate:+"--dissociate"} ${depth:+"$depth"} || exit - git config submodule."$sm_name".url "$realrepo" - - git add --no-warn-embedded-repo $force "$sm_path" || - die "$(eval_gettext "Failed to add submodule '\$sm_path'")" - - git submodule--helper config submodule."$sm_name".path "$sm_path" && - git submodule--helper config submodule."$sm_name".url "$repo" && - if test -n "$branch" - then - git submodule--helper config submodule."$sm_name".branch "$branch" - fi && - git add --force .gitmodules || - die "$(eval_gettext "Failed to register submodule '\$sm_path'")" - - # NEEDSWORK: In a multi-working-tree world, this needs to be - # set in the per-worktree config. - if git config --get submodule.active >/dev/null - then - # If the submodule being adding isn't already covered by the - # current configured pathspec, set the submodule's active flag - if ! git submodule--helper is-active "$sm_path" - then - git config submodule."$sm_name".active "true" - fi - else - git config submodule."$sm_name".active "true" - fi + git submodule--helper add-config ${force:+--force} ${branch:+--branch "$branch"} --url "$repo" --resolved-url "$realrepo" --path "$sm_path" --name "$sm_name" } #