Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
push: new config option "push.autoSetupRemote" supports "simple" push
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
TaoK authored and gitster committed Apr 29, 2022
1 parent 8a649be commit 05d5775
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Documentation/config/push.txt
@@ -1,3 +1,14 @@
push.autoSetupRemote::

This comment has been minimized.

Copy link
@ckcr4lyf

ckcr4lyf Jul 5, 2022

@TaoK this is awesome, thanks a lot! One thing I was wondering is, why this feature didn't make it into the release notes? Can't find it at https://github.com/git/git/blob/v2.37.0/Documentation/RelNotes/2.37.0.txt

(accidentally discovered since I always just try git push and then use https://github.com/nvbn/thefuck to autofix it)

This comment has been minimized.

Copy link
@dscho

dscho Jul 5, 2022

Member

@ckcr4lyf Thank you for writing up a bug report. However, this is not the place the Git projects expects bug reports: please send it to the Git mailing list. See https://git-scm.com/community for more details.

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).
Expand Down
44 changes: 36 additions & 8 deletions builtin/push.c
Expand Up @@ -195,24 +195,40 @@ 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);

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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
14 changes: 14 additions & 0 deletions t/t5528-push-default.sh
Expand Up @@ -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 &&
Expand Down
1 change: 1 addition & 0 deletions transport.h
Expand Up @@ -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);

Expand Down

0 comments on commit 05d5775

Please sign in to comment.