Merge branch 'js/branch-track-inherit'

"git -c branch.autosetupmerge=inherit branch new old" makes "new"
to have the same upstream as the "old" branch, instead of marking
"old" itself as its upstream.

* js/branch-track-inherit:
  config: require lowercase for branch.*.autosetupmerge
  branch: add flags and config to inherit tracking
  branch: accept multiple upstream branches for tracking
This commit is contained in:
Junio C Hamano 2022-01-10 11:52:54 -08:00
commit 0669bdf4eb
16 changed files with 313 additions and 68 deletions

View File

@ -7,7 +7,8 @@ branch.autoSetupMerge::
automatic setup is done; `true` -- automatic setup is done when the automatic setup is done; `true` -- automatic setup is done when the
starting point is a remote-tracking branch; `always` -- starting point is a remote-tracking branch; `always` --
automatic setup is done when the starting point is either a automatic setup is done when the starting point is either a
local branch or remote-tracking 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. This option defaults to true.
branch.autoSetupRebase:: branch.autoSetupRebase::

View File

@ -16,7 +16,7 @@ SYNOPSIS
[--points-at <object>] [--format=<format>] [--points-at <object>] [--format=<format>]
[(-r | --remotes) | (-a | --all)] [(-r | --remotes) | (-a | --all)]
[--list] [<pattern>...] [--list] [<pattern>...]
'git branch' [--track | --no-track] [-f] <branchname> [<start-point>] 'git branch' [--track [direct|inherit] | --no-track] [-f] <branchname> [<start-point>]
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>] 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
'git branch' --unset-upstream [<branchname>] 'git branch' --unset-upstream [<branchname>]
'git branch' (-m | -M) [<oldbranch>] <newbranch> 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@ -206,24 +206,34 @@ This option is only applicable in non-verbose mode.
Display the full sha1s in the output listing rather than abbreviating them. Display the full sha1s in the output listing rather than abbreviating them.
-t:: -t::
--track:: --track [inherit|direct]::
When creating a new branch, set up `branch.<name>.remote` and When creating a new branch, set up `branch.<name>.remote` and
`branch.<name>.merge` configuration entries to mark the `branch.<name>.merge` configuration entries to set "upstream" tracking
start-point branch as "upstream" from the new branch. This configuration for the new branch. This
configuration will tell git to show the relationship between the configuration will tell git to show the relationship between the
two branches in `git status` and `git branch -v`. Furthermore, two branches in `git status` and `git branch -v`. Furthermore,
it directs `git pull` without arguments to pull from the it directs `git pull` without arguments to pull from the
upstream when the new branch is checked out. upstream when the new branch is checked out.
+ +
This behavior is the default when the start point is a remote-tracking branch. The exact upstream branch is chosen depending on the optional argument:
`--track` or `--track direct` means to use the start-point branch 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 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` 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 were given. Set it to `always` if you want this behavior when the
start-point is either a local or remote-tracking branch. 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.
+
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.
--no-track:: --no-track::
Do not set up "upstream" configuration, even if the Do not set up "upstream" configuration, even if the
branch.autoSetupMerge configuration variable is true. branch.autoSetupMerge configuration variable is set.
--set-upstream:: --set-upstream::
As this option had confusing syntax, it is no longer supported. As this option had confusing syntax, it is no longer supported.

View File

@ -156,7 +156,7 @@ of it").
linkgit:git-branch[1] for details. linkgit:git-branch[1] for details.
-t:: -t::
--track:: --track [direct|inherit]::
When creating a new branch, set up "upstream" configuration. See When creating a new branch, set up "upstream" configuration. See
"--track" in linkgit:git-branch[1] for details. "--track" in linkgit:git-branch[1] for details.
+ +

View File

@ -151,7 +151,7 @@ should result in deletion of the path).
attached to a terminal, regardless of `--quiet`. attached to a terminal, regardless of `--quiet`.
-t:: -t::
--track:: --track [direct|inherit]::
When creating a new branch, set up "upstream" configuration. When creating a new branch, set up "upstream" configuration.
`-c` is implied. See `--track` in linkgit:git-branch[1] for `-c` is implied. See `--track` in linkgit:git-branch[1] for
details. details.

194
branch.c
View File

@ -11,7 +11,7 @@
struct tracking { struct tracking {
struct refspec_item spec; struct refspec_item spec;
char *src; struct string_list *srcs;
const char *remote; const char *remote;
int matches; int matches;
}; };
@ -22,11 +22,11 @@ static int find_tracked_branch(struct remote *remote, void *priv)
if (!remote_find_tracking(remote, &tracking->spec)) { if (!remote_find_tracking(remote, &tracking->spec)) {
if (++tracking->matches == 1) { if (++tracking->matches == 1) {
tracking->src = tracking->spec.src; string_list_append(tracking->srcs, tracking->spec.src);
tracking->remote = remote->name; tracking->remote = remote->name;
} else { } else {
free(tracking->spec.src); free(tracking->spec.src);
FREE_AND_NULL(tracking->src); string_list_clear(tracking->srcs, 0);
} }
tracking->spec.src = NULL; tracking->spec.src = NULL;
} }
@ -49,25 +49,46 @@ static int should_setup_rebase(const char *origin)
return 0; return 0;
} }
static const char tracking_advice[] = /**
N_("\n" * Install upstream tracking configuration for a branch; specifically, add
"After fixing the error cause you may try to fix up\n" * `branch.<name>.remote` and `branch.<name>.merge` entries.
"the remote tracking information by invoking\n" *
"\"git branch --set-upstream-to=%s%s%s\"."); * `flag` contains integer flags for options; currently only
* BRANCH_CONFIG_VERBOSE is checked.
int install_branch_config(int flag, const char *local, const char *origin, const char *remote) *
* `local` is the name of the branch whose configuration we're installing.
*
* `origin` is the name of the remote owning the upstream branches. NULL means
* the upstream branches are local to this repo.
*
* `remotes` is a list of refs that are upstream of local
*/
static int install_branch_config_multiple_remotes(int flag, const char *local,
const char *origin, struct string_list *remotes)
{ {
const char *shortname = NULL; const char *shortname = NULL;
struct strbuf key = STRBUF_INIT; struct strbuf key = STRBUF_INIT;
struct string_list_item *item;
int rebasing = should_setup_rebase(origin); int rebasing = should_setup_rebase(origin);
if (skip_prefix(remote, "refs/heads/", &shortname) if (!remotes->nr)
&& !strcmp(local, shortname) BUG("must provide at least one remote for branch config");
&& !origin) { if (rebasing && remotes->nr > 1)
warning(_("not setting branch %s as its own upstream"), die(_("cannot inherit upstream tracking configuration of "
local); "multiple refs when rebasing is requested"));
return 0;
} /*
* If the new branch is trying to track itself, something has gone
* wrong. Warn the user and don't proceed any further.
*/
if (!origin)
for_each_string_list_item(item, remotes)
if (skip_prefix(item->string, "refs/heads/", &shortname)
&& !strcmp(local, shortname)) {
warning(_("not setting branch '%s' as its own upstream"),
local);
return 0;
}
strbuf_addf(&key, "branch.%s.remote", local); strbuf_addf(&key, "branch.%s.remote", local);
if (git_config_set_gently(key.buf, origin ? origin : ".") < 0) if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
@ -75,8 +96,17 @@ int install_branch_config(int flag, const char *local, const char *origin, const
strbuf_reset(&key); strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.merge", local); strbuf_addf(&key, "branch.%s.merge", local);
if (git_config_set_gently(key.buf, remote) < 0) /*
* We want to overwrite any existing config with all the branches in
* "remotes". Override any existing config, then write our branches. If
* more than one is provided, use CONFIG_REGEX_NONE to preserve what
* we've written so far.
*/
if (git_config_set_gently(key.buf, NULL) < 0)
goto out_err; goto out_err;
for_each_string_list_item(item, remotes)
if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
goto out_err;
if (rebasing) { if (rebasing) {
strbuf_reset(&key); strbuf_reset(&key);
@ -87,29 +117,40 @@ int install_branch_config(int flag, const char *local, const char *origin, const
strbuf_release(&key); strbuf_release(&key);
if (flag & BRANCH_CONFIG_VERBOSE) { if (flag & BRANCH_CONFIG_VERBOSE) {
if (shortname) { struct strbuf tmp_ref_name = STRBUF_INIT;
if (origin) struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
printf_ln(rebasing ?
_("Branch '%s' set up to track remote branch '%s' from '%s' by rebasing.") : for_each_string_list_item(item, remotes) {
_("Branch '%s' set up to track remote branch '%s' from '%s'."), shortname = item->string;
local, shortname, origin); skip_prefix(shortname, "refs/heads/", &shortname);
else if (origin) {
printf_ln(rebasing ? strbuf_addf(&tmp_ref_name, "%s/%s",
_("Branch '%s' set up to track local branch '%s' by rebasing.") : origin, shortname);
_("Branch '%s' set up to track local branch '%s'."), string_list_append_nodup(
local, shortname); &friendly_ref_names,
} else { strbuf_detach(&tmp_ref_name, NULL));
if (origin) } else {
printf_ln(rebasing ? string_list_append(
_("Branch '%s' set up to track remote ref '%s' by rebasing.") : &friendly_ref_names, shortname);
_("Branch '%s' set up to track remote ref '%s'."), }
local, remote);
else
printf_ln(rebasing ?
_("Branch '%s' set up to track local ref '%s' by rebasing.") :
_("Branch '%s' set up to track local ref '%s'."),
local, remote);
} }
if (remotes->nr == 1) {
/*
* Rebasing is only allowed in the case of a single
* upstream branch.
*/
printf_ln(rebasing ?
_("branch '%s' set up to track '%s' by rebasing.") :
_("branch '%s' set up to track '%s'."),
local, friendly_ref_names.items[0].string);
} else {
printf_ln(_("branch '%s' set up to track:"), local);
for_each_string_list_item(item, &friendly_ref_names)
printf_ln(" %s", item->string);
}
string_list_clear(&friendly_ref_names, 0);
} }
return 0; return 0;
@ -118,14 +159,64 @@ out_err:
strbuf_release(&key); strbuf_release(&key);
error(_("unable to write upstream branch configuration")); error(_("unable to write upstream branch configuration"));
advise(_(tracking_advice), advise(_("\nAfter fixing the error cause you may try to fix up\n"
origin ? origin : "", "the remote tracking information by invoking:"));
origin ? "/" : "", if (remotes->nr == 1)
shortname ? shortname : remote); advise(" git branch --set-upstream-to=%s%s%s",
origin ? origin : "",
origin ? "/" : "",
remotes->items[0].string);
else {
advise(" git config --add branch.\"%s\".remote %s",
local, origin ? origin : ".");
for_each_string_list_item(item, remotes)
advise(" git config --add branch.\"%s\".merge %s",
local, item->string);
}
return -1; return -1;
} }
int install_branch_config(int flag, const char *local, const char *origin,
const char *remote)
{
int ret;
struct string_list remotes = STRING_LIST_INIT_DUP;
string_list_append(&remotes, remote);
ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
string_list_clear(&remotes, 0);
return ret;
}
static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
{
const char *bare_ref;
struct branch *branch;
int i;
bare_ref = orig_ref;
skip_prefix(orig_ref, "refs/heads/", &bare_ref);
branch = branch_get(bare_ref);
if (!branch->remote_name) {
warning(_("asked to inherit tracking from '%s', but no remote is set"),
bare_ref);
return -1;
}
if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
bare_ref);
return -1;
}
tracking->remote = xstrdup(branch->remote_name);
for (i = 0; i < branch->merge_nr; i++)
string_list_append(tracking->srcs, branch->merge_name[i]);
return 0;
}
/* /*
* This is called when new_ref is branched off of orig_ref, and tries * This is called when new_ref is branched off of orig_ref, and tries
* to infer the settings for branch.<new_ref>.{remote,merge} from the * to infer the settings for branch.<new_ref>.{remote,merge} from the
@ -135,11 +226,15 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
enum branch_track track, int quiet) enum branch_track track, int quiet)
{ {
struct tracking tracking; struct tracking tracking;
struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
memset(&tracking, 0, sizeof(tracking)); memset(&tracking, 0, sizeof(tracking));
tracking.spec.dst = (char *)orig_ref; tracking.spec.dst = (char *)orig_ref;
if (for_each_remote(find_tracked_branch, &tracking)) tracking.srcs = &tracking_srcs;
if (track != BRANCH_TRACK_INHERIT)
for_each_remote(find_tracked_branch, &tracking);
else if (inherit_tracking(&tracking, orig_ref))
return; return;
if (!tracking.matches) if (!tracking.matches)
@ -147,6 +242,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
case BRANCH_TRACK_ALWAYS: case BRANCH_TRACK_ALWAYS:
case BRANCH_TRACK_EXPLICIT: case BRANCH_TRACK_EXPLICIT:
case BRANCH_TRACK_OVERRIDE: case BRANCH_TRACK_OVERRIDE:
case BRANCH_TRACK_INHERIT:
break; break;
default: default:
return; return;
@ -156,11 +252,13 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
die(_("not tracking: ambiguous information for ref %s"), die(_("not tracking: ambiguous information for ref %s"),
orig_ref); orig_ref);
if (install_branch_config(config_flags, new_ref, tracking.remote, if (tracking.srcs->nr < 1)
tracking.src ? tracking.src : orig_ref) < 0) string_list_append(tracking.srcs, orig_ref);
if (install_branch_config_multiple_remotes(config_flags, new_ref,
tracking.remote, tracking.srcs) < 0)
exit(-1); exit(-1);
free(tracking.src); string_list_clear(tracking.srcs, 0);
} }
int read_branch_desc(struct strbuf *buf, const char *branch_name) int read_branch_desc(struct strbuf *buf, const char *branch_name)

View File

@ -10,7 +10,8 @@ enum branch_track {
BRANCH_TRACK_REMOTE, BRANCH_TRACK_REMOTE,
BRANCH_TRACK_ALWAYS, BRANCH_TRACK_ALWAYS,
BRANCH_TRACK_EXPLICIT, BRANCH_TRACK_EXPLICIT,
BRANCH_TRACK_OVERRIDE BRANCH_TRACK_OVERRIDE,
BRANCH_TRACK_INHERIT,
}; };
extern enum branch_track git_branch_track; extern enum branch_track git_branch_track;

View File

@ -638,8 +638,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
OPT__VERBOSE(&filter.verbose, OPT__VERBOSE(&filter.verbose,
N_("show hash and subject, give twice for upstream branch")), N_("show hash and subject, give twice for upstream branch")),
OPT__QUIET(&quiet, N_("suppress informational messages")), OPT__QUIET(&quiet, N_("suppress informational messages")),
OPT_SET_INT('t', "track", &track, N_("set up tracking mode (see git-pull(1))"), OPT_CALLBACK_F('t', "track", &track, "direct|inherit",
BRANCH_TRACK_EXPLICIT), N_("set branch tracking configuration"),
PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
parse_opt_tracking_mode),
OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"), OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN), BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")), OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),

View File

@ -1549,8 +1549,10 @@ static struct option *add_common_switch_branch_options(
{ {
struct option options[] = { struct option options[] = {
OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")),
OPT_SET_INT('t', "track", &opts->track, N_("set upstream info for new branch"), OPT_CALLBACK_F('t', "track", &opts->track, "direct|inherit",
BRANCH_TRACK_EXPLICIT), N_("set up tracking mode (see git-pull(1))"),
PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
parse_opt_tracking_mode),
OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
PARSE_OPT_NOCOMPLETE), PARSE_OPT_NOCOMPLETE),
OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")), OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")),

View File

@ -1559,9 +1559,12 @@ static int git_default_i18n_config(const char *var, const char *value)
static int git_default_branch_config(const char *var, const char *value) static int git_default_branch_config(const char *var, const char *value)
{ {
if (!strcmp(var, "branch.autosetupmerge")) { if (!strcmp(var, "branch.autosetupmerge")) {
if (value && !strcasecmp(value, "always")) { if (value && !strcmp(value, "always")) {
git_branch_track = BRANCH_TRACK_ALWAYS; git_branch_track = BRANCH_TRACK_ALWAYS;
return 0; return 0;
} else if (value && !strcmp(value, "inherit")) {
git_branch_track = BRANCH_TRACK_INHERIT;
return 0;
} }
git_branch_track = git_config_bool(var, value); git_branch_track = git_config_bool(var, value);
return 0; return 0;

View File

@ -1,5 +1,6 @@
#include "git-compat-util.h" #include "git-compat-util.h"
#include "parse-options.h" #include "parse-options.h"
#include "branch.h"
#include "cache.h" #include "cache.h"
#include "commit.h" #include "commit.h"
#include "color.h" #include "color.h"
@ -293,3 +294,18 @@ int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset
return 0; return 0;
} }
int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
{
if (unset)
*(enum branch_track *)opt->value = BRANCH_TRACK_NEVER;
else if (!arg || !strcmp(arg, "direct"))
*(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT;
else if (!strcmp(arg, "inherit"))
*(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT;
else
return error(_("option `%s' expects \"%s\" or \"%s\""),
"--track", "direct", "inherit");
return 0;
}

