From patchwork Fri Feb 25 18:52:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Klerks X-Patchwork-Id: 12760812 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 3C35CC433EF for ; Fri, 25 Feb 2022 18:52:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233465AbiBYSxK (ORCPT ); Fri, 25 Feb 2022 13:53:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232683AbiBYSxG (ORCPT ); Fri, 25 Feb 2022 13:53:06 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A074C2E681 for ; Fri, 25 Feb 2022 10:52:26 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id d28so5860622wra.4 for ; Fri, 25 Feb 2022 10:52:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=NMBxTBIthEBnTsfAoQFAD6Sr4eiKRXcmVrXzk4e+WWg=; b=THVp4mR9G2HsKUuUB+P6Wkuh4gp/vF9MAAWmg0Mr3RUsIRwk7M+xUp1JksoonfHUwO upqgjx8ywxmRLbmeCFSVfvHiPamacUSeMCK/sUtr8p5ye+pg86SVhlq0hByi63b9o77M xli/55ujAQ5YLadPpoLQxteDvB0NlUSHxUnP42AUtRvwAdiMs8cF8AKPqmm0tXIBbtmw 9sIDH0c10OK1a5o5JxpZkooieIDZzIwjluSk0UhMheP7JRF0p+niD030J7lf+2LqOWA4 6Jrr1eg4WTM3wVKfCA8Twir4k1gLmh6IbZijdSk25nWM5Ya7TvsnjSYyaNWnxFbpgNkj O7Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=NMBxTBIthEBnTsfAoQFAD6Sr4eiKRXcmVrXzk4e+WWg=; b=ynsB+Ac/HalDFXrB/v6bH5WaqPMhP7XqCg3Du22VOGnoJ9EV7VjvZH0KTSSD1y/5eG NbnEM4cd4TrMAEWz3S9ilIq0igaoH9LTGPqrjrffZMw3pbfE6gdrrkcvQVOVEOQ+YHQp Cd9XmW1POH1n0bJekwDf5yYUOhudQETD6oO5iYv4zc3Vz6iX76DvRDPcQMCWk0VJndoA lzC5yxO/AbmV5CyOMJdP7nByZcLHC8bBP9ozLTpTFOm6mi3CYnHYwwb9fW0J2jHGdL+z sSjYyAPVU8vQbMGgdQamVV/u4dbLnqzso0kYHT46+Y6RhNS7jaiBrJGPVmCjWaX2E+bo rKcQ== X-Gm-Message-State: AOAM5320QbLLS/+heA7JhsBoWjdVsKLN8JjvPxKy89hv3m2shZxGhlFG 71W5tSBpB9V1w+8RmXTIsTltEG+hF6M= X-Google-Smtp-Source: ABdhPJxx4VrOowjleNG98IEbekRfdVSXxxMWZ9uLQt5MaF0gV6aUCAxaGShfTk7xxtJejsKzNtx2Qw== X-Received: by 2002:adf:fa4a:0:b0:1ef:731c:21e8 with SMTP id y10-20020adffa4a000000b001ef731c21e8mr2622018wrr.518.1645815144993; Fri, 25 Feb 2022 10:52:24 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id a9-20020a05600c2d4900b0038100a95903sm6316899wmg.41.2022.02.25.10.52.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 10:52:24 -0800 (PST) Message-Id: <890e016bfc0809d25a4ae8ae924b23895f520810.1645815142.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 25 Feb 2022 18:52:21 +0000 Subject: [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Tao Klerks , Tao Klerks Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Tao Klerks From: Tao Klerks This commit introduces a new option to the branch.autosetupmerge setting, "simple", which is intended to be consistent with and complementary to the push.default "simple" option. The push.defaut option "simple" helps produce predictable/understandable behavior for beginners, where they don't accidentally push to the "wrong" branch in centralized workflows. If they create a local branch with a different name and then try to do a plain push, it will helpfully fail and explain why. However, such users can often find themselves confused by the behavior of git after they first branch, and before they push. At that stage, their upstream tracking branch is the original remote branch, and pull will be bringing in "upstream changes" - eg all changes to "main", in a typical project where that's where they branched from. On the other hand, once they push their new branch (dealing with the initial error, following instructions to push to the right name), subsequent "pull" calls will behave as expected, only bring in any changes to that new branch they pushed. The new option introduced here, with push.default set to simple, ensures that push/pull behavior is generally consistent - tracking will be automatically set up for branches that push will work for (and pull will be consistent for) only. Signed-off-by: Tao Klerks --- Documentation/config/branch.txt | 4 +++- Documentation/git-branch.txt | 18 +++++++++++------- branch.c | 19 +++++++++++++++++++ branch.h | 1 + config.c | 3 +++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Documentation/config/branch.txt b/Documentation/config/branch.txt index 1e0c7af014b..8df10d07129 100644 --- a/Documentation/config/branch.txt +++ b/Documentation/config/branch.txt @@ -9,7 +9,9 @@ branch.autoSetupMerge:: automatic setup is done when the starting point is either a local branch or remote-tracking branch; `inherit` -- if the starting point has a tracking configuration, it is copied to the new - branch. This option defaults to true. + branch; `simple` -- automatic setup is done only when the starting point + is a remote-tracking branch and the new branch has the same name as the + remote branch. This option defaults to true. branch.autoSetupRebase:: When a new branch is created with 'git branch', 'git switch' or 'git checkout' diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt index c8b4f9ce3c7..ae82378349d 100644 --- a/Documentation/git-branch.txt +++ b/Documentation/git-branch.txt @@ -221,13 +221,17 @@ The exact upstream branch is chosen depending on the optional argument: itself as the upstream; `--track=inherit` means to copy the upstream configuration of the start-point branch. + -`--track=direct` is the default when the start point is a remote-tracking branch. -Set the branch.autoSetupMerge configuration variable to `false` if you -want `git switch`, `git checkout` and `git branch` to always behave as if `--no-track` -were given. Set it to `always` if you want this behavior when the -start-point is either a local or remote-tracking branch. Set it to -`inherit` if you want to copy the tracking configuration from the -branch point. +The branch.autoSetupMerge configuration variable specifies how `git switch`, +`git checkout` and `git branch` should behave when neither `--track` nor +`--no-track` are specified: ++ +The default option, `true`, behaves as though `--track=direct` +were given whenever the start-point is a remote-tracking branch. +`false` behaves as if `--no-track` were given. `always` behaves as though +`--track=direct` were given. `inherit` behaves as though `--track=inherit` +were given. `simple` behaves as though `--track=direct` were given only when +the start-point is a remote-tracking branch and the new branch has the same +name as the remote branch. + See linkgit:git-pull[1] and linkgit:git-config[1] for additional discussion on how the `branch..remote` and `branch..merge` options are used. diff --git a/branch.c b/branch.c index 6b31df539a5..81613ade8bf 100644 --- a/branch.c +++ b/branch.c @@ -252,10 +252,29 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, goto cleanup; } + /* + * This check does not apply to the BRANCH_TRACK_INHERIT + * option; you can inherit one or more tracking entries + * and the tracking.matches counter is not incremented. + */ if (tracking.matches > 1) die(_("not tracking: ambiguous information for ref %s"), orig_ref); + if (track == BRANCH_TRACK_SIMPLE) { + /* + * Only track if remote branch name matches. + * Reaching into items[0].string is safe because + * we know there is at least one and not more than + * one entry (because not BRANCH_TRACK_INHERIT). + */ + const char *tracked_branch; + if (!skip_prefix(tracking.srcs->items[0].string, + "refs/heads/", &tracked_branch) || + strcmp(tracked_branch, new_ref)) + return; + } + if (tracking.srcs->nr < 1) string_list_append(tracking.srcs, orig_ref); if (install_branch_config_multiple_remotes(config_flags, new_ref, diff --git a/branch.h b/branch.h index 04df2aa5b51..560b6b96a8f 100644 --- a/branch.h +++ b/branch.h @@ -12,6 +12,7 @@ enum branch_track { BRANCH_TRACK_EXPLICIT, BRANCH_TRACK_OVERRIDE, BRANCH_TRACK_INHERIT, + BRANCH_TRACK_SIMPLE, }; extern enum branch_track git_branch_track; diff --git a/config.c b/config.c index e0c03d154c9..cc586ac816c 100644 --- a/config.c +++ b/config.c @@ -1673,6 +1673,9 @@ static int git_default_branch_config(const char *var, const char *value) } else if (value && !strcmp(value, "inherit")) { git_branch_track = BRANCH_TRACK_INHERIT; return 0; + } else if (value && !strcmp(value, "simple")) { + git_branch_track = BRANCH_TRACK_SIMPLE; + return 0; } git_branch_track = git_config_bool(var, value); return 0; From patchwork Fri Feb 25 18:52:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Klerks X-Patchwork-Id: 12760813 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 2C2BDC433EF for ; Fri, 25 Feb 2022 18:52:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233485AbiBYSxN (ORCPT ); Fri, 25 Feb 2022 13:53:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233235AbiBYSxH (ORCPT ); Fri, 25 Feb 2022 13:53:07 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87A4537A1D for ; Fri, 25 Feb 2022 10:52:27 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id c192so2177752wma.4 for ; Fri, 25 Feb 2022 10:52:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=e7Zl/FeBFM+SXKGzdeD7uESsrx8p8RQRmwdLO6qv5nQ=; b=awD181z+MTmyJ/+zZDw1Y9jDYZ2b+AwosZIWKqzd2JvzB5K2xhx0qFjfE0Y8HdCop/ VW/3a45Di6kJb6xM1K4Tig2roJghukHsr7gcqsuyDDG6OcznqrxDlTNZ/349jYnAmN2s R0F0ly70zLaGn0a3ZaQOeHrzKbNUzvkDII7eu2MZEEqzYsF3CdAvc59Ra/ZCjoLfeDcx /l5MrkFmkVwnRxng+GANL/RXaSYbCt666HJYoq+S9xQtW2jez7nioQ3FdCGOcH5ZFw51 tsWCADn6e+vmWyRhxQ6/RXQqhfoFSsdxtrW0pLG+5gY26J4BEdX/9MoMBjOmcHPPKc0Z AEAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=e7Zl/FeBFM+SXKGzdeD7uESsrx8p8RQRmwdLO6qv5nQ=; b=fsAswnu8fbDfWtwatg/1FwWDi/wS85KqSqyw6XcqTNRmJPQg5Gy7cSajUHld+KJqzd jyxXzHEBbUCphiV3Ju03n4oP2X65hotDeHbVJFcHdKtOAHZVORpjc4Ha3X3SAVxbW16/ d1Bts4xF4dFyn80LHqmi9dbdA0xlLh6O9H27bJjanwfu8pCr87gAPfWx8WtMpltJ/sxN WF2EX0c2LPJ6kEaaFA2WQFUppE1U2vYWsW0aCOH1ZykFMXbvQPNDeFzoPG42aviQm9bj elIv3qb6KdK1CRJho32ZheS2gnvTu6hOtFUbLiQh2oE1bK2yh7eoAJ+rhFcrvWbse65d DcZw== X-Gm-Message-State: AOAM533U2ZDimREvvtlRUE+apmR/ur7UKyapr8T4wBwhEMmPpuiDV1fF E4nGspJZcrWlUcF+GXdNwYuMJkKonfM= X-Google-Smtp-Source: ABdhPJyxqdnyWF3ciDzQZVb/GnZdxlMxfRZpe4NmUw+1B47cFobA6ZzgoXmBzLMbpPIkxl7uotZxMg== X-Received: by 2002:a05:600c:4c9a:b0:381:2d4a:759a with SMTP id g26-20020a05600c4c9a00b003812d4a759amr3856932wmp.57.1645815145828; Fri, 25 Feb 2022 10:52:25 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w26-20020a7bc11a000000b0037bf8fa8c02sm3158692wmi.13.2022.02.25.10.52.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 10:52:25 -0800 (PST) Message-Id: In-Reply-To: References: Date: Fri, 25 Feb 2022 18:52:22 +0000 Subject: [PATCH v2 2/2] t3200: tests for new branch.autosetupmerge option "simple" Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Tao Klerks , Tao Klerks Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Tao Klerks From: Tao Klerks In the previous commit a new autosetupmerge option was introduced. Here the existing branch tests are extended with three new cases testing this option - the obvious matching-name and non-matching-name cases, and also a non-matching-ref-type case. The matching-name case needs to temporarily create an independent repo to fetch from, as the general strategy of using the local repo as the remote in these tests precludes locally branching with the same name as in the "remote". Signed-off-by: Tao Klerks --- t/t3200-branch.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 7a0ff75ba86..15cc58f1e64 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -886,6 +886,41 @@ test_expect_success 'branch from tag w/--track causes failure' ' test_must_fail git branch --track my11 foobar ' +test_expect_success 'simple tracking works when remote branch name matches' ' + test_create_repo otherserver && + test_commit -C otherserver my_commit 1 && + git -C otherserver branch feature && + git config branch.autosetupmerge simple && + git config remote.otherserver.url otherserver && + git config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* && + git fetch otherserver && + git branch feature otherserver/feature && + rm -fr otherserver && + test $(git config branch.feature.remote) = otherserver && + test $(git config branch.feature.merge) = refs/heads/feature +' + +test_expect_success 'simple tracking skips when remote branch name does not match' ' + git config branch.autosetupmerge simple && + git config remote.local.url . && + git config remote.local.fetch refs/heads/*:refs/remotes/local/* && + (git show-ref -q refs/remotes/local/main || git fetch local) && + git branch my-other local/main && + test -z "$(git config branch.my-other.remote)" && + test -z "$(git config branch.my-other.merge)" +' + +test_expect_success 'simple tracking skips when remote ref is not a branch' ' + git config branch.autosetupmerge simple && + git tag mytag12 main && + git config remote.localtags.url . && + git config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* && + (git show-ref -q refs/remotes/localtags/mytag12 || git fetch localtags) && + git branch mytag12 localtags/mytag12 && + test -z "$(git config branch.mytag12.remote)" && + test -z "$(git config branch.mytag12.merge)" +' + test_expect_success '--set-upstream-to fails on multiple branches' ' echo "fatal: too many arguments to set new upstream" >expect && test_must_fail git branch --set-upstream-to main a b c 2>err &&