Skip to content

Commit 05d5775

Browse files
TaoKgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8a649be commit 05d5775

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

Documentation/config/push.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
push.autoSetupRemote::
2+
If set to "true" assume `--set-upstream` on default push when no
3+
upstream tracking exists for the current branch; this option
4+
takes effect with push.default options 'simple', 'upstream',
5+
and 'current'. It is useful if by default you want new branches
6+
to be pushed to the default remote (like the behavior of
7+
'push.default=current') and you also want the upstream tracking
8+
to be set. Workflows most likely to benefit from this option are
9+
'simple' central workflows where all branches are expected to
10+
have the same name on the remote.
11+
112
push.default::
213
Defines the action `git push` should take if no refspec is
314
given (whether from the command-line, config, or elsewhere).

builtin/push.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,40 @@ static const char message_detached_head_die[] =
195195
"\n"
196196
" git push %s HEAD:<name-of-remote-branch>\n");
197197

198-
static const char *get_upstream_ref(struct branch *branch, const char *remote_name)
198+
static const char *get_upstream_ref(int flags, struct branch *branch, const char *remote_name)
199199
{
200-
if (!branch->merge_nr || !branch->merge || !branch->remote_name)
200+
if (branch->merge_nr == 0 && (flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
201+
/* if missing, assume same; set_upstream will be defined later */
202+
return branch->refname;
203+
}
204+
205+
if (!branch->merge_nr || !branch->merge || !branch->remote_name) {
206+
const char *advice_autosetup_maybe = "";
207+
if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
208+
advice_autosetup_maybe = _("\n"
209+
"To have this happen automatically for "
210+
"branches without a tracking\n"
211+
"upstream, see 'push.autoSetupRemote' "
212+
"in 'git help config'.\n");
213+
}
201214
die(_("The current branch %s has no upstream branch.\n"
202215
"To push the current branch and set the remote as upstream, use\n"
203216
"\n"
204-
" git push --set-upstream %s %s\n"),
217+
" git push --set-upstream %s %s\n"
218+
"%s"),
205219
branch->name,
206220
remote_name,
207-
branch->name);
221+
branch->name,
222+
advice_autosetup_maybe);
223+
}
208224
if (branch->merge_nr != 1)
209225
die(_("The current branch %s has multiple upstream branches, "
210226
"refusing to push."), branch->name);
211227

212228
return branch->merge[0]->src;
213229
}
214230

215-
static void setup_default_push_refspecs(struct remote *remote)
231+
static void setup_default_push_refspecs(int *flags, struct remote *remote)
216232
{
217233
struct branch *branch;
218234
const char *dst;
@@ -244,7 +260,7 @@ static void setup_default_push_refspecs(struct remote *remote)
244260
case PUSH_DEFAULT_SIMPLE:
245261
if (!same_remote)
246262
break;
247-
if (strcmp(branch->refname, get_upstream_ref(branch, remote->name)))
263+
if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name)))
248264
die_push_simple(branch, remote);
249265
break;
250266

@@ -254,13 +270,21 @@ static void setup_default_push_refspecs(struct remote *remote)
254270
"your current branch '%s', without telling me what to push\n"
255271
"to update which remote branch."),
256272
remote->name, branch->name);
257-
dst = get_upstream_ref(branch, remote->name);
273+
dst = get_upstream_ref(*flags, branch, remote->name);
258274
break;
259275

260276
case PUSH_DEFAULT_CURRENT:
261277
break;
262278
}
263279

280+
/*
281+
* this is a default push - if auto-upstream is enabled and there is
282+
* no upstream defined, then set it (with options 'simple', 'upstream',
283+
* and 'current').
284+
*/
285+
if ((*flags & TRANSPORT_PUSH_AUTO_UPSTREAM) && branch->merge_nr == 0)
286+
*flags |= TRANSPORT_PUSH_SET_UPSTREAM;
287+
264288
refspec_appendf(&rs, "%s:%s", branch->refname, dst);
265289
}
266290

@@ -411,7 +435,7 @@ static int do_push(int flags,
411435
if (remote->push.nr) {
412436
push_refspec = &remote->push;
413437
} else if (!(flags & TRANSPORT_PUSH_MIRROR))
414-
setup_default_push_refspecs(remote);
438+
setup_default_push_refspecs(&flags, remote);
415439
}
416440
errs = 0;
417441
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)
482506
else
483507
*flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
484508
return 0;
509+
} else if (!strcmp(k, "push.autosetupremote")) {
510+
if (git_config_bool(k, v))
511+
*flags |= TRANSPORT_PUSH_AUTO_UPSTREAM;
512+
return 0;
485513
} else if (!strcmp(k, "push.gpgsign")) {
486514
const char *value;
487515
if (!git_config_get_value("push.gpgsign", &value)) {

t/t5528-push-default.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ test_expect_success 'push from/to branch with tracking fails with nothing ' '
162162
test_push_failure nothing
163163
'
164164

165+
test_expect_success 'push from/to new branch succeeds with upstream if push.autoSetupRemote' '
166+
git checkout -b new-branch-a &&
167+
test_config push.autoSetupRemote true &&
168+
test_config branch.new-branch-a.remote parent1 &&
169+
test_push_success upstream new-branch-a
170+
'
171+
172+
test_expect_success 'push from/to new branch succeeds with simple if push.autoSetupRemote' '
173+
git checkout -b new-branch-c &&
174+
test_config push.autoSetupRemote true &&
175+
test_config branch.new-branch-c.remote parent1 &&
176+
test_push_success simple new-branch-c
177+
'
178+
165179
test_expect_success '"matching" fails if none match' '
166180
git init --bare empty &&
167181
test_must_fail git push empty : 2>actual &&

transport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ struct transport {
145145
#define TRANSPORT_PUSH_OPTIONS (1<<14)
146146
#define TRANSPORT_RECURSE_SUBMODULES_ONLY (1<<15)
147147
#define TRANSPORT_PUSH_FORCE_IF_INCLUDES (1<<16)
148+
#define TRANSPORT_PUSH_AUTO_UPSTREAM (1<<17)
148149

149150
int transport_summary_width(const struct ref *refs);
150151

0 commit comments

Comments
 (0)