View File

@ -301,6 +301,8 @@ enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
const char *, int); const char *, int);
int parse_opt_passthru(const struct option *, const char *, int); int parse_opt_passthru(const struct option *, const char *, int);
int parse_opt_passthru_argv(const struct option *, const char *, int); int parse_opt_passthru_argv(const struct option *, const char *, int);
/* value is enum branch_track* */
int parse_opt_tracking_mode(const struct option *, const char *, int);
#define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h)) #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h))
#define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h)) #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h))

View File

@ -63,8 +63,17 @@ test_expect_success '--orphan ignores branch.autosetupmerge' '
git checkout main && git checkout main &&
git config branch.autosetupmerge always && git config branch.autosetupmerge always &&
git checkout --orphan gamma && git checkout --orphan gamma &&
test -z "$(git config branch.gamma.merge)" && test_cmp_config "" --default "" branch.gamma.merge &&
test refs/heads/gamma = "$(git symbolic-ref HEAD)" && test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
test_must_fail git rev-parse --verify HEAD^ &&
git checkout main &&
git config branch.autosetupmerge inherit &&
git checkout --orphan eta &&
test_cmp_config "" --default "" branch.eta.merge &&
test_cmp_config "" --default "" branch.eta.remote &&
echo refs/heads/eta >expected &&
git symbolic-ref HEAD >actual &&
test_cmp expected actual &&
test_must_fail git rev-parse --verify HEAD^ test_must_fail git rev-parse --verify HEAD^
' '

