From: Junio C Hamano Date: Mon, 10 Jan 2022 19:52:54 +0000 (-0800) Subject: Merge branch 'js/branch-track-inherit' X-Git-Tag: v2.35.0-rc0~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0669bdf4ebc095a2b26654292e1ff38245ccf88a;p=thirdparty%2Fgit.git 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 --- 0669bdf4ebc095a2b26654292e1ff38245ccf88a diff --cc branch.c index 2cfe496d24,a4e4631ef1..829130bb11 --- a/branch.c +++ b/branch.c @@@ -59,15 -68,27 +68,27 @@@ static int install_branch_config_multip { const char *shortname = NULL; struct strbuf key = STRBUF_INIT; + struct string_list_item *item; int rebasing = should_setup_rebase(origin); - if (skip_prefix(remote, "refs/heads/", &shortname) - && !strcmp(local, shortname) - && !origin) { - warning(_("not setting branch %s as its own upstream"), - local); - return 0; - } + if (!remotes->nr) + BUG("must provide at least one remote for branch config"); + if (rebasing && remotes->nr > 1) + die(_("cannot inherit upstream tracking configuration of " + "multiple refs when rebasing is requested")); + + /* + * 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."), ++ warning(_("not setting branch '%s' as its own upstream"), + local); + return 0; + } strbuf_addf(&key, "branch.%s.remote", local); if (git_config_set_gently(key.buf, origin ? origin : ".") < 0) @@@ -116,12 -157,22 +157,22 @@@ out_err: strbuf_release(&key); - error(_("Unable to write upstream branch configuration")); + error(_("unable to write upstream branch configuration")); - advise(_(tracking_advice), - origin ? origin : "", - origin ? "/" : "", - shortname ? shortname : remote); + advise(_("\nAfter fixing the error cause you may try to fix up\n" + "the remote tracking information by invoking:")); + if (remotes->nr == 1) + 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; } @@@ -153,14 -249,16 +249,16 @@@ static void setup_tracking(const char * } if (tracking.matches > 1) - die(_("Not tracking: ambiguous information for ref %s"), + die(_("not tracking: ambiguous information for ref %s"), orig_ref); - if (install_branch_config(config_flags, new_ref, tracking.remote, - tracking.src ? tracking.src : orig_ref) < 0) + if (tracking.srcs->nr < 1) + string_list_append(tracking.srcs, orig_ref); + if (install_branch_config_multiple_remotes(config_flags, new_ref, + tracking.remote, tracking.srcs) < 0) exit(-1); - free(tracking.src); + string_list_clear(tracking.srcs, 0); } int read_branch_desc(struct strbuf *buf, const char *branch_name) diff --cc t/t3200-branch.sh index 8a619d785e,a74b2e06a1..1bc3795847 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@@ -979,10 -950,10 +979,10 @@@ test_expect_success 'disabled option -- 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 && cat >expect <<-\EOF && - warning: not setting branch my13 as its own upstream - warning: not setting branch 'my13' as its own upstream. ++ warning: not setting branch 'my13' as its own upstream EOF test_expect_code 1 git config branch.my13.remote && test_expect_code 1 git config branch.my13.merge &&