195
"\n"
196
" git push %s HEAD:<name-of-remote-branch>\n");
197
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)
199
{
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
+ }
214
die(_("The current branch %s has no upstream branch.\n"
215
"To push the current branch and set the remote as upstream, use\n"
216
"\n"
204
- " git push --set-upstream %s %s\n"),
217
+ " git push --set-upstream %s %s\n"
218
+ "%s"),
219
branch->name,
220
remote_name,
207
- branch->name);
221
+ branch->name,
222
+ advice_autosetup_maybe);
223
+ }
224
if (branch->merge_nr != 1)
225
die(_("The current branch %s has multiple upstream branches, "
226
"refusing to push."), branch->name);
228
return branch->merge[0]->src;
229
}
230
215
-static void setup_default_push_refspecs(struct remote *remote)
231
+static void setup_default_push_refspecs(int *flags, struct remote *remote)
232
{
233
struct branch *branch;
234
const char *dst;
260
case PUSH_DEFAULT_SIMPLE:
261
if (!same_remote)
262
break;
247
- if (strcmp(branch->refname, get_upstream_ref(branch, remote->name)))
263
+ if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name)))
264
die_push_simple(branch, remote);
265
break;
266
270
"your current branch '%s', without telling me what to push\n"
271
"to update which remote branch."),
272
remote->name, branch->name);
257
- dst = get_upstream_ref(branch, remote->name);
273
+ dst = get_upstream_ref(*flags, branch, remote->name);
274
break;
275
276
case PUSH_DEFAULT_CURRENT:
277
break;
278
}
279
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
+
288
refspec_appendf(&rs, "%s:%s", branch->refname, dst);
289
}
290
435
if (remote->push.nr) {
436
push_refspec = &remote->push;
437
} else if (!(flags & TRANSPORT_PUSH_MIRROR))
414
- setup_default_push_refspecs(remote);
438
+ setup_default_push_refspecs(&flags, remote);
439
}
440
errs = 0;
441
url_nr = push_url_of_remote(remote, &url);
506
else
507
*flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
508
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;
513
} else if (!strcmp(k, "push.gpgsign")) {
514
const char *value;
515
if (!git_config_get_value("push.gpgsign", &value)) {