View File

@ -24,4 +24,27 @@ test_expect_success 'checkout --track -b rejects an extra path argument' '
test_i18ngrep "cannot be used with updating paths" err test_i18ngrep "cannot be used with updating paths" err
' '
test_expect_success 'checkout --track -b overrides autoSetupMerge=inherit' '
# Set up tracking config on main
test_config branch.main.remote origin &&
test_config branch.main.merge refs/heads/some-branch &&
test_config branch.autoSetupMerge inherit &&
# With --track=inherit, we copy the tracking config from main
git checkout --track=inherit -b b1 main &&
test_cmp_config origin branch.b1.remote &&
test_cmp_config refs/heads/some-branch branch.b1.merge &&
# With branch.autoSetupMerge=inherit, we do the same
git checkout -b b2 main &&
test_cmp_config origin branch.b2.remote &&
test_cmp_config refs/heads/some-branch branch.b2.merge &&
# But --track overrides this
git checkout --track -b b3 main &&
test_cmp_config . branch.b3.remote &&
test_cmp_config refs/heads/main branch.b3.merge &&
# And --track=direct does as well
git checkout --track=direct -b b4 main &&
test_cmp_config . branch.b4.remote &&
test_cmp_config refs/heads/main branch.b4.merge
'
test_done test_done

