From patchwork Thu Dec 9 18:49:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12668073 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 38898C433FE for ; Thu, 9 Dec 2021 18:50:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229676AbhLISxd (ORCPT ); Thu, 9 Dec 2021 13:53:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229487AbhLISxc (ORCPT ); Thu, 9 Dec 2021 13:53:32 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02866C061746 for ; Thu, 9 Dec 2021 10:49:59 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id b11-20020a17090acc0b00b001a9179dc89fso6506677pju.6 for ; Thu, 09 Dec 2021 10:49:58 -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=E3LrP3r6xpljbSDxcgOQahsQREQN4Oaf3V26Y54PY+o=; b=ZH5F87Nr59P/RAxthskkmvteyHfPcRFbaXaoLcDyC1qOb8JJdJEaZGoH5XbWrwW+iQ Jq0wV0ljlF2DdtDrhq/g9eOPB/uq1VE/yczAcLlC0H4B46txbXf4pS6rQwOjdFGEXUPt falH3G4W1XTUAy0kstG6vOC/tfBUIQOJCldBc5XEtl2aAW5aerCN/RYri1L5Ymt15UH6 D+JH2vIq4clQ7XCzO1TsMlBQy4Lez13lEBc4mivicfM55FBLuUdczYJdX3QQkmiiTHCL YuDCZr9756u+6dZm3GfKAcCXN3Ri2bdGWyZ2p/6ETA85Eeh9S8IOLfnDZJtfc/bgF23T Rbfg== 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=E3LrP3r6xpljbSDxcgOQahsQREQN4Oaf3V26Y54PY+o=; b=qWBm9h1oKmbHd7C1e4F8nHOgHUU0Vlck403TaaBhSWDrmTuCbhgKYrATol2Z31icKy xTpZV1wbNo0gZrGm5rSwwMC/2Aoov4pOfLxMUfcDMZG7MPAVgP3c6tzI2mxLiCWO9dUV E2TiJWgpP6U+7xd5IzuPiW237pXLmlwXTZhvUcwkI4A7744Ar4GEhOGGcBRzr2PPRNgE 8NH/3FGxrUo4QZBKh6+VRbySrqivDWeuKwp8wovWocCQQ3xGd71XmoFpb2a/WVmVX7vx shwg1Q/+Rzn80e3NUvTS96UbaAM2rqszv/McltIDPr4W/ZXIRGos1TlHHMK746vi67qb 3i0w== X-Gm-Message-State: AOAM533rspWgSJ/L2D1cdxh1peAejlWtzFEozquHI3LdDJ6ZdzssFzGv LUQ6Jrmky7JYf+EOwyyLjq6iy4XVFrjgMMRwE0o5OPkyCAkcM4xzbqEwIqLFX0zbe60RCJCEwVw V3yTNbAt84BBX303owfZKqciciJ6xp2IjsW72Fuvjoblg7Vva/OSWGM1ez0wdr3I= X-Google-Smtp-Source: ABdhPJzSb1Bba/TWypEzuVa5F1CKyeCaHF6x9y4+0SdvBw2fD6nDBtiAWfr3Pr+uH/O3uQSkerhJB55STjCvdQ== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:9b8f:b0:142:7999:67fb with SMTP id y15-20020a1709029b8f00b00142799967fbmr69215931plp.24.1639075798413; Thu, 09 Dec 2021 10:49:58 -0800 (PST) Date: Thu, 9 Dec 2021 10:49:24 -0800 In-Reply-To: <20211209184928.71413-1-chooglen@google.com> Message-Id: <20211209184928.71413-2-chooglen@google.com> Mime-Version: 1.0 References: <20211206215528.97050-1-chooglen@google.com> <20211209184928.71413-1-chooglen@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v3 1/5] branch: move --set-upstream-to behavior to 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 refactor is motivated by a desire to add a "dry_run" parameter to create_branch() that will validate whether or not a branch can be created without actually creating it - this behavior be used in a subsequent commit that adds `git branch --recurse-submodules topic`. Adding "dry_run" is not obvious because create_branch() is also used to set tracking information without creating a branch, i.e. when using --set-upstream-to. This appears to be a leftover from 4fc5006676 (Add branch --set-upstream, 2010-01-18), when --set-upstream would sometimes create a branch and sometimes update tracking information without creating a branch. However, we no longer support --set-upstream, so it makes more sense to set tracking information with another function, like setup_tracking(), and use create_branch() only to create branches. When this is done, it will be trivial to add "dry_run". Do this refactor by moving the branch validation and dwim logic from create_branch() into a new function, validate_branch_start(), and call it from setup_tracking(). Now that setup_tracking() can perform dwim and tracking setup without creating a branch, use it in `git branch --set-upstream-to` and remove unnecessary behavior from create_branch(). Since there were none, add tests for creating a branch with `--force`. Signed-off-by: Glen Choo --- branch.c | 147 +++++++++++++++++++++++++++------------------- branch.h | 13 +++- builtin/branch.c | 7 +-- t/t3200-branch.sh | 17 ++++++ 4 files changed, 117 insertions(+), 67 deletions(-) diff --git a/branch.c b/branch.c index 29e835d367..9429936734 100644 --- a/branch.c +++ b/branch.c @@ -209,20 +209,26 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref) return 0; } -/* - * 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. - */ -static void setup_tracking(const char *new_ref, const char *orig_ref, - enum branch_track track, int quiet) +static void validate_branch_start(struct repository *r, const char *start_name, + enum branch_track track, + struct object_id *oid, char **real_ref); + +void setup_tracking(const char *new_ref, const char *orig_ref, + enum branch_track track, int quiet, int expand_orig) { struct tracking tracking; struct string_list tracking_srcs = STRING_LIST_INIT_DUP; int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; + char *full_orig_ref; + struct object_id unused_oid; memset(&tracking, 0, sizeof(tracking)); - tracking.spec.dst = (char *)orig_ref; + if (expand_orig) + validate_branch_start(the_repository, orig_ref, track, &unused_oid, &full_orig_ref); + else + full_orig_ref = xstrdup(orig_ref); + + tracking.spec.dst = full_orig_ref; tracking.srcs = &tracking_srcs; if (track != BRANCH_TRACK_INHERIT) for_each_remote(find_tracked_branch, &tracking); @@ -236,20 +242,22 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, case BRANCH_TRACK_OVERRIDE: break; default: - return; + goto cleanup; } if (tracking.matches > 1) die(_("Not tracking: ambiguous information for ref %s"), - orig_ref); + full_orig_ref); if (tracking.srcs->nr < 1) - string_list_append(tracking.srcs, orig_ref); + string_list_append(tracking.srcs, full_orig_ref); if (install_branch_config_multiple_remotes(config_flags, new_ref, tracking.remote, tracking.srcs) < 0) exit(-1); +cleanup: string_list_clear(tracking.srcs, 0); + free(full_orig_ref); } int read_branch_desc(struct strbuf *buf, const char *branch_name) @@ -332,33 +340,35 @@ 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) +/** + * Validates whether a ref is a valid starting point for a branch, 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 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) + * + * - oid is an out parameter containing the object_id of start_name + * + * - real_ref is an out parameter containing the full, 'real' form of + * start_name e.g. refs/heads/main instead of main + * + */ +static void validate_branch_start(struct repository *r, const char *start_name, + enum branch_track track, + struct object_id *oid, char **real_ref) { 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 (repo_get_oid_mb(r, start_name, oid)) { if (explicit_tracking) { if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) { error(_(upstream_missing), start_name); @@ -370,7 +380,8 @@ void create_branch(struct repository *r, die(_("Not a valid object name: '%s'."), start_name); } - switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) { + switch (repo_dwim_ref(r, start_name, strlen(start_name), oid, real_ref, + 0)) { case 0: /* Not branching from any existing branch */ if (explicit_tracking) @@ -378,12 +389,12 @@ void create_branch(struct repository *r, break; case 1: /* Unique completion -- good, only if it is a real branch */ - if (!starts_with(real_ref, "refs/heads/") && - validate_remote_tracking_branch(real_ref)) { + if (!starts_with(*real_ref, "refs/heads/") && + validate_remote_tracking_branch(*real_ref)) { if (explicit_tracking) die(_(upstream_not_branch), start_name); else - FREE_AND_NULL(real_ref); + FREE_AND_NULL(*real_ref); } break; default: @@ -391,37 +402,55 @@ void create_branch(struct repository *r, break; } - if ((commit = lookup_commit_reference(r, &oid)) == NULL) + if ((commit = lookup_commit_reference(r, oid)) == NULL) die(_("Not a valid branch point: '%s'."), start_name); - oidcpy(&oid, &commit->object.oid); + oidcpy(oid, &commit->object.oid); +} + +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; + struct ref_transaction *transaction; + struct strbuf err = STRBUF_INIT; + char *msg; + + 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; + } + + validate_branch_start(r, start_name, track, &oid, &real_ref); 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); + setup_tracking(ref.buf + 11, real_ref, track, quiet, 0); strbuf_release(&ref); free(real_ref); diff --git a/branch.h b/branch.h index 6484bda8a2..46951c446b 100644 --- a/branch.h +++ b/branch.h @@ -18,6 +18,15 @@ extern enum branch_track git_branch_track; /* Functions for acting on the information about branches. */ +/* + * This sets the branch..{remote,merge} config settings so that + * branch 'new_ref' tracks 'orig_ref'. This is called when branches are + * created, or when branch configs are updated (e.g. with + * git branch --set-upstream-to). + */ +void setup_tracking(const char *new_ref, const char *orig_ref, + enum branch_track track, int quiet, int expand_orig); + /* * Creates a new branch, where: * @@ -30,8 +39,8 @@ extern enum branch_track git_branch_track; * * - 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/builtin/branch.c b/builtin/branch.c index 81a29edb4a..e19aab5356 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -823,12 +823,7 @@ 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); + setup_tracking(branch->name, new_upstream, BRANCH_TRACK_OVERRIDE, quiet, 1); } else if (unset_upstream) { struct branch *branch = branch_get(argv[0]); struct strbuf buf = STRBUF_INIT; diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 267a624671..18e285a876 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 Thu Dec 9 18:49:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12668075 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 78C1FC433EF for ; Thu, 9 Dec 2021 18:50:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229693AbhLISxg (ORCPT ); Thu, 9 Dec 2021 13:53:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229681AbhLISxe (ORCPT ); Thu, 9 Dec 2021 13:53:34 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20FE3C061746 for ; Thu, 9 Dec 2021 10:50:01 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id p8-20020a17090a748800b001a6cceee8afso4121237pjk.4 for ; Thu, 09 Dec 2021 10:50:01 -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=8gDIpSjgsxdkrFB+PSm70WgF5BKfUXhUrmfWPrnp41w=; b=RUxW3VZZtV7CxND5V60iH87x8UWN/VJ9D0LyVOOMyo9YrZVVtve52o7LCamp6EDx6b a5ewwdtETch058urRJ5mJ2scJjjkZLmWiI3FG0H8gO5tiYe4fnIfyfviC+rH23KrTTQ8 4BB6ifCrwO6LTRnWEmqRELef3Y/Oc+6MidQi8tIs7sOdjjHAJsP8r13Y00fv3l/ICfl7 +YhXNPFWq+8UfRyjbMvpPXDTOjiaFwFednxRnqS05GgHg50WCwhESZCrUBfftbswuPK/ RvJkccYL3D5r/eeidESYWKYNk9g1usWLXxyt0tkpFvFrg/CloLGc9Mn/9etL2AprtQKi kDsA== 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=8gDIpSjgsxdkrFB+PSm70WgF5BKfUXhUrmfWPrnp41w=; b=LBEveM/7cATecxhObl2dBCmqs0/Qvyuk9f0WBmBegM9C6W/8UILwNz5Jr6hCeommzA Bp3+c0g/LfD40Qpwj0RduKS8nx3WbUsMclknyq91UBrP/DOU9yLCUveJtNsn+gQwHVH6 1t50UmLQQx/ygiyDQ7D29mNBKDrW9SkOwq6EyPgePfa1U2B244uZAq1GO0GJU1SND9bH yz4PnUB8ccy7vm8XwyhfHsz5huMg1npPwLJvc/da1CK+D0TEwLPMbaF+W8xFCn1E6UL5 j9qyBqcng7VFhZu/s508+zprcqw8atPPoVSNlR4ucRunz4d9BZRfSI2pL8kvWq3yvHu5 KzfA== X-Gm-Message-State: AOAM530VQszj9RKMNpxj2bxYGQfI+di4njpU+Lz2kTJQjLAJ9ZqGez+X 9fYijJTlsEKNf4U57uGTx6MPX7NdhB8Dtjwxv4XeIiHkG4+4hF7RYF3Tn4fRaaOZFWnPxBdgkLO jbSNHozUom86gf58G30phIwI3NySat2YkdZyhXxv7eYwdFsR0EoNFEKsr37m9Ypo= X-Google-Smtp-Source: ABdhPJyuYZ9YrJI4zKHdXTe6oLAYELUMgzxuad43d983+9GhVjakZhXprUPvaTihgIFTgphAtxqxGWSgl+qFBw== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a62:14c7:0:b0:4a2:a6f2:2227 with SMTP id 190-20020a6214c7000000b004a2a6f22227mr13463490pfu.22.1639075800447; Thu, 09 Dec 2021 10:50:00 -0800 (PST) Date: Thu, 9 Dec 2021 10:49:25 -0800 In-Reply-To: <20211209184928.71413-1-chooglen@google.com> Message-Id: <20211209184928.71413-3-chooglen@google.com> Mime-Version: 1.0 References: <20211206215528.97050-1-chooglen@google.com> <20211209184928.71413-1-chooglen@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v3 2/5] branch: remove forward declaration of validate_branch_start() 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 In the previous commit, validate_branch_start() was forward declared in order to preserve the function order and minimize the diff. Since the forward declaration is no longer needed, remove it by moving setup_tracking() to the appropriate position. Signed-off-by: Glen Choo --- This patch is logically part of the previous patch because it just cleans up the artificial forward declaration that exists only to shrink the diff for reviewers. As such, if/when this series is merged, I would prefer for this patch to be squashed with the previous one. branch.c | 98 +++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/branch.c b/branch.c index 9429936734..6b9d64cdf9 100644 --- a/branch.c +++ b/branch.c @@ -209,57 +209,6 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref) return 0; } -static void validate_branch_start(struct repository *r, const char *start_name, - enum branch_track track, - struct object_id *oid, char **real_ref); - -void setup_tracking(const char *new_ref, const char *orig_ref, - enum branch_track track, int quiet, int expand_orig) -{ - struct tracking tracking; - struct string_list tracking_srcs = STRING_LIST_INIT_DUP; - int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; - char *full_orig_ref; - struct object_id unused_oid; - - memset(&tracking, 0, sizeof(tracking)); - if (expand_orig) - validate_branch_start(the_repository, orig_ref, track, &unused_oid, &full_orig_ref); - else - full_orig_ref = xstrdup(orig_ref); - - tracking.spec.dst = full_orig_ref; - tracking.srcs = &tracking_srcs; - if (track != BRANCH_TRACK_INHERIT) - for_each_remote(find_tracked_branch, &tracking); - else if (inherit_tracking(&tracking, orig_ref)) - return; - - if (!tracking.matches) - switch (track) { - case BRANCH_TRACK_ALWAYS: - case BRANCH_TRACK_EXPLICIT: - case BRANCH_TRACK_OVERRIDE: - break; - default: - goto cleanup; - } - - if (tracking.matches > 1) - die(_("Not tracking: ambiguous information for ref %s"), - full_orig_ref); - - if (tracking.srcs->nr < 1) - string_list_append(tracking.srcs, full_orig_ref); - if (install_branch_config_multiple_remotes(config_flags, new_ref, tracking.remote, - tracking.srcs) < 0) - exit(-1); - -cleanup: - string_list_clear(tracking.srcs, 0); - free(full_orig_ref); -} - int read_branch_desc(struct strbuf *buf, const char *branch_name) { char *v = NULL; @@ -407,6 +356,53 @@ static void validate_branch_start(struct repository *r, const char *start_name, oidcpy(oid, &commit->object.oid); } +void setup_tracking(const char *new_ref, const char *orig_ref, + enum branch_track track, int quiet, int expand_orig) +{ + struct tracking tracking; + struct string_list tracking_srcs = STRING_LIST_INIT_DUP; + int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; + char *full_orig_ref; + struct object_id unused_oid; + + memset(&tracking, 0, sizeof(tracking)); + if (expand_orig) + validate_branch_start(the_repository, orig_ref, track, &unused_oid, &full_orig_ref); + else + full_orig_ref = xstrdup(orig_ref); + + tracking.spec.dst = full_orig_ref; + tracking.srcs = &tracking_srcs; + if (track != BRANCH_TRACK_INHERIT) + for_each_remote(find_tracked_branch, &tracking); + else if (inherit_tracking(&tracking, orig_ref)) + return; + + if (!tracking.matches) + switch (track) { + case BRANCH_TRACK_ALWAYS: + case BRANCH_TRACK_EXPLICIT: + case BRANCH_TRACK_OVERRIDE: + break; + default: + goto cleanup; + } + + if (tracking.matches > 1) + die(_("Not tracking: ambiguous information for ref %s"), + full_orig_ref); + + if (tracking.srcs->nr < 1) + string_list_append(tracking.srcs, full_orig_ref); + if (install_branch_config_multiple_remotes(config_flags, new_ref, tracking.remote, + tracking.srcs) < 0) + exit(-1); + +cleanup: + string_list_clear(tracking.srcs, 0); + free(full_orig_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) From patchwork Thu Dec 9 18:49:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12668077 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 84B72C433F5 for ; Thu, 9 Dec 2021 18:50:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229688AbhLISxh (ORCPT ); Thu, 9 Dec 2021 13:53:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbhLISxg (ORCPT ); Thu, 9 Dec 2021 13:53:36 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA10EC061A32 for ; Thu, 9 Dec 2021 10:50:02 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id n22-20020a6563d6000000b0029261ffde9bso3688218pgv.22 for ; Thu, 09 Dec 2021 10:50: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=s77Sk63FkyMGDOdpbDwa6E11AC4W+xYYzmEtXBSi5rU=; b=DyA91FiLkiZChPUDm8iZt7zBk1VACOxij94/NdIuj95SZPcZcYlC34eFEJEMiA00QH c3NcfN2U79WNQ+286WNV598BBJb0eroD4SURHMjhzXSzLusCzD04WsEsJ3AvULdzKKHP 7/XEwh43kcTKAcy3pCvOFLpDhZc6mmMPpaRHBFrSRfEOwRhR9w1MqanLy7qao5yoJF7F LDbyDGI8UkrSLYSNLutg0Xlt0dEJxJ2vsiCIWb1NcFvw/yRqMlEt8g0Zp3Ej07wdm+Xl gJ4mKfmf21z3E6ey+ZzYRO5LHiwKuXhuuH+X+gQnpSS7/2AahntoJXlEQBPPpanOTJlQ pC+g== 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=s77Sk63FkyMGDOdpbDwa6E11AC4W+xYYzmEtXBSi5rU=; b=qILPx2jddy9Au3m1UZhe0n9hItDDnl4NSmx6KGe2aomnLuQ9MaOc3IuCvdPvxjZBe+ DZu77uW+S1Hx2WRZsxFBDOsHIhwokvgZFZlwS2zUD+w1A+MmnCkzeYYqkUWPAKF+X4Do r4s8TkKmQbpKXSKezcX1QisPd5v9FVo7ORfhnGYdI1VdbJIk3EHoRKG6xEc4UXE7+v0r nlbRBkbZ06q6oJ9MR36olCJgTr0muaylJUv4/BwKBCTM/RmS8cvtTjSpxRChKgCjzAVs G/Muqzb9TIs04ZoHc/K5oabJFnNw7mrKEfhaPzl5s1EIuxiStahr/npsIy/Qjw4ZdgFK JnDw== X-Gm-Message-State: AOAM5335nBi2gqkc+B39rPuGSKR4e1C51yjqZjkQCZ1pXz6i9jXhx2Vl C6or6eZmzLHiMkBiODO1QpweqH/RoS9ix7IKvG0mFFlckEP9NrMQv3gmwYtAiwNOrXdmtZL/Ste v/WibuhxH8Xnk8BrnIfoZhoYmcYVDoRsdx1ceJ3s37gk2HwMJU3SpjFFZUf9jnd0= X-Google-Smtp-Source: ABdhPJyPistYGjDScFqbWCUk5JNr1SAPhFcl4dMxqq8tfRZWJD7hqXv85FIP5/HUdwMH5h9+ZYYmwEcG8v6vPQ== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a63:495b:: with SMTP id y27mr36191664pgk.601.1639075802322; Thu, 09 Dec 2021 10:50:02 -0800 (PST) Date: Thu, 9 Dec 2021 10:49:26 -0800 In-Reply-To: <20211209184928.71413-1-chooglen@google.com> Message-Id: <20211209184928.71413-4-chooglen@google.com> Mime-Version: 1.0 References: <20211206215528.97050-1-chooglen@google.com> <20211209184928.71413-1-chooglen@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v3 3/5] builtin/branch: clean up 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 Add a variable to cmd_branch() that will tell us whether or not cmd_branch() will default to creating a branch (instead of performing another action). 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. 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). Signed-off-by: Glen Choo --- builtin/branch.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index e19aab5356..14aff33a50 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -27,7 +27,7 @@ static const char * const builtin_branch_usage[] = { N_("git branch [] [-r | -a] [--merged] [--no-merged]"), - N_("git branch [] [-l] [-f] []"), + N_("git branch [] [-l] [...]"), N_("git branch [] [-r] (-d | -D) ..."), N_("git branch [] (-m | -M) [] "), N_("git branch [] (-c | -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, force = 0, list = 0, + unset_upstream = 0, show_current = 0, edit_description = 0; + int noncreate_actions = 0; + /* possible options */ + int reflog = 0, quiet = 0, icase = 0; const char *new_upstream = NULL; enum branch_track track; struct ref_filter filter; - int icase = 0; static struct ref_sorting *sorting; struct string_list sorting_options = STRING_LIST_INIT_DUP; struct ref_format format = REF_FORMAT_INIT; @@ -708,8 +709,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) @@ -847,7 +850,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 Thu Dec 9 18:49:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12668079 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 53BA3C433F5 for ; Thu, 9 Dec 2021 18:50:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229706AbhLISxj (ORCPT ); Thu, 9 Dec 2021 13:53:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbhLISxi (ORCPT ); Thu, 9 Dec 2021 13:53:38 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC92DC061746 for ; Thu, 9 Dec 2021 10:50:04 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id w6-20020aa79a06000000b004b0b8817661so918228pfj.19 for ; Thu, 09 Dec 2021 10:50: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=is8+CIDCYsKEYNdNdpLdADDNT2tz43DlDj1jOgAYb6A=; b=tMtMMY4DCEJDE8fLPgQjyH0Fk9zjoJ7Q5+UMOyTfSYjsWb1xer/HY5DQDtQ4Dflygx nteIgY4Xa8F1SFQy2zBX3aamv7x0sj0EJjSsMHWzuV3pKdjzuN2MZFsgm3NvH2Rlylo8 3DMC9SAVWkAPauyCkQFal2OBV2RGdfo2Ty//CHV2vDErvpC77etHZ9gGLPjSByxxwj5T /Ycz/f+smpvq+PT8McbvlR506Z7uyxIQY6y3H8NgKNtliBKeGxC5fGr9heMjX7/MD3tx CVKIHIhvqewcI1LWBJuMXsjmCJZlQAZrX/eycctbDOmIFSsalVEdYAh/eRkfr4C1STsf 92nQ== 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=is8+CIDCYsKEYNdNdpLdADDNT2tz43DlDj1jOgAYb6A=; b=vZmxxW+7GEkNBxHSLdT4dKS4tEAQK3mT0c4kMo/oFOIJa/tYglc7k7zxjT8V69QKNy x+qe17bX1AWWKq4gFZsm0CiHnLQsGR08YazAUk1v97UqdmsoSn/tQ6icFQnLok7jltTO xmUNYVwYb67XE/QlslpgscaZ15gPDTEN0Q7+jK95Xh4DxiTULZAUQCKmF8oOe4zI3lan 85gawSFcYilEEC9IqB1Qx+E4LXO2q9zKA6mARB0YKtieZTYNVXPpLqcTzVhVC0/xBwpu bVT2i08pX7MMLKnxq4ljXl/cH6wHTN6jbtrBFNzd2ucpVPuISCN3MlHLzoijUmKo+53V O5Cg== X-Gm-Message-State: AOAM531KkIqAkyiOeDTFkBgdNA2CLcALcBrPdiX7DGVHWooCQSnZAt66 V3p9TD6fiGdP68vTlqPv0Uo6/7Gjac6Oh4WzTExHbpuWF3CgWcyqTklh+lIlyzjvd/fNnhbD6Ln Hx1zw4jLa3pz75XzlMzC9RX/KDvfq3h7iiOV3JxVLZ8/WUba9qRvz3/JRa0lIu3w= X-Google-Smtp-Source: ABdhPJygKhIcgK6s1jIPAsjpOzduA9FM9UogS9yC6LTlhSwqHP4LZzuRmlhvQVN3VKmGQB51lGz2tM7Y87kKvg== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:90a:e005:: with SMTP id u5mr17519402pjy.17.1639075804301; Thu, 09 Dec 2021 10:50:04 -0800 (PST) Date: Thu, 9 Dec 2021 10:49:27 -0800 In-Reply-To: <20211209184928.71413-1-chooglen@google.com> Message-Id: <20211209184928.71413-5-chooglen@google.com> Mime-Version: 1.0 References: <20211206215528.97050-1-chooglen@google.com> <20211209184928.71413-1-chooglen@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v3 4/5] 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 semantics e.g. `git checkout` does not recursively checkout the expected branches created by `git branch` yet. To ensure that the correct set of semantics is used, this commit introduces a new configuration value, `submodule.propagateBranches`, which enables submodule branching when true (defaults to false). This commit includes changes that allow Git to work with submodules that are in trees (and not just the index): * 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 Other changes * add a "dry_run" parameter to create_branch() in order to support `git submodule--helper create-branch --dry-run` Signed-off-by: Glen Choo --- Documentation/config/advice.txt | 3 + Documentation/config/submodule.txt | 8 + advice.c | 1 + advice.h | 1 + branch.c | 129 ++++++++++++- branch.h | 31 +++- builtin/branch.c | 40 +++- builtin/checkout.c | 3 +- builtin/submodule--helper.c | 38 ++++ submodule-config.c | 35 ++++ submodule-config.h | 35 ++++ submodule.c | 11 +- submodule.h | 3 + t/t3207-branch-submodule.sh | 284 +++++++++++++++++++++++++++++ 14 files changed, 609 insertions(+), 13 deletions(-) create mode 100755 t/t3207-branch-submodule.sh diff --git a/Documentation/config/advice.txt b/Documentation/config/advice.txt index 063eec2511..e52262dc69 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` 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..52b35964c0 100644 --- a/Documentation/config/submodule.txt +++ b/Documentation/config/submodule.txt @@ -72,6 +72,14 @@ submodule.recurse:: For these commands a workaround is to temporarily change the configuration value by using `git -c submodule.recurse=0`. +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. + submodule.fetchJobs:: Specifies how many submodules are fetched/cloned at the same time. A positive integer allows up to that number of submodules fetched 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 6b9d64cdf9..305154de0b 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; @@ -405,7 +407,7 @@ void setup_tracking(const char *new_ref, const char *orig_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) + int reflog, int quiet, enum branch_track track, int dry_run) { struct object_id oid; char *real_ref; @@ -425,6 +427,8 @@ void create_branch(struct repository *r, const char *name, } validate_branch_start(r, start_name, track, &oid, &real_ref); + if (dry_run) + goto cleanup; if (reflog) log_all_ref_updates = LOG_REFS_NORMAL; @@ -448,10 +452,133 @@ void create_branch(struct repository *r, const char *name, if (real_ref && track) setup_tracking(ref.buf + 11, real_ref, track, quiet, 0); +cleanup: strbuf_release(&ref); free(real_ref); } +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); + 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. */ + validate_branch_start(r, start_name, BRANCH_TRACK_NEVER, &super_oid, + &branch_point); + + /* + * 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, 0); + + 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 46951c446b..8e28a9507f 100644 --- a/branch.h +++ b/branch.h @@ -49,12 +49,35 @@ void setup_tracking(const char *new_ref, const char *orig_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); +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 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 14aff33a50..9adbf70aa7 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -38,6 +38,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] = { @@ -99,6 +101,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); } @@ -621,7 +632,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) unset_upstream = 0, show_current = 0, edit_description = 0; int noncreate_actions = 0; /* possible options */ - int reflog = 0, quiet = 0, icase = 0; + int reflog = 0, quiet = 0, icase = 0, recurse_submodules_explicit = 0; const char *new_upstream = NULL; enum branch_track track; struct ref_filter filter; @@ -673,6 +684,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(), }; @@ -715,6 +727,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; @@ -851,6 +874,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 ?")); @@ -858,10 +884,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); - + 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/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); } diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index e630f0c730..44b6283c08 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; @@ -3389,6 +3426,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..f246359d63 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,40 @@ const struct submodule *submodule_from_path(struct repository *r, return config_from(r->submodule_cache, treeish_name, path, lookup_path); } +void submodules_of_tree(struct repository *r, + 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; + + name_entry = xmalloc(sizeof(*name_entry)); + + CALLOC_ARRAY(out->entries, 0); + out->entry_nr = 0; + out->entry_alloc = 0; + + fill_tree_descriptor(r, &tree, treeish_name); + while (tree_entry(&tree, name_entry)) { + if (!S_ISGITLINK(name_entry->mode) || !is_tree_submodule_active(r, treeish_name, name_entry->path)) { + continue; + } + + st_entry = xmalloc(sizeof(*st_entry)); + st_entry->name_entry = name_entry; + st_entry->submodule = + submodule_from_path(r, treeish_name, name_entry->path); + st_entry->repo = xmalloc(sizeof(*st_entry->repo)); + if (repo_submodule_init(st_entry->repo, r, name_entry->path, + treeish_name)) + FREE_AND_NULL(st_entry->repo); + + ALLOC_GROW(out->entries, out->entry_nr + 1, out->entry_alloc); + out->entries[out->entry_nr++] = *st_entry; + } +} + void submodule_free(struct repository *r) { if (r->submodule_cache) diff --git a/submodule-config.h b/submodule-config.h index 65875b94ea..56c12af03f 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,38 @@ 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. This only reads + * one level of the tree, so it will not return nested submodules; + * callers that require nested submodules are expected to handle the + * recursion 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 c689070524..5ace18a7d9 100644 --- a/submodule.c +++ b/submodule.c @@ -267,7 +267,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; @@ -275,7 +277,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) @@ -317,6 +319,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..2dd0e2b01f --- /dev/null +++ b/t/t3207-branch-submodule.sh @@ -0,0 +1,284 @@ +#!/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 + +test_expect_success 'setup superproject and submodule' ' + git init super && + test_commit foo && + git init sub-sub-upstream && + test_commit -C sub-sub-upstream foo && + git init sub-upstream && + git -C sub-upstream submodule add "$TRASH_DIRECTORY/sub-sub-upstream" sub-sub && + git -C sub-upstream commit -m "add submodule" && + git -C super submodule add "$TRASH_DIRECTORY/sub-upstream" sub && + git -C super commit -m "add submodule" && + git -C super config submodule.propagateBranches true && + git -C super/sub submodule update --init +' + +CLEANUP_SCRIPT_PATH="$TRASH_DIRECTORY/cleanup_branches.sh" + +cat >"$CLEANUP_SCRIPT_PATH" <<'EOF' + #!/bin/sh + + super_dir="$1" + shift + ( + cd "$super_dir" && + git checkout main && + for branch_name in "$@"; do + git branch -D "$branch_name" + git submodule foreach "$TRASH_DIRECTORY/cleanup_branches.sh . $branch_name || true" + done + ) +EOF +chmod +x "$CLEANUP_SCRIPT_PATH" + +cleanup_branches() { + TRASH_DIRECTORY="\"$TRASH_DIRECTORY\"" "$CLEANUP_SCRIPT_PATH" "$@" +} >/dev/null 2>/dev/null + +# Test the argument parsing +test_expect_success '--recurse-submodules should create branches' ' + test_when_finished "cleanup_branches super branch-a" && + ( + 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 + ) +' + +test_expect_success '--recurse-submodules should die if submodule.propagateBranches is false' ' + test_when_finished "cleanup_branches super branch-a" && + ( + 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 "cleanup_branches super branch-a" && + ( + 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 --abbrev-ref branch-a && + git -C sub rev-parse --abbrev-ref branch-a + ) +' + +test_expect_success 'should respect submodule.recurse when creating branches' ' + test_when_finished "cleanup_branches super branch-a" && + ( + cd super && + git -c submodule.recurse=true branch branch-a && + git rev-parse --abbrev-ref branch-a && + git -C sub rev-parse --abbrev-ref branch-a + ) +' + +test_expect_success 'should ignore submodule.recurse when not creating branches' ' + test_when_finished "cleanup_branches super branch-a" && + ( + cd super && + git branch --recurse-submodules branch-a && + git -c submodule.recurse=true branch -D branch-a && + test_must_fail git rev-parse --abbrev-ref branch-a && + git -C sub rev-parse --abbrev-ref branch-a + ) +' + +# Test branch creation behavior +test_expect_success 'should create branches based off commit id in superproject' ' + test_when_finished "cleanup_branches super branch-a branch-b" && + ( + 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 "cleanup_branches super branch-a" && + ( + 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 && + + cat >expected <<-EOF && + submodule ${SQ}sub${SQ}: fatal: A branch named ${SQ}branch-a${SQ} already exists. + fatal: submodule ${SQ}sub${SQ}: cannot create branch ${SQ}branch-a${SQ} + EOF + test_cmp expected actual + ) +' + +test_expect_success 'should create branches if branch exists and --force is given' ' + test_when_finished "cleanup_branches super branch-a" && + ( + cd super && + git -C sub rev-parse HEAD >expected && + test_commit -C sub baz && + git -C sub branch branch-a HEAD~1 && + git branch --recurse-submodules --force branch-a && + git rev-parse branch-a && + # assert that sub:branch-a was moved + git -C sub rev-parse branch-a >actual && + test_cmp expected actual + ) +' + +test_expect_success 'should create branch when submodule is not in HEAD .gitmodules' ' + test_when_finished "cleanup_branches super branch-a branch-b branch-c" && + ( + 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 rev-parse branch-c && + git -C sub rev-parse branch-c && + git checkout --recurse-submodules branch-c && + git -C sub2 rev-parse branch-c + ) +' + +test_expect_success 'should set up tracking of local branches with track=always' ' + test_when_finished "cleanup_branches super branch-a" && + ( + 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 "cleanup_branches super branch-a" && + ( + 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 "cleanup_branches super branch-a" && + ( + 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)" = "" + ) +' + +test_expect_success 'should not create branches in inactive submodules' ' + test_when_finished "cleanup_branches super branch-a" && + 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 'setup remote-tracking tests' ' + ( + 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 +' + +test_expect_success 'should not create branch when submodule is not in .git/modules' ' + # The cleanup needs to delete sub2 separately because main does not have sub2 + test_when_finished "git -C super-clone/sub2 branch -D branch-b && \ + git -C super-clone/sub2/sub-sub branch -D branch-b && \ + cleanup_branches super-clone branch-a branch-b" && + ( + cd super-clone && + # This should succeed because super-clone has sub. + git branch --recurse-submodules branch-a origin/branch-a && + # This should fail because super-clone does not have sub2. + test_must_fail git branch --recurse-submodules branch-b origin/branch-b 2>actual && + cat >expected <<-EOF && + hint: You may try updating the submodules using ${SQ}git checkout origin/branch-b && git submodule update --init${SQ} + fatal: submodule ${SQ}sub2${SQ}: unable to find submodule + EOF + test_cmp expected 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 "cleanup_branches super-clone branch-a" && + ( + 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 "cleanup_branches super-clone branch-a && \ + git -C super-clone remote rename ex-origin origin" && + ( + 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 Thu Dec 9 18:49:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12668081 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 6B924C433EF for ; Thu, 9 Dec 2021 18:50:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229750AbhLISxp (ORCPT ); Thu, 9 Dec 2021 13:53:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229711AbhLISxk (ORCPT ); Thu, 9 Dec 2021 13:53:40 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC547C061A32 for ; Thu, 9 Dec 2021 10:50:06 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id r23-20020a17090a941700b001a74be6cf80so4131784pjo.2 for ; Thu, 09 Dec 2021 10:50: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=ceiIiKjHHxs4kxfdiDQ7XtGkI2rq7i8hS9RoA31blxQ=; b=S7XSeutx7ZlfCht1BwNDMwHLZAF+qbdPHDIGVE7oG/DtgGQqM+CQ95NTMvaWm2p9tk DEcXJf1oUgidauHBkgda8vX+4bbTQtTZZCiJLsUcuslfjJGOykTrVE0E1TyFowihepJk faZqdoxxmzZW07VPerFxliHRsGGMOaKu3xbW7JouUcoltgb7NS2wODXnCkegNReZ7D+0 J5qwyGgBosu1Ylw9r+0NDYI6ggRNbBNj4G4B8WOK6p3DJAWmUuLG8AUNPNwrcIs0CH7I YZaQzTtMM+4namlLRiOH4hSHBiM+VkWFOsEr1Ese3H/sqhZKWcPjl8ln5gsLB9sHVpHE LnMQ== 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=ceiIiKjHHxs4kxfdiDQ7XtGkI2rq7i8hS9RoA31blxQ=; b=i2CHkOPxUfw33cuk6AYjVOQvm2J2HKzvaLE73KYt4+65Soe09zNfDHbU9X0APNqQKv Trv/nAjhe3Qukw89238gky9PCwcSYy2AUnb4mxTA83utHQFkGVpBMBBbfHHvBX6MFcOe fb8Z7QmCIpjAUeLtXBkCZTUdrRPzBZibyLRUF69T6+rA/hlxGlIEn4Gpp2+vvIMF92UE ge0X46MXMxzS4t9TxUD3fWBCu5+RI7F08o0/lUKo/CRoGUzuFu9WJbzTUgMvhMolBj+9 RBLaANgWXqZ4z+tE2dtXFPZ3vz9xo2Ac26hKhZc9ycfYLReqDYNis0KMt7DORPJq+jhO FVtQ== X-Gm-Message-State: AOAM532WFzAAlVOInSdDxqKYN7wDN6ZGOZxaNKQ+GvCOG3STP/Jo0UT1 LhghHspBu6G6+Pz7aLOYv5RVer4NKacj4c/RO4/+JnKT1ut+tC2UAGwt75RVYapY79rHuGapByK 2p2z7JZ/vSDZ9eN2QixAxHDsZd5UjdwJcr1+FBiNi1+I2JPPe8Ayauef1C59TDko= X-Google-Smtp-Source: ABdhPJz2Dt5YuBqrdo+A9qm1jH3Wsfz5+h6XJWP4dhurCNFsLKswOSzSwIeyuz3H66Vw5RX72Xw8X0unJYUiTQ== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:903:2445:b0:142:2471:644e with SMTP id l5-20020a170903244500b001422471644emr69503133pls.48.1639075806326; Thu, 09 Dec 2021 10:50:06 -0800 (PST) Date: Thu, 9 Dec 2021 10:49:28 -0800 In-Reply-To: <20211209184928.71413-1-chooglen@google.com> Message-Id: <20211209184928.71413-6-chooglen@google.com> Mime-Version: 1.0 References: <20211206215528.97050-1-chooglen@google.com> <20211209184928.71413-1-chooglen@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v3 5/5] branch.c: replace questionable exit() codes 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 Replace exit() calls in branch.c that have questionable exit codes: * in setup_tracking(), exit(-1) was introduced in 27852b2c53 (branch: report errors in tracking branch setup, 2016-02-22). This may have been a mechanical typo because the same commit changes the return type of setup_tracking() from int to void. * in validate_branch_start(), the exit code changes depending on whether or not advice is enabled. This behavior was not discussed upstream (see caa2036b3b (branch: give advice when tracking start-point is missing, 2013-04-02)). Signed-off-by: Glen Choo --- I don't know what the 'correct' exit codes should be, only that Junio makes a good case that the existing exit codes are wrong. My best, non-prescriptive, choice is 128, to be consistent with the surrounding code and Documentation/technical/api-error-handling.txt. branch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/branch.c b/branch.c index 305154de0b..ad70ddd120 100644 --- a/branch.c +++ b/branch.c @@ -324,7 +324,7 @@ static void validate_branch_start(struct repository *r, const char *start_name, if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) { error(_(upstream_missing), start_name); advise(_(upstream_advice)); - exit(1); + exit(128); } die(_(upstream_missing), start_name); } @@ -398,7 +398,7 @@ void setup_tracking(const char *new_ref, const char *orig_ref, string_list_append(tracking.srcs, full_orig_ref); if (install_branch_config_multiple_remotes(config_flags, new_ref, tracking.remote, tracking.srcs) < 0) - exit(-1); + exit(128); cleanup: string_list_clear(tracking.srcs, 0);