* [PATCH 0/3] adding new branch.autosetupmerge option "simple" @ 2022-02-24 9:45 Tao Klerks via GitGitGadget 2022-02-24 9:45 ` [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget ` (3 more replies) 0 siblings, 4 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-24 9:45 UTC (permalink / raw) To: git; +Cc: 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 (for example) behaves very differently to how it later does when they create their own same-name remote branch. This new option (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. Tao Klerks (3): merge: new autosetupmerge option 'simple' for matching branches t3200: tests for new branch.autosetupmerge option "simple" branch documentation: new autosetupmerge option "simple" Documentation/config/branch.txt | 4 +++- Documentation/git-branch.txt | 4 +++- branch.c | 9 +++++++++ branch.h | 1 + config.c | 3 +++ t/t3200-branch.sh | 35 +++++++++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 2 deletions(-) base-commit: dab1b7905d0b295f1acef9785bb2b9cbb0fdec84 Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1161%2FTaoK%2Ffeature-branch-autosetupmerge-simple-v1 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1161/TaoK/feature-branch-autosetupmerge-simple-v1 Pull-Request: https://github.com/gitgitgadget/git/pull/1161 -- gitgitgadget ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches 2022-02-24 9:45 [PATCH 0/3] adding new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget @ 2022-02-24 9:45 ` Tao Klerks via GitGitGadget 2022-02-24 19:20 ` Junio C Hamano 2022-02-24 9:45 ` [PATCH 2/3] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget ` (2 subsequent siblings) 3 siblings, 1 reply; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-24 9:45 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> 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 (for example) behaves very differently to how it later does when they create their own same-name remote branch. 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. It will set up automatic tracking for a new branch only if the remote ref is a branch and that remote branch name matches the new local branch name. It is a reduction in scope of the existing default option, "true". Signed-off-by: Tao Klerks <tao@klerks.biz> --- branch.c | 9 +++++++++ branch.h | 1 + config.c | 3 +++ 3 files changed, 13 insertions(+) diff --git a/branch.c b/branch.c index 6b31df539a5..246bc82ce3c 100644 --- a/branch.c +++ b/branch.c @@ -256,6 +256,15 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, die(_("not tracking: ambiguous information for ref %s"), orig_ref); + if (track == BRANCH_TRACK_SIMPLE) { + // only track if remote branch name matches + // (tracking.srcs must contain only one entry from find_tracked_branch with this config) + if (strncmp(tracking.srcs->items[0].string, "refs/heads/", 11)) + return; + if (strcmp(tracking.srcs->items[0].string + 11, 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; -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* Re: [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches 2022-02-24 9:45 ` [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-02-24 19:20 ` Junio C Hamano 0 siblings, 0 replies; 41+ messages in thread From: Junio C Hamano @ 2022-02-24 19:20 UTC (permalink / raw) To: Tao Klerks via GitGitGadget; +Cc: git, Tao Klerks "Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Tao Klerks <tao@klerks.biz> > > The push.defaut option "simple" helps produce The cover letter wrappeed around 70 columns, which was much easier to read. Please re-read Documentation/SubmittingPatches[[describe-changes]] section before going forward. > 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 (for example) behaves > very differently to how it later does when they > create their own same-name remote branch. Instead of saying "very differently", explain what happens before and after the behaviour-change-triggering-event. > 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. > > It will set up automatic tracking for a new > branch only if the remote ref is a branch and > that remote branch name matches the new local > branch name. It is a reduction in scope of > the existing default option, "true". > > Signed-off-by: Tao Klerks <tao@klerks.biz> > --- > branch.c | 9 +++++++++ > branch.h | 1 + > config.c | 3 +++ > 3 files changed, 13 insertions(+) > > diff --git a/branch.c b/branch.c > index 6b31df539a5..246bc82ce3c 100644 > --- a/branch.c > +++ b/branch.c > @@ -256,6 +256,15 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, > die(_("not tracking: ambiguous information for ref %s"), > orig_ref); > > + if (track == BRANCH_TRACK_SIMPLE) { > + // only track if remote branch name matches > + // (tracking.srcs must contain only one entry from find_tracked_branch with this config) /* * Our multi-line comments look exactly * like this. They are not overly long, * have their opening and closing slash-aster * and aster-slash on their own line. */ > + if (strncmp(tracking.srcs->items[0].string, "refs/heads/", 11)) > + return; > + if (strcmp(tracking.srcs->items[0].string + 11, new_ref)) > + return; Don't count hardcoded string length. char *tracked_branch; if (!skip_prefix(tracking.srcs->items[0].string, "refs/heads/", &tracked_branch) || strcmp(tracked_branch, new_ref)) return; or something along the line, perhaps? But the post-context in this hunk makes the refernece to items[0] in the above look very wrong. It says tracking.srcs may not have even a single item at this point in the original code flow. If that is true, the above reference to ->items[0] may not be safely done at all. Also, what happens when there are more than one in the items[] array? What makes it sensible to use the first one, ignoring the others? > + } > + > 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; These two hunks look perfect. ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH 2/3] t3200: tests for new branch.autosetupmerge option "simple" 2022-02-24 9:45 [PATCH 0/3] adding new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-02-24 9:45 ` [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-02-24 9:45 ` Tao Klerks via GitGitGadget 2022-02-24 9:45 ` [PATCH 3/3] branch documentation: new autosetupmerge " Tao Klerks via GitGitGadget 2022-02-25 18:52 ` [PATCH v2 0/2] adding new branch.autosetupmerge " Tao Klerks via GitGitGadget 3 siblings, 0 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-24 9:45 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> 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 in these tests of using the local repo as the remote precludes locally branching with the same name as the "remote". Signed-off-by: Tao Klerks <tao@klerks.biz> --- 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 && -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* [PATCH 3/3] branch documentation: new autosetupmerge option "simple" 2022-02-24 9:45 [PATCH 0/3] adding new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-02-24 9:45 ` [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-02-24 9:45 ` [PATCH 2/3] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget @ 2022-02-24 9:45 ` Tao Klerks via GitGitGadget 2022-02-24 19:38 ` Junio C Hamano 2022-02-25 18:52 ` [PATCH v2 0/2] adding new branch.autosetupmerge " Tao Klerks via GitGitGadget 3 siblings, 1 reply; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-24 9:45 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> Updating the branch and config documentation to reflect the new "simple" option to branch.autosetupmerge. Signed-off-by: Tao Klerks <tao@klerks.biz> --- Documentation/config/branch.txt | 4 +++- Documentation/git-branch.txt | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Documentation/config/branch.txt b/Documentation/config/branch.txt index 1e0c7af014b..7b4e5ca5b74 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 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..f99d6a6b008 100644 --- a/Documentation/git-branch.txt +++ b/Documentation/git-branch.txt @@ -227,7 +227,9 @@ want `git switch`, `git checkout` and `git branch` to always behave as if `--no- 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. +branch point. Set it to `simple` if you want this behavior only when +the start-point is a remote 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.<name>.remote` and `branch.<name>.merge` options are used. -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* Re: [PATCH 3/3] branch documentation: new autosetupmerge option "simple" 2022-02-24 9:45 ` [PATCH 3/3] branch documentation: new autosetupmerge " Tao Klerks via GitGitGadget @ 2022-02-24 19:38 ` Junio C Hamano 0 siblings, 0 replies; 41+ messages in thread From: Junio C Hamano @ 2022-02-24 19:38 UTC (permalink / raw) To: Tao Klerks via GitGitGadget; +Cc: git, Tao Klerks "Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Tao Klerks <tao@klerks.biz> > > Updating the branch and config documentation to reflect > the new "simple" option to branch.autosetupmerge. Documentation/Submittingpatches[[describe-changes]]. But it would be moot; these changes are better done as part of [1/3] and in that case, updating the documentation (or testing the desired behaviour, for that matter) is not something we need to justify separately. It is something we must done as part of the change. > diff --git a/Documentation/config/branch.txt b/Documentation/config/branch.txt > index 1e0c7af014b..7b4e5ca5b74 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 when the starting point is It may be clearer to say "done only when". I dunno. > + a remote-tracking branch and the new branch has the same name as the > + remote branch. This option defaults to true. > diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt > index c8b4f9ce3c7..f99d6a6b008 100644 > --- a/Documentation/git-branch.txt > +++ b/Documentation/git-branch.txt > @@ -227,7 +227,9 @@ want `git switch`, `git checkout` and `git branch` to always behave as if `--no- > 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. > +branch point. Set it to `simple` if you want this behavior only when > +the start-point is a remote branch and the new branch has the same name > +as the remote branch. The existing "if you want this behaviour when" is already awkward. What it means is that only those who want to use the "start-point" itself as the upstream whether the start-point is local or remote-tracking,can use "always" and does not get hurt. But using the phrase for "simple" makes it even worse, as the condition that the tracking behaviour kicks in is even narrower. If you know that start-point is not a remote-tracking branch (by the way, do not say "remote branch" when you mean "remote-tracking brnach"), or its name is not the same as the local branch, you just do not pass --track=simple from the command line. Strike everything after "Set it to `simple`" and replace with something like `--track=simple` sets up the upstream information only when the start-point is a remote-tracking branch and ... perhaps? Thanks. ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v2 0/2] adding new branch.autosetupmerge option "simple" 2022-02-24 9:45 [PATCH 0/3] adding new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget ` (2 preceding siblings ...) 2022-02-24 9:45 ` [PATCH 3/3] branch documentation: new autosetupmerge " Tao Klerks via GitGitGadget @ 2022-02-25 18:52 ` Tao Klerks via GitGitGadget 2022-02-25 18:52 ` [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget ` (2 more replies) 3 siblings, 3 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-25 18:52 UTC (permalink / raw) To: git; +Cc: Tao Klerks Re-sending with proposed fixes to concerns raised by Junio. This patchset 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. Tao Klerks (2): merge: new autosetupmerge option 'simple' for matching branches t3200: tests for new branch.autosetupmerge option "simple" Documentation/config/branch.txt | 4 +++- Documentation/git-branch.txt | 18 ++++++++++------- branch.c | 19 ++++++++++++++++++ branch.h | 1 + config.c | 3 +++ t/t3200-branch.sh | 35 +++++++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 8 deletions(-) base-commit: dab1b7905d0b295f1acef9785bb2b9cbb0fdec84 Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1161%2FTaoK%2Ffeature-branch-autosetupmerge-simple-v2 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1161/TaoK/feature-branch-autosetupmerge-simple-v2 Pull-Request: https://github.com/gitgitgadget/git/pull/1161 Range-diff vs v1: 1: 89efc1e1564 ! 1: 890e016bfc0 merge: new autosetupmerge option 'simple' for matching branches @@ Metadata ## Commit message ## merge: new autosetupmerge option 'simple' for matching branches - 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. + 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. - 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 (for example) behaves - very differently to how it later does when they - create their own same-name remote branch. + 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. - 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. + 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. - It will set up automatic tracking for a new - branch only if the remote ref is a branch and - that remote branch name matches the new local - branch name. It is a reduction in scope of - the existing default option, "true". + 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 <tao@klerks.biz> + ## Documentation/config/branch.txt ## +@@ Documentation/config/branch.txt: 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' + + ## Documentation/git-branch.txt ## +@@ Documentation/git-branch.txt: 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.<name>.remote` and `branch.<name>.merge` options are used. + ## branch.c ## @@ branch.c: 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 -+ // (tracking.srcs must contain only one entry from find_tracked_branch with this config) -+ if (strncmp(tracking.srcs->items[0].string, "refs/heads/", 11)) -+ return; -+ if (strcmp(tracking.srcs->items[0].string + 11, new_ref)) ++ /* ++ * 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; + } + 2: 3fa56f1d2a0 ! 2: c16a8fe01e7 t3200: tests for new branch.autosetupmerge option "simple" @@ Commit message The matching-name case needs to temporarily create an independent repo to fetch from, as the general - strategy in these tests of using the local repo as - the remote precludes locally branching with the same - name as the "remote". + 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 <tao@klerks.biz> 3: 39c14906e7b < -: ----------- branch documentation: new autosetupmerge option "simple" -- gitgitgadget ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches 2022-02-25 18:52 ` [PATCH v2 0/2] adding new branch.autosetupmerge " Tao Klerks via GitGitGadget @ 2022-02-25 18:52 ` Tao Klerks via GitGitGadget 2022-02-25 20:15 ` Junio C Hamano 2022-02-25 18:52 ` [PATCH v2 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 0/2] adding " Tao Klerks via GitGitGadget 2 siblings, 1 reply; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-25 18:52 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> 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 <tao@klerks.biz> --- 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.<name>.remote` and `branch.<name>.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; -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* Re: [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches 2022-02-25 18:52 ` [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-02-25 20:15 ` Junio C Hamano 2022-02-27 23:59 ` Tao Klerks 0 siblings, 1 reply; 41+ messages in thread From: Junio C Hamano @ 2022-02-25 20:15 UTC (permalink / raw) To: Tao Klerks via GitGitGadget; +Cc: git, Tao Klerks "Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Tao Klerks <tao@klerks.biz> > > 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. Documentation/SubmittingPatches. We do not say "This commit does this". Instead, we say "Add a new option that does X". Usually that is done after the explanation of the status quo is finished to make readers understand what the problem the change is trying to solve is. So... > 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. ... this would be a better first paragraph to start the proposed log message with. With push.default set to "simple", the users fork from a local branch from a remote-tracking branch of the same name, and are protected from a mistake to push to a wrong branch. If they create a ... 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, Depending on how they "branch", they may or may not be confused. Be more specific to illustrate what problem you are solving, e.g. ... after they create a new local branch from a remote-tracking branch with a different name. > 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. OK. So "pull" tries to grab from the upstream (which is most likely an integration branch with bland name like 'master', 'main' or 'trunk'), while "push" does not allow the work on a branch (which is named after the theme of the work and not a bland name suitable for integration branches) to be pushed to the upstream. It may probably not be so clear why it is a problem to many readers, I suspect. Isn't that what happens in a typical triangular workflow to work with a project with a centralized repository? You fork from the integration branch shared among project participants, you work on your own branch, occasionally rebasing on top of the updated upstream, and when you are done, try to push it out to the integration branch, and that final leg needs to be explicit to make sure you won't push out to a wrong branch (in this case, a new branch at the remote with the same name as your local topic branch) by mistake? > 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. Is that because the upstream for this local branch is updated? The "following instructions..." part may want to clarify. It somehow feels that a better solution might be to suggest updating the push.default to 'upstream' when it happens? I dunno. In any case, now we have explained what happens with today's code, here is a good place to propose a solution. Do so in imperative, e.g. Allow branch.autosetupmerge to take a new value, 'simple', which sets the upstream of the new branch only when the local branch being created has the same name as the remote-tracking branch it was created out of. Otherwise the new local branch will not get any tracking information and or something, perhaps? > + /* > + * 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). > + */ OK, because in the pre-context of this hunk, we would have jumped to cleanup: if there were no .matches; so we know there should at least be one, and we rejected ambiguous matches already, so we know there is only one. > + const char *tracked_branch; > + if (!skip_prefix(tracking.srcs->items[0].string, > + "refs/heads/", &tracked_branch) || > + strcmp(tracked_branch, new_ref)) > + return; > + } That looks sensible. Sometimes we do not set tracking information and just return. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches 2022-02-25 20:15 ` Junio C Hamano @ 2022-02-27 23:59 ` Tao Klerks 0 siblings, 0 replies; 41+ messages in thread From: Tao Klerks @ 2022-02-27 23:59 UTC (permalink / raw) To: Junio C Hamano; +Cc: Tao Klerks via GitGitGadget, git On Fri, Feb 25, 2022 at 9:15 PM Junio C Hamano <gitster@pobox.com> wrote: > > "Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com> writes: > > > 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. > > Documentation/SubmittingPatches. > > We do not say "This commit does this". Instead, we say "Add a new > option that does X". Usually that is done after the explanation of > the status quo is finished to make readers understand what the > problem the change is trying to solve is. So... Yep, sorry, thx! (fixed, reroll coming!) > > > 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. > > ... this would be a better first paragraph to start the proposed log > message with. > > With push.default set to "simple", the users fork from a > local branch from a remote-tracking branch of the same name, > and are protected from a mistake to push to a wrong branch. > If they create a ... 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, > > Depending on how they "branch", they may or may not be confused. Be > more specific to illustrate what problem you are solving, e.g. > > ... after they create a new local branch from a > remote-tracking branch with a different name. > > > 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. > > OK. So "pull" tries to grab from the upstream (which is most likely > an integration branch with bland name like 'master', 'main' or > 'trunk'), while "push" does not allow the work on a branch (which is > named after the theme of the work and not a bland name suitable for > integration branches) to be pushed to the upstream. > > It may probably not be so clear why it is a problem to many readers, > I suspect. Isn't that what happens in a typical triangular workflow > to work with a project with a centralized repository? You fork from > the integration branch shared among project participants, you work on > your own branch, occasionally rebasing on top of the updated upstream, > and when you are done, try to push it out to the integration branch, > and that final leg needs to be explicit to make sure you won't push > out to a wrong branch (in this case, a new branch at the remote with > the same name as your local topic branch) by mistake? > > > 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. > > Is that because the upstream for this local branch is updated? > The "following instructions..." part may want to clarify. > > It somehow feels that a better solution might be to suggest > updating the push.default to 'upstream' when it happens? I dunno. > > In any case, now we have explained what happens with today's code, > here is a good place to propose a solution. Do so in imperative, > e.g. > > Allow branch.autosetupmerge to take a new value, 'simple', which > sets the upstream of the new branch only when the local branch > being created has the same name as the remote-tracking branch it > was created out of. Otherwise the new local branch will not get > any tracking information and > > or something, perhaps? Thank you for taking the time to make sense of the rambling / largely incoherent message and helping me identify some context other reviewers will expect. I've rewritten the whole thing to try to address these concerns, but of course I may well have introduced a whole new set. If nothing else, it's become even more rambling. Is there a recommended limit to the length of a commit message? ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v2 2/2] t3200: tests for new branch.autosetupmerge option "simple" 2022-02-25 18:52 ` [PATCH v2 0/2] adding new branch.autosetupmerge " Tao Klerks via GitGitGadget 2022-02-25 18:52 ` [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-02-25 18:52 ` Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 0/2] adding " Tao Klerks via GitGitGadget 2 siblings, 0 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-25 18:52 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> 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 <tao@klerks.biz> --- 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 && -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* [PATCH v3 0/2] adding new branch.autosetupmerge option "simple" 2022-02-25 18:52 ` [PATCH v2 0/2] adding new branch.autosetupmerge " Tao Klerks via GitGitGadget 2022-02-25 18:52 ` [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-02-25 18:52 ` [PATCH v2 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget @ 2022-02-28 7:14 ` Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget ` (2 more replies) 2 siblings, 3 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-28 7:14 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks Re-sending with proposed fixes to concerns raised by Junio. This patchset 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. Tao Klerks (2): merge: new autosetupmerge option 'simple' for matching branches t3200: tests for new branch.autosetupmerge option "simple" Documentation/config/branch.txt | 4 +++- Documentation/git-branch.txt | 18 ++++++++++------- branch.c | 19 ++++++++++++++++++ branch.h | 1 + config.c | 3 +++ t/t3200-branch.sh | 35 +++++++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 8 deletions(-) base-commit: dab1b7905d0b295f1acef9785bb2b9cbb0fdec84 Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1161%2FTaoK%2Ffeature-branch-autosetupmerge-simple-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1161/TaoK/feature-branch-autosetupmerge-simple-v3 Pull-Request: https://github.com/gitgitgadget/git/pull/1161 Range-diff vs v2: 1: 890e016bfc0 ! 1: 0b5d4789512 merge: new autosetupmerge option 'simple' for matching branches @@ Metadata ## Commit message ## merge: new autosetupmerge option 'simple' for matching branches - 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. + With the default push.default option, "simple", beginners are + protected from accidentally pushing to the "wrong" branch in + centralized workflows: if the remote tracking branch they would push + to does not have the same name as the local branch, and they try to do + a "default push", they get an error and explanation with options. + + There is a particular centralized workflow where this often happens: + a user branches to a new local feature branch from an existing + upstream branch, eg with "checkout -b feature1 origin/master". With + the default branch.autosetupmerge configuration (value "true"), git + will automatically add origin/master as the remote tracking branch. + + When the user pushes with "git push", they get an error, and (amongst + other things) a suggestion to run "git push origin HEAD". Eventually + they figure out to add "-u" to change the tracking branch, or they set + push.default to "current", or some tooling does one or the other of + these things for them. + + When one of their coworkers works on the same branch, they don't get + any of that weirdness. They just "git checkout feature1" and + everything works exactly as they expect, with the shared remote branch + set up as remote tracking branch, and push and pull working out of the + box. + + The "stable state" for this way of working is that local branches have + the same-name remote tracking branch (origin/feature1 in this + example), and multiple people can work on that remote feature branch + at the same time, trusting "git pull" to merge or rebase as required + for them to be able to push their interim changes to that same feature + branch on that same remote. + + (merging from the upstream "master" branch, and merging back to it, + are separate more involved processes in this flow). + + There is a problem in this flow/way of working, however, which is that + the first user, when they first branched from origin/master, ended up + with the "wrong" remote tracking branch (different from the stable + state). For a while, before they pushed (and maybe longer, if they + don't use -u/--set-upstream), their "git pull" wasn't getting other + users' changes to the feature branch - it was getting any changes from + the remote "master" branch instead (a completely different class of + changes!) + + Any experienced git user will presumably say "well yeah, that's what + it means to have the remote tracking branch set to origin/master!" - + but that user didn't *ask* to have the remote master branch added as + remote tracking branch - that just happened automatically when they + branched their feature branch. They didn't necessarily even notice or + understand the meaning of the "set up to track 'origin/master'" + message when they created the branch - especially if they are using a + GUI. + + Looking at how to fix this, you might think "OK, so disable auto setup + of remote tracking - set branch.autosetupmerge to false" - but that + will inconvenience the *second* user in this story - the one who just + wanted to start working on the feature branch. The first and second + users swap roles at different points in time of course - they should + both have a sane configuration that does the right thing in both + situations. + + Make these flows painless by introducing a new branch.autosetupmerge + option called "simple", to match the same-name "push.default" option + that makes similar assumptions. + + This new option automatically sets up tracking in a *subset* of the + current default situations: when the original ref is a remote tracking + branch *and* has the same branch name on the remote (as the new local + branch name). + + With this new configuration, in the example situation above, the first + user does *not* get origin/master set up as the tracking branch for + the new local branch. If they "git pull" in their new local-only + branch, they get an error explaining there is no upstream branch - + which makes sense and is helpful. If they "git push", they get an + error explaining how to push *and* suggesting they specify + --set-upstream - which is exactly the right thing to do for them. + + This new option is likely not appropriate for users intentionally + implementing a "triangular workflow" with a shared upstream tracking + branch, that they "git pull" in and a "private" feature branch that + they push/force-push to just for remote safe-keeping until they are + ready to push up to the shared branch explicitly/separately. Such + users are likely to prefer keeping the current default + merge.autosetupmerge=true behavior, and change their push.default to + "current". Signed-off-by: Tao Klerks <tao@klerks.biz> 2: c16a8fe01e7 ! 2: d5b18c7949f t3200: tests for new branch.autosetupmerge option "simple" @@ Metadata ## Commit message ## t3200: tests for new branch.autosetupmerge option "simple" - 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. + In the previous commit a new autosetupmerge option was introduced. - 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 + Extend the existing branch tests 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 <tao@klerks.biz> -- gitgitgadget ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches 2022-02-28 7:14 ` [PATCH v3 0/2] adding " Tao Klerks via GitGitGadget @ 2022-02-28 7:14 ` Tao Klerks via GitGitGadget 2022-02-28 10:39 ` Ævar Arnfjörð Bjarmason 2022-02-28 7:14 ` [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-03-21 6:17 ` [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2 siblings, 1 reply; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-28 7:14 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> With the default push.default option, "simple", beginners are protected from accidentally pushing to the "wrong" branch in centralized workflows: if the remote tracking branch they would push to does not have the same name as the local branch, and they try to do a "default push", they get an error and explanation with options. There is a particular centralized workflow where this often happens: a user branches to a new local feature branch from an existing upstream branch, eg with "checkout -b feature1 origin/master". With the default branch.autosetupmerge configuration (value "true"), git will automatically add origin/master as the remote tracking branch. When the user pushes with "git push", they get an error, and (amongst other things) a suggestion to run "git push origin HEAD". Eventually they figure out to add "-u" to change the tracking branch, or they set push.default to "current", or some tooling does one or the other of these things for them. When one of their coworkers works on the same branch, they don't get any of that weirdness. They just "git checkout feature1" and everything works exactly as they expect, with the shared remote branch set up as remote tracking branch, and push and pull working out of the box. The "stable state" for this way of working is that local branches have the same-name remote tracking branch (origin/feature1 in this example), and multiple people can work on that remote feature branch at the same time, trusting "git pull" to merge or rebase as required for them to be able to push their interim changes to that same feature branch on that same remote. (merging from the upstream "master" branch, and merging back to it, are separate more involved processes in this flow). There is a problem in this flow/way of working, however, which is that the first user, when they first branched from origin/master, ended up with the "wrong" remote tracking branch (different from the stable state). For a while, before they pushed (and maybe longer, if they don't use -u/--set-upstream), their "git pull" wasn't getting other users' changes to the feature branch - it was getting any changes from the remote "master" branch instead (a completely different class of changes!) Any experienced git user will presumably say "well yeah, that's what it means to have the remote tracking branch set to origin/master!" - but that user didn't *ask* to have the remote master branch added as remote tracking branch - that just happened automatically when they branched their feature branch. They didn't necessarily even notice or understand the meaning of the "set up to track 'origin/master'" message when they created the branch - especially if they are using a GUI. Looking at how to fix this, you might think "OK, so disable auto setup of remote tracking - set branch.autosetupmerge to false" - but that will inconvenience the *second* user in this story - the one who just wanted to start working on the feature branch. The first and second users swap roles at different points in time of course - they should both have a sane configuration that does the right thing in both situations. Make these flows painless by introducing a new branch.autosetupmerge option called "simple", to match the same-name "push.default" option that makes similar assumptions. This new option automatically sets up tracking in a *subset* of the current default situations: when the original ref is a remote tracking branch *and* has the same branch name on the remote (as the new local branch name). With this new configuration, in the example situation above, the first user does *not* get origin/master set up as the tracking branch for the new local branch. If they "git pull" in their new local-only branch, they get an error explaining there is no upstream branch - which makes sense and is helpful. If they "git push", they get an error explaining how to push *and* suggesting they specify --set-upstream - which is exactly the right thing to do for them. This new option is likely not appropriate for users intentionally implementing a "triangular workflow" with a shared upstream tracking branch, that they "git pull" in and a "private" feature branch that they push/force-push to just for remote safe-keeping until they are ready to push up to the shared branch explicitly/separately. Such users are likely to prefer keeping the current default merge.autosetupmerge=true behavior, and change their push.default to "current". Signed-off-by: Tao Klerks <tao@klerks.biz> --- 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.<name>.remote` and `branch.<name>.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; -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* Re: [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches 2022-02-28 7:14 ` [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-02-28 10:39 ` Ævar Arnfjörð Bjarmason 2022-03-02 9:35 ` Tao Klerks 0 siblings, 1 reply; 41+ messages in thread From: Ævar Arnfjörð Bjarmason @ 2022-02-28 10:39 UTC (permalink / raw) To: Tao Klerks via GitGitGadget; +Cc: git, Tao Klerks On Mon, Feb 28 2022, Tao Klerks via GitGitGadget wrote: I think squashing 2/2 inot this would make this much easier to follow, i.e. to have tests along with the new feature. > + /* > + * 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); This function is the only user of find_tracked_branch(). For e.g. "git checkout we emit"; fatal: builtin/checkout.c:1246: 'foo' matched multiple (4) remote tracking branches Perhaps we can do something similar here, and even with some advise() emit information about what other branches conflicted. > + 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; > + } > + I wondered when reading this if there isn't a way to merge this and the "branch_get" call made in "inherit_tracking" earlier in this function in the "track != BRANCH_TRACK_INHERIT" case. But maybe not, and that whole API entry point is a bit messy in needing to cover both the use-case of an existing branch & nonexisting (i.e. initial creation). ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches 2022-02-28 10:39 ` Ævar Arnfjörð Bjarmason @ 2022-03-02 9:35 ` Tao Klerks 2022-03-20 17:00 ` Tao Klerks 0 siblings, 1 reply; 41+ messages in thread From: Tao Klerks @ 2022-03-02 9:35 UTC (permalink / raw) To: Ævar Arnfjörð Bjarmason; +Cc: Tao Klerks via GitGitGadget, git On Mon, Feb 28, 2022 at 11:56 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote: > > On Mon, Feb 28 2022, Tao Klerks via GitGitGadget wrote: > > I think squashing 2/2 inot this would make this much easier to follow, > i.e. to have tests along with the new feature. > OK! Doing. > > + /* > > + * 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); > > This function is the only user of find_tracked_branch(). For e.g. "git > checkout we emit"; > > fatal: builtin/checkout.c:1246: 'foo' matched multiple (4) remote tracking branches > > Perhaps we can do something similar here I'm not sure what you're pointing to specifically - the fact that the checkout message provides a count? If so I guess I understand/agree, find_tracked_branch() could be enhanced to keep counting rather than exiting at the first sign of trouble, to support such a slightly-more-explicit message here. I'm not convinced that this situation is common enough to warrant change: mapping multiple remotes to the same remote-tracking path seems like a strange setup - is this something we recommend or document anywhere? maybe to have 2 "remotes" that correspond to the same server over different protocols appear as one set of tracking branches? On the other hand I am of course happy to make things better if we think this will do that! > even with some advise() > emit information about what other branches conflicted. I believe the conflict is not about different "branches" exactly, but about *refspecs* that map to the tracking branch. If I understand correctly this change would entail creating a new advice type (and documenting it), and figuring out what the advice should look like - something like "find and disambiguate your fetch refspecs to enable auto tracking setup! If you want to keep your ambiguous refspecs, set auto tracking setup to false!" - but nicer :) > > > + 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; > > + } > > + > > I wondered when reading this if there isn't a way to merge this and the > "branch_get" call made in "inherit_tracking" earlier in this function in > the "track != BRANCH_TRACK_INHERIT" case. > > But maybe not, and that whole API entry point is a bit messy in needing > to cover both the use-case of an existing branch & nonexisting > (i.e. initial creation). Hmm, I had a hard time understanding this comment. I *think* you were saying "why don't you use an existing API to get the full ref name of the new local branch, and compare that to the full name of the remote branch you already have, rather than messing with a "refs/heads/" prefix explicitly/redundantly"... Is that right? ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches 2022-03-02 9:35 ` Tao Klerks @ 2022-03-20 17:00 ` Tao Klerks 0 siblings, 0 replies; 41+ messages in thread From: Tao Klerks @ 2022-03-20 17:00 UTC (permalink / raw) To: Ævar Arnfjörð Bjarmason; +Cc: Tao Klerks via GitGitGadget, git On Wed, Mar 2, 2022 at 10:35 AM Tao Klerks <tao@klerks.biz> wrote: > > On Mon, Feb 28, 2022 at 11:56 AM Ævar Arnfjörð Bjarmason > <avarab@gmail.com> wrote: > > > > > > This function is the only user of find_tracked_branch(). For e.g. "git > > checkout we emit"; > > > > fatal: builtin/checkout.c:1246: 'foo' matched multiple (4) remote tracking branches > > > > Perhaps we can do something similar here > > I'm not sure what you're pointing to specifically - the fact that the > checkout message provides a count? If so I guess I understand/agree, > find_tracked_branch() could be enhanced to keep counting rather than > exiting at the first sign of trouble, to support such a > slightly-more-explicit message here. > > I'm not convinced that this situation is common enough to warrant > change: mapping multiple remotes to the same remote-tracking path > seems like a strange setup - is this something we recommend or > document anywhere? maybe to have 2 "remotes" that correspond to the > same server over different protocols appear as one set of tracking > branches? > > On the other hand I am of course happy to make things better if we > think this will do that! Having finally understood the logic in play here, I now see that find_tracked_branch() does not "exit at the first sign of trouble" as I thought, so there isn't much change required to produce a marginally richer error message here, but I've decided to work on this proposed enhancement in a separate patch. The more I look at this, the less confident I am about exactly the right thing to do - and I'd rather not hold up the (in my opinion) net-good branch.autosetupmerge=simple work. The specific concern I have is about changing the "fatal: Not tracking: ambiguous information for ref refs/remotes/origin/master" message. Having understood when it can occur, I've realized it is probably quite common - I at least have certainly seen it a few times, as the situation it describes is what happens if you copy/paste a "remote" section in your git config file, to create a new remote with the same setup as an existing one, without remembering to adjust the refspec for the new remote name. > > even with some advise() > > emit information about what other branches conflicted. > > I believe the conflict is not about different "branches" exactly, but > about *refspecs* that map to the tracking branch. > > If I understand correctly this change would entail creating a new > advice type (and documenting it), and figuring out what the advice > should look like - something like "find and disambiguate your fetch > refspecs to enable auto tracking setup! If you want to keep your > ambiguous refspecs, set auto tracking setup to false!" - but nicer :) In addition to the mechanics of creating a new advice type, I eventually realized that the right message would list the *remotes* that have refspecs mapping to the same tracking ref - which would mean newly tracking those in the per-remote find_tracked_branch() looping. I initially thought this situation was too rare to warrant this kind of change, but now, understanding how I myself have reached this situation a few times *and it took me a while to understand what I did wrong* (at least the first time), I think it's worthwhile work in and of itself. Expect a new separate patchset sometime. ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" 2022-02-28 7:14 ` [PATCH v3 0/2] adding " Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-02-28 7:14 ` Tao Klerks via GitGitGadget 2022-02-28 9:34 ` Ævar Arnfjörð Bjarmason 2022-03-21 6:17 ` [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2 siblings, 1 reply; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-02-28 7:14 UTC (permalink / raw) To: git; +Cc: Tao Klerks, Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> In the previous commit a new autosetupmerge option was introduced. Extend the existing branch tests 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 <tao@klerks.biz> --- 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 && -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* Re: [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" 2022-02-28 7:14 ` [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget @ 2022-02-28 9:34 ` Ævar Arnfjörð Bjarmason 2022-03-01 2:58 ` Eric Sunshine 2022-03-01 9:59 ` Tao Klerks 0 siblings, 2 replies; 41+ messages in thread From: Ævar Arnfjörð Bjarmason @ 2022-02-28 9:34 UTC (permalink / raw) To: Tao Klerks via GitGitGadget; +Cc: git, Tao Klerks On Mon, Feb 28 2022, Tao Klerks via GitGitGadget wrote: > From: Tao Klerks <tao@klerks.biz> > > In the previous commit a new autosetupmerge option was introduced. > > Extend the existing branch tests 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 <tao@klerks.biz> > --- > 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/* && Shouldn't these use test_config, or if the tests below need them do that via a helper, so later added tests don't need to reset this state? > + git fetch otherserver && > + git branch feature otherserver/feature && > + rm -fr otherserver && Instead of "rm -rf" after, do above: test_when_finished "rm -rf otherserver" && git init otherserver (you don't need "test_create_repo" either, just use "git init") > + test $(git config branch.feature.remote) = otherserver && > + test $(git config branch.feature.merge) = refs/heads/feature Use: echo otherserver >expect && git config ... >actual && test_cmp expect actual etc., the pattern you're using here will hide git's exit code on segfaults, abort() etc., and also makes for less useful debug info on failure than test_cmp. > +' > + > +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/* && ditto config setup above, this is quite hard to follow in sequence since yo uneed to reason about all existing config. Let's start with a clean slate for each test_expect_success and setup the specific config we want instead.fallow since > + (git show-ref -q refs/remotes/local/main || git fetch local) && This likewise hides segfaults etc. Use: test_might_fail git show-ref ... But maybe this whole thing should use "git rev-parse --verify" or something? > + git branch my-other local/main && > + test -z "$(git config branch.my-other.remote)" && > + test -z "$(git config branch.my-other.merge)" ditto test_cmp comments, but here: git ... >out && test_must_be_empty out > +' > + > +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)" ditto above. > +' > + > 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 && ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" 2022-02-28 9:34 ` Ævar Arnfjörð Bjarmason @ 2022-03-01 2:58 ` Eric Sunshine 2022-03-01 9:59 ` Tao Klerks 2022-03-01 9:59 ` Tao Klerks 1 sibling, 1 reply; 41+ messages in thread From: Eric Sunshine @ 2022-03-01 2:58 UTC (permalink / raw) To: Ævar Arnfjörð Bjarmason Cc: Tao Klerks via GitGitGadget, Git List, Tao Klerks On Mon, Feb 28, 2022 at 5:54 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote: > On Mon, Feb 28 2022, Tao Klerks via GitGitGadget wrote: > > + test $(git config branch.feature.remote) = otherserver && > > + test $(git config branch.feature.merge) = refs/heads/feature > > Use: > > echo otherserver >expect && > git config ... >actual && > test_cmp expect actual > > etc., the pattern you're using here will hide git's exit code on > segfaults, abort() etc., and also makes for less useful debug info on > failure than test_cmp. Better yet, use test_cmp_config(): test_cmp_config otherserver branch.feature.remote && ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" 2022-03-01 2:58 ` Eric Sunshine @ 2022-03-01 9:59 ` Tao Klerks 0 siblings, 0 replies; 41+ messages in thread From: Tao Klerks @ 2022-03-01 9:59 UTC (permalink / raw) To: Eric Sunshine Cc: Ævar Arnfjörð Bjarmason, Tao Klerks via GitGitGadget, Git List On Tue, Mar 1, 2022 at 3:59 AM Eric Sunshine <sunshine@sunshineco.com> wrote: > > On Mon, Feb 28, 2022 at 5:54 AM Ævar Arnfjörð Bjarmason > <avarab@gmail.com> wrote: > > On Mon, Feb 28 2022, Tao Klerks via GitGitGadget wrote: > > > + test $(git config branch.feature.remote) = otherserver && > > > + test $(git config branch.feature.merge) = refs/heads/feature > > > > Use: > > > > echo otherserver >expect && > > git config ... >actual && > > test_cmp expect actual > > > > etc., the pattern you're using here will hide git's exit code on > > segfaults, abort() etc., and also makes for less useful debug info on > > failure than test_cmp. > > Better yet, use test_cmp_config(): > > test_cmp_config otherserver branch.feature.remote && Noted, thx. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" 2022-02-28 9:34 ` Ævar Arnfjörð Bjarmason 2022-03-01 2:58 ` Eric Sunshine @ 2022-03-01 9:59 ` Tao Klerks 1 sibling, 0 replies; 41+ messages in thread From: Tao Klerks @ 2022-03-01 9:59 UTC (permalink / raw) To: Ævar Arnfjörð Bjarmason; +Cc: Tao Klerks via GitGitGadget, git On Mon, Feb 28, 2022 at 10:39 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote: > > > On Mon, Feb 28 2022, Tao Klerks via GitGitGadget wrote: > > > +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/* && > > Shouldn't these use test_config, or if the tests below need them do that > via a helper, so later added tests don't need to reset this state? Yes, I will look at this; I was naively (and clearly incorrectly) following a pattern I saw in this same test file. > > > + git fetch otherserver && > > + git branch feature otherserver/feature && > > + rm -fr otherserver && > > Instead of "rm -rf" after, do above: > > test_when_finished "rm -rf otherserver" && > git init otherserver > > (you don't need "test_create_repo" either, just use "git init") Will do, thx! > > > + test $(git config branch.feature.remote) = otherserver && > > + test $(git config branch.feature.merge) = refs/heads/feature > > Use: > > echo otherserver >expect && > git config ... >actual && > test_cmp expect actual > > etc., the pattern you're using here will hide git's exit code on > segfaults, abort() etc., and also makes for less useful debug info on > failure than test_cmp. Again, thank you! (I will look at test_cmp_config() as Eric suggested) > > > > +' > > + > > +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/* && > > ditto config setup above, this is quite hard to follow in sequence since > yo uneed to reason about all existing config. Let's start with a clean > slate for each test_expect_success and setup the specific config we want > instead.fallow since > > > + (git show-ref -q refs/remotes/local/main || git fetch local) && > > This likewise hides segfaults etc. Use: > > test_might_fail git show-ref ... > > But maybe this whole thing should use "git rev-parse --verify" or > something? Honestly, I think this bad pattern is just a premature optimization against a pretty-fast local fetch. Will simplify, and do the same for existing patterns in this file. > > > + git branch my-other local/main && > > + test -z "$(git config branch.my-other.remote)" && > > + test -z "$(git config branch.my-other.merge)" > > ditto test_cmp comments, but here: > > git ... >out && > test_must_be_empty out > OK, will look, thx. ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-02-28 7:14 ` [PATCH v3 0/2] adding " Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget @ 2022-03-21 6:17 ` Tao Klerks via GitGitGadget 2022-04-18 18:15 ` Josh Steadmon 2022-04-29 9:56 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget 2 siblings, 2 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-03-21 6:17 UTC (permalink / raw) To: git Cc: Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine, Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> With the default push.default option, "simple", beginners are protected from accidentally pushing to the "wrong" branch in centralized workflows: if the remote tracking branch they would push to does not have the same name as the local branch, and they try to do a "default push", they get an error and explanation with options. There is a particular centralized workflow where this often happens: a user branches to a new local feature branch from an existing upstream branch, eg with "checkout -b feature1 origin/master". With the default branch.autosetupmerge configuration (value "true"), git will automatically add origin/master as the remote tracking branch. When the user pushes with "git push", they get an error, and (amongst other things) a suggestion to run "git push origin HEAD". Eventually they figure out to add "-u" to change the tracking branch, or they set push.default to "current", or some tooling does one or the other of these things for them. When one of their coworkers works on the same branch, they don't get any of that weirdness. They just "git checkout feature1" and everything works exactly as they expect, with the shared remote branch set up as remote tracking branch, and push and pull working out of the box. The "stable state" for this way of working is that local branches have the same-name remote tracking branch (origin/feature1 in this example), and multiple people can work on that remote feature branch at the same time, trusting "git pull" to merge or rebase as required for them to be able to push their interim changes to that same feature branch on that same remote. (merging from the upstream "master" branch, and merging back to it, are separate more involved processes in this flow). There is a problem in this flow/way of working, however, which is that the first user, when they first branched from origin/master, ended up with the "wrong" remote tracking branch (different from the stable state). For a while, before they pushed (and maybe longer, if they don't use -u/--set-upstream), their "git pull" wasn't getting other users' changes to the feature branch - it was getting any changes from the remote "master" branch instead (a completely different class of changes!) Any experienced git user will presumably say "well yeah, that's what it means to have the remote tracking branch set to origin/master!" - but that user didn't *ask* to have the remote master branch added as remote tracking branch - that just happened automatically when they branched their feature branch. They didn't necessarily even notice or understand the meaning of the "set up to track 'origin/master'" message when they created the branch - especially if they are using a GUI. Looking at how to fix this, you might think "OK, so disable auto setup of remote tracking - set branch.autosetupmerge to false" - but that will inconvenience the *second* user in this story - the one who just wanted to start working on the feature branch. The first and second users swap roles at different points in time of course - they should both have a sane configuration that does the right thing in both situations. Make these flows painless by introducing a new branch.autosetupmerge option called "simple", to match the same-name "push.default" option that makes similar assumptions. This new option automatically sets up tracking in a *subset* of the current default situations: when the original ref is a remote tracking branch *and* has the same branch name on the remote (as the new local branch name). With this new configuration, in the example situation above, the first user does *not* get origin/master set up as the tracking branch for the new local branch. If they "git pull" in their new local-only branch, they get an error explaining there is no upstream branch - which makes sense and is helpful. If they "git push", they get an error explaining how to push *and* suggesting they specify --set-upstream - which is exactly the right thing to do for them. This new option is likely not appropriate for users intentionally implementing a "triangular workflow" with a shared upstream tracking branch, that they "git pull" in and a "private" feature branch that they push/force-push to just for remote safe-keeping until they are ready to push up to the shared branch explicitly/separately. Such users are likely to prefer keeping the current default merge.autosetupmerge=true behavior, and change their push.default to "current". Also extend the existing branch tests 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 <tao@klerks.biz> --- adding new branch.autosetupmerge option "simple" This patchset 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. Changes since v3: * squashed new-tests commit into main changes, as per Ævar's advice * added some hopefully-helpful comments in some prior existing code * improved tests to use better idioms following Ævar and Eric's advice Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1161%2FTaoK%2Ffeature-branch-autosetupmerge-simple-v4 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1161/TaoK/feature-branch-autosetupmerge-simple-v4 Pull-Request: https://github.com/gitgitgadget/git/pull/1161 Range-diff vs v3: 1: 0b5d4789512 ! 1: eca8ab2eb7b merge: new autosetupmerge option 'simple' for matching branches @@ Commit message merge.autosetupmerge=true behavior, and change their push.default to "current". + Also extend the existing branch tests 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 <tao@klerks.biz> ## Documentation/config/branch.txt ## @@ Documentation/git-branch.txt: The exact upstream branch is chosen depending on t how the `branch.<name>.remote` and `branch.<name>.merge` options are used. ## branch.c ## +@@ branch.c: static int find_tracked_branch(struct remote *remote, void *priv) + free(tracking->spec.src); + string_list_clear(tracking->srcs, 0); + } ++ /* remote_find_tracking() searches by src if present */ + tracking->spec.src = NULL; + } +- + return 0; + } + @@ branch.c: static void setup_tracking(const char *new_ref, const char *orig_ref, + + if (!tracking.matches) + switch (track) { ++ /* If ref is not remote, still use local */ + case BRANCH_TRACK_ALWAYS: + case BRANCH_TRACK_EXPLICIT: + case BRANCH_TRACK_OVERRIDE: ++ /* Remote matches not evaluated */ + case BRANCH_TRACK_INHERIT: + break; ++ /* Otherwise, if no remote don't track */ + default: 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. ++ * This check does not apply to BRANCH_TRACK_INHERIT; ++ * that supports multiple entries in tracking_srcs but ++ * leaves tracking.matches at 0. + */ if (tracking.matches > 1) die(_("not tracking: ambiguous information for ref %s"), @@ branch.c: static void setup_tracking(const char *new_ref, const char *orig_ref, + * 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). ++ * one entry (because only BRANCH_TRACK_INHERIT can ++ * produce more than one entry). + */ + const char *tracked_branch; + if (!skip_prefix(tracking.srcs->items[0].string, @@ config.c: static int git_default_branch_config(const char *var, const char *valu } git_branch_track = git_config_bool(var, value); return 0; + + ## t/t3200-branch.sh ## +@@ t/t3200-branch.sh: 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_when_finished "rm -rf otherserver" && ++ git init otherserver && ++ test_commit -C otherserver my_commit 1 && ++ git -C otherserver branch feature && ++ test_config branch.autosetupmerge simple && ++ test_config remote.otherserver.url otherserver && ++ test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* && ++ git fetch otherserver && ++ git branch feature otherserver/feature && ++ test_cmp_config otherserver branch.feature.remote && ++ test_cmp_config refs/heads/feature branch.feature.merge ++' ++ ++test_expect_success 'simple tracking skips when remote branch name does not match' ' ++ test_config branch.autosetupmerge simple && ++ test_config remote.local.url . && ++ test_config remote.local.fetch refs/heads/*:refs/remotes/local/* && ++ git fetch local && ++ git branch my-other local/main && ++ test_cmp_config "" --default "" branch.my-other.remote && ++ test_cmp_config "" --default "" branch.my-other.merge ++' ++ ++test_expect_success 'simple tracking skips when remote ref is not a branch' ' ++ test_config branch.autosetupmerge simple && ++ test_config remote.localtags.url . && ++ test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* && ++ git tag mytag12 main && ++ git fetch localtags && ++ git branch mytag12 localtags/mytag12 && ++ test_cmp_config "" --default "" branch.mytag12.remote && ++ test_cmp_config "" --default "" 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 && 2: d5b18c7949f < -: ----------- t3200: tests for new branch.autosetupmerge option "simple" Documentation/config/branch.txt | 4 +++- Documentation/git-branch.txt | 18 ++++++++++------- branch.c | 25 ++++++++++++++++++++++- branch.h | 1 + config.c | 3 +++ t/t3200-branch.sh | 35 +++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 9 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.<name>.remote` and `branch.<name>.merge` options are used. diff --git a/branch.c b/branch.c index 6b31df539a5..86ea91e76f8 100644 --- a/branch.c +++ b/branch.c @@ -30,9 +30,9 @@ static int find_tracked_branch(struct remote *remote, void *priv) free(tracking->spec.src); string_list_clear(tracking->srcs, 0); } + /* remote_find_tracking() searches by src if present */ tracking->spec.src = NULL; } - return 0; } @@ -243,19 +243,42 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, if (!tracking.matches) switch (track) { + /* If ref is not remote, still use local */ case BRANCH_TRACK_ALWAYS: case BRANCH_TRACK_EXPLICIT: case BRANCH_TRACK_OVERRIDE: + /* Remote matches not evaluated */ case BRANCH_TRACK_INHERIT: break; + /* Otherwise, if no remote don't track */ default: goto cleanup; } + /* + * This check does not apply to BRANCH_TRACK_INHERIT; + * that supports multiple entries in tracking_srcs but + * leaves tracking.matches at 0. + */ 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 only BRANCH_TRACK_INHERIT can + * produce more than one entry). + */ + 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 e78397725c9..8de87400085 100644 --- a/config.c +++ b/config.c @@ -1686,6 +1686,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; diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 7a0ff75ba86..7a5a44a1ebf 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_when_finished "rm -rf otherserver" && + git init otherserver && + test_commit -C otherserver my_commit 1 && + git -C otherserver branch feature && + test_config branch.autosetupmerge simple && + test_config remote.otherserver.url otherserver && + test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* && + git fetch otherserver && + git branch feature otherserver/feature && + test_cmp_config otherserver branch.feature.remote && + test_cmp_config refs/heads/feature branch.feature.merge +' + +test_expect_success 'simple tracking skips when remote branch name does not match' ' + test_config branch.autosetupmerge simple && + test_config remote.local.url . && + test_config remote.local.fetch refs/heads/*:refs/remotes/local/* && + git fetch local && + git branch my-other local/main && + test_cmp_config "" --default "" branch.my-other.remote && + test_cmp_config "" --default "" branch.my-other.merge +' + +test_expect_success 'simple tracking skips when remote ref is not a branch' ' + test_config branch.autosetupmerge simple && + test_config remote.localtags.url . && + test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* && + git tag mytag12 main && + git fetch localtags && + git branch mytag12 localtags/mytag12 && + test_cmp_config "" --default "" branch.mytag12.remote && + test_cmp_config "" --default "" 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 && base-commit: 74cc1aa55f30ed76424a0e7226ab519aa6265061 -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-03-21 6:17 ` [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-04-18 18:15 ` Josh Steadmon 2022-04-20 5:12 ` Tao Klerks 2022-04-29 9:56 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget 1 sibling, 1 reply; 41+ messages in thread From: Josh Steadmon @ 2022-04-18 18:15 UTC (permalink / raw) To: Tao Klerks via GitGitGadget Cc: git, Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine On 2022.03.21 06:17, Tao Klerks via GitGitGadget wrote: > From: Tao Klerks <tao@klerks.biz> > > With the default push.default option, "simple", beginners are > protected from accidentally pushing to the "wrong" branch in > centralized workflows: if the remote tracking branch they would push > to does not have the same name as the local branch, and they try to do > a "default push", they get an error and explanation with options. > > There is a particular centralized workflow where this often happens: > a user branches to a new local feature branch from an existing > upstream branch, eg with "checkout -b feature1 origin/master". With > the default branch.autosetupmerge configuration (value "true"), git > will automatically add origin/master as the remote tracking branch. > > When the user pushes with "git push", they get an error, and (amongst > other things) a suggestion to run "git push origin HEAD". Eventually > they figure out to add "-u" to change the tracking branch, or they set > push.default to "current", or some tooling does one or the other of > these things for them. > > When one of their coworkers works on the same branch, they don't get > any of that weirdness. They just "git checkout feature1" and > everything works exactly as they expect, with the shared remote branch > set up as remote tracking branch, and push and pull working out of the > box. > > The "stable state" for this way of working is that local branches have > the same-name remote tracking branch (origin/feature1 in this > example), and multiple people can work on that remote feature branch > at the same time, trusting "git pull" to merge or rebase as required > for them to be able to push their interim changes to that same feature > branch on that same remote. > > (merging from the upstream "master" branch, and merging back to it, > are separate more involved processes in this flow). > > There is a problem in this flow/way of working, however, which is that > the first user, when they first branched from origin/master, ended up > with the "wrong" remote tracking branch (different from the stable > state). For a while, before they pushed (and maybe longer, if they > don't use -u/--set-upstream), their "git pull" wasn't getting other > users' changes to the feature branch - it was getting any changes from > the remote "master" branch instead (a completely different class of > changes!) > > Any experienced git user will presumably say "well yeah, that's what > it means to have the remote tracking branch set to origin/master!" - > but that user didn't *ask* to have the remote master branch added as > remote tracking branch - that just happened automatically when they > branched their feature branch. They didn't necessarily even notice or > understand the meaning of the "set up to track 'origin/master'" > message when they created the branch - especially if they are using a > GUI. > > Looking at how to fix this, you might think "OK, so disable auto setup > of remote tracking - set branch.autosetupmerge to false" - but that > will inconvenience the *second* user in this story - the one who just > wanted to start working on the feature branch. The first and second > users swap roles at different points in time of course - they should > both have a sane configuration that does the right thing in both > situations. > > Make these flows painless by introducing a new branch.autosetupmerge > option called "simple", to match the same-name "push.default" option > that makes similar assumptions. > > This new option automatically sets up tracking in a *subset* of the > current default situations: when the original ref is a remote tracking > branch *and* has the same branch name on the remote (as the new local > branch name). > > With this new configuration, in the example situation above, the first > user does *not* get origin/master set up as the tracking branch for > the new local branch. If they "git pull" in their new local-only > branch, they get an error explaining there is no upstream branch - > which makes sense and is helpful. If they "git push", they get an > error explaining how to push *and* suggesting they specify > --set-upstream - which is exactly the right thing to do for them. > > This new option is likely not appropriate for users intentionally > implementing a "triangular workflow" with a shared upstream tracking > branch, that they "git pull" in and a "private" feature branch that > they push/force-push to just for remote safe-keeping until they are > ready to push up to the shared branch explicitly/separately. Such > users are likely to prefer keeping the current default > merge.autosetupmerge=true behavior, and change their push.default to > "current". I think this is a good solution for relatively inexperienced users, and I don't see any issues with the implementation or tests. However, I wonder how users for whom this may be useful are going to discover this option? I don't expect that such users are going to be watching Git's release notes looking for new features such as this, or carefully reading documentation changes. In the discussion on v3 of this series, you mentioned you were thinking about adding an advice setting to point users here; is there a reason why that didn't make it into v4? It seems appropriate to me to add one, perhaps at the point where a user with "autosetupmerge=true" would run into a failure when trying to push? ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-18 18:15 ` Josh Steadmon @ 2022-04-20 5:12 ` Tao Klerks 2022-04-20 17:19 ` Josh Steadmon 2022-04-20 17:43 ` Junio C Hamano 0 siblings, 2 replies; 41+ messages in thread From: Tao Klerks @ 2022-04-20 5:12 UTC (permalink / raw) To: Josh Steadmon, Tao Klerks via GitGitGadget, git, Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine On Mon, Apr 18, 2022 at 8:15 PM Josh Steadmon <steadmon@google.com> wrote: > > > I think this is a good solution for relatively inexperienced users, and > I don't see any issues with the implementation or tests. Yay, thanks for the feedback! > However, I > wonder how users for whom this may be useful are going to discover this > option? I don't expect that such users are going to be watching Git's > release notes looking for new features such as this, or carefully > reading documentation changes. Honestly, I was being a bit selfish here - I effectively control the gitconfig of "my" users, so I was planning on enabling this by default and letting it "settle in" in git at large, eventually proposing to change the default. I understand/agree that this is a little naive - if no-one has reason to try the new behavior, very little information as to its usefulness/appropriateness is likely to emerge, and it will never be an obviously good idea to change the default. > > In the discussion on v3 of this series, you mentioned you were thinking > about adding an advice setting to point users here; is there a reason > why that didn't make it into v4? The advice I mentioned I would work on wasn't actually about this new setting/behavior, but rather about the previously existing (and reasonably unrelated) "not tracking: ambiguous information for ref" error, which I found to be unreasonably cryptic. I submitted that advice change as https://lore.kernel.org/git/pull.1183.v7.git.1648793113943.gitgitgadget@gmail.com/, and it's gone out in a recent release. > It seems appropriate to me to add one, > perhaps at the point where a user with "autosetupmerge=true" would run > into a failure when trying to push? Having thought about this a bit, I agree. On the one hand I'm a little nervous about adding this kind of public behavior change as I would imagine it's more likely to encounter resistance here, on the other hand I do think it will make the changes themselves much more useful. Also, this patchset hasn't moved in a while, so "holding it up" with new changes may not be a significant concern. the current advice looks something like: --- fatal: The upstream branch of your current branch does not match the name of your current branch. To push to the upstream branch on the remote, use git push origin HEAD:master To push to the branch of the same name on the remote, use git push origin HEAD To choose either option permanently, see push.default in 'git help config'. --- I would propose to add one sentence at the end along the lines of: --- To instead avoid automatically configuring upstream branches when their name doesn't match the local branch, see option 'simple' of branch.autosetupmerge in 'git help config'. --- Does that make sense to you? ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-20 5:12 ` Tao Klerks @ 2022-04-20 17:19 ` Josh Steadmon 2022-04-20 17:43 ` Junio C Hamano 1 sibling, 0 replies; 41+ messages in thread From: Josh Steadmon @ 2022-04-20 17:19 UTC (permalink / raw) To: Tao Klerks Cc: Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine On 2022.04.20 07:12, Tao Klerks wrote: > On Mon, Apr 18, 2022 at 8:15 PM Josh Steadmon <steadmon@google.com> wrote: > > It seems appropriate to me to add one, > > perhaps at the point where a user with "autosetupmerge=true" would run > > into a failure when trying to push? > > Having thought about this a bit, I agree. On the one hand I'm a little > nervous about adding this kind of public behavior change as I would > imagine it's more likely to encounter resistance here, on the other > hand I do think it will make the changes themselves much more useful. > Also, this patchset hasn't moved in a while, so "holding it up" with > new changes may not be a significant concern. > > the current advice looks something like: > --- > fatal: The upstream branch of your current branch does not match > the name of your current branch. To push to the upstream branch > on the remote, use > > git push origin HEAD:master > > To push to the branch of the same name on the remote, use > > git push origin HEAD > > To choose either option permanently, see push.default in 'git help config'. > --- > > I would propose to add one sentence at the end along the lines of: > --- > To instead avoid automatically configuring upstream branches when > their name doesn't match the local branch, see option 'simple' of > branch.autosetupmerge in 'git help config'. > --- > > Does that make sense to you? Sounds good to me. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-20 5:12 ` Tao Klerks 2022-04-20 17:19 ` Josh Steadmon @ 2022-04-20 17:43 ` Junio C Hamano 2022-04-20 21:31 ` Tao Klerks 1 sibling, 1 reply; 41+ messages in thread From: Junio C Hamano @ 2022-04-20 17:43 UTC (permalink / raw) To: Tao Klerks Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine Tao Klerks <tao@klerks.biz> writes: >> However, I >> wonder how users for whom this may be useful are going to discover this >> option? I don't expect that such users are going to be watching Git's >> release notes looking for new features such as this, or carefully >> reading documentation changes. > > Honestly, I was being a bit selfish here - I effectively control the > gitconfig of "my" users, so I was planning on enabling this by default > and letting it "settle in" in git at large, eventually proposing to > change the default. I am afraid that it is double disservice to your users. Once they graduate your organization, they notice that their Git does not work as they expect and puzzled. > ... > To choose either option permanently, see push.default in 'git help config'. > --- > > I would propose to add one sentence at the end along the lines of: > --- > To instead avoid automatically configuring upstream branches when > their name doesn't match the local branch, see option 'simple' of > branch.autosetupmerge in 'git help config'. > --- > > Does that make sense to you? Two questions. - If a user follows the push.default advice, does it have any advantage to set branch.autosetupmerge=simple at all? - If a user follows the branch.autosetupmerge=simple advice, what happens their "git push" on a branch that the .merge is not set due to this configuration? Shouldn't they have to set up the push.default for these branches anyway? While it might be a good thing to mention branch.autosetupmerge configuration variable, I am not sure if "To instead avoid" is a good thing to say here. It sounds as if the user can ignore push.default as long as branch.autosetupmerge is taken care of, but I suspect that is not the case. Setting the latter to 'simple' means there are *MORE* branches that do not have .remote/.merge set up, doesn't it? Which in turn means that we are relying more on what push.default is set to, right? ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-20 17:43 ` Junio C Hamano @ 2022-04-20 21:31 ` Tao Klerks 2022-04-21 1:53 ` Junio C Hamano 0 siblings, 1 reply; 41+ messages in thread From: Tao Klerks @ 2022-04-20 21:31 UTC (permalink / raw) To: Junio C Hamano Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine On Wed, Apr 20, 2022 at 7:43 PM Junio C Hamano <gitster@pobox.com> wrote: > > Tao Klerks <tao@klerks.biz> writes: > > > ... > > To choose either option permanently, see push.default in 'git help config'. > > --- > > > > I would propose to add one sentence at the end along the lines of: > > --- > > To instead avoid automatically configuring upstream branches when > > their name doesn't match the local branch, see option 'simple' of > > branch.autosetupmerge in 'git help config'. > > --- > > > > Does that make sense to you? > > Two questions. > > - If a user follows the push.default advice, does it have any > advantage to set branch.autosetupmerge=simple at all? Probably not? It really depends what they set push.default to: * If they set it to upstream/tracking, then branch.autosetupmerge=simple doesn't make much sense. You can set both, but the outcome is effectively the same as setting push.default to simple - not very useful. * If they set it to "current", then it probably doesn't make sense because what they're angling for is probably a triangular workflow, which branch.autosetupmerge=simple very explicitly doesn't support / doesn't make sense for. "matching" seems to be an extreme version of the same setup. * If they set it to "nothing" I'm not sure - I haven't understood in what workflows that makes sense. Generally, I expect that branch.autosetupmerge=simple makes the most sense with push.default left at the default of "simple", for... "simple" workflows :) > > - If a user follows the branch.autosetupmerge=simple advice, what > happens their "git push" on a branch that the .merge is not set > due to this configuration? Shouldn't they have to set up the > push.default for these branches anyway? If the user follows the branch.autosetupmerge=simple advice (and leaves push.default at the "simple" default), what they get at push time will depend on whether they branched from a same-name remote branch or anything else: If they branched from a same-name remote branch, their "git push" will be perfectly uneventful / unsurprising: they will simply push to the remote branch. This is the same as without branch.autosetupmerge=simple. If they branched from a different-name remote branch (they created an new / independent local branch), then no remote tracking relationship will have been set up, and instead of the "fatal: The upstream branch of your current branch does not match the name of your current branch" error and advice, they will get a much simpler error and advice: --- fatal: The current branch whatevs has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin whatevs --- When they follow those instructions, they will be in the "simple" setup same as if they had just branched from same-name. Importantly, as soon as they enable branch.autosetupmerge=simple, they never see the original mismatching-name error and advice anymore - they never again end up with mismatching names at all. (except in edge cases like branch renames) > > While it might be a good thing to mention branch.autosetupmerge > configuration variable, I am not sure if "To instead avoid" is a > good thing to say here. It sounds as if the user can ignore > push.default as long as branch.autosetupmerge is taken care of, but > I suspect that is not the case. I disagree. If they get that error and advice, then their push.default is set to "simple". If they then set their branch.autosetupmerge to "simple" also, this is the simple coherent setup that I, at least, would recommend to non-experts. > Setting the latter to 'simple' > means there are *MORE* branches that do not have .remote/.merge set > up, doesn't it? Which in turn means that we are relying more on > what push.default is set to, right? No - the idea here is that instead of telling push.default to do something *independent* of the tracking branch (like, for example, "current"), the setup the user ends up with is one where the tracking branch, if there is one, is always the same-name where you will push to. When you create a new branch (by branching with a new name), your new branch doesn't initially have an upstream tracking branch - and that's right and correct, there's literally nothing on the server for you to track yet - but the first time you push, the (existing) advice encourages you to set up that tracking relationship. In this flow you very explicitly *don't* rely on push.default, because you never want to end up in a confusing (un-simple) situation where what you're pulling from and what you're pushing to aren't the same thing - a triangular workflow. The "push the current branch and set the remote as upstream" advice is consistent with how many/most GUIs will handle first push for a branch that does not have an upstream tracking relationship yet - GUIs will typically automatically specify (or set the UI default to) the "--set-upstream" option on that first push. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-20 21:31 ` Tao Klerks @ 2022-04-21 1:53 ` Junio C Hamano 2022-04-21 10:04 ` Tao Klerks 0 siblings, 1 reply; 41+ messages in thread From: Junio C Hamano @ 2022-04-21 1:53 UTC (permalink / raw) To: Tao Klerks Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine Tao Klerks <tao@klerks.biz> writes: > If they branched from a different-name remote branch (they created an > new / independent local branch), then no remote tracking relationship > will have been set up, and instead of the "fatal: The upstream branch > of your current branch does not match > the name of your current branch" error and advice, they will get a > much simpler error and advice: > > --- > fatal: The current branch whatevs has no upstream branch. > To push the current branch and set the remote as upstream, use > > git push --set-upstream origin whatevs > --- > > When they follow those instructions, they will be in the "simple" > setup same as if they had just branched from same-name. Which means that they need to see an error once, offered to either set push.default or branch.autosetupmerge (it is not "and/or", but "or", because you want to tell them to set "instead of push.default, set branch.autosetupmerge"), and if they follow the latter, they have to then hit a different error and be told to do the "set-upstream" individually. I am wondering if that is more irritating than it is worth. Instead, if you tell them to use branch.autosetupmerge=simple and use push.default to something better than simple, wouldn't that cover more cases and give fewer roadblocks to the end-user with unnecessary errors? >> Setting the latter to 'simple' >> means there are *MORE* branches that do not have .remote/.merge set >> up, doesn't it? Which in turn means that we are relying more on >> what push.default is set to, right? > > No Why no? if setupauto is yes, then any new branch forked from a remote-tracking branch will get .remote/.merge set up, and with these specific configuration they can "push" back to the configured place. If it is set to simple, only new branches forked from a remote-tracking branch that happens to have the same name will get it, and others do not get .remote/.merge set up. Which means user's "git push" will then consult push.default settings, and setting it right becomes more important, no? ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-21 1:53 ` Junio C Hamano @ 2022-04-21 10:04 ` Tao Klerks 2022-04-22 2:27 ` Junio C Hamano 0 siblings, 1 reply; 41+ messages in thread From: Tao Klerks @ 2022-04-21 10:04 UTC (permalink / raw) To: Junio C Hamano Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine On Thu, Apr 21, 2022 at 3:53 AM Junio C Hamano <gitster@pobox.com> wrote: > > Tao Klerks <tao@klerks.biz> writes: > > > If they branched from a different-name remote branch (they created an > > new / independent local branch), then no remote tracking relationship > > will have been set up, and instead of the "fatal: The upstream branch > > of your current branch does not match > > the name of your current branch" error and advice, they will get a > > much simpler error and advice: > > > > --- > > fatal: The current branch whatevs has no upstream branch. > > To push the current branch and set the remote as upstream, use > > > > git push --set-upstream origin whatevs > > --- > > > > When they follow those instructions, they will be in the "simple" > > setup same as if they had just branched from same-name. > > Which means that they need to see an error once, offered to either > set push.default or branch.autosetupmerge (it is not "and/or", but > "or", because you want to tell them to set "instead of push.default, > set branch.autosetupmerge"), and if they follow the latter, they have > to then hit a different error and be told to do the "set-upstream" > individually. They don't *have* to hit that error, they can set --set-upstream pre-emptively, but if they're "just following prompts" then that's what happens, yes. > I am wondering if that is more irritating than it is > worth. Instead, if you tell them to use branch.autosetupmerge=simple > and use push.default to something better than simple, wouldn't that > cover more cases and give fewer roadblocks to the end-user with > unnecessary errors? I think you're on to something I missed here. Unfortunately, I'm not sure what "something better than simple" for push.default actually is, in the current system. The most obvious option is to set it to "current", so: - you only get branch-time tracking for same-name branches because of branch.autosetupmerge=simple, and - you always get same-name pushes regardless of whether there is an upstream or not thanks to push.default, so you never see a "do this other thing to push" message... But then you have a new problem: While new branches push consistently, they never have an upstream tracking ref! This in turn means these no-tracking-ref branches, although they push smoothly, do not show ahead/behind state in "git status", and simply don't support a regular "git pull". That's not "simple". Where I think you're onto something, is that I believe there *should* be a way to say "if I request a default push and there is *no* remote tracking branch, then just push to the first remote, using the same branch name, *and set up tracking*". Now that would be simple. I don't know whether that behavior would require yet another push.default value, or if there's a better way of integrating it into the existing options/behaviors. I'm also not sure what should happen, in this scheme, if I happened to clash/overlap with an existing remote tracking branch. But this does seem like where I would like to end up. > > >> Setting the latter to 'simple' > >> means there are *MORE* branches that do not have .remote/.merge set > >> up, doesn't it? Which in turn means that we are relying more on > >> what push.default is set to, right? > > > > No > > Why no? if setupauto is yes, then any new branch forked from a > remote-tracking branch will get .remote/.merge set up, and with these > specific configuration they can "push" back to the configured place. > If it is set to simple, only new branches forked from a remote-tracking > branch that happens to have the same name will get it, and others do > not get .remote/.merge set up. But as long as push.default is set to "simple", *which is the only way you get the above message ever*, those cases where the new setupauto option avoids a tracking branch altogether simply change the error message from "your remote branch name does not match - you have lots of options" to "you do not have a remote branch yet - push like this (and you'll be all set for this branch henceforth)". Insofar as you can only ever get the "you might want to set setupauto to simple" message when push.default is set to simple, the set of cases where you get an error on push ends up being the exact same set of cases - you just get a clearer more sensible error. > Which means user's "git push" will then > consult push.default settings, and setting it right becomes more > important, no? If there were another push.default option that led to more automatic *and* correct outcomes, I would agree - and I believe that pursuing the existence of such an option makes sense. Do you agree that none of the push.default options available today are "right" for this flow? Do you have a preference or opinion as to whether: * push.default=current should be changed to set up tracking when absent, or * push.default=simple should be changed to "simply" push and set up tracking when there is no tracking, or * a new push.default option should be introduced for this behavior, or * some other configuration should be introduced to specify "and set up tracking on default push if missing" (and if so, under what circumstances should it kick in?) ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-21 10:04 ` Tao Klerks @ 2022-04-22 2:27 ` Junio C Hamano 2022-04-22 9:24 ` Tao Klerks 0 siblings, 1 reply; 41+ messages in thread From: Junio C Hamano @ 2022-04-22 2:27 UTC (permalink / raw) To: Tao Klerks Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine Tao Klerks <tao@klerks.biz> writes: >> I am wondering if that is more irritating than it is >> worth. Instead, if you tell them to use branch.autosetupmerge=simple >> and use push.default to something better than simple, wouldn't that >> cover more cases and give fewer roadblocks to the end-user with >> unnecessary errors? > > I think you're on to something I missed here. > > Unfortunately, I'm not sure what "something better than simple" for > push.default actually is, in the current system. "none", probably. Much better than "current" that can create new branches on the other side, which you would want to do with an explicit end-user instruction (i.e. not with "git push", but with "git push origin topic"). This depends on what you are really trying to achieve. If we think it through, perhaps it may turn out to be a combination of a bit flawed workflow with a bit inadequate toolset. With "simple" (both in branch.autosetupmerge and push.default), I can see that if you create "main" from their "main" and "maint" from their "maint", you want to see that (1) your "git pull" to integrate what happend on their "main" or "maint" respectively, and (2) your "git push" to push what you did on your "main" to their "main", and "maint" to "maint". But it is totally unclear what you really want to do on "topic" you created this way: $ git checkout -b topic origin/main Currently, with both set to "simple", you do not even get .remote and .merge for the "topic" branch, so your "git pull" simply does not work. And "git push" will also refuse to work. But then why are you [*] forking from origin/main in the first place? What is the purpose you created 'topic' and what do you plan to do with the result you develop on 'topic'? Side note: "you" do not refer to"Tao, the advocate of the 'simple' configuration", but figuratively the user who followed the "simple" route and created topic out of origin/main that is not connected to origin/main. Whatever you commit on topic eventually becomes part of what you'd push to origin or elsewhere. I'd assume it would be origin, because as the user who choose 'simple', you have some branches that you push back to the same name over there. Presumably, those are the primary integration branches the project has, like 'trunk', 'main', 'master', etc. So perhaps the user would have been better off to fork off of the LOCAL branch that would eventually be pushed back? In other words, the above user who created 'topic' would have done $ git checkout -b main origin/main to use as a local integration branch that collects the work you will do locally that is targetted for their 'main' track, so to create a topic that aims to be part of what is pushed back to their 'main' track, you would want to do $ git checkout -b topic main instead? That way, "git push" would either not get .merge/.remote (when branch.autosetupmerge is set to 'true') or point at your local 'main' branch. - The symptom you get from the former is no better than what you get from branch.autosetupmerge=simple but it is not worse. "push" and "pull" refuses to work and suggest you to do something additional. - The latter would make your "git push" and "git pull" on 'topic' to work with your local 'main', treating your 'main' in a way very similar to how you treat your remote 'main' when you are on your own 'main', which is quite reasonable if your change flow is designed to be "work on topic, when the changes on topic proves OK, send that to main, and when the changes on main proves OK, send that to their main". I guess I am esseentially saying that the usefulness of "simple" for branch.autosetupmerge is dubious. > Do you agree that none of the push.default options available today are > "right" for this flow? Do you have a preference or opinion as to > whether: > * push.default=current should be changed to set up tracking when absent, or > * push.default=simple should be changed to "simply" push and set up > tracking when there is no tracking, or > * a new push.default option should be introduced for this behavior, or > * some other configuration should be introduced to specify "and set up > tracking on default push if missing" (and if so, under what > circumstances should it kick in?) None of the above, I guess. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-22 2:27 ` Junio C Hamano @ 2022-04-22 9:24 ` Tao Klerks 2022-04-22 13:27 ` Tao Klerks 2022-04-23 4:44 ` Junio C Hamano 0 siblings, 2 replies; 41+ messages in thread From: Tao Klerks @ 2022-04-22 9:24 UTC (permalink / raw) To: Junio C Hamano Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine On Fri, Apr 22, 2022 at 4:27 AM Junio C Hamano <gitster@pobox.com> wrote: > > Tao Klerks <tao@klerks.biz> writes: > > >> I am wondering if that is more irritating than it is > >> worth. Instead, if you tell them to use branch.autosetupmerge=simple > >> and use push.default to something better than simple, wouldn't that > >> cover more cases and give fewer roadblocks to the end-user with > >> unnecessary errors? > > > > I think you're on to something I missed here. > > > > Unfortunately, I'm not sure what "something better than simple" for > > push.default actually is, in the current system. > > "none", probably. Much better than "current" that can create new > branches on the other side, which you would want to do with an > explicit end-user instruction (i.e. not with "git push", but with > "git push origin topic"). > Hmm, I don't understand you here. You either mean "simple is the best option you could choose for push.default, when branch.autosetupmerge=simple, none of the other options are better", or there's a small typo and you're saying "push.default=nothing would be better". I'll assume the latter, but I'm not sure because I don't see how it can be a good-faith statement. "nothing" is a good setting for someone who needs and/or wants to make a conscious choice about where they push to, *every time* they push, regardless of any remote tracking information. For whom the operational and mental overhead of choosing a target ref to push to every time is less than the cost of defaulting to *any* given workflow or "local to remote branch mapping strategy". How could this possibly be something we recommend or think is generally "best" for arbitrary, especially novice, users?? > This depends on what you are really trying to achieve. If we think > it through, perhaps it may turn out to be a combination of a bit > flawed workflow with a bit inadequate toolset. > > With "simple" (both in branch.autosetupmerge and push.default), I > can see that if you create "main" from their "main" and "maint" from > their "maint", you want to see that > > (1) your "git pull" to integrate what happend on their "main" or > "maint" respectively, and > > (2) your "git push" to push what you did on your "main" to their > "main", and "maint" to "maint". > > But it is totally unclear what you really want to do on "topic" you > created this way: > > $ git checkout -b topic origin/main > The idea of the "simple" workflow which I propose to better support, is that in creating this branch in this way, you are very clearly saying: --- I want to create a new topic branch 'topic' that starts at the current state of 'origin/main': * I will want to be able to "back up" my topic branch by pushing it to the server - and "git push" should do the right thing, just that - push my current branch to the server. * I will want to be able to collaborate with others on this topic branch - after I have pushed, and they have checked out this same branch, we will all be able to push and pull on this branch seamlessly * I will choose whether and when to merge in newer changes from "origin/main", and do so explicitly (not using a simple "git pull") * I will choose whether and when to rebase on top of "origin/main", assuming I work alone on this branch or my collaborators are sufficiently comfortable with rebasing workflows that it will be ok, and do so explicitly (not using a simple "git pull") * I will choose whether and when to push my changes on this branch back to master, and do so by explicitly pushing master after having merged in this topic branch --- I would argue that git generally has a "problem", in that "branch.XXX.merge" entries have two classes of meanings/interpretation: * That is the "parent branch" ** The one I want changes from, when I say "update me with changes" ** The one I eventually want to get my changes to * That is the "remote instance/address" of *this* branch ** If I pull, it's to get changes to the same branch that others might have made ** When I push, it's to get this branch onto the server (not to get my changes into the "upstream") For local-only branches, git currently encourages the former interpretation; when you create the new branch, by default you get the tracking branch set up. As soon as you wish to keep your changes on the remote, however, and especially if you're going to share this topic branch with others, *you have to give up that interpretation*, for that branch! Git does not provide you with any facility to indicate a "parent branch", besides using "branch.XXX.merge" on *local* branches. There's also no way to share a concept of "parent branch" with others, via a remote, inherent in git. You can of course look at the commits on your branch and compare to the commit histories of other branches, you can use naming strategies to indicate an intended parent, etc - but there's no inherent storage and signalling mechanism for this idea of a "parent branch" that you create a topic branch from, with the intent of merging back eventually. I call this a "problem" because, in my experience, it confuses people. The system defaults to setting a "branch.XXX.merge" relationship, presumably in the hopes of being helpful, but as soon as you want to share your topic branch (or even just back it up on the server without jumping through strange hoops), you need to give up that "helpfulness" and switch to the other model where that just stores the remote instance of the same branch, rather than the parent. My proposal here is to support a workflow that accepts, and assumes, that git does not really have the concept of a "parent branch", and that "branch.XXX.merge" relationships exist primarily to support the relationship between local branches and their remote instances. That of course introduces a tradeoff/compromise: What the user loses, in this workflow, is the ability to have many very short-lived local-only branches, all with the same "branch.XXX.merge" upstream, treating that upstream as the "parent" implicitly. This workflow does not of course prevent you or discourage you from creating lots of short-lived local branches - but it does take away the *assumption* that they're local-only, and the corresponding facility to treat the upstream as the thing push and pull should work with. Based on your feedback here, maybe "simple" is not the right name to associate with workflow, its assumptions and tradeoffs - I believe is accurately represents the intent and closely relates to the apparent design intent behind the push.default=simple option, but I'd love proposals as to how to name (and do) it better! > Currently, with both set to "simple", you do not even get .remote > and .merge for the "topic" branch, so your "git pull" simply does > not work. And "git push" will also refuse to work. > That's right - because the assumption is that you've just created a new independent branch - independent by name, and therefore independent by default. You can of course add "--track" if you know what you're doing and know this is a local-only branch and you want it to track what you branched from and have "pull" bring in changes from there (without explicitly specifying so)! > But then why are you [*] forking from origin/main in the first > place? What is the purpose you created 'topic' and what do you > plan to do with the result you develop on 'topic'? The assumption, in this workflow, is that you plan to work on that branch, potentially push to origin to back up or share your work, and will decide explicitly when to merge in changes from the origin you branched (forked) from, or merge changes up there. > > Side note: "you" do not refer to"Tao, the advocate of the > 'simple' configuration", but figuratively the user who > followed the "simple" route and created topic out of > origin/main that is not connected to origin/main. > > Whatever you commit on topic eventually becomes part of what you'd > push to origin or elsewhere. I'd assume it would be origin, because > as the user who choose 'simple', you have some branches that you > push back to the same name over there. Presumably, those are the > primary integration branches the project has, like 'trunk', 'main', > 'master', etc. > > So perhaps the user would have been better off to fork off of the > LOCAL branch that would eventually be pushed back? In other words, > the above user who created 'topic' would have done > > $ git checkout -b main origin/main > (completely beside the point, but they would be more likely to have just done "git checkout main", for the same outcome) > to use as a local integration branch that collects the work you will > do locally that is targetted for their 'main' track, so to create a > topic that aims to be part of what is pushed back to their 'main' > track, you would want to do > > $ git checkout -b topic main > > instead? That way, "git push" would either not get .merge/.remote > (when branch.autosetupmerge is set to 'true') or point at your local > 'main' branch. I'm not sure I understand or agree with what you're saying here with "would [otherwise] point at your local 'main' branch". I have to assume you mean that would be the outcome with "always", while the former would be the outcome with "true" and "false" (and the proposed "simple"), and there would be a third possible outcome with "inherit", where "topic" would end up tracking "origin/main" directly. > > - The symptom you get from the former is no better than what you > get from branch.autosetupmerge=simple but it is not worse. > "push" and "pull" refuses to work and suggest you to do something > additional. In suggesting the user could/should have done that (in order to get a sane workflow, presumably), you are also suggesting that they should keep the state of that "local version of the upstream they eventually want to get their changes into" up-to-date: They should first check out master (for example), pull on master to get the state they expect, and *then* create their new differently-named local branch. If they take a shortcut (specify the origin branch), they would get the wrong behavior, and stand a good chance of not understanding what is happening. I think this is a "bad" process - a bad thing to force users to learn/understand in order for them to be productive. > > - The latter would make your "git push" and "git pull" on 'topic' > to work with your local 'main', treating your 'main' in a way > very similar to how you treat your remote 'main' when you are on > your own 'main', which is quite reasonable if your change flow is > designed to be "work on topic, when the changes on topic proves > OK, send that to main, and when the changes on main proves OK, > send that to their main". (assuming you were referring to a "branch.autosetupmerge=always" outcome) It can be considered "reasonable" if this branch is local-only, yes. As a user, you then need to understand this duality / distinction between local-only branches that pull directly against some "semantic upstream", and local-and-remote branches that > > I guess I am esseentially saying that the usefulness of "simple" for > branch.autosetupmerge is dubious. > I understand that, and respectfully disagree :) > > Do you agree that none of the push.default options available today are > > "right" for this flow? Do you have a preference or opinion as to > > whether: > > * push.default=current should be changed to set up tracking when absent, or > > * push.default=simple should be changed to "simply" push and set up > > tracking when there is no tracking, or > > * a new push.default option should be introduced for this behavior, or > > * some other configuration should be introduced to specify "and set up > > tracking on default push if missing" (and if so, under what > > circumstances should it kick in?) > > None of the above, I guess. I made a mistake here, in under-emphasising "for this flow"; your answer seems to be more of an "in general, git is powerful enough that if the user knows to and chooses to do the right thing, they get the right outcome, and this proposed flow is flawed because it under-supports local short-lived never-individually-pushed branches". I completely agree with the former, and while I agree I would love to have an even better flow that could easily and transparently support "parent branch" and "server representation of the branch" as separate concepts - git simply isn't there at this time (and I don't know how to get it there, and I suspect you and others would not want to bake such concepts into git). You stated earlier that I would do my users a disservice in setting things up to support this flow (by default), without making it significantly discoverable for the wider git user community, because they would find that git later behaved differently in other settings. This is true, but doing them a disservice in terms of git expertise across contexts is far less important, to me in my context, than making them comfortable and productive in this specific context - and coming from , the blinkered workflow I propose is a veritable utopia of power & flexibility compared to the very-central VCS they come from. It is also well in-line with how our governance processes and DevOps processes work, in terms of the meaning of "branch" on the shared server. Anyway, I've gone way off-topic I think. I hope I can convince you that this workflow makes sense for some segment of the git current and future population, that (with adjustments yet to be made), pushing to same-name on the remote with tracking implicitly/by default makes sense, and that making this workflow discoverable to users beyond my org would also have value. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-22 9:24 ` Tao Klerks @ 2022-04-22 13:27 ` Tao Klerks 2022-04-23 4:44 ` Junio C Hamano 1 sibling, 0 replies; 41+ messages in thread From: Tao Klerks @ 2022-04-22 13:27 UTC (permalink / raw) To: Junio C Hamano Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine On Fri, Apr 22, 2022 at 11:24 AM Tao Klerks <tao@klerks.biz> wrote: > > On Fri, Apr 22, 2022 at 4:27 AM Junio C Hamano <gitster@pobox.com> wrote: > > > > Tao Klerks <tao@klerks.biz> writes: > > > > > > > > Unfortunately, I'm not sure what "something better than simple" for > > > push.default actually is, in the current system. > > > > "none", probably. Much better than "current" that can create new > > branches on the other side, which you would want to do with an > > explicit end-user instruction (i.e. not with "git push", but with > > "git push origin topic"). > > > > Hmm, I don't understand you here. You either mean "simple is the best > option you could choose for push.default, when > branch.autosetupmerge=simple, none of the other options are better", > or there's a small typo and you're saying "push.default=nothing would > be better". I'll assume the latter, but I'm not sure because I don't > see how it can be a good-faith statement. > My apologies for the tone here, I clearly sent before re-reading properly. I cannot presume to state what you did mean; I meant to say something like "I assume you mean either X or Y", or "my plausible interpretations are X or Y", or something similarly reflective of my own limitations. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-22 9:24 ` Tao Klerks 2022-04-22 13:27 ` Tao Klerks @ 2022-04-23 4:44 ` Junio C Hamano 2022-04-24 11:57 ` Tao Klerks 1 sibling, 1 reply; 41+ messages in thread From: Junio C Hamano @ 2022-04-23 4:44 UTC (permalink / raw) To: Tao Klerks Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine Tao Klerks <tao@klerks.biz> writes: >> "none", probably. Much better than "current" that can create new >> branches on the other side, which you would want to do with an >> explicit end-user instruction (i.e. not with "git push", but with >> "git push origin topic"). Sorry, "nothing" was what I meant. Those non-simple branches are left unconfigured with ASU=simple. We both agree that the user does not want to see the "with push.default=simple you have, you cannot push from it" but the reason why they do not want to see can be multiple. You are assuming that they want to push to somewhere else. I am taking into account that they may not want to push them at all, but only use locally. If the intended workflow is git checkout -b main [origin/main] ;# assuming DWIM is on git checkout -b topic origin/main ... work work work ... git checkout main git merge topic ... test test test ... ... ahh, no it does not work, back to fix topic ... git reset --hard ORIG_HEAD git checkout topic ... work work work ... git rebase -i ... git checkout main git merge topic ... test test test ... ... yay, this time it is perfect and we can push it out ... git push i.e. matching "simple" branches like main are used to locally bundle what you locally worked on, and the result is pushed out to the other side from there, while non-simple branches like topic are used to locally work on your real changes, it is reasonable to expect that the user wants "git push" to fail when the 'topic' branch is checked out. But unfortunately that does not work at the last step, as "nothing" unfortunately affects the last step that tries to check out 'main', too. push.default='simple' would make it work. > I would argue that git generally has a "problem", in that > "branch.XXX.merge" entries have two classes of > meanings/interpretation: > * That is the "parent branch" > ** The one I want changes from, when I say "update me with changes" > ** The one I eventually want to get my changes to > * That is the "remote instance/address" of *this* branch > ** If I pull, it's to get changes to the same branch that others might have made > ** When I push, it's to get this branch onto the server (not to get my > changes into the "upstream") Yes, that is very well known, and there arey mechanisms to support some workflows that separates "where I get changes from" and "where I publish my work" (look for "triangular workflows" in the list archive). The thing is, "simple" is *NOT* meant for triangular workflow. It was to cater to novice users who are used to cvs/svn style centralized "there is one place everybody pulls from and pushes to, which is where they meet" model. > Based on your feedback here, maybe "simple" is not the right name to > associate with workflow, its assumptions and tradeoffs - I believe is > accurately represents the intent and closely relates to the apparent > design intent behind the push.default=simple option, but I'd love > proposals as to how to name (and do) it better! > >> Currently, with both set to "simple", you do not even get .remote >> and .merge for the "topic" branch, so your "git pull" simply does >> not work. And "git push" will also refuse to work. >> > > That's right - because the assumption is that you've just created a > new independent branch - independent by name, and therefore > independent by default. You can of course add "--track" if you know > what you're doing and know this is a local-only branch and you want it > to track what you branched from and have "pull" bring in changes from > there (without explicitly specifying so)! > >> But then why are you [*] forking from origin/main in the first >> place? What is the purpose you created 'topic' and what do you >> plan to do with the result you develop on 'topic'? > > The assumption, in this workflow, is that you plan to work on that > branch, potentially push to origin to back up or share your work, and > will decide explicitly when to merge in changes from the origin you > branched (forked) from, or merge changes up there. > >> >> Side note: "you" do not refer to"Tao, the advocate of the >> 'simple' configuration", but figuratively the user who >> followed the "simple" route and created topic out of >> origin/main that is not connected to origin/main. >> >> Whatever you commit on topic eventually becomes part of what you'd >> push to origin or elsewhere. I'd assume it would be origin, because >> as the user who choose 'simple', you have some branches that you >> push back to the same name over there. Presumably, those are the >> primary integration branches the project has, like 'trunk', 'main', >> 'master', etc. >> >> So perhaps the user would have been better off to fork off of the >> LOCAL branch that would eventually be pushed back? In other words, >> the above user who created 'topic' would have done >> >> $ git checkout -b main origin/main >> > > (completely beside the point, but they would be more likely to have > just done "git checkout main", for the same outcome) > >> to use as a local integration branch that collects the work you will >> do locally that is targetted for their 'main' track, so to create a >> topic that aims to be part of what is pushed back to their 'main' >> track, you would want to do >> >> $ git checkout -b topic main >> >> instead? That way, "git push" would either not get .merge/.remote >> (when branch.autosetupmerge is set to 'true') or point at your local >> 'main' branch. > > I'm not sure I understand or agree with what you're saying here with > "would [otherwise] point at your local 'main' branch". I have to > assume you mean that would be the outcome with "always", Yeah, I meant to add the matching (when ... is set to ...) after the sentence and forgot. You inferred what I meant to say correctly. > In suggesting the user could/should have done that (in order to get a > sane workflow, presumably), you are also suggesting that they should > keep the state of that "local version of the upstream they eventually > want to get their changes into" up-to-date: They should first check > out master (for example), pull on master to get the state they expect, > and *then* create their new differently-named local branch. FWIW, I am not. I do not think it is healthy nor necessary to make your local work "catch up" too often with the outside world unnecessarily, be it done with rebase or with merge. They _can_ update 'master' when outside world has something worth adding to your topic extra dependency on and then update 'topic' to include what you took to 'master' from the outside. Dissociating the 'topic' from outside world is one way to encourage a better workflow. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-23 4:44 ` Junio C Hamano @ 2022-04-24 11:57 ` Tao Klerks 2022-04-29 7:31 ` Tao Klerks 0 siblings, 1 reply; 41+ messages in thread From: Tao Klerks @ 2022-04-24 11:57 UTC (permalink / raw) To: Junio C Hamano Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine On Sat, Apr 23, 2022 at 6:44 AM Junio C Hamano <gitster@pobox.com> wrote: > > Tao Klerks <tao@klerks.biz> writes: > > >> "none", probably. Much better than "current" that can create new > >> branches on the other side, which you would want to do with an > >> explicit end-user instruction (i.e. not with "git push", but with > >> "git push origin topic"). > > Sorry, "nothing" was what I meant. Those non-simple branches are > left unconfigured with ASU=simple. We both agree that the user does > not want to see the "with push.default=simple you have, you cannot > push from it" but the reason why they do not want to see can be > multiple. You are assuming that they want to push to somewhere > else. I am taking into account that they may not want to push them > at all, but only use locally. If the intended workflow is > > git checkout -b main [origin/main] ;# assuming DWIM is on > git checkout -b topic origin/main > ... work work work ... > git checkout main > git merge topic > ... test test test ... > ... ahh, no it does not work, back to fix topic ... > git reset --hard ORIG_HEAD > git checkout topic > ... work work work ... > git rebase -i ... > git checkout main > git merge topic > ... test test test ... > ... yay, this time it is perfect and we can push it out ... > git push (two interesting/surprising things here: 1) The user chooses to merge to master *before* testing (does not test on topic) 2) The user does not use CI pipelines of any kind ) > > i.e. matching "simple" branches like main are used to locally bundle > what you locally worked on, and the result is pushed out to the > other side from there, while non-simple branches like topic are used > to locally work on your real changes, it is reasonable to expect > that the user wants "git push" to fail when the 'topic' branch is > checked out. I would argue that the user who wants push to fail here is a very rare user. Presumably they thought they were somewhere else, and the "git push" was a complete mistake? (why else would you run the command??) For users of a central repo (github, gitlab, bitbucket, teamhub, etc etc) when they say "git push" it is normally/typically to, well... push their changes to the remote! To back them up because they're worried about losing them, or to share them with teammates/collaborators, to run CI, or for any other reason. I'm not arguing that "nothing" is useless to everyone, but I am arguing that for the workflow you have highlighted above, "nothing" is not a valuable setting. It provides close to no practical safety benefits, and makes pushing shared branches much more awkward. > > But unfortunately that does not work at the last step, as "nothing" > unfortunately affects the last step that tries to check out 'main', > too. push.default='simple' would make it work. > OK > > I would argue that git generally has a "problem", in that > > "branch.XXX.merge" entries have two classes of > > meanings/interpretation: > > * That is the "parent branch" > > ** The one I want changes from, when I say "update me with changes" > > ** The one I eventually want to get my changes to > > * That is the "remote instance/address" of *this* branch > > ** If I pull, it's to get changes to the same branch that others might have made > > ** When I push, it's to get this branch onto the server (not to get my > > changes into the "upstream") > > Yes, that is very well known, and there arey mechanisms to support > some workflows that separates "where I get changes from" and "where > I publish my work" (look for "triangular workflows" in the list > archive). Yes, the best/simplest summary I've seen so far is the github blog post https://github.blog/2015-07-29-git-2-5-including-multiple-worktrees-and-triangular-workflows/ In the particular model highlighted there, you use "branch.XXX.merge" entries to indicate "the parent branch" while keeping the "remote branch instance" separate, by leveraging the "push.default=current" setting, but of course this "parent branch" information is still local-only, and you cannot collaborate on your feature/topic branch with others. If someone else checks out your topic branch from your server and pushes some changes to it, then your flow breaks, because your "git pull" means "bring in changes from the parent", not "bring in any changes that might have occurred on the topic branch". I understand there are techniques/flows that users can choose to use, but I don't think this changes the fundamental and, for beginners, problematic, ambiguity of meaning of "branch.XXX.merge". The "branch.autosetupmerge=simple" proposal is to simplify it down to "branch.XXX.merge entries indicate what the remote instance of this branch is, and will normally be aligned with the name of the local branch". This "simplification" is incompatible with the particular triangular workflow highlighted above. > > > In suggesting the user could/should have done that (in order to get a > > sane workflow, presumably), you are also suggesting that they should > > keep the state of that "local version of the upstream they eventually > > want to get their changes into" up-to-date: They should first check > > out master (for example), pull on master to get the state they expect, > > and *then* create their new differently-named local branch. > > FWIW, I am not. Fair enough, sorry I misunderstood. What I meant is that you need to "maintain" your local master when you do eventually want to push up any topic branch, *and* any other time you do want to "catch up" with upstream changes; assuming you work on multiple topic branches in parallel (which is one of the "superpowers" of git), the local master has lots of different reasons to change. > > I do not think it is healthy nor necessary to make your local work > "catch up" too often with the outside world unnecessarily, be it > done with rebase or with merge. They _can_ update 'master' when > outside world has something worth adding to your topic extra > dependency on and then update 'topic' to include what you took to > 'master' from the outside. Dissociating the 'topic' from outside > world is one way to encourage a better workflow. On this we agree, I guess :) I will have another go at proposing a complete, easy-to-understand, easy-to-enter, "simple" workflow that emphasises local and remote branch "correspondence" by encouraging "branch.XXX.merge" to always and automatically be set to the same-name branch on the remote (and not any other "parent" you might have branched from when creating a topic branch), and a reasonable non-intrusive, non-misleading way to on-ramp into it. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches 2022-04-24 11:57 ` Tao Klerks @ 2022-04-29 7:31 ` Tao Klerks 0 siblings, 0 replies; 41+ messages in thread From: Tao Klerks @ 2022-04-29 7:31 UTC (permalink / raw) To: Junio C Hamano Cc: Josh Steadmon, Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine On Sun, Apr 24, 2022 at 1:57 PM Tao Klerks <tao@klerks.biz> wrote: > > > I will have another go at proposing a complete, easy-to-understand, > easy-to-enter, "simple" workflow that emphasises local and remote > branch "correspondence" by encouraging "branch.XXX.merge" to always > and automatically be set to the same-name branch on the remote (and > not any other "parent" you might have branched from when creating a > topic branch), and a reasonable non-intrusive, non-misleading way to > on-ramp into it. I now have a complete proposal that I think is coherent, clear, improves the user experience as desired, and does not interfere with any of the existing functionality/workflows. There are a couple niggles around naming that I'd like feedback on. I expect to submit this new proposal today. (nb: it's become a patch series again) ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v5 0/3] New options to support "simple" centralized workflow 2022-03-21 6:17 ` [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-04-18 18:15 ` Josh Steadmon @ 2022-04-29 9:56 ` Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 1/3] branch: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget ` (3 more replies) 1 sibling, 4 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-04-29 9:56 UTC (permalink / raw) To: git Cc: Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine, Josh Steadmon, Tao Klerks This patchset introduces two new configuration options, intended to be consistent with and complementary to the push.default "simple" option. It also improves remote-defaulting in "default push" scenarios. In some "simple" centralized workflows, users expect remote tracking branch names to match local branch names. "git push" pushes to the remote version/instance of the branch, and "git pull" pulls any changes to the remote branch (changes made by the same user in another place, or by other users). The default push.default option, "simple", supports this kind of workflow by "raising eyebrows" if a user looks like they are trying to push to the "wrong" remote tracking branch. None of the existing branch.autosetupmerge settings support this workflow/expectation well, so the new "branch.autosetupmerge=simple" option addresses this - acting like the default "remote" option only when the remote branch and (new) local branch have the same name. The new option is referred to in new advice in the push.default=simple mismatching remote branch error text. At a later stage in the new-branch workflow, when a user first goes to push a new branch to the remote, the default "git push" will complain that there is no remote tracking branch (unless push.default=current). For users that always expect remote branch names to match local branch names, on a single remote, this is inconvenient. New config setting "push.autoSetupRemote" addresses this by automatically specifying "--set-upstream" (and allowing the push) when there is no configured remote for push.default options "simple", "upstream", and "current". In the case of "current", this helps make "pull" work correctly (under these workflow assumptions). For the other two options the primary benefit is being able to simply say "git push" and not be interrupted with an unnecessary "but this is a new branch!" error. Along the way, we also enhance the remote-defaulting behavior for "git push" (and ls-remote) to not only support "origin" as the default remote, but rather any single configured remote. Default push should only fail for lack of a remote if there are none, or if there is more than one and none are called "origin". Changes since v4: * Changed patchset subject to "New options to support "simple" centralized workflow", reflecting the fact that there are now two new config options available * Added some advice to the default push "mismatching remote tracking branch name" error, offering the new branch.autosetupmerge=simple option, so that new users can potentially discover and benefit from it * Introduced a new commit improving the defaulting of remote for "default push" (and ls-remote), and fixing and adding related tests * Introduced a new commit for new config setting push.autoSetupRemote, which will avoid the need for users to explicitly push to a specific origin, explicitly requesting tracking, when doing a default push for a new branch (with advice and tests). * Rebased onto current 'master' Open questions: * The exact text of the two new pieces of advice should get some review, it is likely improvable * The name and config help of the "push.autoSetupRemote" config setting should also be reviewed - there is confusion (at least in my mind) between "upstream", "remote tracking", and "remote merge" concepts. Tao Klerks (3): branch: new autosetupmerge option 'simple' for matching branches push: default to single remote even when not named origin push: new config option "push.autoSetupRemote" supports "simple" push Documentation/config/branch.txt | 9 ++-- Documentation/config/push.txt | 11 +++++ Documentation/git-branch.txt | 18 +++++--- branch.c | 27 +++++++++++- branch.h | 1 + builtin/push.c | 64 +++++++++++++++++++++------ config.c | 3 ++ remote.c | 2 + t/t3200-branch.sh | 35 +++++++++++++++ t/t5512-ls-remote.sh | 17 ++++++-- t/t5528-push-default.sh | 77 ++++++++++++++++++++++++++++++++- transport.h | 1 + 12 files changed, 237 insertions(+), 28 deletions(-) base-commit: 6cd33dceed60949e2dbc32e3f0f5e67c4c882e1e Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1161%2FTaoK%2Ffeature-branch-autosetupmerge-simple-v5 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1161/TaoK/feature-branch-autosetupmerge-simple-v5 Pull-Request: https://github.com/gitgitgadget/git/pull/1161 Range-diff vs v4: 1: eca8ab2eb7b ! 1: 5b08edcdeef merge: new autosetupmerge option 'simple' for matching branches @@ Metadata Author: Tao Klerks <tao@klerks.biz> ## Commit message ## - merge: new autosetupmerge option 'simple' for matching branches + branch: new autosetupmerge option 'simple' for matching branches With the default push.default option, "simple", beginners are protected from accidentally pushing to the "wrong" branch in @@ Commit message a "default push", they get an error and explanation with options. There is a particular centralized workflow where this often happens: - a user branches to a new local feature branch from an existing - upstream branch, eg with "checkout -b feature1 origin/master". With + a user branches to a new local topic branch from an existing + remote branch, eg with "checkout -b feature1 origin/master". With the default branch.autosetupmerge configuration (value "true"), git - will automatically add origin/master as the remote tracking branch. + will automatically add origin/master as the upstream tracking branch. - When the user pushes with "git push", they get an error, and (amongst - other things) a suggestion to run "git push origin HEAD". Eventually - they figure out to add "-u" to change the tracking branch, or they set - push.default to "current", or some tooling does one or the other of - these things for them. + When the user pushes with a default "git push", with the intention of + pushing their (new) topic branch to the remote, they get an error, and + (amongst other things) a suggestion to run "git push origin HEAD". - When one of their coworkers works on the same branch, they don't get - any of that weirdness. They just "git checkout feature1" and - everything works exactly as they expect, with the shared remote branch - set up as remote tracking branch, and push and pull working out of the - box. + If they follow this suggestion the push succeeds, but on subsequent + default pushes they continue to get an error - so eventually they + figure out to add "-u" to change the tracking branch, or they spelunk + the push.default config doc as proposed and set it to "current", or + some GUI tooling does one or the other of these things for them. + + When one of their coworkers later works on the same topic branch, + they don't get any of that "weirdness". They just "git checkout + feature1" and everything works exactly as they expect, with the shared + remote branch set up as remote tracking branch, and push and pull + working out of the box. The "stable state" for this way of working is that local branches have the same-name remote tracking branch (origin/feature1 in this @@ Commit message the remote "master" branch instead (a completely different class of changes!) - Any experienced git user will presumably say "well yeah, that's what - it means to have the remote tracking branch set to origin/master!" - - but that user didn't *ask* to have the remote master branch added as - remote tracking branch - that just happened automatically when they - branched their feature branch. They didn't necessarily even notice or - understand the meaning of the "set up to track 'origin/master'" + An experienced git user might say "well yeah, that's what it means to + have the remote tracking branch set to origin/master!" - but the + original user above didn't *ask* to have the remote master branch + added as remote tracking branch - that just happened automatically + when they branched their feature branch. They didn't necessarily even + notice or understand the meaning of the "set up to track 'origin/master'" message when they created the branch - especially if they are using a GUI. Looking at how to fix this, you might think "OK, so disable auto setup of remote tracking - set branch.autosetupmerge to false" - but that will inconvenience the *second* user in this story - the one who just - wanted to start working on the feature branch. The first and second + wanted to start working on the topic branch. The first and second users swap roles at different points in time of course - they should both have a sane configuration that does the right thing in both situations. - Make these flows painless by introducing a new branch.autosetupmerge - option called "simple", to match the same-name "push.default" option - that makes similar assumptions. + Make this "branches have the same name locally as on the remote" + workflow less painful / more obvious by introducing a new + branch.autosetupmerge option called "simple", to match the same-name + "push.default" option that makes similar assumptions. This new option automatically sets up tracking in a *subset* of the current default situations: when the original ref is a remote tracking branch *and* has the same branch name on the remote (as the new local branch name). + Update the error displayed when the 'push.default=simple' configuration + rejects a mismatching-upstream-name default push, to offer this new + branch.autosetupmerge option that will prevent this class of error. + With this new configuration, in the example situation above, the first user does *not* get origin/master set up as the tracking branch for the new local branch. If they "git pull" in their new local-only @@ Documentation/git-branch.txt: The exact upstream branch is chosen depending on t ## branch.c ## @@ branch.c: static int find_tracked_branch(struct remote *remote, void *priv) - free(tracking->spec.src); string_list_clear(tracking->srcs, 0); + break; } + /* remote_find_tracking() searches by src if present */ tracking->spec.src = NULL; @@ branch.c: static void setup_tracking(const char *new_ref, const char *orig_ref, + * that supports multiple entries in tracking_srcs but + * leaves tracking.matches at 0. + */ - if (tracking.matches > 1) - die(_("not tracking: ambiguous information for ref %s"), - orig_ref); + if (tracking.matches > 1) { + int status = die_message(_("not tracking: ambiguous information for ref '%s'"), + orig_ref); +@@ branch.c: static void setup_tracking(const char *new_ref, const char *orig_ref, + exit(status); + } + if (track == BRANCH_TRACK_SIMPLE) { + /* @@ branch.c: static void setup_tracking(const char *new_ref, const char *orig_ref, if (tracking.srcs->nr < 1) string_list_append(tracking.srcs, orig_ref); if (install_branch_config_multiple_remotes(config_flags, new_ref, +@@ branch.c: static int submodule_create_branch(struct repository *r, + /* Default for "git checkout". Do not pass --track. */ + case BRANCH_TRACK_REMOTE: + /* Default for "git branch". Do not pass --track. */ ++ case BRANCH_TRACK_SIMPLE: ++ /* Config-driven only. Do not pass --track. */ + break; + } + ## branch.h ## @@ branch.h: enum branch_track { @@ branch.h: enum branch_track { extern enum branch_track git_branch_track; + ## builtin/push.c ## +@@ + * "git push" + */ + #include "cache.h" ++#include "branch.h" + #include "config.h" + #include "refs.h" + #include "refspec.h" +@@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, + * upstream to a non-branch, we should probably be showing + * them the big ugly fully qualified ref. + */ +- const char *advice_maybe = ""; ++ const char *advice_pushdefault_maybe = ""; ++ const char *advice_automergesimple_maybe = ""; + const char *short_upstream = branch->merge[0]->src; + + skip_prefix(short_upstream, "refs/heads/", &short_upstream); +@@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, + * push.default. + */ + if (push_default == PUSH_DEFAULT_UNSPECIFIED) +- advice_maybe = _("\n" ++ advice_pushdefault_maybe = _("\n" + "To choose either option permanently, " +- "see push.default in 'git help config'."); ++ "see push.default in 'git help config'.\n"); ++ if (git_branch_track != BRANCH_TRACK_SIMPLE) ++ advice_automergesimple_maybe = _("\n" ++ "To avoid automatically configuring " ++ "upstream branches when their name\n" ++ "doesn't match the local branch, see option " ++ "'simple' of branch.autosetupmerge\n" ++ "in 'git help config'.\n"); + die(_("The upstream branch of your current branch does not match\n" + "the name of your current branch. To push to the upstream branch\n" + "on the remote, use\n" +@@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, + "To push to the branch of the same name on the remote, use\n" + "\n" + " git push %s HEAD\n" +- "%s"), ++ "%s%s"), + remote->name, short_upstream, +- remote->name, advice_maybe); ++ remote->name, advice_pushdefault_maybe, ++ advice_automergesimple_maybe); + } + + static const char message_detached_head_die[] = + ## config.c ## @@ config.c: static int git_default_branch_config(const char *var, const char *value) } else if (value && !strcmp(value, "inherit")) { -: ----------- > 2: 31184c3a65d push: default to single remote even when not named origin -: ----------- > 3: 41c88e51ac6 push: new config option "push.autoSetupRemote" supports "simple" push -- gitgitgadget ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCH v5 1/3] branch: new autosetupmerge option 'simple' for matching branches 2022-04-29 9:56 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget @ 2022-04-29 9:56 ` Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 2/3] push: default to single remote even when not named origin Tao Klerks via GitGitGadget ` (2 subsequent siblings) 3 siblings, 0 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-04-29 9:56 UTC (permalink / raw) To: git Cc: Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine, Josh Steadmon, Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> With the default push.default option, "simple", beginners are protected from accidentally pushing to the "wrong" branch in centralized workflows: if the remote tracking branch they would push to does not have the same name as the local branch, and they try to do a "default push", they get an error and explanation with options. There is a particular centralized workflow where this often happens: a user branches to a new local topic branch from an existing remote branch, eg with "checkout -b feature1 origin/master". With the default branch.autosetupmerge configuration (value "true"), git will automatically add origin/master as the upstream tracking branch. When the user pushes with a default "git push", with the intention of pushing their (new) topic branch to the remote, they get an error, and (amongst other things) a suggestion to run "git push origin HEAD". If they follow this suggestion the push succeeds, but on subsequent default pushes they continue to get an error - so eventually they figure out to add "-u" to change the tracking branch, or they spelunk the push.default config doc as proposed and set it to "current", or some GUI tooling does one or the other of these things for them. When one of their coworkers later works on the same topic branch, they don't get any of that "weirdness". They just "git checkout feature1" and everything works exactly as they expect, with the shared remote branch set up as remote tracking branch, and push and pull working out of the box. The "stable state" for this way of working is that local branches have the same-name remote tracking branch (origin/feature1 in this example), and multiple people can work on that remote feature branch at the same time, trusting "git pull" to merge or rebase as required for them to be able to push their interim changes to that same feature branch on that same remote. (merging from the upstream "master" branch, and merging back to it, are separate more involved processes in this flow). There is a problem in this flow/way of working, however, which is that the first user, when they first branched from origin/master, ended up with the "wrong" remote tracking branch (different from the stable state). For a while, before they pushed (and maybe longer, if they don't use -u/--set-upstream), their "git pull" wasn't getting other users' changes to the feature branch - it was getting any changes from the remote "master" branch instead (a completely different class of changes!) An experienced git user might say "well yeah, that's what it means to have the remote tracking branch set to origin/master!" - but the original user above didn't *ask* to have the remote master branch added as remote tracking branch - that just happened automatically when they branched their feature branch. They didn't necessarily even notice or understand the meaning of the "set up to track 'origin/master'" message when they created the branch - especially if they are using a GUI. Looking at how to fix this, you might think "OK, so disable auto setup of remote tracking - set branch.autosetupmerge to false" - but that will inconvenience the *second* user in this story - the one who just wanted to start working on the topic branch. The first and second users swap roles at different points in time of course - they should both have a sane configuration that does the right thing in both situations. Make this "branches have the same name locally as on the remote" workflow less painful / more obvious by introducing a new branch.autosetupmerge option called "simple", to match the same-name "push.default" option that makes similar assumptions. This new option automatically sets up tracking in a *subset* of the current default situations: when the original ref is a remote tracking branch *and* has the same branch name on the remote (as the new local branch name). Update the error displayed when the 'push.default=simple' configuration rejects a mismatching-upstream-name default push, to offer this new branch.autosetupmerge option that will prevent this class of error. With this new configuration, in the example situation above, the first user does *not* get origin/master set up as the tracking branch for the new local branch. If they "git pull" in their new local-only branch, they get an error explaining there is no upstream branch - which makes sense and is helpful. If they "git push", they get an error explaining how to push *and* suggesting they specify --set-upstream - which is exactly the right thing to do for them. This new option is likely not appropriate for users intentionally implementing a "triangular workflow" with a shared upstream tracking branch, that they "git pull" in and a "private" feature branch that they push/force-push to just for remote safe-keeping until they are ready to push up to the shared branch explicitly/separately. Such users are likely to prefer keeping the current default merge.autosetupmerge=true behavior, and change their push.default to "current". Also extend the existing branch tests 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 <tao@klerks.biz> --- Documentation/config/branch.txt | 4 +++- Documentation/git-branch.txt | 18 ++++++++++------- branch.c | 27 ++++++++++++++++++++++++- branch.h | 1 + builtin/push.c | 20 ++++++++++++++----- config.c | 3 +++ t/t3200-branch.sh | 35 +++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 14 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.<name>.remote` and `branch.<name>.merge` options are used. diff --git a/branch.c b/branch.c index 01ecb816d5c..962aa7c8609 100644 --- a/branch.c +++ b/branch.c @@ -44,9 +44,9 @@ static int find_tracked_branch(struct remote *remote, void *priv) string_list_clear(tracking->srcs, 0); break; } + /* remote_find_tracking() searches by src if present */ tracking->spec.src = NULL; } - return 0; } @@ -264,15 +264,23 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, if (!tracking.matches) switch (track) { + /* If ref is not remote, still use local */ case BRANCH_TRACK_ALWAYS: case BRANCH_TRACK_EXPLICIT: case BRANCH_TRACK_OVERRIDE: + /* Remote matches not evaluated */ case BRANCH_TRACK_INHERIT: break; + /* Otherwise, if no remote don't track */ default: goto cleanup; } + /* + * This check does not apply to BRANCH_TRACK_INHERIT; + * that supports multiple entries in tracking_srcs but + * leaves tracking.matches at 0. + */ if (tracking.matches > 1) { int status = die_message(_("not tracking: ambiguous information for ref '%s'"), orig_ref); @@ -307,6 +315,21 @@ static void setup_tracking(const char *new_ref, const char *orig_ref, exit(status); } + 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 only BRANCH_TRACK_INHERIT can + * produce more than one entry). + */ + 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, @@ -603,6 +626,8 @@ static int submodule_create_branch(struct repository *r, /* Default for "git checkout". Do not pass --track. */ case BRANCH_TRACK_REMOTE: /* Default for "git branch". Do not pass --track. */ + case BRANCH_TRACK_SIMPLE: + /* Config-driven only. Do not pass --track. */ break; } 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/builtin/push.c b/builtin/push.c index cad997965a7..447f91f5b47 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -2,6 +2,7 @@ * "git push" */ #include "cache.h" +#include "branch.h" #include "config.h" #include "refs.h" #include "refspec.h" @@ -151,7 +152,8 @@ static NORETURN void die_push_simple(struct branch *branch, * upstream to a non-branch, we should probably be showing * them the big ugly fully qualified ref. */ - const char *advice_maybe = ""; + const char *advice_pushdefault_maybe = ""; + const char *advice_automergesimple_maybe = ""; const char *short_upstream = branch->merge[0]->src; skip_prefix(short_upstream, "refs/heads/", &short_upstream); @@ -161,9 +163,16 @@ static NORETURN void die_push_simple(struct branch *branch, * push.default. */ if (push_default == PUSH_DEFAULT_UNSPECIFIED) - advice_maybe = _("\n" + advice_pushdefault_maybe = _("\n" "To choose either option permanently, " - "see push.default in 'git help config'."); + "see push.default in 'git help config'.\n"); + if (git_branch_track != BRANCH_TRACK_SIMPLE) + advice_automergesimple_maybe = _("\n" + "To avoid automatically configuring " + "upstream branches when their name\n" + "doesn't match the local branch, see option " + "'simple' of branch.autosetupmerge\n" + "in 'git help config'.\n"); die(_("The upstream branch of your current branch does not match\n" "the name of your current branch. To push to the upstream branch\n" "on the remote, use\n" @@ -173,9 +182,10 @@ static NORETURN void die_push_simple(struct branch *branch, "To push to the branch of the same name on the remote, use\n" "\n" " git push %s HEAD\n" - "%s"), + "%s%s"), remote->name, short_upstream, - remote->name, advice_maybe); + remote->name, advice_pushdefault_maybe, + advice_automergesimple_maybe); } static const char message_detached_head_die[] = diff --git a/config.c b/config.c index a5e11aad7fe..8dbeb1932e5 100644 --- a/config.c +++ b/config.c @@ -1781,6 +1781,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; diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index e12db593615..9723c2827cc 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_when_finished "rm -rf otherserver" && + git init otherserver && + test_commit -C otherserver my_commit 1 && + git -C otherserver branch feature && + test_config branch.autosetupmerge simple && + test_config remote.otherserver.url otherserver && + test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* && + git fetch otherserver && + git branch feature otherserver/feature && + test_cmp_config otherserver branch.feature.remote && + test_cmp_config refs/heads/feature branch.feature.merge +' + +test_expect_success 'simple tracking skips when remote branch name does not match' ' + test_config branch.autosetupmerge simple && + test_config remote.local.url . && + test_config remote.local.fetch refs/heads/*:refs/remotes/local/* && + git fetch local && + git branch my-other local/main && + test_cmp_config "" --default "" branch.my-other.remote && + test_cmp_config "" --default "" branch.my-other.merge +' + +test_expect_success 'simple tracking skips when remote ref is not a branch' ' + test_config branch.autosetupmerge simple && + test_config remote.localtags.url . && + test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* && + git tag mytag12 main && + git fetch localtags && + git branch mytag12 localtags/mytag12 && + test_cmp_config "" --default "" branch.mytag12.remote && + test_cmp_config "" --default "" 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 && -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* [PATCH v5 2/3] push: default to single remote even when not named origin 2022-04-29 9:56 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 1/3] branch: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget @ 2022-04-29 9:56 ` Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 3/3] push: new config option "push.autoSetupRemote" supports "simple" push Tao Klerks via GitGitGadget 2022-04-29 18:50 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Junio C Hamano 3 siblings, 0 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-04-29 9:56 UTC (permalink / raw) To: git Cc: Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine, Josh Steadmon, Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> With "push.default=current" configured, a simple "git push" will push to the same-name branch on the current branch's branch.<name>.pushRemote, or remote.pushDefault, or origin. If none of these are defined, the push will fail with error "fatal: No configured push destination". The same "default to origin if no config" behavior applies with "push.default=matching". Other commands use "origin" as a default when there are multiple options, but default to the single remote when there is only one - for example, "git checkout <something>". This "assume the single remote if there is only one" behavior is more friendly/useful than a defaulting behavior that only uses the name "origin" no matter what. Update "git push" to also default to the single remote (and finally fall back to "origin" as default if there are several), for "push.default=current" and for other current and future remote-defaulting push behaviors. This change also modifies the behavior of ls-remote in a consistent way, so defaulting not only supplies 'origin', but any single configured remote also. Document the change in behavior, correct incorrect assumptions in related tests, and add test cases reflecting this new single-remote-defaulting behavior. Signed-off-by: Tao Klerks <tao@klerks.biz> --- Documentation/config/branch.txt | 5 +-- remote.c | 2 ++ t/t5512-ls-remote.sh | 17 +++++++-- t/t5528-push-default.sh | 63 ++++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/Documentation/config/branch.txt b/Documentation/config/branch.txt index 8df10d07129..445341a906b 100644 --- a/Documentation/config/branch.txt +++ b/Documentation/config/branch.txt @@ -40,8 +40,9 @@ branch.<name>.remote:: may be overridden with `remote.pushDefault` (for all branches). The remote to push to, for the current branch, may be further overridden by `branch.<name>.pushRemote`. If no remote is - configured, or if you are not on any branch, it defaults to - `origin` for fetching and `remote.pushDefault` for pushing. + configured, or if you are not on any branch and there is more than + one remote defined in the repository, it defaults to `origin` for + fetching and `remote.pushDefault` for pushing. Additionally, `.` (a period) is the current local repository (a dot-repository), see `branch.<name>.merge`'s final note below. diff --git a/remote.c b/remote.c index 42a4e7106e1..930fdc9c2f6 100644 --- a/remote.c +++ b/remote.c @@ -543,6 +543,8 @@ static const char *remotes_remote_for_branch(struct remote_state *remote_state, } if (explicit) *explicit = 0; + if (remote_state->remotes_nr == 1) + return remote_state->remotes[0]->name; return "origin"; } diff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh index f53f58895a1..20d063fb9ae 100755 --- a/t/t5512-ls-remote.sh +++ b/t/t5512-ls-remote.sh @@ -15,6 +15,10 @@ generate_references () { done } +test_expect_success 'dies when no remote found' ' + test_must_fail git ls-remote +' + test_expect_success setup ' >file && git add file && @@ -30,7 +34,8 @@ test_expect_success setup ' git show-ref -d >refs && sed -e "s/ / /" refs >>expected.all && - git remote add self "$(pwd)/.git" + git remote add self "$(pwd)/.git" && + git remote add self2 "." ' test_expect_success 'ls-remote --tags .git' ' @@ -83,11 +88,17 @@ test_expect_success 'ls-remote --sort="-refname" --tags self' ' test_cmp expect actual ' -test_expect_success 'dies when no remote specified and no default remotes found' ' +test_expect_success 'dies when no remote specified, multiple remotes found, and no default specified' ' test_must_fail git ls-remote ' -test_expect_success 'use "origin" when no remote specified' ' +test_expect_success 'succeeds when no remote specified but only one found' ' + test_when_finished git remote add self2 "." && + git remote remove self2 && + git ls-remote +' + +test_expect_success 'use "origin" when no remote specified and multiple found' ' URL="$(pwd)/.git" && echo "From $URL" >exp_err && diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh index f280e00eb79..0d6c9869ed3 100755 --- a/t/t5528-push-default.sh +++ b/t/t5528-push-default.sh @@ -94,13 +94,74 @@ test_expect_success '"upstream" does not push when remotes do not match' ' test_must_fail git push parent2 ' -test_expect_success 'push from/to new branch with upstream, matching and simple' ' +test_expect_success '"current" does not push when multiple remotes and none origin' ' + git checkout main && + test_config push.default current && + test_commit current-multi && + test_must_fail git push +' + +test_expect_success '"current" pushes when remote explicitly specified' ' + git checkout main && + test_config push.default current && + test_commit current-specified && + git push parent1 +' + +test_expect_success '"current" pushes to origin when no remote specified among multiple' ' + git checkout main && + test_config remote.origin.url repo1 && + test_config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" && + test_commit current-origin && + test_push_success current main +' + +test_expect_success '"current" pushes to single remote even when not specified' ' + git checkout main && + test_when_finished git remote add parent1 repo1 && + git remote remove parent1 && + test_commit current-implied && + test_push_success current main repo2 +' + +test_expect_success 'push from/to new branch with non-defaulted remote fails with upstream, matching, current and simple ' ' git checkout -b new-branch && test_push_failure simple && test_push_failure matching && + test_push_failure upstream && + test_push_failure current +' + +test_expect_success 'push from/to new branch fails with upstream and simple ' ' + git checkout -b new-branch-1 && + test_config branch.new-branch-1.remote parent1 && + test_push_failure simple && test_push_failure upstream ' +# The behavior here is surprising but not entirely wrong: +# - the current branch is used to determine the target remote +# - the "matching" push default pushes matching branches, *ignoring* the +# current new branch as it does not have upstream tracking +# - the default push succeeds +# +# A previous test expected this to fail, but for the wrong reasons: +# it expected a fail becaause the branch is new and cannot be pushed, but +# in fact it was failing because of an ambiguous remote +# +test_expect_failure 'push from/to new branch fails with matching ' ' + git checkout -b new-branch-2 && + test_config branch.new-branch-2.remote parent1 && + test_push_failure matching +' + +test_expect_success 'push from/to branch with tracking fails with nothing ' ' + git checkout -b tracked-branch && + test_config branch.tracked-branch.remote parent1 && + test_config branch.tracked-branch.merge refs/heads/tracked-branch && + test_push_failure nothing +' + test_expect_success '"matching" fails if none match' ' git init --bare empty && test_must_fail git push empty : 2>actual && -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* [PATCH v5 3/3] push: new config option "push.autoSetupRemote" supports "simple" push 2022-04-29 9:56 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 1/3] branch: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 2/3] push: default to single remote even when not named origin Tao Klerks via GitGitGadget @ 2022-04-29 9:56 ` Tao Klerks via GitGitGadget 2022-04-29 18:50 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Junio C Hamano 3 siblings, 0 replies; 41+ messages in thread From: Tao Klerks via GitGitGadget @ 2022-04-29 9:56 UTC (permalink / raw) To: git Cc: Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine, Josh Steadmon, Tao Klerks, Tao Klerks From: Tao Klerks <tao@klerks.biz> In some "simple" centralized workflows, users expect remote tracking branch names to match local branch names. "git push" pushes to the remote version/instance of the branch, and "git pull" pulls any changes to the remote branch (changes made by the same user in another place, or by other users). This expectation is supported by the push.default default option "simple" which refuses a default push for a mismatching tracking branch name, and by the new branch.autosetupmerge option, "simple", which only sets up remote tracking for same-name remote branches. When a new branch has been created by the user and has not yet been pushed (and push.default is not set to "current"), the user is prompted with a "The current branch %s has no upstream branch" error, and instructions on how to push and add tracking. This error is helpful in that following the advice once per branch "resolves" the issue for that branch forever, but inconvenient in that for the "simple" centralized workflow, this is always the right thing to do, so it would be better to just do it. Support this workflow with a new config setting, push.autoSetupRemote, which will cause a default push, when there is no remote tracking branch configured, to push to the same-name on the remote and --set-upstream. Also add a hint offering this new option when the "The current branch %s has no upstream branch" error is encountered, and add corresponding tests. Signed-off-by: Tao Klerks <tao@klerks.biz> --- Documentation/config/push.txt | 11 +++++++++ builtin/push.c | 44 ++++++++++++++++++++++++++++------- t/t5528-push-default.sh | 14 +++++++++++ transport.h | 1 + 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/Documentation/config/push.txt b/Documentation/config/push.txt index 632033638c4..e32801e6c91 100644 --- a/Documentation/config/push.txt +++ b/Documentation/config/push.txt @@ -1,3 +1,14 @@ +push.autoSetupRemote:: + If set to "true" assume `--set-upstream` on default push when no + upstream tracking exists for the current branch; this option + takes effect with push.default options 'simple', 'upstream', + and 'current'. It is useful if by default you want new branches + to be pushed to the default remote (like the behavior of + 'push.default=current') and you also want the upstream tracking + to be set. Workflows most likely to benefit from this option are + 'simple' central workflows where all branches are expected to + have the same name on the remote. + push.default:: Defines the action `git push` should take if no refspec is given (whether from the command-line, config, or elsewhere). diff --git a/builtin/push.c b/builtin/push.c index 447f91f5b47..86b44f8aa71 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -195,16 +195,32 @@ static const char message_detached_head_die[] = "\n" " git push %s HEAD:<name-of-remote-branch>\n"); -static const char *get_upstream_ref(struct branch *branch, const char *remote_name) +static const char *get_upstream_ref(int flags, struct branch *branch, const char *remote_name) { - if (!branch->merge_nr || !branch->merge || !branch->remote_name) + if (branch->merge_nr == 0 && (flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) { + /* if missing, assume same; set_upstream will be defined later */ + return branch->refname; + } + + if (!branch->merge_nr || !branch->merge || !branch->remote_name) { + const char *advice_autosetup_maybe = ""; + if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) { + advice_autosetup_maybe = _("\n" + "To have this happen automatically for " + "branches without a tracking\n" + "upstream, see 'push.autoSetupRemote' " + "in 'git help config'.\n"); + } die(_("The current branch %s has no upstream branch.\n" "To push the current branch and set the remote as upstream, use\n" "\n" - " git push --set-upstream %s %s\n"), + " git push --set-upstream %s %s\n" + "%s"), branch->name, remote_name, - branch->name); + branch->name, + advice_autosetup_maybe); + } if (branch->merge_nr != 1) die(_("The current branch %s has multiple upstream branches, " "refusing to push."), branch->name); @@ -212,7 +228,7 @@ static const char *get_upstream_ref(struct branch *branch, const char *remote_na return branch->merge[0]->src; } -static void setup_default_push_refspecs(struct remote *remote) +static void setup_default_push_refspecs(int *flags, struct remote *remote) { struct branch *branch; const char *dst; @@ -244,7 +260,7 @@ static void setup_default_push_refspecs(struct remote *remote) case PUSH_DEFAULT_SIMPLE: if (!same_remote) break; - if (strcmp(branch->refname, get_upstream_ref(branch, remote->name))) + if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name))) die_push_simple(branch, remote); break; @@ -254,13 +270,21 @@ static void setup_default_push_refspecs(struct remote *remote) "your current branch '%s', without telling me what to push\n" "to update which remote branch."), remote->name, branch->name); - dst = get_upstream_ref(branch, remote->name); + dst = get_upstream_ref(*flags, branch, remote->name); break; case PUSH_DEFAULT_CURRENT: break; } + /* + * this is a default push - if auto-upstream is enabled and there is + * no upstream defined, then set it (with options 'simple', 'upstream', + * and 'current'). + */ + if ((*flags & TRANSPORT_PUSH_AUTO_UPSTREAM) && branch->merge_nr == 0) + *flags |= TRANSPORT_PUSH_SET_UPSTREAM; + refspec_appendf(&rs, "%s:%s", branch->refname, dst); } @@ -411,7 +435,7 @@ static int do_push(int flags, if (remote->push.nr) { push_refspec = &remote->push; } else if (!(flags & TRANSPORT_PUSH_MIRROR)) - setup_default_push_refspecs(remote); + setup_default_push_refspecs(&flags, remote); } errs = 0; url_nr = push_url_of_remote(remote, &url); @@ -482,6 +506,10 @@ static int git_push_config(const char *k, const char *v, void *cb) else *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS; return 0; + } else if (!strcmp(k, "push.autosetupremote")) { + if (git_config_bool(k, v)) + *flags |= TRANSPORT_PUSH_AUTO_UPSTREAM; + return 0; } else if (!strcmp(k, "push.gpgsign")) { const char *value; if (!git_config_get_value("push.gpgsign", &value)) { diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh index 0d6c9869ed3..284e20fefda 100755 --- a/t/t5528-push-default.sh +++ b/t/t5528-push-default.sh @@ -162,6 +162,20 @@ test_expect_success 'push from/to branch with tracking fails with nothing ' ' test_push_failure nothing ' +test_expect_success 'push from/to new branch succeeds with upstream if push.autoSetupRemote' ' + git checkout -b new-branch-a && + test_config push.autoSetupRemote true && + test_config branch.new-branch-a.remote parent1 && + test_push_success upstream new-branch-a +' + +test_expect_success 'push from/to new branch succeeds with simple if push.autoSetupRemote' ' + git checkout -b new-branch-c && + test_config push.autoSetupRemote true && + test_config branch.new-branch-c.remote parent1 && + test_push_success simple new-branch-c +' + test_expect_success '"matching" fails if none match' ' git init --bare empty && test_must_fail git push empty : 2>actual && diff --git a/transport.h b/transport.h index 12bc08fc339..b5bf7b3e704 100644 --- a/transport.h +++ b/transport.h @@ -145,6 +145,7 @@ struct transport { #define TRANSPORT_PUSH_OPTIONS (1<<14) #define TRANSPORT_RECURSE_SUBMODULES_ONLY (1<<15) #define TRANSPORT_PUSH_FORCE_IF_INCLUDES (1<<16) +#define TRANSPORT_PUSH_AUTO_UPSTREAM (1<<17) int transport_summary_width(const struct ref *refs); -- gitgitgadget ^ permalink raw reply related [flat|nested] 41+ messages in thread
* Re: [PATCH v5 0/3] New options to support "simple" centralized workflow 2022-04-29 9:56 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget ` (2 preceding siblings ...) 2022-04-29 9:56 ` [PATCH v5 3/3] push: new config option "push.autoSetupRemote" supports "simple" push Tao Klerks via GitGitGadget @ 2022-04-29 18:50 ` Junio C Hamano 2022-04-30 15:48 ` Tao Klerks 3 siblings, 1 reply; 41+ messages in thread From: Junio C Hamano @ 2022-04-29 18:50 UTC (permalink / raw) To: Tao Klerks via GitGitGadget Cc: git, Tao Klerks, Ævar Arnfjörð Bjarmason, Eric Sunshine, Josh Steadmon "Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com> writes: > This patchset introduces two new configuration options, intended to be > consistent with and complementary to the push.default "simple" option. It > also improves remote-defaulting in "default push" scenarios. Thanks. I still do not know offhand if the 'simple' thing makes sense without thinking it through, but I think that the 'missing origin is fine and we can use the unique remote if exists' is a really good idea, especially if some push strategies already do so and some don't, which seems to be the case. Will queue. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH v5 0/3] New options to support "simple" centralized workflow 2022-04-29 18:50 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Junio C Hamano @ 2022-04-30 15:48 ` Tao Klerks 0 siblings, 0 replies; 41+ messages in thread From: Tao Klerks @ 2022-04-30 15:48 UTC (permalink / raw) To: Junio C Hamano Cc: Tao Klerks via GitGitGadget, git, Ævar Arnfjörð Bjarmason, Eric Sunshine, Josh Steadmon On Fri, Apr 29, 2022 at 8:50 PM Junio C Hamano <gitster@pobox.com> wrote: > > I still do not know offhand if the 'simple' thing makes > sense without thinking it through, At the risk of insisting too much, I'd like to break this down into 3 parts: 1) To what extent does a "there is one remote, and local and remote branches have the same name unless I explicitly choose to do something different" perspective make sense to a given population of users, and how large is that population of users? 2) For those users, can and should we have a better UX? 3) To what extent does it make sense to call this mode of working "simple", what are the best UX changes to make, what should any new options be called, and how should discoverability be implemented? 1. Audience: I understand that git was designed as a distributed VCS, and that's a completely fundamental aspect of its power and success... but the reality (or "my claim"?) is that the vast majority of users end up using git with a single remote per repo. I don't know how to categorically confirm this - I suspect the github/microsoft, google and other sponsor-type folks here will have more access to research on the topic. I don't want to imply that git should do less, but just that the idea of "multiple remotes" is alien to almost every git user I've ever interacted with. Obviously as I work in a corporate environment I have a particular perspective... but I find this to be true of github users also. Within the context of such "single remote per repo" users, I've spoken with a dozen users of varying git experience levels to try to understand whether *any* of them intentionally end up with an "upstream tracking branch different from the local branch name" scenario, and what they use it for. I found two users who had ever done this intentionally: One who had done it once, when faced with a project with crazy machine-generated branch names, and another who does it routinely to have nice short local branch names (very much an advanced user and enthusiast). To the majority it's only ever happened by accident, and they didn't even understand what was going on. It was just a weird message they got and eventually worked around. Amusingly, one was an old hand, and still avoided the default "git push" because he remembered a time when that pushed all branches, and did not realize the default behavior had changed to "current branch, as long as remote tracking name matches" (aka push.default=simple) 8 years ago. All these users are aware that there are options that change git's behavior, but the only one who ever took the time to understand and consider changing the defaults was the expert user enthusiast. I realize all this is anecdotal, I'm a hobbyist and a novice in this community, and the deployment I support only has a few hundred users at the moment - but surely there must be a way to confirm whether it's true that git's primary value to millions of users in the world is in a context where there is a single remote, and branches normally and intentionally have exactly the same name locally and on that single remote? 2. Current Experience Taking the user model/workflow above and its statistical significance as a given, what's the "problem"? A) a user can accidentally end up in an unexpected state, and not easily understand why or what's going on, if they do "git checkout -b mybranch origin/whatever" - that is, if they choose to branch from a known remote state, rather than creating a local branch for that remote branch first. In this unexpected state their "git pull" is not doing what they expect (it's bringing in changes from a *different* branch), and their "git push" is not working. Furthermore, the error message for "git push" is not actually giving them the right option to solve their problem - it suggests they push to the same-name remote branch, but does not propose the "-u" option, because git can't be sure the mismatching branch name isn't intentional and "-u" would be a kind of destructive change! So they will remain in this weird/unexpected state unless/until they figure out for themselves to specify -u or otherwise change the tracking upstream. I've seen people delete the local branch, and recreate it, just to sort out the remote tracking, because it's just not obvious to them what is going on! Other flows don't have this issue, eg if they first "git checkout master" (potentially creating a new master branch with tracking from remote) and then "git checkout -b mybranch". That inconsistency is part of the problem - it forces affected users to think about remote tracking branches in a way they shouldn't need to, in a way that is basically alien to their day-to-day experience and expectations of the relationship between local and remote. B) When a user creates a new branch and they want to push it, they get an error that spits out a magical incantation hint, they repeat the magical incantation, and then things are working as expected. This is a lot better than lacking the hint, of course, but is a completely unnecessary interruption in their workflow, *given the assumption that remote branches for these users always have the same name as local branches anyway*. The intention of a default "git push", in this (in my opinion vast-majority) situation, is simply to make this branch work with its remote equivalent. 3) Naming & changes to git behaviors One way to approach the desired flow above would be to do away with or ignore the concept of upstream tracking branches altogether, and have a git behavior mode in which "git pull", "git push", and "git status" all work automatically and consistently with the same-name remote branch. I think there are a few problems with that approach: - It would not be an on-ramp to slightly different behaviors / modes of functioning - We'd have to figure out what to do with any then-ignored upstream tracking entries for existing branches - It would involve a lot of code changes - It would be hard to explain in relation to all the rest of the doc/behaviors - A user interested in working with just a single locally-differently-named branch (eg because they're working on a server with remote branch names that they can't change and are inconveniently long, or have complex prexif/namespacing requirements) would not be able to make use of such a mode - they'd have to switch to the "full/normal" mode. Therefore, it makes more sense to figure out the smallest changes in behavior that lead to meeting the expectations/conveniences above, and don't prevent still keeping branches that have a different name to the remote, when that is very explicitly desired & specified. Hence the proposals in this patch series. I do truly believe that the two small changes (new "don't auto-track differently-named upstream branches" option, and new "automatically add remote tracking for same-name branch if missing" option) are the right thing. What I don't know, is whether they are *named* in the best possible way, and whether the text of the proposed "hints" is the best way to help the (in my opinion) majority of users who will probably benefit from setting things up this way. > but I think that the 'missing > origin is fine and we can use the unique remote if exists' is a > really good idea, Cool, that's an easy one, and a separate commit if you want to split it off. It's a prerequisite for the "push.autoSetupRemote" to work well (in repos that have a single remote not called "origin"), but it does not depend on the other proposed changes. > especially if some push strategies already do so > and some don't, which seems to be the case. Not exactly - there are other *commands* that do this kind of "the single remote" defaulting, but not other push strategies. The reason I called out only two default push strategies explicitly, is that they are the ones that can work without a remote tracking branch being configured at all (as long as there is a remote called origin); the other strategies depend on a remote being explicitly configured as push default, or as branch remote, or as branch push remote. > > Will queue. Great thx. ^ permalink raw reply [flat|nested] 41+ messages in thread
end of thread, other threads:[~2022-04-30 15:51 UTC | newest] Thread overview: 41+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-02-24 9:45 [PATCH 0/3] adding new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-02-24 9:45 ` [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-02-24 19:20 ` Junio C Hamano 2022-02-24 9:45 ` [PATCH 2/3] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-02-24 9:45 ` [PATCH 3/3] branch documentation: new autosetupmerge " Tao Klerks via GitGitGadget 2022-02-24 19:38 ` Junio C Hamano 2022-02-25 18:52 ` [PATCH v2 0/2] adding new branch.autosetupmerge " Tao Klerks via GitGitGadget 2022-02-25 18:52 ` [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-02-25 20:15 ` Junio C Hamano 2022-02-27 23:59 ` Tao Klerks 2022-02-25 18:52 ` [PATCH v2 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 0/2] adding " Tao Klerks via GitGitGadget 2022-02-28 7:14 ` [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-02-28 10:39 ` Ævar Arnfjörð Bjarmason 2022-03-02 9:35 ` Tao Klerks 2022-03-20 17:00 ` Tao Klerks 2022-02-28 7:14 ` [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget 2022-02-28 9:34 ` Ævar Arnfjörð Bjarmason 2022-03-01 2:58 ` Eric Sunshine 2022-03-01 9:59 ` Tao Klerks 2022-03-01 9:59 ` Tao Klerks 2022-03-21 6:17 ` [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-04-18 18:15 ` Josh Steadmon 2022-04-20 5:12 ` Tao Klerks 2022-04-20 17:19 ` Josh Steadmon 2022-04-20 17:43 ` Junio C Hamano 2022-04-20 21:31 ` Tao Klerks 2022-04-21 1:53 ` Junio C Hamano 2022-04-21 10:04 ` Tao Klerks 2022-04-22 2:27 ` Junio C Hamano 2022-04-22 9:24 ` Tao Klerks 2022-04-22 13:27 ` Tao Klerks 2022-04-23 4:44 ` Junio C Hamano 2022-04-24 11:57 ` Tao Klerks 2022-04-29 7:31 ` Tao Klerks 2022-04-29 9:56 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 1/3] branch: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 2/3] push: default to single remote even when not named origin Tao Klerks via GitGitGadget 2022-04-29 9:56 ` [PATCH v5 3/3] push: new config option "push.autoSetupRemote" supports "simple" push Tao Klerks via GitGitGadget 2022-04-29 18:50 ` [PATCH v5 0/3] New options to support "simple" centralized workflow Junio C Hamano 2022-04-30 15:48 ` Tao Klerks
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.