View File

@ -107,4 +107,32 @@ test_expect_success 'not switching when something is in progress' '
test_must_fail git switch -d @^ test_must_fail git switch -d @^
' '
test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
# default config does not copy tracking info
git switch -c foo-no-inherit foo &&
test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
# with --track=inherit, we copy tracking info from foo
git switch --track=inherit -c foo2 foo &&
test_cmp_config origin branch.foo2.remote &&
test_cmp_config refs/heads/foo branch.foo2.merge &&
# with autoSetupMerge=inherit, we do the same
test_config branch.autoSetupMerge inherit &&
git switch -c foo3 foo &&
test_cmp_config origin branch.foo3.remote &&
test_cmp_config refs/heads/foo branch.foo3.merge &&
# with --track, we override autoSetupMerge
git switch --track -c foo4 foo &&
test_cmp_config . branch.foo4.remote &&
test_cmp_config refs/heads/foo branch.foo4.merge &&
# and --track=direct does as well
git switch --track=direct -c foo5 foo &&
test_cmp_config . branch.foo5.remote &&
test_cmp_config refs/heads/foo branch.foo5.merge &&
# no tracking info to inherit from main
git switch -c main2 main &&
test_cmp_config "" --default "" branch.main2.remote &&
test_cmp_config "" --default "" branch.main2.merge
'
test_done test_done

