8
#include "environment.h"
11
#include "run-command.h"
14
#include "parse-options.h"
16
#include "repository.h"
18
#include "submodule-config.h"
23
static const char * const push_usage[] = {
24
N_("git push [<options>] [<repository> [<refspec>...]]"),
28
static int push_use_color = -1;
29
static char push_colors[][COLOR_MAXLEN] = {
39
static int parse_push_color_slot(const char *slot)
41
if (!strcasecmp(slot, "reset"))
42
return PUSH_COLOR_RESET;
43
if (!strcasecmp(slot, "error"))
44
return PUSH_COLOR_ERROR;
48
static const char *push_get_color(enum color_push ix)
50
if (want_color_stderr(push_use_color))
51
return push_colors[ix];
57
static const char *receivepack;
59
static int progress = -1;
60
static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
61
static enum transport_family family;
63
static struct push_cas_option cas;
65
static struct refspec rs = REFSPEC_INIT_PUSH;
67
static struct string_list push_options_config = STRING_LIST_INIT_DUP;
69
static void refspec_append_mapped(struct refspec *refspec, const char *ref,
70
struct remote *remote, struct ref *matched)
72
const char *branch_name;
74
if (remote->push.nr) {
75
struct refspec_item query;
76
memset(&query, 0, sizeof(struct refspec_item));
77
query.src = matched->name;
78
if (!query_refspecs(&remote->push, &query) && query.dst) {
79
refspec_appendf(refspec, "%s%s:%s",
80
query.force ? "+" : "",
81
query.src, query.dst);
86
if (push_default == PUSH_DEFAULT_UPSTREAM &&
87
skip_prefix(matched->name, "refs/heads/", &branch_name)) {
88
struct branch *branch = branch_get(branch_name);
89
if (branch->merge_nr == 1 && branch->merge[0]->src) {
90
refspec_appendf(refspec, "%s:%s",
91
ref, branch->merge[0]->src);
96
refspec_append(refspec, ref);
99
static void set_refspecs(const char **refs, int nr, struct remote *remote)
101
struct ref *local_refs = NULL;
104
for (i = 0; i < nr; i++) {
105
const char *ref = refs[i];
106
if (!strcmp("tag", ref)) {
108
die(_("tag shorthand without <tag>"));
111
refspec_appendf(&rs, ":refs/tags/%s", ref);
113
refspec_appendf(&rs, "refs/tags/%s", ref);
114
} else if (deleterefs) {
115
if (strchr(ref, ':') || !*ref)
116
die(_("--delete only accepts plain target ref names"));
117
refspec_appendf(&rs, ":%s", ref);
118
} else if (!strchr(ref, ':')) {
119
struct ref *matched = NULL;
123
local_refs = get_local_heads();
126
if (count_refspec_match(ref, local_refs, &matched) != 1)
127
refspec_append(&rs, ref);
129
refspec_append_mapped(&rs, ref, remote, matched);
131
refspec_append(&rs, ref);
133
free_refs(local_refs);
136
static NORETURN void die_push_simple(struct branch *branch,
137
struct remote *remote)
147
const char *advice_pushdefault_maybe = "";
148
const char *advice_automergesimple_maybe = "";
149
const char *short_upstream = branch->merge[0]->src;
151
skip_prefix(short_upstream, "refs/heads/", &short_upstream);
157
if (push_default == PUSH_DEFAULT_UNSPECIFIED)
158
advice_pushdefault_maybe = _("\n"
159
"To choose either option permanently, "
160
"see push.default in 'git help config'.\n");
161
if (git_branch_track != BRANCH_TRACK_SIMPLE)
162
advice_automergesimple_maybe = _("\n"
163
"To avoid automatically configuring "
164
"an upstream branch when its name\n"
165
"won't match the local branch, see option "
166
"'simple' of branch.autoSetupMerge\n"
167
"in 'git help config'.\n");
168
die(_("The upstream branch of your current branch does not match\n"
169
"the name of your current branch. To push to the upstream branch\n"
170
"on the remote, use\n"
172
" git push %s HEAD:%s\n"
174
"To push to the branch of the same name on the remote, use\n"
176
" git push %s HEAD\n"
178
remote->name, short_upstream,
179
remote->name, advice_pushdefault_maybe,
180
advice_automergesimple_maybe);
183
static const char message_detached_head_die[] =
184
N_("You are not currently on a branch.\n"
185
"To push the history leading to the current (detached HEAD)\n"
188
" git push %s HEAD:<name-of-remote-branch>\n");
190
static const char *get_upstream_ref(int flags, struct branch *branch, const char *remote_name)
192
if (branch->merge_nr == 0 && (flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
194
return branch->refname;
197
if (!branch->merge_nr || !branch->merge || !branch->remote_name) {
198
const char *advice_autosetup_maybe = "";
199
if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
200
advice_autosetup_maybe = _("\n"
201
"To have this happen automatically for "
202
"branches without a tracking\n"
203
"upstream, see 'push.autoSetupRemote' "
204
"in 'git help config'.\n");
206
die(_("The current branch %s has no upstream branch.\n"
207
"To push the current branch and set the remote as upstream, use\n"
209
" git push --set-upstream %s %s\n"
214
advice_autosetup_maybe);
216
if (branch->merge_nr != 1)
217
die(_("The current branch %s has multiple upstream branches, "
218
"refusing to push."), branch->name);
220
return branch->merge[0]->src;
223
static void setup_default_push_refspecs(int *flags, struct remote *remote)
225
struct branch *branch;
229
switch (push_default) {
230
case PUSH_DEFAULT_MATCHING:
231
refspec_append(&rs, ":");
234
case PUSH_DEFAULT_NOTHING:
235
die(_("You didn't specify any refspecs to push, and "
236
"push.default is \"nothing\"."));
242
branch = branch_get(NULL);
244
die(_(message_detached_head_die), remote->name);
246
dst = branch->refname;
247
same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
249
switch (push_default) {
251
case PUSH_DEFAULT_UNSPECIFIED:
252
case PUSH_DEFAULT_SIMPLE:
255
if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name)))
256
die_push_simple(branch, remote);
259
case PUSH_DEFAULT_UPSTREAM:
261
die(_("You are pushing to remote '%s', which is not the upstream of\n"
262
"your current branch '%s', without telling me what to push\n"
263
"to update which remote branch."),
264
remote->name, branch->name);
265
dst = get_upstream_ref(*flags, branch, remote->name);
268
case PUSH_DEFAULT_CURRENT:
277
if ((*flags & TRANSPORT_PUSH_AUTO_UPSTREAM) && branch->merge_nr == 0)
278
*flags |= TRANSPORT_PUSH_SET_UPSTREAM;
280
refspec_appendf(&rs, "%s:%s", branch->refname, dst);
283
static const char message_advice_pull_before_push[] =
284
N_("Updates were rejected because the tip of your current branch is behind\n"
285
"its remote counterpart. If you want to integrate the remote changes,\n"
286
"use 'git pull' before pushing again.\n"
287
"See the 'Note about fast-forwards' in 'git push --help' for details.");
289
static const char message_advice_checkout_pull_push[] =
290
N_("Updates were rejected because a pushed branch tip is behind its remote\n"
291
"counterpart. If you want to integrate the remote changes, use 'git pull'\n"
292
"before pushing again.\n"
293
"See the 'Note about fast-forwards' in 'git push --help' for details.");
295
static const char message_advice_ref_fetch_first[] =
296
N_("Updates were rejected because the remote contains work that you do not\n"
297
"have locally. This is usually caused by another repository pushing to\n"
298
"the same ref. If you want to integrate the remote changes, use\n"
299
"'git pull' before pushing again.\n"
300
"See the 'Note about fast-forwards' in 'git push --help' for details.");
302
static const char message_advice_ref_already_exists[] =
303
N_("Updates were rejected because the tag already exists in the remote.");
305
static const char message_advice_ref_needs_force[] =
306
N_("You cannot update a remote ref that points at a non-commit object,\n"
307
"or update a remote ref to make it point at a non-commit object,\n"
308
"without using the '--force' option.\n");
310
static const char message_advice_ref_needs_update[] =
311
N_("Updates were rejected because the tip of the remote-tracking branch has\n"
312
"been updated since the last checkout. If you want to integrate the\n"
313
"remote changes, use 'git pull' before pushing again.\n"
314
"See the 'Note about fast-forwards' in 'git push --help' for details.");
316
static void advise_pull_before_push(void)
318
if (!advice_enabled(ADVICE_PUSH_NON_FF_CURRENT) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
320
advise(_(message_advice_pull_before_push));
323
static void advise_checkout_pull_push(void)
325
if (!advice_enabled(ADVICE_PUSH_NON_FF_MATCHING) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
327
advise(_(message_advice_checkout_pull_push));
330
static void advise_ref_already_exists(void)
332
if (!advice_enabled(ADVICE_PUSH_ALREADY_EXISTS) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
334
advise(_(message_advice_ref_already_exists));
337
static void advise_ref_fetch_first(void)
339
if (!advice_enabled(ADVICE_PUSH_FETCH_FIRST) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
341
advise(_(message_advice_ref_fetch_first));
344
static void advise_ref_needs_force(void)
346
if (!advice_enabled(ADVICE_PUSH_NEEDS_FORCE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
348
advise(_(message_advice_ref_needs_force));
351
static void advise_ref_needs_update(void)
353
if (!advice_enabled(ADVICE_PUSH_REF_NEEDS_UPDATE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
355
advise(_(message_advice_ref_needs_update));
358
static int push_with_options(struct transport *transport, struct refspec *rs,
362
unsigned int reject_reasons;
363
char *anon_url = transport_anonymize_url(transport->url);
365
transport_set_verbosity(transport, verbosity, progress);
366
transport->family = family;
369
transport_set_option(transport,
370
TRANS_OPT_RECEIVEPACK, receivepack);
371
transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
373
if (!is_empty_cas(&cas)) {
374
if (!transport->smart_options)
375
die("underlying transport does not support --%s option",
377
transport->smart_options->cas = &cas;
381
fprintf(stderr, _("Pushing to %s\n"), anon_url);
382
trace2_region_enter("push", "transport_push", the_repository);
383
err = transport_push(the_repository, transport,
384
rs, flags, &reject_reasons);
385
trace2_region_leave("push", "transport_push", the_repository);
387
fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
388
error(_("failed to push some refs to '%s'"), anon_url);
389
fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
392
err |= transport_disconnect(transport);
397
if (reject_reasons & REJECT_NON_FF_HEAD) {
398
advise_pull_before_push();
399
} else if (reject_reasons & REJECT_NON_FF_OTHER) {
400
advise_checkout_pull_push();
401
} else if (reject_reasons & REJECT_ALREADY_EXISTS) {
402
advise_ref_already_exists();
403
} else if (reject_reasons & REJECT_FETCH_FIRST) {
404
advise_ref_fetch_first();
405
} else if (reject_reasons & REJECT_NEEDS_FORCE) {
406
advise_ref_needs_force();
407
} else if (reject_reasons & REJECT_REF_NEEDS_UPDATE) {
408
advise_ref_needs_update();
414
static int do_push(int flags,
415
const struct string_list *push_options,
416
struct remote *remote)
420
struct refspec *push_refspec = &rs;
422
if (push_options->nr)
423
flags |= TRANSPORT_PUSH_OPTIONS;
425
if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) {
426
if (remote->push.nr) {
427
push_refspec = &remote->push;
428
} else if (!(flags & TRANSPORT_PUSH_MIRROR))
429
setup_default_push_refspecs(&flags, remote);
432
url = push_url_of_remote(remote);
433
for (i = 0; i < url->nr; i++) {
434
struct transport *transport =
435
transport_get(remote, url->v[i]);
436
if (flags & TRANSPORT_PUSH_OPTIONS)
437
transport->push_options = push_options;
438
if (push_with_options(transport, push_refspec, flags))
444
static int option_parse_recurse_submodules(const struct option *opt,
445
const char *arg, int unset)
447
int *recurse_submodules = opt->value;
450
*recurse_submodules = RECURSE_SUBMODULES_OFF;
452
if (!strcmp(arg, "only-is-on-demand")) {
453
if (*recurse_submodules == RECURSE_SUBMODULES_ONLY) {
454
warning(_("recursing into submodule with push.recurseSubmodules=only; using on-demand instead"));
455
*recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
458
*recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
465
static void set_push_cert_flags(int *flags, int v)
468
case SEND_PACK_PUSH_CERT_NEVER:
469
*flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
471
case SEND_PACK_PUSH_CERT_ALWAYS:
472
*flags |= TRANSPORT_PUSH_CERT_ALWAYS;
473
*flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
475
case SEND_PACK_PUSH_CERT_IF_ASKED:
476
*flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
477
*flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
483
static int git_push_config(const char *k, const char *v,
484
const struct config_context *ctx, void *cb)
486
const char *slot_name;
489
if (!strcmp(k, "push.followtags")) {
490
if (git_config_bool(k, v))
491
*flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
493
*flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
495
} else if (!strcmp(k, "push.autosetupremote")) {
496
if (git_config_bool(k, v))
497
*flags |= TRANSPORT_PUSH_AUTO_UPSTREAM;
499
} else if (!strcmp(k, "push.gpgsign")) {
500
switch (git_parse_maybe_bool(v)) {
502
set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
505
set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
508
if (!strcasecmp(v, "if-asked"))
509
set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
511
return error(_("invalid value for '%s'"), k);
513
} else if (!strcmp(k, "push.recursesubmodules")) {
514
recurse_submodules = parse_push_recurse_submodules_arg(k, v);
515
} else if (!strcmp(k, "submodule.recurse")) {
516
int val = git_config_bool(k, v) ?
517
RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
518
recurse_submodules = val;
519
} else if (!strcmp(k, "push.pushoption")) {
521
return config_error_nonbool(k);
524
string_list_clear(&push_options_config, 0);
526
string_list_append(&push_options_config, v);
528
} else if (!strcmp(k, "color.push")) {
529
push_use_color = git_config_colorbool(k, v);
531
} else if (skip_prefix(k, "color.push.", &slot_name)) {
532
int slot = parse_push_color_slot(slot_name);
536
return config_error_nonbool(k);
537
return color_parse(v, push_colors[slot]);
538
} else if (!strcmp(k, "push.useforceifincludes")) {
539
if (git_config_bool(k, v))
540
*flags |= TRANSPORT_PUSH_FORCE_IF_INCLUDES;
542
*flags &= ~TRANSPORT_PUSH_FORCE_IF_INCLUDES;
546
return git_default_config(k, v, ctx, NULL);
549
int cmd_push(int argc, const char **argv, const char *prefix)
555
const char *repo = NULL;
556
struct string_list push_options_cmdline = STRING_LIST_INIT_DUP;
557
struct string_list *push_options;
558
const struct string_list_item *item;
559
struct remote *remote;
561
struct option options[] = {
562
OPT__VERBOSITY(&verbosity),
563
OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
564
OPT_BIT( 0 , "all", &flags, N_("push all branches"), TRANSPORT_PUSH_ALL),
565
OPT_ALIAS( 0 , "branches", "all"),
566
OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
567
(TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
568
OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
569
OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --branches or --mirror)")),
570
OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
571
OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
572
OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
573
OPT_CALLBACK_F(0, "force-with-lease", &cas, N_("<refname>:<expect>"),
574
N_("require old value of ref to be at this value"),
575
PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option),
576
OPT_BIT(0, TRANS_OPT_FORCE_IF_INCLUDES, &flags,
577
N_("require remote updates to be integrated locally"),
578
TRANSPORT_PUSH_FORCE_IF_INCLUDES),
579
OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
580
N_("control recursive pushing of submodules"), option_parse_recurse_submodules),
581
OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
582
OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
583
OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
584
OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
585
TRANSPORT_PUSH_SET_UPSTREAM),
586
OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
587
OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
588
TRANSPORT_PUSH_PRUNE),
589
OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
590
OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
591
TRANSPORT_PUSH_FOLLOW_TAGS),
592
OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
593
PARSE_OPT_OPTARG, option_parse_push_signed),
594
OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
595
OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")),
596
OPT_IPVERSION(&family),
600
packet_trace_identity("push");
601
git_config(git_push_config, &flags);
602
argc = parse_options(argc, argv, prefix, options, push_usage, 0);
603
push_options = (push_options_cmdline.nr
604
? &push_options_cmdline
605
: &push_options_config);
606
set_push_cert_flags(&flags, push_cert);
608
die_for_incompatible_opt4(deleterefs, "--delete",
610
flags & TRANSPORT_PUSH_ALL, "--all/--branches",
611
flags & TRANSPORT_PUSH_MIRROR, "--mirror");
612
if (deleterefs && argc < 2)
613
die(_("--delete doesn't make sense without any refs"));
615
if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
616
flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
617
else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
618
flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
619
else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
620
flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
623
refspec_append(&rs, "refs/tags/*");
628
remote = pushremote_get(repo);
631
die(_("bad repository '%s'"), repo);
632
die(_("No configured push destination.\n"
633
"Either specify the URL from the command-line or configure a remote repository using\n"
635
" git remote add <name> <url>\n"
637
"and then push using the remote name\n"
639
" git push <name>\n"));
643
set_refspecs(argv + 1, argc - 1, remote);
646
flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
648
if (flags & TRANSPORT_PUSH_ALL) {
650
die(_("--all can't be combined with refspecs"));
652
if (flags & TRANSPORT_PUSH_MIRROR) {
654
die(_("--mirror can't be combined with refspecs"));
657
if (!is_empty_cas(&cas) && (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES))
658
cas.use_force_if_includes = 1;
660
for_each_string_list_item(item, push_options)
661
if (strchr(item->string, '\n'))
662
die(_("push options must not have new line characters"));
664
rc = do_push(flags, push_options, remote);
665
string_list_clear(&push_options_cmdline, 0);
666
string_list_clear(&push_options_config, 0);
668
usage_with_options(push_usage, options);