Skip to content

Commit c60bb48

Browse files
TaoKgitster
authored andcommitted
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. Signed-off-by: Tao Klerks <tao@klerks.biz> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent dab1b79 commit c60bb48

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

Documentation/config/branch.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ branch.autoSetupMerge::
99
automatic setup is done when the starting point is either a
1010
local branch or remote-tracking branch; `inherit` -- if the starting point
1111
has a tracking configuration, it is copied to the new
12-
branch. This option defaults to true.
12+
branch; `simple` -- automatic setup is done only when the starting point
13+
is a remote-tracking branch and the new branch has the same name as the
14+
remote branch. This option defaults to true.
1315

1416
branch.autoSetupRebase::
1517
When a new branch is created with 'git branch', 'git switch' or 'git checkout'

Documentation/git-branch.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,17 @@ The exact upstream branch is chosen depending on the optional argument:
221221
itself as the upstream; `--track=inherit` means to copy the upstream
222222
configuration of the start-point branch.
223223
+
224-
`--track=direct` is the default when the start point is a remote-tracking branch.
225-
Set the branch.autoSetupMerge configuration variable to `false` if you
226-
want `git switch`, `git checkout` and `git branch` to always behave as if `--no-track`
227-
were given. Set it to `always` if you want this behavior when the
228-
start-point is either a local or remote-tracking branch. Set it to
229-
`inherit` if you want to copy the tracking configuration from the
230-
branch point.
224+
The branch.autoSetupMerge configuration variable specifies how `git switch`,
225+
`git checkout` and `git branch` should behave when neither `--track` nor
226+
`--no-track` are specified:
227+
+
228+
The default option, `true`, behaves as though `--track=direct`
229+
were given whenever the start-point is a remote-tracking branch.
230+
`false` behaves as if `--no-track` were given. `always` behaves as though
231+
`--track=direct` were given. `inherit` behaves as though `--track=inherit`
232+
were given. `simple` behaves as though `--track=direct` were given only when
233+
the start-point is a remote-tracking branch and the new branch has the same
234+
name as the remote branch.
231235
+
232236
See linkgit:git-pull[1] and linkgit:git-config[1] for additional discussion on
233237
how the `branch.<name>.remote` and `branch.<name>.merge` options are used.

branch.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,29 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
252252
goto cleanup;
253253
}
254254

255+
/*
256+
* This check does not apply to the BRANCH_TRACK_INHERIT
257+
* option; you can inherit one or more tracking entries
258+
* and the tracking.matches counter is not incremented.
259+
*/
255260
if (tracking.matches > 1)
256261
die(_("not tracking: ambiguous information for ref %s"),
257262
orig_ref);
258263

264+
if (track == BRANCH_TRACK_SIMPLE) {
265+
/*
266+
* Only track if remote branch name matches.
267+
* Reaching into items[0].string is safe because
268+
* we know there is at least one and not more than
269+
* one entry (because not BRANCH_TRACK_INHERIT).
270+
*/
271+
const char *tracked_branch;
272+
if (!skip_prefix(tracking.srcs->items[0].string,
273+
"refs/heads/", &tracked_branch) ||
274+
strcmp(tracked_branch, new_ref))
275+
return;
276+
}
277+
259278
if (tracking.srcs->nr < 1)
260279
string_list_append(tracking.srcs, orig_ref);
261280
if (install_branch_config_multiple_remotes(config_flags, new_ref,

branch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum branch_track {
1212
BRANCH_TRACK_EXPLICIT,
1313
BRANCH_TRACK_OVERRIDE,
1414
BRANCH_TRACK_INHERIT,
15+
BRANCH_TRACK_SIMPLE,
1516
};
1617

1718
extern enum branch_track git_branch_track;

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,9 @@ static int git_default_branch_config(const char *var, const char *value)
16731673
} else if (value && !strcmp(value, "inherit")) {
16741674
git_branch_track = BRANCH_TRACK_INHERIT;
16751675
return 0;
1676+
} else if (value && !strcmp(value, "simple")) {
1677+
git_branch_track = BRANCH_TRACK_SIMPLE;
1678+
return 0;
16761679
}
16771680
git_branch_track = git_config_bool(var, value);
16781681
return 0;

0 commit comments

Comments
 (0)