View File

@ -979,15 +979,15 @@ test_expect_success 'disabled option --set-upstream fails' '
test_must_fail git branch --set-upstream origin/main test_must_fail git branch --set-upstream origin/main
' '
test_expect_success '--set-upstream-to notices an error to set branch as own upstream' ' test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
git branch --set-upstream-to refs/heads/my13 my13 2>actual && git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
cat >expect <<-\EOF && cat >expect <<-\EOF &&
warning: not setting branch my13 as its own upstream warning: not setting branch 'my13' as its own upstream
EOF EOF
test_expect_code 1 git config branch.my13.remote && test_expect_code 1 git config branch.my13.remote &&
test_expect_code 1 git config branch.my13.merge && test_expect_code 1 git config branch.my13.merge &&
test_cmp expect actual test_cmp expect actual
' "
# Keep this test last, as it changes the current branch # Keep this test last, as it changes the current branch
cat >expect <<EOF cat >expect <<EOF
@ -1461,4 +1461,37 @@ test_expect_success 'invalid sort parameter in configuration' '
) )
' '
test_expect_success 'tracking info copied with --track=inherit' '
git branch --track=inherit foo2 my1 &&
test_cmp_config local branch.foo2.remote &&
test_cmp_config refs/heads/main branch.foo2.merge
'
test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
test_unconfig branch.autoSetupMerge &&
# default config does not copy tracking info
git branch foo-no-inherit my1 &&
test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
# with autoSetupMerge=inherit, we copy tracking info from my1
test_config branch.autoSetupMerge inherit &&
git branch foo3 my1 &&
test_cmp_config local branch.foo3.remote &&
test_cmp_config refs/heads/main branch.foo3.merge &&
# no tracking info to inherit from main
git branch main2 main &&
test_cmp_config "" --default "" branch.main2.remote &&
test_cmp_config "" --default "" branch.main2.merge
'
test_expect_success '--track overrides branch.autoSetupMerge' '
test_config branch.autoSetupMerge inherit &&
git branch --track=direct foo4 my1 &&
test_cmp_config . branch.foo4.remote &&
test_cmp_config refs/heads/my1 branch.foo4.merge &&
git branch --no-track foo5 my1 &&
test_cmp_config "" --default "" branch.foo5.remote &&
test_cmp_config "" --default "" branch.foo5.merge
'
test_done test_done

View File

@ -658,4 +658,21 @@ test_expect_success 'custom merge driver with checkout -m' '
test_cmp expect arm test_cmp expect arm
' '
test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
git reset --hard main &&
# default config does not copy tracking info
git checkout -b foo-no-inherit koala/bear &&
test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
# with autoSetupMerge=inherit, we copy tracking info from koala/bear
test_config branch.autoSetupMerge inherit &&
git checkout -b foo koala/bear &&
test_cmp_config origin branch.foo.remote &&
test_cmp_config refs/heads/koala/bear branch.foo.merge &&
# no tracking info to inherit from main
git checkout -b main2 main &&
test_cmp_config "" --default "" branch.main2.remote &&
test_cmp_config "" --default "" branch.main2.merge
'
test_done test_done