From patchwork Mon Jan 24 20:44:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12722990 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 166E5C433F5 for ; Mon, 24 Jan 2022 22:33:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381757AbiAXWc6 (ORCPT ); Mon, 24 Jan 2022 17:32:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1582626AbiAXWPd (ORCPT ); Mon, 24 Jan 2022 17:15:33 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88943C04A2C4 for ; Mon, 24 Jan 2022 12:45:02 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id y17-20020a170902d65100b0014a17faf0bdso3961521plh.12 for ; Mon, 24 Jan 2022 12:45:02 -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; bh=qbm8RFqn3Of+2Q951Tq6ed+SI643yO7op5S/t3iQ/r4=; b=tOu2mxvTmLfQG5vIeE8n7IxRyQTUfCKMwA24F/MlCHcObWwH2LYEz4mvDniOBl6pX/ cGOnoxfDZP6oAtSym1idaAIsFzY8TbQAoo21oBB/HEbgt8CpfyO16pPmGYQ9RNE2Wfm4 b4BNe/HTz28C1AOnRYyD5lH2cqZa8j/wDXYrT9DNhPOIklvyyw7Rb0aKBZs+zg+tny/T CFzefNptFmv0FbVCc6ucHobOC8MLThcMD5nrAoNfYt5rVI+ahVzLchJXCj0R7VXWHzD/ +zWqMEKci5cuaO7vk0uTD5YGBUxWsB9R9WidAR/SVTx4mKcJn3TjVV9Pl6I2O7T9jM93 KmAg== 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; bh=qbm8RFqn3Of+2Q951Tq6ed+SI643yO7op5S/t3iQ/r4=; b=yPu/k06YnpAwHSXGyex+b55YplIb8I3wt/fyNA49jYDnMhVdQ6yeQUG7E2DCV95gkD sQR4wfL5/N3yEwfERgCO43n8/yxpimeTAqPa/QMHSikwzVlRF9dFeWMNMcUOuPHrK3Yb 0nkXZovQk4wPveQYKtAJc/7aQbcxE+2akBLP3nAOt/ZTA6PYABYxFvC7Xj36akSexD/9 HBF2pemK3MBVAkBYblaEpoNuDX0dYWP3SaEJRc2l8LO4CYc4MpC9Z0NzEYGEiU3+U7zT P3nt6vav2WNm/lDJtFUWJP1CACrpDOX6HjeziHyft089PNjfskusZ5gJeloOB//KyDdT 9RqA== X-Gm-Message-State: AOAM532wcrlWPvlrA/zz7rLmcKEVr1qHWEhkxFr7wDfxNSrWImUzn/15 lf1be+wOJ/d++dF3uY7m+WprGQzZ2jpKT17E3I+64Xu7L3E8Xx8qAd54ZpFfsoqBX+Up6bvPPCj LvFytmadZW8TXrJvpam8WNYXbRC/muWgqZ/g57b2h8JwlNggcH6NI/wgcOVIP6LU= X-Google-Smtp-Source: ABdhPJwKHtwIArJrUNN3FKNoLIixgvpAnsXqlmlog0+PWR3n/TjU0Q9JqhVdD7CDM+WIK9szKWz+9ud6uW4TPw== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:aa7:84cf:0:b0:4c8:7234:e6ca with SMTP id x15-20020aa784cf000000b004c87234e6camr8829397pfn.35.1643057101906; Mon, 24 Jan 2022 12:45:01 -0800 (PST) Date: Mon, 24 Jan 2022 12:44:37 -0800 In-Reply-To: <20220124204442.39353-1-chooglen@google.com> Message-Id: <20220124204442.39353-2-chooglen@google.com> Mime-Version: 1.0 References: <20211220233459.45739-1-chooglen@google.com> <20220124204442.39353-1-chooglen@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 1/6] branch: move --set-upstream-to behavior to dwim_and_setup_tracking() From: Glen Choo To: git@vger.kernel.org Cc: Glen Choo , Jonathan Tan , Josh Steadmon , Emily Shaffer , " =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= " , Philippe Blain , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This commit is preparation for a future commit that will simplify create_branch() so that it always creates a branch. This will allow create_branch() to accept a dry_run parameter (which is needed for "git branch --recurse-submodules"). create_branch() used to always create a branch, but 4fc5006676 (Add branch --set-upstream, 2010-01-18) changed it to also be able to set tracking information without creating a branch. Refactor the code that sets tracking information into its own functions dwim_branch_start() and dwim_and_setup_tracking(). Also change an invocation of create_branch() in cmd_branch() in builtin/branch.c to use dwim_and_setup_tracking(), since that invocation is only for setting tracking information (in "git branch --set-upstream-to"). As of this commit, create_branch() is no longer invoked in a way that does not create branches. Helped-by: Jonathan Tan Signed-off-by: Glen Choo --- branch.c | 87 +++++++++++++++++++++++++++++++++++++----------- branch.h | 22 ++++++++++++ builtin/branch.c | 9 ++--- 3 files changed, 92 insertions(+), 26 deletions(-) diff --git a/branch.c b/branch.c index a4e4631ef1..f3a31930fb 100644 --- a/branch.c +++ b/branch.c @@ -218,9 +218,11 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref) } /* - * This is called when new_ref is branched off of orig_ref, and tries - * to infer the settings for branch..{remote,merge} from the - * config. + * Used internally to set the branch..{remote,merge} config + * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike + * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main" + * will not be expanded to "refs/remotes/origin/main", so it is not safe + * for 'orig_ref' to be raw user input. */ static void setup_tracking(const char *new_ref, const char *orig_ref, enum branch_track track, int quiet) @@ -341,31 +343,37 @@ N_("\n" "will track its remote counterpart, you may want to use\n" "\"git push -u\" to set the upstream config as you push."); -void create_branch(struct repository *r, - const char *name, const char *start_name, - int force, int clobber_head_ok, int reflog, - int quiet, enum branch_track track) +/** + * DWIMs a user-provided ref to determine the starting point for a + * branch and validates it, where: + * + * - r is the repository to validate the branch for + * + * - start_name is the ref that we would like to test. This is + * expanded with DWIM and assigned to out_real_ref. + * + * - track is the tracking mode of the new branch. If tracking is + * explicitly requested, start_name must be a branch (because + * otherwise start_name cannot be tracked) + * + * - out_oid is an out parameter containing the object_id of start_name + * + * - out_real_ref is an out parameter containing the full, 'real' form + * of start_name e.g. refs/heads/main instead of main + * + */ +static void dwim_branch_start(struct repository *r, const char *start_name, + enum branch_track track, char **out_real_ref, + struct object_id *out_oid) { struct commit *commit; struct object_id oid; char *real_ref; - struct strbuf ref = STRBUF_INIT; - int forcing = 0; - int dont_change_ref = 0; int explicit_tracking = 0; if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE) explicit_tracking = 1; - if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok) - ? validate_branchname(name, &ref) - : validate_new_branchname(name, &ref, force)) { - if (!force) - dont_change_ref = 1; - else - forcing = 1; - } - real_ref = NULL; if (get_oid_mb(start_name, &oid)) { if (explicit_tracking) { @@ -402,7 +410,37 @@ void create_branch(struct repository *r, if ((commit = lookup_commit_reference(r, &oid)) == NULL) die(_("Not a valid branch point: '%s'."), start_name); - oidcpy(&oid, &commit->object.oid); + if (out_real_ref) { + *out_real_ref = real_ref; + real_ref = NULL; + } + if (out_oid) + oidcpy(out_oid, &commit->object.oid); + + FREE_AND_NULL(real_ref); +} + +void create_branch(struct repository *r, + const char *name, const char *start_name, + int force, int clobber_head_ok, int reflog, + int quiet, enum branch_track track) +{ + struct object_id oid; + char *real_ref; + struct strbuf ref = STRBUF_INIT; + int forcing = 0; + int dont_change_ref = 0; + + if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok) + ? validate_branchname(name, &ref) + : validate_new_branchname(name, &ref, force)) { + if (!force) + dont_change_ref = 1; + else + forcing = 1; + } + + dwim_branch_start(r, start_name, track, &real_ref, &oid); if (reflog) log_all_ref_updates = LOG_REFS_NORMAL; @@ -436,6 +474,15 @@ void create_branch(struct repository *r, free(real_ref); } +void dwim_and_setup_tracking(struct repository *r, const char *new_ref, + const char *orig_ref, enum branch_track track, + int quiet) +{ + char *real_orig_ref; + dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL); + setup_tracking(new_ref, real_orig_ref, track, quiet); +} + void remove_merge_branch_state(struct repository *r) { unlink(git_path_merge_head(r)); diff --git a/branch.h b/branch.h index 815dcd40c0..ab2315c611 100644 --- a/branch.h +++ b/branch.h @@ -18,6 +18,28 @@ extern enum branch_track git_branch_track; /* Functions for acting on the information about branches. */ +/** + * Sets branch..{remote,merge} config settings such that + * new_ref tracks orig_ref according to the specified tracking mode. + * + * - new_ref is the name of the branch that we are setting tracking + * for. + * + * - orig_ref is the name of the ref that is 'upstream' of new_ref. + * orig_ref will be expanded with DWIM so that the config settings + * are in the correct format e.g. "refs/remotes/origin/main" instead + * of "origin/main". + * + * - track is the tracking mode e.g. BRANCH_TRACK_REMOTE causes + * new_ref to track orig_ref directly, whereas BRANCH_TRACK_INHERIT + * causes new_ref to track whatever orig_ref tracks. + * + * - quiet suppresses tracking information. + */ +void dwim_and_setup_tracking(struct repository *r, const char *new_ref, + const char *orig_ref, enum branch_track track, + int quiet); + /* * Creates a new branch, where: * diff --git a/builtin/branch.c b/builtin/branch.c index 1890afd4e5..59768a3f67 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -822,12 +822,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (!ref_exists(branch->refname)) die(_("branch '%s' does not exist"), branch->name); - /* - * create_branch takes care of setting up the tracking - * info and making sure new_upstream is correct - */ - create_branch(the_repository, branch->name, new_upstream, - 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE); + dwim_and_setup_tracking(the_repository, branch->name, + new_upstream, BRANCH_TRACK_OVERRIDE, + quiet); } else if (unset_upstream) { struct branch *branch = branch_get(argv[0]); struct strbuf buf = STRBUF_INIT; From patchwork Mon Jan 24 20:44:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12722989 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 537ECC433EF for ; Mon, 24 Jan 2022 22:32:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382337AbiAXWcw (ORCPT ); Mon, 24 Jan 2022 17:32:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1582627AbiAXWPd (ORCPT ); Mon, 24 Jan 2022 17:15:33 -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 91AF5C04A2C7 for ; Mon, 24 Jan 2022 12:45:04 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id c75-20020a63354e000000b003408e4153d1so10523299pga.9 for ; Mon, 24 Jan 2022 12:45:04 -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; bh=Z2cI7+fidAOey6BoZpTflTD7ym3i9+9ULEDvvtZ51HM=; b=XbXnTbuyoV/aY6Fkx4QH+iJmyy4TPbMRj0EWcprwGS6hNcOlJvWagcSqLNNf6m/MsH G4edIQRc7Vb/8AMARKWS5dilyhQLNXPfeYEu94mkHU0KQSGcDfWFMVrgs9Kf6xniXlOf r8xSkLpfg/CgncOv/g1G/Czz/Imo2WbvEQl5ECzgE4dO1VfdX2y+wCh/c8wPnDo5IoTJ 19HwcN8Gych+r4nPL42PSiUscPoE5jekXXAl6ZPBNo4YTsIWKBV4foban+YPRMvxa9Yh UGfnwewOBD58EmfQRXdVL50B1HdB3PiFrj5cJZOYFdPY9UIZKMAq70eGXFXyvsWWxT/U 88zA== 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; bh=Z2cI7+fidAOey6BoZpTflTD7ym3i9+9ULEDvvtZ51HM=; b=hbSpCT+Tjm0GTjpZUn3PN3UTekrfoyrdJyZw6QmG2LkC81L4P/U3xGWtUHSdB06ZWT FSY2gaIcE7HmqNHBRIcT12K0c1Ef2jvrcW+qSbWTPMcXbm7nk5/dAruY4+9LGtkQu4iE z9KPyiawfnPRNCPnmZFl0ywNskWKDSgX/aOJggyQNmP3aWNBNbN6zIC9BYfzWKdZB2M+ tGUadV04kVAAEZRflH1Y4fhUki3FJwV1jHSYWpXlLF9ppzU6ldkEAnFbx5PYJmsIyxOQ ERvWBBNya1D76cltWlCqO/2HcEx18HhyPtdWm1hI6qy96JMgPgH3Q4zYZa//Qcv9nOcG /lbw== X-Gm-Message-State: AOAM531HsY7oryPz5TAYioY/Lgm36MrqUvhsVPdVE9Go7N8IsAzmIsQF LG/hY6N8RNB/QQMRk7Px7wJzoDDJo7+VFl2ByA3IgtYR1FECydzUfY20jSWapAUSKJzC/vssV6K SAlsfFsOgVlPTHsyyAauu2ai/UP9WBX1R+OvhgtWAYYii7p82B2mSr0VSqAq+4XA= X-Google-Smtp-Source: ABdhPJyUSwvNOsuNbbKIosURuBc0pw1Ig4KMzDc7VA5MYstPa4jiUMzr8eSzu/V0LRFDM/jRyApNLqIRwRkdeA== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:c111:b0:14b:528d:3745 with SMTP id 17-20020a170902c11100b0014b528d3745mr5759376pli.38.1643057103993; Mon, 24 Jan 2022 12:45:03 -0800 (PST) Date: Mon, 24 Jan 2022 12:44:38 -0800 In-Reply-To: <20220124204442.39353-1-chooglen@google.com> Message-Id: <20220124204442.39353-3-chooglen@google.com> Mime-Version: 1.0 References: <20211220233459.45739-1-chooglen@google.com> <20220124204442.39353-1-chooglen@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 2/6] branch: make create_branch() always create a branch From: Glen Choo To: git@vger.kernel.org Cc: Glen Choo , Jonathan Tan , Josh Steadmon , Emily Shaffer , " =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= " , Philippe Blain , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org With the previous commit, there are no more invocations of create_branch() that do not create a branch because: * BRANCH_TRACK_OVERRIDE is no longer passed * clobber_head_ok = true and force = false is never passed Assert these situations, delete dead code and ensure that we're handling clobber_head_ok and force correctly by introducing tests for `git branch --force`. As a result, create_branch() now always creates a branch. Helped-by: Jonathan Tan Signed-off-by: Glen Choo --- branch.c | 57 ++++++++++++++++++++++------------------------- branch.h | 4 ++-- t/t3200-branch.sh | 17 ++++++++++++++ 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/branch.c b/branch.c index f3a31930fb..df24021f27 100644 --- a/branch.c +++ b/branch.c @@ -429,15 +429,19 @@ void create_branch(struct repository *r, char *real_ref; struct strbuf ref = STRBUF_INIT; int forcing = 0; - int dont_change_ref = 0; - - if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok) - ? validate_branchname(name, &ref) - : validate_new_branchname(name, &ref, force)) { - if (!force) - dont_change_ref = 1; - else - forcing = 1; + struct ref_transaction *transaction; + struct strbuf err = STRBUF_INIT; + char *msg; + + if (track == BRANCH_TRACK_OVERRIDE) + BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?"); + if (clobber_head_ok && !force) + BUG("'clobber_head_ok' can only be used with 'force'"); + + if (clobber_head_ok ? + validate_branchname(name, &ref) : + validate_new_branchname(name, &ref, force)) { + forcing = 1; } dwim_branch_start(r, start_name, track, &real_ref, &oid); @@ -445,27 +449,20 @@ void create_branch(struct repository *r, if (reflog) log_all_ref_updates = LOG_REFS_NORMAL; - if (!dont_change_ref) { - struct ref_transaction *transaction; - struct strbuf err = STRBUF_INIT; - char *msg; - - if (forcing) - msg = xstrfmt("branch: Reset to %s", start_name); - else - msg = xstrfmt("branch: Created from %s", start_name); - - transaction = ref_transaction_begin(&err); - if (!transaction || - ref_transaction_update(transaction, ref.buf, - &oid, forcing ? NULL : null_oid(), - 0, msg, &err) || - ref_transaction_commit(transaction, &err)) - die("%s", err.buf); - ref_transaction_free(transaction); - strbuf_release(&err); - free(msg); - } + if (forcing) + msg = xstrfmt("branch: Reset to %s", start_name); + else + msg = xstrfmt("branch: Created from %s", start_name); + transaction = ref_transaction_begin(&err); + if (!transaction || + ref_transaction_update(transaction, ref.buf, + &oid, forcing ? NULL : null_oid(), + 0, msg, &err) || + ref_transaction_commit(transaction, &err)) + die("%s", err.buf); + ref_transaction_free(transaction); + strbuf_release(&err); + free(msg); if (real_ref && track) setup_tracking(ref.buf + 11, real_ref, track, quiet); diff --git a/branch.h b/branch.h index ab2315c611..cf3a4d3ff3 100644 --- a/branch.h +++ b/branch.h @@ -52,8 +52,8 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref, * * - force enables overwriting an existing (non-head) branch * - * - clobber_head_ok allows the currently checked out (hence existing) - * branch to be overwritten; without 'force', it has no effect. + * - clobber_head_ok, when enabled with 'force', allows the currently + * checked out (head) branch to be overwritten * * - reflog creates a reflog for the branch * diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index a74b2e06a1..9157382ad0 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -42,6 +42,23 @@ test_expect_success 'git branch abc should create a branch' ' git branch abc && test_path_is_file .git/refs/heads/abc ' +test_expect_success 'git branch abc should fail when abc exists' ' + test_must_fail git branch abc +' + +test_expect_success 'git branch --force abc should fail when abc is checked out' ' + test_when_finished git switch main && + git switch abc && + test_must_fail git branch --force abc HEAD~1 +' + +test_expect_success 'git branch --force abc should succeed when abc exists' ' + git rev-parse HEAD~1 >expect && + git branch --force abc HEAD~1 && + git rev-parse abc >actual && + test_cmp expect actual +' + test_expect_success 'git branch a/b/c should create a branch' ' git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c ' From patchwork Mon Jan 24 20:44:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12722991 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 E541CC433FE for ; Mon, 24 Jan 2022 22:33:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1588519AbiAXWdE (ORCPT ); Mon, 24 Jan 2022 17:33:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1582686AbiAXWPj (ORCPT ); Mon, 24 Jan 2022 17:15:39 -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 55F77C04A2C8 for ; Mon, 24 Jan 2022 12:45:06 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id f18-20020a63dc52000000b0034d062c66a0so9552619pgj.17 for ; Mon, 24 Jan 2022 12:45:06 -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; bh=vrmeo+0N5xTuGfudtJmBXuN2U1rFP8aHWOkCNhhsJTE=; b=GuuQibZFi4L7ekFQNlUpTaFX2ftnu/7ftQqGeV1/SlXs30ji2OI3loxppKEov3FBCL l6ZNruHxXZuCRBjddbTE/FC/R1bPGncoZGc+ZJR/6wPGV3xp3KJwDyZYVi4xrA7ohjJ/ MYHzJzhDnkpvpmJ+1Rihr9e0wCu3BC4kgM50AhhvSnVaFuErkbagFNoNl0AR27ImPMq5 XP2l5xN2diufu6zkMhWVlMmE+qiZohHv6Ynk+Le58F6jNYfmeXk6pijUaYDnAGCm/dkQ mUNw/7gBG3l6vuCHqvi7hCyvx9/dy5G9PQy2bdBAWUqzKr5PQ3tavRix9dCv643BpyDI jHEQ== 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; bh=vrmeo+0N5xTuGfudtJmBXuN2U1rFP8aHWOkCNhhsJTE=; b=WWkdMUHp8woo9JIqJB4ab+qbfMVwlhCwnWq7FU02cxeoaC3BvEWj0KW/eJGjtcVTb1 cJV8Dt4+kRWcXeoG82PmG46fjscpnOvqXkms8eX6KjSBzRI2zrExLkThlXtYbyzXtS4V 11B8bHhjl/ZHHMcWE+hgzbtZ5sMcWDBfY1KIrZKyYGjF1o8N04Vmm3ermJHis4t1vAi6 eVIQ5+9td5KdBiC+GkJteDvHhocWFc1YaH2aradQWRm9JCWMeLFQyLFn+JRw4OnqAKyE rvozldkp93HnT+HnIPPHlOVT4TjJROwHbSLZmgJ6hY+6CGhytduX7++liSWo0tLj2vWu Ojnw== X-Gm-Message-State: AOAM532FenK2r7j0kVF8f/6+ZH2gGGV0OcVzKxHjX7vCnfGvSWorfktO CG84XT+ntSus7B2R/nylCq9FTr8OXDTkqOhV8HWBpVph2jXsF3cWDoc+0Hf8U4dJLSv0l0CA0I6 MDJIwtyogFJbg3n+DWoI/mzOH0tky/SEXrfIC5TsZoOHjyQCca5aD+uFIE1XDSzA= X-Google-Smtp-Source: ABdhPJx3GzBubzbj7CwpNc69bnt85eCYI4NK6mbzMYP6KEOxnulKsR1TEZUKYXjo06owcGYLrXSEMC0rWIZpxg== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:6901:b0:149:4e89:2d45 with SMTP id j1-20020a170902690100b001494e892d45mr15410217plk.22.1643057105739; Mon, 24 Jan 2022 12:45:05 -0800 (PST) Date: Mon, 24 Jan 2022 12:44:39 -0800 In-Reply-To: <20220124204442.39353-1-chooglen@google.com> Message-Id: <20220124204442.39353-4-chooglen@google.com> Mime-Version: 1.0 References: <20211220233459.45739-1-chooglen@google.com> <20220124204442.39353-1-chooglen@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 3/6] branch: add a dry_run parameter to create_branch() From: Glen Choo To: git@vger.kernel.org Cc: Glen Choo , Jonathan Tan , Josh Steadmon , Emily Shaffer , " =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= " , Philippe Blain , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a dry_run parameter to create_branch() such that dry_run = 1 will validate a new branch without trying to create it. This will be used in `git branch --recurse-submodules` to ensure that the new branch can be created in all submodules. Signed-off-by: Glen Choo --- branch.c | 5 ++++- branch.h | 5 ++++- builtin/branch.c | 2 +- builtin/checkout.c | 3 ++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/branch.c b/branch.c index df24021f27..02d46a69b8 100644 --- a/branch.c +++ b/branch.c @@ -423,7 +423,7 @@ static void dwim_branch_start(struct repository *r, const char *start_name, void create_branch(struct repository *r, const char *name, const char *start_name, int force, int clobber_head_ok, int reflog, - int quiet, enum branch_track track) + int quiet, enum branch_track track, int dry_run) { struct object_id oid; char *real_ref; @@ -445,6 +445,8 @@ void create_branch(struct repository *r, } dwim_branch_start(r, start_name, track, &real_ref, &oid); + if (dry_run) + goto cleanup; if (reflog) log_all_ref_updates = LOG_REFS_NORMAL; @@ -467,6 +469,7 @@ void create_branch(struct repository *r, if (real_ref && track) setup_tracking(ref.buf + 11, real_ref, track, quiet); +cleanup: strbuf_release(&ref); free(real_ref); } diff --git a/branch.h b/branch.h index cf3a4d3ff3..509cfcc34e 100644 --- a/branch.h +++ b/branch.h @@ -62,11 +62,14 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref, * - track causes the new branch to be configured to merge the remote branch * that start_name is a tracking branch for (if any). * + * - dry_run causes the branch to be validated but not created. + * */ void create_branch(struct repository *r, const char *name, const char *start_name, int force, int clobber_head_ok, - int reflog, int quiet, enum branch_track track); + int reflog, int quiet, enum branch_track track, + int dry_run); /* * Check if 'name' can be a valid name for a branch; die otherwise. diff --git a/builtin/branch.c b/builtin/branch.c index 59768a3f67..d1e3ad6ebf 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -858,7 +858,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) create_branch(the_repository, argv[0], (argc == 2) ? argv[1] : head, - force, 0, reflog, quiet, track); + force, 0, reflog, quiet, track, 0); } else usage_with_options(builtin_branch_usage, options); diff --git a/builtin/checkout.c b/builtin/checkout.c index 8d511aa6b7..fb4323dfca 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -893,7 +893,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, opts->new_branch_force ? 1 : 0, opts->new_branch_log, opts->quiet, - opts->track); + opts->track, + 0); new_branch_info->name = opts->new_branch; setup_branch_path(new_branch_info); } From patchwork Mon Jan 24 20:44:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12722992 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 90939C433F5 for ; Mon, 24 Jan 2022 22:33:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1588572AbiAXWdU (ORCPT ); Mon, 24 Jan 2022 17:33:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355477AbiAXWRQ (ORCPT ); Mon, 24 Jan 2022 17:17:16 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CD4CC04A2CB for ; Mon, 24 Jan 2022 12:45:08 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id z15-20020a25bb0f000000b00613388c7d99so37286062ybg.8 for ; Mon, 24 Jan 2022 12:45:08 -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; bh=VCvjaeO8S8pcqyqAs+39F769/rd44ldNSRBvHy/41q8=; b=i3ylm38IbKpnBn//krMlXAXFBtbZz4iYLErSwtcE9vqFxcstWp1odq0sdCTGndbgE0 cWpCQtjjxj0scUStpZU/vGGg37G/TWGtS6pUhsgIbdX9IvcsiLwuXFRpiuQfTlM1UR7f Lb5PkTsOsPmjIarBugaNzEppY+T6p1y9pv5vhMkPo3/VLlB6NvmT7G+lgpoK1baDLh3W GO8Rl1B5oqQmmaAZ9jYYlRf1grHQf/09IICdbYsoqmvXVvsKJjK98tJNv6FHo8npfCwM wduQD994IHmLfEfmiNmbaaxtFoDbAWdi6zMRT8i8cCEC4GZ772D24KR5hIkqx06bbjGo Ea9Q== 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; bh=VCvjaeO8S8pcqyqAs+39F769/rd44ldNSRBvHy/41q8=; b=R85lRZV89/s8Kgq9XYQOtcVuzdnNEpICTkz3ODl5rOgAC3xCRJkD65noK8BpOd2XXI j7mJ445Xhoar9HIPHZjZ2jbf8IBz3vK0JGkTftUnjugsRIEG8zR0ukrmNjYUP46w6h8t NvfdmIiujwhqjOMhJeRo/K+WIBanK4YjhOqhA87WiawOcLXUgNXoMBiyciuMkRPYuIFw nDkJw1gp55NPbV/+G11QLEGncIT7vhVIre61sfJroWqXPlyzXMcFpQGOAsMKSsQxL+GS ulq2ttkx+c73c9VPqLbOY55uO0OltelVCVBraumbkmCyKDDxDsp3g5lzPP0NPYh2pnKw xJ9w== X-Gm-Message-State: AOAM5334hFziacjKKIlbNqcV/RWCjD1kesfRWSj//f0JZwSSMU5ysJzw VyaeD/xde2CrY2xrN1n8MJEmft2BLGIWeXezRVwovTP1SSaVbQouXGIOIRaUXuFhlOHG7G3C7A9 ei75tS85GnC3hjT+xOcPVxizYS4vE+2tpYLoS7Nqlj2A+n6vT8U6FHonyKL6S3rY= X-Google-Smtp-Source: ABdhPJy0rNp1pnN8gfookJdGFvqtsFu2KAxxlGNWO1RXOZOtQhv0LO/9RVz+hUgVMqP8UqL/nEq30wFlqxvmiA== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a25:d0c:: with SMTP id 12mr12070391ybn.249.1643057107696; Mon, 24 Jan 2022 12:45:07 -0800 (PST) Date: Mon, 24 Jan 2022 12:44:40 -0800 In-Reply-To: <20220124204442.39353-1-chooglen@google.com> Message-Id: <20220124204442.39353-5-chooglen@google.com> Mime-Version: 1.0 References: <20211220233459.45739-1-chooglen@google.com> <20220124204442.39353-1-chooglen@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 4/6] builtin/branch: consolidate action-picking logic in cmd_branch() From: Glen Choo To: git@vger.kernel.org Cc: Glen Choo , Jonathan Tan , Josh Steadmon , Emily Shaffer , " =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= " , Philippe Blain , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Consolidate the logic for deciding when to create a new branch in cmd_branch(), and save the result for reuse. Besides making the function more explicit, this allows us to validate options that can only be used when creating a branch. Such an option does not exist yet, but one will be introduced in a subsequent commit. Helped-by: Jonathan Tan Signed-off-by: Glen Choo --- builtin/branch.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index d1e3ad6ebf..c274fbdfcf 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -616,14 +616,15 @@ static int edit_branch_description(const char *branch_name) int cmd_branch(int argc, const char **argv, const char *prefix) { - int delete = 0, rename = 0, copy = 0, force = 0, list = 0; - int show_current = 0; - int reflog = 0, edit_description = 0; - int quiet = 0, unset_upstream = 0; + /* possible actions */ + int delete = 0, rename = 0, copy = 0, list = 0, + unset_upstream = 0, show_current = 0, edit_description = 0; const char *new_upstream = NULL; + int noncreate_actions = 0; + /* possible options */ + int reflog = 0, quiet = 0, icase = 0, force = 0; enum branch_track track; struct ref_filter filter; - int icase = 0; static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; struct ref_format format = REF_FORMAT_INIT; @@ -707,8 +708,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix) filter.reachable_from || filter.unreachable_from || filter.points_at.nr) list = 1; - if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current + - list + edit_description + unset_upstream > 1) + noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream + + !!show_current + !!list + !!edit_description + + !!unset_upstream; + if (noncreate_actions > 1) usage_with_options(builtin_branch_usage, options); if (filter.abbrev == -1) @@ -848,7 +851,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) strbuf_addf(&buf, "branch.%s.merge", branch->name); git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); strbuf_release(&buf); - } else if (argc > 0 && argc <= 2) { + } else if (!noncreate_actions && argc > 0 && argc <= 2) { if (filter.kind != FILTER_REFS_BRANCHES) die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n" "Did you mean to use: -a|-r --list ?")); From patchwork Mon Jan 24 20:44:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12722993 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 A1338C4332F for ; Mon, 24 Jan 2022 22:33:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1588632AbiAXWdb (ORCPT ); Mon, 24 Jan 2022 17:33:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355487AbiAXWRR (ORCPT ); Mon, 24 Jan 2022 17:17:17 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1925C04A2CC for ; Mon, 24 Jan 2022 12:45:10 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id n196-20020a25d6cd000000b006139bdfade9so35749625ybg.17 for ; Mon, 24 Jan 2022 12:45:10 -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; bh=0xm9A4MAwtXVSM9yOn5jU/MXeOtYvRknyTl9+pwX81g=; b=WXdZHsLffR15G+UcurbMDDBNACiM9kWtLifnhs4NeuI5LCYMfTeRNt2be1gnYLWC1C tJHGRHxhKkLjgot2kK9YIRkbDSnCA9BKiO1k0TQq0lzyzEOeJawOL6jebjW6GDT/Ow3Q +I1XGFXOf/XNpzwdwscJFBuH94fj6r5gFb6KTQhLvRjI5jd9X5/k+pJ4YxzOEbSFNyT2 ZTyWO6964Ei1p+Qz3O4FI4I5NGSNfM+6vvPwTAXJFQzEJu7JMT3ab9Axhv27QUgBB3R7 3vwW+gLV8NexLqlSGvmWxs9wMT8B8wav/S56uPlEW4Lxi850TMxYCKDMfr3rawgfia+x EjfQ== 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; bh=0xm9A4MAwtXVSM9yOn5jU/MXeOtYvRknyTl9+pwX81g=; b=GBfLpaDFFFBDoR3Y1RVO81S6flLsiT5Drv3CgSdoXuuE7Dhcz335iukkbY2smmIj5c a9/SwM5EqPo2a/8FiFIi4ldG7C/7B4M5mR9mzQyWtirGCuVpQ3iZKDkPQELMWYAY+1Pc Q62bGtAj1m2uM24v7U9LYnMu88UbaJ64vGdmogWSg4x4zks1QcPAZviJOIVrd4aowUKo s2PIqUoLA5Y9DoeCFwFErHkKhnoN7hLjhvwNsDlRINYto5WpEI1rqmmgFo0Z9mW0wHmI PwJUMotpTyUI+qHXYhnjFIhAlxPMCmT7O+PrgD8GxlABzXUdNO26Eivo/q6NG90BBKFK natg== X-Gm-Message-State: AOAM530D1j6xXdXh8rUQJ0FwnBBAakmGlIvNLmTl/uu3t2RY1WGy8wUJ rJU5WWOjNNccsBOyzFvKezNmAMxiCbsqpy3dDSIuulAOxrg39rN9i1OGzSdZTRpNNkHk2MjL5yB Qmepcwci6yVXv1kangIQqHrV9Se4RmS9ZLmdFbwwUMXIk5Zx0+ng6YWTsBhx1nHI= X-Google-Smtp-Source: ABdhPJw8Olg7YHr+1OUrIMCQRjTdrcTaPmNdA3RpXZhLqcq/X3ondnD2HIQ3U0vFvcfyRO7afmMbwVXEWxA4Dg== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a25:2b09:: with SMTP id r9mr24279526ybr.365.1643057109873; Mon, 24 Jan 2022 12:45:09 -0800 (PST) Date: Mon, 24 Jan 2022 12:44:41 -0800 In-Reply-To: <20220124204442.39353-1-chooglen@google.com> Message-Id: <20220124204442.39353-6-chooglen@google.com> Mime-Version: 1.0 References: <20211220233459.45739-1-chooglen@google.com> <20220124204442.39353-1-chooglen@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 5/6] branch: add --recurse-submodules option for branch creation From: Glen Choo To: git@vger.kernel.org Cc: Glen Choo , Jonathan Tan , Josh Steadmon , Emily Shaffer , " =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= " , Philippe Blain , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org To improve the submodules UX, we would like to teach Git to handle branches in submodules. Start this process by teaching "git branch" the --recurse-submodules option so that "git branch --recurse-submodules topic" will create the `topic` branch in the superproject and its submodules. Although this commit does not introduce breaking changes, it is incompatible with existing --recurse-submodules commands because "git branch --recurse-submodules" writes to the submodule ref store, but most commands only consider the superproject gitlink and ignore the submodule ref store. For example, "git checkout --recurse-submodules" will check out the commits in the superproject gitlinks (and put the submodules in detached HEAD) instead of checking out the submodule branches. Because of this, this commit introduces a new configuration value, `submodule.propagateBranches`. The plan is for Git commands to prioritize submodule ref store information over superproject gitlinks if this value is true. Because "git branch --recurse-submodules" writes to submodule ref stores, for the sake of clarity, it will not function unless this configuration value is set. This commit also includes changes that support working with submodules from a superproject commit because "branch --recurse-submodules" (and future commands) need to read .gitmodules and gitlinks from the superproject commit, but submodules are typically read from the filesystem's .gitmodules and the index's gitlinks. These changes are: * add a submodules_of_tree() helper that gives the relevant information of an in-tree submodule (e.g. path and oid) and initializes the repository * add is_tree_submodule_active() by adding a treeish_name parameter to is_submodule_active() * add the "submoduleNotUpdated" advice to advise users to update the submodules in their trees Incidentally, fix an incorrect usage string that combined the 'list' usage of git branch (-l) with the 'create' usage; this string has been incorrect since its inception, a8dfd5eac4 (Make builtin-branch.c use parse_options., 2007-10-07). Helped-by: Jonathan Tan Signed-off-by: Glen Choo --- To Jonathan: When describing the differences between "git branch" and "git checkout", I opted to emphasize the fact that "git checkout" checks out the gitlink in detached HEAD and not the branches themselves (vs your suggestion of mentioning that checkout would check out the wrong version if the branch is subsequently updated). Documentation/config/advice.txt | 3 + Documentation/config/submodule.txt | 37 ++-- Documentation/git-branch.txt | 11 +- advice.c | 1 + advice.h | 1 + branch.c | 137 ++++++++++++++ branch.h | 22 +++ builtin/branch.c | 44 ++++- builtin/submodule--helper.c | 38 ++++ submodule-config.c | 60 ++++++ submodule-config.h | 34 ++++ submodule.c | 11 +- submodule.h | 3 + t/t3207-branch-submodule.sh | 282 +++++++++++++++++++++++++++++ 14 files changed, 664 insertions(+), 20 deletions(-) create mode 100755 t/t3207-branch-submodule.sh diff --git a/Documentation/config/advice.txt b/Documentation/config/advice.txt index 063eec2511..adee26fbbb 100644 --- a/Documentation/config/advice.txt +++ b/Documentation/config/advice.txt @@ -116,6 +116,9 @@ advice.*:: submoduleAlternateErrorStrategyDie:: Advice shown when a submodule.alternateErrorStrategy option configured to "die" causes a fatal error. + submodulesNotUpdated:: + Advice shown when a user runs a submodule command that fails + because `git submodule update --init` was not run. addIgnoredFile:: Advice shown if a user attempts to add an ignored file to the index. diff --git a/Documentation/config/submodule.txt b/Documentation/config/submodule.txt index ee454f8126..6490527b45 100644 --- a/Documentation/config/submodule.txt +++ b/Documentation/config/submodule.txt @@ -59,18 +59,33 @@ submodule.active:: submodule.recurse:: A boolean indicating if commands should enable the `--recurse-submodules` - option by default. - Applies to all commands that support this option - (`checkout`, `fetch`, `grep`, `pull`, `push`, `read-tree`, `reset`, - `restore` and `switch`) except `clone` and `ls-files`. + option by default. Defaults to false. ++ +When set to true, it can be deactivated via the +`--no-recurse-submodules` option. Note that some Git commands +lacking this option may call some of the above commands affected by +`submodule.recurse`; for instance `git remote update` will call +`git fetch` but does not have a `--no-recurse-submodules` option. +For these commands a workaround is to temporarily change the +configuration value by using `git -c submodule.recurse=0`. ++ +The following list shows the commands that accept +`--recurse-submodules` and whether they are supported by this +setting. + +* `checkout`, `fetch`, `grep`, `pull`, `push`, `read-tree`, +`reset`, `restore` and `switch` are always supported. +* `clone` and `ls-files` are not supported. +* `branch` is supported only if `submodule.propagateBranches` is +enabled + +submodule.propagateBranches:: + [EXPERIMENTAL] A boolean that enables branching support when + using `--recurse-submodules` or `submodule.recurse=true`. + Enabling this will allow certain commands to accept + `--recurse-submodules` and certain commands that already accept + `--recurse-submodules` will now consider branches. Defaults to false. - When set to true, it can be deactivated via the - `--no-recurse-submodules` option. Note that some Git commands - lacking this option may call some of the above commands affected by - `submodule.recurse`; for instance `git remote update` will call - `git fetch` but does not have a `--no-recurse-submodules` option. - For these commands a workaround is to temporarily change the - configuration value by using `git -c submodule.recurse=0`. submodule.fetchJobs:: Specifies how many submodules are fetched/cloned at the same time. diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt index 75beea7bac..871ca3c3eb 100644 --- a/Documentation/git-branch.txt +++ b/Documentation/git-branch.txt @@ -16,7 +16,8 @@ SYNOPSIS [--points-at ] [--format=] [(-r | --remotes) | (-a | --all)] [--list] [...] -'git branch' [--track [direct|inherit] | --no-track] [-f] [] +'git branch' [--track [direct|inherit] | --no-track] [-f] + [--recurse-submodules] [] 'git branch' (--set-upstream-to= | -u ) [] 'git branch' --unset-upstream [] 'git branch' (-m | -M) [] @@ -235,6 +236,14 @@ how the `branch..remote` and `branch..merge` options are used. Do not set up "upstream" configuration, even if the branch.autoSetupMerge configuration variable is set. +--recurse-submodules:: + THIS OPTION IS EXPERIMENTAL! Causes the current command to + recurse into submodules if `submodule.propagateBranches` is + enabled. See `submodule.propagateBranches` in + linkgit:git-config[1]. + + + Currently, only branch creation is supported. + --set-upstream:: As this option had confusing syntax, it is no longer supported. Please use `--track` or `--set-upstream-to` instead. diff --git a/advice.c b/advice.c index 1dfc91d176..e00d30254c 100644 --- a/advice.c +++ b/advice.c @@ -70,6 +70,7 @@ static struct { [ADVICE_STATUS_HINTS] = { "statusHints", 1 }, [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 }, [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 }, + [ADVICE_SUBMODULES_NOT_UPDATED] = { "submodulesNotUpdated", 1 }, [ADVICE_UPDATE_SPARSE_PATH] = { "updateSparsePath", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, }; diff --git a/advice.h b/advice.h index 601265fd10..a7521d6087 100644 --- a/advice.h +++ b/advice.h @@ -44,6 +44,7 @@ struct string_list; ADVICE_STATUS_HINTS, ADVICE_STATUS_U_OPTION, ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE, + ADVICE_SUBMODULES_NOT_UPDATED, ADVICE_UPDATE_SPARSE_PATH, ADVICE_WAITING_FOR_EDITOR, ADVICE_SKIPPED_CHERRY_PICKS, diff --git a/branch.c b/branch.c index 02d46a69b8..be33fe09fa 100644 --- a/branch.c +++ b/branch.c @@ -8,6 +8,8 @@ #include "sequencer.h" #include "commit.h" #include "worktree.h" +#include "submodule-config.h" +#include "run-command.h" struct tracking { struct refspec_item spec; @@ -483,6 +485,141 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref, setup_tracking(new_ref, real_orig_ref, track, quiet); } +/** + * Creates a branch in a submodule by calling + * create_branches_recursively() in a child process. The child process + * is necessary because install_branch_config_multiple_remotes() (which + * is called by setup_tracking()) does not support writing configs to + * submodules. + */ +static int submodule_create_branch(struct repository *r, + const struct submodule *submodule, + const char *name, const char *start_oid, + const char *start_name, int force, + int reflog, int quiet, + enum branch_track track, int dry_run) +{ + int ret = 0; + struct child_process child = CHILD_PROCESS_INIT; + struct strbuf child_err = STRBUF_INIT; + struct strbuf out_buf = STRBUF_INIT; + char *out_prefix = xstrfmt("submodule '%s': ", submodule->name); + child.git_cmd = 1; + child.err = -1; + child.stdout_to_stderr = 1; + + prepare_other_repo_env(&child.env_array, r->gitdir); + /* + * submodule_create_branch() is indirectly invoked by "git + * branch", but we cannot invoke "git branch" in the child + * process because it does not let us set start_name and + * start_oid separately (see create_branches_recursively()). + */ + strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL); + if (dry_run) + strvec_push(&child.args, "--dry-run"); + if (force) + strvec_push(&child.args, "--force"); + if (quiet) + strvec_push(&child.args, "--quiet"); + if (reflog) + strvec_push(&child.args, "--create-reflog"); + if (track == BRANCH_TRACK_ALWAYS || track == BRANCH_TRACK_EXPLICIT) + strvec_push(&child.args, "--track"); + + strvec_pushl(&child.args, name, start_oid, start_name, NULL); + + if ((ret = start_command(&child))) + return ret; + ret = finish_command(&child); + strbuf_read(&child_err, child.err, 0); + strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len); + + if (ret) + fprintf(stderr, "%s", out_buf.buf); + else + printf("%s", out_buf.buf); + + strbuf_release(&child_err); + strbuf_release(&out_buf); + return ret; +} + +void create_branches_recursively(struct repository *r, const char *name, + const char *start_name, + const char *tracking_name, int force, + int reflog, int quiet, enum branch_track track, + int dry_run) +{ + int i = 0; + char *branch_point = NULL; + struct object_id super_oid; + struct submodule_entry_list submodule_entry_list; + + /* Perform dwim on start_name to get super_oid and branch_point. */ + dwim_branch_start(r, start_name, BRANCH_TRACK_NEVER, &branch_point, + &super_oid); + + /* + * If we were not given an explicit name to track, then assume we are at + * the top level and, just like the non-recursive case, the tracking + * name is the branch point. + */ + if (!tracking_name) + tracking_name = branch_point; + + submodules_of_tree(r, &super_oid, &submodule_entry_list); + /* + * Before creating any branches, first check that the branch can + * be created in every submodule. + */ + for (i = 0; i < submodule_entry_list.entry_nr; i++) { + if (submodule_entry_list.entries[i].repo == NULL) { + if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED)) + advise(_("You may try updating the submodules using 'git checkout %s && git submodule update --init'"), + start_name); + die(_("submodule '%s': unable to find submodule"), + submodule_entry_list.entries[i].submodule->name); + } + + if (submodule_create_branch( + submodule_entry_list.entries[i].repo, + submodule_entry_list.entries[i].submodule, name, + oid_to_hex(&submodule_entry_list.entries[i] + .name_entry->oid), + tracking_name, force, reflog, quiet, track, 1)) + die(_("submodule '%s': cannot create branch '%s'"), + submodule_entry_list.entries[i].submodule->name, + name); + } + + create_branch(the_repository, name, start_name, force, 0, reflog, quiet, + BRANCH_TRACK_NEVER, dry_run); + if (dry_run) + return; + /* + * NEEDSWORK If tracking was set up in the superproject but not the + * submodule, users might expect "git branch --recurse-submodules" to + * fail or give a warning, but this is not yet implemented because it is + * tedious to determine whether or not tracking was set up in the + * superproject. + */ + setup_tracking(name, tracking_name, track, quiet); + + for (i = 0; i < submodule_entry_list.entry_nr; i++) { + if (submodule_create_branch( + submodule_entry_list.entries[i].repo, + submodule_entry_list.entries[i].submodule, name, + oid_to_hex(&submodule_entry_list.entries[i] + .name_entry->oid), + tracking_name, force, reflog, quiet, track, 0)) + die(_("submodule '%s': cannot create branch '%s'"), + submodule_entry_list.entries[i].submodule->name, + name); + repo_clear(submodule_entry_list.entries[i].repo); + } +} + void remove_merge_branch_state(struct repository *r) { unlink(git_path_merge_head(r)); diff --git a/branch.h b/branch.h index 509cfcc34e..a59905a211 100644 --- a/branch.h +++ b/branch.h @@ -71,6 +71,28 @@ void create_branch(struct repository *r, int reflog, int quiet, enum branch_track track, int dry_run); +/* + * Creates a new branch in repository and its submodules (and its + * submodules, recursively). Besides these exceptions, the parameters + * function identically to create_branch(): + * + * - start_name is the name of the ref, in repository r, that the new + * branch should start from. In submodules, branches will start from + * the respective gitlink commit ids in start_name's tree. + * + * - tracking_name is the name used of the ref that will be used to set + * up tracking, e.g. origin/main. This is propagated to submodules so + * that tracking information will appear as if the branch branched off + * tracking_name instead of start_name (which is a plain commit id for + * submodules). If omitted, start_name is used for tracking (just like + * create_branch()). + * + */ +void create_branches_recursively(struct repository *r, const char *name, + const char *start_name, + const char *tracking_name, int force, + int reflog, int quiet, enum branch_track track, + int dry_run); /* * Check if 'name' can be a valid name for a branch; die otherwise. * Return 1 if the named branch already exists; return 0 otherwise. diff --git a/builtin/branch.c b/builtin/branch.c index c274fbdfcf..a720a6683d 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -27,7 +27,8 @@ static const char * const builtin_branch_usage[] = { N_("git branch [] [-r | -a] [--merged] [--no-merged]"), - N_("git branch [] [-l] [-f] []"), + N_("git branch [] [-f] [--recurse-submodules] []"), + N_("git branch [] [-l] [...]"), N_("git branch [] [-r] (-d | -D) ..."), N_("git branch [] (-m | -M) [] "), N_("git branch [] (-c | -C) [] "), @@ -38,6 +39,8 @@ static const char * const builtin_branch_usage[] = { static const char *head; static struct object_id head_oid; +static int recurse_submodules = 0; +static int submodule_propagate_branches = 0; static int branch_use_color = -1; static char branch_colors[][COLOR_MAXLEN] = { @@ -100,6 +103,15 @@ static int git_branch_config(const char *var, const char *value, void *cb) return config_error_nonbool(var); return color_parse(value, branch_colors[slot]); } + if (!strcmp(var, "submodule.recurse")) { + recurse_submodules = git_config_bool(var, value); + return 0; + } + if (!strcasecmp(var, "submodule.propagateBranches")) { + submodule_propagate_branches = git_config_bool(var, value); + return 0; + } + return git_color_default_config(var, value, cb); } @@ -622,7 +634,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix) const char *new_upstream = NULL; int noncreate_actions = 0; /* possible options */ - int reflog = 0, quiet = 0, icase = 0, force = 0; + int reflog = 0, quiet = 0, icase = 0, force = 0, + recurse_submodules_explicit = 0; enum branch_track track; struct ref_filter filter; static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; @@ -672,6 +685,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"), N_("print only branches of the object"), parse_opt_object_name), OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), + OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit, N_("recurse through submodules")), OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), OPT_END(), }; @@ -714,6 +728,17 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (noncreate_actions > 1) usage_with_options(builtin_branch_usage, options); + if (recurse_submodules_explicit) { + if (!submodule_propagate_branches) + die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled")); + if (noncreate_actions) + die(_("--recurse-submodules can only be used to create branches")); + } + + recurse_submodules = + (recurse_submodules || recurse_submodules_explicit) && + submodule_propagate_branches; + if (filter.abbrev == -1) filter.abbrev = DEFAULT_ABBREV; filter.ignore_case = icase; @@ -852,6 +877,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix) git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); strbuf_release(&buf); } else if (!noncreate_actions && argc > 0 && argc <= 2) { + const char *branch_name = argv[0]; + const char *start_name = argc == 2 ? argv[1] : head; + if (filter.kind != FILTER_REFS_BRANCHES) die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n" "Did you mean to use: -a|-r --list ?")); @@ -859,10 +887,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (track == BRANCH_TRACK_OVERRIDE) die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead.")); - create_branch(the_repository, - argv[0], (argc == 2) ? argv[1] : head, - force, 0, reflog, quiet, track, 0); - + if (recurse_submodules) { + create_branches_recursively(the_repository, branch_name, + start_name, NULL, force, + reflog, quiet, track, 0); + return 0; + } + create_branch(the_repository, branch_name, start_name, force, 0, + reflog, quiet, track, 0); } else usage_with_options(builtin_branch_usage, options); diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 6298cbdd4e..eba2a6e9b0 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -20,6 +20,7 @@ #include "diff.h" #include "object-store.h" #include "advice.h" +#include "branch.h" #define OPT_QUIET (1 << 0) #define OPT_CACHED (1 << 1) @@ -2983,6 +2984,42 @@ static int module_set_branch(int argc, const char **argv, const char *prefix) return !!ret; } +static int module_create_branch(int argc, const char **argv, const char *prefix) +{ + enum branch_track track; + int quiet = 0, force = 0, reflog = 0, dry_run = 0; + + struct option options[] = { + OPT__QUIET(&quiet, N_("print only error messages")), + OPT__FORCE(&force, N_("force creation"), 0), + OPT_BOOL(0, "create-reflog", &reflog, + N_("create the branch's reflog")), + OPT_SET_INT('t', "track", &track, + N_("set up tracking mode (see git-pull(1))"), + BRANCH_TRACK_EXPLICIT), + OPT__DRY_RUN(&dry_run, + N_("show whether the branch would be created")), + OPT_END() + }; + const char *const usage[] = { + N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] "), + NULL + }; + + git_config(git_default_config, NULL); + track = git_branch_track; + argc = parse_options(argc, argv, prefix, options, usage, 0); + + if (argc != 3) + usage_with_options(usage, options); + + if (!quiet && !dry_run) + printf_ln(_("creating branch '%s'"), argv[0]); + + create_branches_recursively(the_repository, argv[0], argv[1], argv[2], + force, reflog, quiet, track, dry_run); + return 0; +} struct add_data { const char *prefix; const char *branch; @@ -3379,6 +3416,7 @@ static struct cmd_struct commands[] = { {"config", module_config, 0}, {"set-url", module_set_url, 0}, {"set-branch", module_set_branch, 0}, + {"create-branch", module_create_branch, 0}, }; int cmd_submodule__helper(int argc, const char **argv, const char *prefix) diff --git a/submodule-config.c b/submodule-config.c index f95344028b..24b8d1a700 100644 --- a/submodule-config.c +++ b/submodule-config.c @@ -7,6 +7,7 @@ #include "strbuf.h" #include "object-store.h" #include "parse-options.h" +#include "tree-walk.h" /* * submodule cache lookup structure @@ -726,6 +727,65 @@ const struct submodule *submodule_from_path(struct repository *r, return config_from(r->submodule_cache, treeish_name, path, lookup_path); } +/** + * Used internally by submodules_of_tree(). Recurses into 'treeish_name' + * and appends submodule entries to 'out'. The submodule_cache expects + * a root-level treeish_name and paths, so keep track of these values + * with 'root_tree' and 'prefix'. + */ +static void traverse_tree_submodules(struct repository *r, + const struct object_id *root_tree, + char *prefix, + const struct object_id *treeish_name, + struct submodule_entry_list *out) +{ + struct tree_desc tree; + struct submodule_tree_entry *st_entry; + struct name_entry *name_entry; + char *tree_path = NULL; + + name_entry = xmalloc(sizeof(*name_entry)); + + fill_tree_descriptor(r, &tree, treeish_name); + while (tree_entry(&tree, name_entry)) { + if (prefix) + tree_path = + mkpathdup("%s/%s", prefix, name_entry->path); + else + tree_path = xstrdup(name_entry->path); + + if (S_ISGITLINK(name_entry->mode) && + is_tree_submodule_active(r, root_tree, tree_path)) { + st_entry = xmalloc(sizeof(*st_entry)); + st_entry->name_entry = name_entry; + st_entry->submodule = + submodule_from_path(r, root_tree, tree_path); + st_entry->repo = xmalloc(sizeof(*st_entry->repo)); + if (repo_submodule_init(st_entry->repo, r, tree_path, + root_tree)) + FREE_AND_NULL(st_entry->repo); + + ALLOC_GROW(out->entries, out->entry_nr + 1, + out->entry_alloc); + out->entries[out->entry_nr++] = *st_entry; + } else if (S_ISDIR(name_entry->mode)) + traverse_tree_submodules(r, root_tree, tree_path, + &name_entry->oid, out); + free(tree_path); + } +} + +void submodules_of_tree(struct repository *r, + const struct object_id *treeish_name, + struct submodule_entry_list *out) +{ + CALLOC_ARRAY(out->entries, 0); + out->entry_nr = 0; + out->entry_alloc = 0; + + traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out); +} + void submodule_free(struct repository *r) { if (r->submodule_cache) diff --git a/submodule-config.h b/submodule-config.h index 65875b94ea..fa229a8b97 100644 --- a/submodule-config.h +++ b/submodule-config.h @@ -6,6 +6,7 @@ #include "hashmap.h" #include "submodule.h" #include "strbuf.h" +#include "tree-walk.h" /** * The submodule config cache API allows to read submodule @@ -101,4 +102,37 @@ int check_submodule_name(const char *name); void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules); void update_clone_config_from_gitmodules(int *max_jobs); +/* + * Submodule entry that contains relevant information about a + * submodule in a tree. + */ +struct submodule_tree_entry { + /* The submodule's tree entry. */ + struct name_entry *name_entry; + /* + * A struct repository corresponding to the submodule. May be + * NULL if the submodule has not been updated. + */ + struct repository *repo; + /* + * A struct submodule containing the submodule config in the + * tree's .gitmodules. + */ + const struct submodule *submodule; +}; + +struct submodule_entry_list { + struct submodule_tree_entry *entries; + int entry_nr; + int entry_alloc; +}; + +/** + * Given a treeish, return all submodules in the tree and its subtrees, + * but excluding nested submodules. Callers that require nested + * submodules are expected to recurse into the submodules themselves. + */ +void submodules_of_tree(struct repository *r, + const struct object_id *treeish_name, + struct submodule_entry_list *ret); #endif /* SUBMODULE_CONFIG_H */ diff --git a/submodule.c b/submodule.c index f3c99634a9..07ae7c266e 100644 --- a/submodule.c +++ b/submodule.c @@ -265,7 +265,9 @@ int option_parse_recurse_submodules_worktree_updater(const struct option *opt, * ie, the config looks like: "[submodule] active\n". * Since that is an invalid pathspec, we should inform the user. */ -int is_submodule_active(struct repository *repo, const char *path) +int is_tree_submodule_active(struct repository *repo, + const struct object_id *treeish_name, + const char *path) { int ret = 0; char *key = NULL; @@ -273,7 +275,7 @@ int is_submodule_active(struct repository *repo, const char *path) const struct string_list *sl; const struct submodule *module; - module = submodule_from_path(repo, null_oid(), path); + module = submodule_from_path(repo, treeish_name, path); /* early return if there isn't a path->module mapping */ if (!module) @@ -315,6 +317,11 @@ int is_submodule_active(struct repository *repo, const char *path) return ret; } +int is_submodule_active(struct repository *repo, const char *path) +{ + return is_tree_submodule_active(repo, null_oid(), path); +} + int is_submodule_populated_gently(const char *path, int *return_error_code) { int ret = 0; diff --git a/submodule.h b/submodule.h index 6bd2c99fd9..784ceffc0e 100644 --- a/submodule.h +++ b/submodule.h @@ -54,6 +54,9 @@ int git_default_submodule_config(const char *var, const char *value, void *cb); struct option; int option_parse_recurse_submodules_worktree_updater(const struct option *opt, const char *arg, int unset); +int is_tree_submodule_active(struct repository *repo, + const struct object_id *treeish_name, + const char *path); int is_submodule_active(struct repository *repo, const char *path); /* * Determine if a submodule has been populated at a given 'path' by checking if diff --git a/t/t3207-branch-submodule.sh b/t/t3207-branch-submodule.sh new file mode 100755 index 0000000000..047b0cd174 --- /dev/null +++ b/t/t3207-branch-submodule.sh @@ -0,0 +1,282 @@ +#!/bin/sh + +test_description='git branch submodule tests' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME + +. ./test-lib.sh +. "$TEST_DIRECTORY"/lib-rebase.sh + +pwd=$(pwd) + +# Creates a clean test environment in "pwd" by copying the repo setup +# from test_dirs. +reset_test () { + rm -fr super && + rm -fr sub-sub-upstream && + rm -fr sub-upstream && + cp -r test_dirs/* . +} + +test_expect_success 'setup superproject and submodule' ' + mkdir test_dirs && + ( + cd test_dirs && + git init super && + test_commit -C super foo && + git init sub-sub-upstream && + test_commit -C sub-sub-upstream foo && + git init sub-upstream && + # Submodule in a submodule + git -C sub-upstream submodule add "${pwd}/test_dirs/sub-sub-upstream" sub-sub && + git -C sub-upstream commit -m "add submodule" && + # Regular submodule + git -C super submodule add "${pwd}/test_dirs/sub-upstream" sub && + # Submodule in a subdirectory + git -C super submodule add "${pwd}/test_dirs/sub-sub-upstream" second/sub && + git -C super commit -m "add submodule" && + git -C super config submodule.propagateBranches true && + git -C super/sub submodule update --init + ) && + reset_test +' + +# Test the argument parsing +test_expect_success '--recurse-submodules should create branches' ' + test_when_finished "reset_test" && + ( + cd super && + git branch --recurse-submodules branch-a && + git rev-parse branch-a && + git -C sub rev-parse branch-a && + git -C sub/sub-sub rev-parse branch-a && + git -C second/sub rev-parse branch-a + ) +' + +test_expect_success '--recurse-submodules should die if submodule.propagateBranches is false' ' + test_when_finished "reset_test" && + ( + cd super && + echo "fatal: branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled" >expected && + test_must_fail git -c submodule.propagateBranches=false branch --recurse-submodules branch-a 2>actual && + test_cmp expected actual + ) +' + +test_expect_success '--recurse-submodules should fail when not creating branches' ' + test_when_finished "reset_test" && + ( + cd super && + git branch --recurse-submodules branch-a && + test_must_fail git branch --recurse-submodules -D branch-a && + # Assert that the branches were not deleted + git rev-parse branch-a && + git -C sub rev-parse branch-a + ) +' + +test_expect_success 'should respect submodule.recurse when creating branches' ' + test_when_finished "reset_test" && + ( + cd super && + git -c submodule.recurse=true branch branch-a && + git rev-parse branch-a && + git -C sub rev-parse branch-a + ) +' + +test_expect_success 'should ignore submodule.recurse when not creating branches' ' + test_when_finished "reset_test" && + ( + cd super && + git branch --recurse-submodules branch-a && + git -c submodule.recurse=true branch -D branch-a && + test_must_fail git rev-parse branch-a && + git -C sub rev-parse branch-a + ) +' + +# Test branch creation behavior +test_expect_success 'should create branches based off commit id in superproject' ' + test_when_finished "reset_test" && + ( + cd super && + git branch --recurse-submodules branch-a && + git checkout --recurse-submodules branch-a && + git -C sub rev-parse HEAD >expected && + # Move the tip of sub:branch-a so that it no longer matches the commit in super:branch-a + git -C sub checkout branch-a && + test_commit -C sub bar && + # Create a new branch-b branch with start-point=branch-a + git branch --recurse-submodules branch-b branch-a && + git rev-parse branch-b && + git -C sub rev-parse branch-b >actual && + # Assert that the commit id of sub:second-branch matches super:branch-a and not sub:branch-a + test_cmp expected actual + ) +' + +test_expect_success 'should not create any branches if branch is not valid for all repos' ' + test_when_finished "reset_test" && + ( + cd super && + git -C sub branch branch-a && + test_must_fail git branch --recurse-submodules branch-a 2>actual && + test_must_fail git rev-parse branch-a && + grep "submodule .sub.: fatal: A branch named .branch-a. already exists" actual + ) +' + +test_expect_success 'should create branches if branch exists and --force is given' ' + test_when_finished "reset_test" && + ( + cd super && + git -C sub rev-parse HEAD >expected && + test_commit -C sub baz && + # branch-a in sub now points to a newer commit. + git -C sub branch branch-a HEAD && + git -C sub rev-parse branch-a >actual-old-branch-a && + git branch --recurse-submodules --force branch-a && + git rev-parse branch-a && + git -C sub rev-parse branch-a >actual-new-branch-a && + test_cmp expected actual-new-branch-a && + # assert that branch --force actually moved the sub + # branch + ! test_cmp expected actual-old-branch-a + ) +' + +test_expect_success 'should create branch when submodule is not in HEAD:.gitmodules' ' + test_when_finished "reset_test" && + ( + cd super && + git branch branch-a && + git checkout -b branch-b && + git submodule add ../sub-upstream sub2 && + git -C sub2 submodule update --init && + # branch-b now has a committed submodule not in branch-a + git commit -m "add second submodule" && + git checkout branch-a && + git branch --recurse-submodules branch-c branch-b && + git checkout --recurse-submodules branch-c && + git -C sub2 rev-parse branch-c && + git -C sub2/sub-sub rev-parse branch-c + ) +' + +test_expect_success 'should not create branches in inactive submodules' ' + test_when_finished "reset_test" && + test_config -C super submodule.sub.active false && + ( + cd super && + git branch --recurse-submodules branch-a && + git rev-parse branch-a && + test_must_fail git -C sub branch-a + ) +' + +test_expect_success 'should set up tracking of local branches with track=always' ' + test_when_finished "reset_test" && + ( + cd super && + git -c branch.autoSetupMerge=always branch --recurse-submodules branch-a main && + git -C sub rev-parse main && + test "$(git -C sub config branch.branch-a.remote)" = . && + test "$(git -C sub config branch.branch-a.merge)" = refs/heads/main + ) +' + +test_expect_success 'should set up tracking of local branches with explicit track' ' + test_when_finished "reset_test" && + ( + cd super && + git branch --track --recurse-submodules branch-a main && + git -C sub rev-parse main && + test "$(git -C sub config branch.branch-a.remote)" = . && + test "$(git -C sub config branch.branch-a.merge)" = refs/heads/main + ) +' + +test_expect_success 'should not set up unnecessary tracking of local branches' ' + test_when_finished "reset_test" && + ( + cd super && + git branch --recurse-submodules branch-a main && + git -C sub rev-parse main && + test "$(git -C sub config branch.branch-a.remote)" = "" && + test "$(git -C sub config branch.branch-a.merge)" = "" + ) +' + +reset_remote_test () { + rm -fr super-clone && + reset_test +} + +test_expect_success 'setup tests with remotes' ' + ( + cd test_dirs && + ( + cd super && + git branch branch-a && + git checkout -b branch-b && + git submodule add ../sub-upstream sub2 && + # branch-b now has a committed submodule not in branch-a + git commit -m "add second submodule" + ) && + git clone --branch main --recurse-submodules super super-clone && + git -C super-clone config submodule.propagateBranches true + ) && + reset_remote_test +' + +test_expect_success 'should get fatal error upon branch creation when submodule is not in .git/modules' ' + test_when_finished "reset_remote_test" && + ( + cd super-clone && + # This should succeed because super-clone has sub in .git/modules + git branch --recurse-submodules branch-a origin/branch-a && + # This should fail because super-clone does not have sub2 .git/modules + test_must_fail git branch --recurse-submodules branch-b origin/branch-b 2>actual && + grep "fatal: submodule .sub2.: unable to find submodule" actual && + test_must_fail git rev-parse branch-b && + test_must_fail git -C sub rev-parse branch-b && + # User can fix themselves by initializing the submodule + git checkout origin/branch-b && + git submodule update --init --recursive && + git branch --recurse-submodules branch-b origin/branch-b + ) +' + +test_expect_success 'should set up tracking of remote-tracking branches' ' + test_when_finished "reset_remote_test" && + ( + cd super-clone && + git branch --recurse-submodules branch-a origin/branch-a && + test "$(git config branch.branch-a.remote)" = origin && + test "$(git config branch.branch-a.merge)" = refs/heads/branch-a && + # "origin/branch-a" does not exist for "sub", but it matches the refspec + # so tracking should be set up + test "$(git -C sub config branch.branch-a.remote)" = origin && + test "$(git -C sub config branch.branch-a.merge)" = refs/heads/branch-a && + test "$(git -C sub/sub-sub config branch.branch-a.remote)" = origin && + test "$(git -C sub/sub-sub config branch.branch-a.merge)" = refs/heads/branch-a + ) +' + +test_expect_success 'should not fail when unable to set up tracking in submodule' ' + test_when_finished "reset_remote_test" && + ( + cd super-clone && + git remote rename origin ex-origin && + git branch --recurse-submodules branch-a ex-origin/branch-a && + test "$(git config branch.branch-a.remote)" = ex-origin && + test "$(git config branch.branch-a.merge)" = refs/heads/branch-a && + test "$(git -C sub config branch.branch-a.remote)" = "" && + test "$(git -C sub config branch.branch-a.merge)" = "" + ) +' + +test_done From patchwork Mon Jan 24 20:44:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12722994 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 2E090C433F5 for ; Mon, 24 Jan 2022 22:33:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382573AbiAXWdw (ORCPT ); Mon, 24 Jan 2022 17:33:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355485AbiAXWRR (ORCPT ); Mon, 24 Jan 2022 17:17: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 93E28C04A2CD for ; Mon, 24 Jan 2022 12:45:12 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id t1-20020a6564c1000000b002e7f31cf59fso10508955pgv.14 for ; Mon, 24 Jan 2022 12:45:12 -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; bh=tdSBCdj83xP4iOMnUELbBmRWAjZFBy7Q3ekNtIBGSAo=; b=LQqJ/3iLQgMzC6sGHLIyqbfRIBw+4PNje5Z/hGNxLDplTGGXSoZIQyYscRF1+dFYFU LZdr65GgakPz2GEtkZEoaBZZAPf1ht8OMAuiZRm5NDJlpOM8oPlj0wFnbx8hBCtOMFKr kbDRq8boNCqPJncnbPJJXFYMEkO5mWCazMQAEYfVqFQa8gLbnhlTOkZVk1UGfVcjTBoA fUzM0qjCioBN38Ejf7hay+oer3wbrlbv1NuPYfYbooCGuJnMGJ98s+0uBTu1u92++me5 JkrJTBG9b7BvAEvl6tc1r/gVNtjgs8TEoKzDmTSQFjjzIzwZe6ctxXNaHkXRPJ98LVbt d1oA== 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; bh=tdSBCdj83xP4iOMnUELbBmRWAjZFBy7Q3ekNtIBGSAo=; b=gyNQxSVfNR545uIYJH064fTFRtHjyNYVo003MIH/stdC3AZHN0/nKQWc8VjpYXNi34 3XlYxA0qrrPOz7IEK0otLGdWUXwWKFsh7rrr8Oc3JC/mHglLU5XeRSQ1glUbNbocJPba RAXO6CGKhQ9Hu2yRFlW5bfkkkcIDRcN+TVasmhD+CbQoGjhQYHmV0yPNkC3vQem51Vji wC/klBQETkzIjhKxjDS0kzUntYhlcuWZBoCbIUpImgzd9ASy/0ijuha2GE2FnRzb1T8W ESrUU5iUnf9bb+xLFqck0crCCGiXeklvPZZLrEZZVnDcCvczvlKzN2JdQ7pvijPHY0XY DPRg== X-Gm-Message-State: AOAM532UBXspRLvaVO4iwscCUE0uxTTzKUSC96spEIEnJKgDqpIjozbT QX4ImLflkKiLgGIX6wMatXwYNrTvLmB/yVOodwPnrU4IOteP8oBOGKP0ohULgL9g9OA+nR2NxSm wf0auuTW/0BRYSDVt0s3Iol3sXeIoDifwrjhpXzg8Ln1m3mpCUSQP5gn6H5eueoo= X-Google-Smtp-Source: ABdhPJztf/8vqjGazrSJIoaciNJr8Pc5X76bMjgV6mTRfQMY2cwaAjiW/ioUtWL63NSHRNd0OMfNrlFBt6XH+w== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:9001:b0:14a:a1b2:1e6d with SMTP id a1-20020a170902900100b0014aa1b21e6dmr15819754plp.124.1643057111998; Mon, 24 Jan 2022 12:45:11 -0800 (PST) Date: Mon, 24 Jan 2022 12:44:42 -0800 In-Reply-To: <20220124204442.39353-1-chooglen@google.com> Message-Id: <20220124204442.39353-7-chooglen@google.com> Mime-Version: 1.0 References: <20211220233459.45739-1-chooglen@google.com> <20220124204442.39353-1-chooglen@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 6/6] branch.c: use 'goto cleanup' in setup_tracking() to fix memory leaks From: Glen Choo To: git@vger.kernel.org Cc: Glen Choo , Jonathan Tan , Josh Steadmon , Emily Shaffer , " =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= " , Philippe Blain , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Signed-off-by: Glen Choo --- branch.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/branch.c b/branch.c index be33fe09fa..1e9a585633 100644 --- a/branch.c +++ b/branch.c @@ -239,7 +239,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, if (track != BRANCH_TRACK_INHERIT) for_each_remote(find_tracked_branch, &tracking); else if (inherit_tracking(&tracking, orig_ref)) - return; + goto cleanup; if (!tracking.matches) switch (track) { @@ -249,7 +249,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, case BRANCH_TRACK_INHERIT: break; default: - return; + goto cleanup; } if (tracking.matches > 1) @@ -262,6 +262,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, tracking.remote, tracking.srcs) < 0) exit(-1); +cleanup: string_list_clear(tracking.srcs, 0); }