4 #define USE_THE_REPOSITORY_VARIABLE
9 #include "environment.h"
12 #include "run-command.h"
14 #include "transport.h"
15 #include "parse-options.h"
17 #include "submodule.h"
18 #include "submodule-config.h"
19 #include "send-pack.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
] = {
31 GIT_COLOR_RED
, /* ERROR */
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
];
56 static int deleterefs
;
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
= {
79 if (!query_refspecs(&remote
->push
, &query
) && query
.dst
) {
80 refspec_appendf(refspec
, "%s%s:%s",
81 query
.force
? "+" : "",
82 query
.src
, query
.dst
);
88 if (push_default
== PUSH_DEFAULT_UPSTREAM
&&
89 skip_prefix(matched
->name
, "refs/heads/", &branch_name
)) {
90 struct branch
*branch
= branch_get(branch_name
);
91 if (branch
->merge_nr
== 1 && branch
->merge
[0]->src
) {
92 refspec_appendf(refspec
, "%s:%s",
93 ref
, branch
->merge
[0]->src
);
98 refspec_append(refspec
, ref
);
101 static void set_refspecs(const char **refs
, int nr
, struct remote
*remote
)
103 struct ref
*local_refs
= NULL
;
106 for (i
= 0; i
< nr
; i
++) {
107 const char *ref
= refs
[i
];
108 if (!strcmp("tag", ref
)) {
110 die(_("tag shorthand without <tag>"));
113 refspec_appendf(&rs
, ":refs/tags/%s", ref
);
115 refspec_appendf(&rs
, "refs/tags/%s", ref
);
116 } else if (deleterefs
) {
117 if (strchr(ref
, ':') || !*ref
)
118 die(_("--delete only accepts plain target ref names"));
119 refspec_appendf(&rs
, ":%s", ref
);
120 } else if (!strchr(ref
, ':')) {
121 struct ref
*matched
= NULL
;
123 /* lazily grab local_refs */
125 local_refs
= get_local_heads();
127 /* Does "ref" uniquely name our ref? */
128 if (count_refspec_match(ref
, local_refs
, &matched
) != 1)
129 refspec_append(&rs
, ref
);
131 refspec_append_mapped(&rs
, ref
, remote
, matched
);
133 refspec_append(&rs
, ref
);
135 free_refs(local_refs
);
138 static NORETURN
void die_push_simple(struct branch
*branch
,
139 struct remote
*remote
)
142 * There's no point in using shorten_unambiguous_ref here,
143 * as the ambiguity would be on the remote side, not what
144 * we have locally. Plus, this is supposed to be the simple
145 * mode. If the user is doing something crazy like setting
146 * upstream to a non-branch, we should probably be showing
147 * them the big ugly fully qualified ref.
149 const char *advice_pushdefault_maybe
= "";
150 const char *advice_automergesimple_maybe
= "";
151 const char *short_upstream
= branch
->merge
[0]->src
;
153 skip_prefix(short_upstream
, "refs/heads/", &short_upstream
);
156 * Don't show advice for people who explicitly set
159 if (push_default
== PUSH_DEFAULT_UNSPECIFIED
)
160 advice_pushdefault_maybe
= _("\n"
161 "To choose either option permanently, "
162 "see push.default in 'git help config'.\n");
163 if (git_branch_track
!= BRANCH_TRACK_SIMPLE
)
164 advice_automergesimple_maybe
= _("\n"
165 "To avoid automatically configuring "
166 "an upstream branch when its name\n"
167 "won't match the local branch, see option "
168 "'simple' of branch.autoSetupMerge\n"
169 "in 'git help config'.\n");
170 die(_("The upstream branch of your current branch does not match\n"
171 "the name of your current branch. To push to the upstream branch\n"
172 "on the remote, use\n"
174 " git push %s HEAD:%s\n"
176 "To push to the branch of the same name on the remote, use\n"
178 " git push %s HEAD\n"
180 remote
->name
, short_upstream
,
181 remote
->name
, advice_pushdefault_maybe
,
182 advice_automergesimple_maybe
);
185 static const char message_detached_head_die
[] =
186 N_("You are not currently on a branch.\n"
187 "To push the history leading to the current (detached HEAD)\n"
190 " git push %s HEAD:<name-of-remote-branch>\n");
192 static const char *get_upstream_ref(int flags
, struct branch
*branch
, const char *remote_name
)
194 if (branch
->merge_nr
== 0 && (flags
& TRANSPORT_PUSH_AUTO_UPSTREAM
)) {
195 /* if missing, assume same; set_upstream will be defined later */
196 return branch
->refname
;
199 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
) {
200 const char *advice_autosetup_maybe
= "";
201 if (!(flags
& TRANSPORT_PUSH_AUTO_UPSTREAM
)) {
202 advice_autosetup_maybe
= _("\n"
203 "To have this happen automatically for "
204 "branches without a tracking\n"
205 "upstream, see 'push.autoSetupRemote' "
206 "in 'git help config'.\n");
208 die(_("The current branch %s has no upstream branch.\n"
209 "To push the current branch and set the remote as upstream, use\n"
211 " git push --set-upstream %s %s\n"
216 advice_autosetup_maybe
);
218 if (branch
->merge_nr
!= 1)
219 die(_("The current branch %s has multiple upstream branches, "
220 "refusing to push."), branch
->name
);
222 return branch
->merge
[0]->src
;
225 static void setup_default_push_refspecs(int *flags
, struct remote
*remote
)
227 struct branch
*branch
;
231 switch (push_default
) {
232 case PUSH_DEFAULT_MATCHING
:
233 refspec_append(&rs
, ":");
236 case PUSH_DEFAULT_NOTHING
:
237 die(_("You didn't specify any refspecs to push, and "
238 "push.default is \"nothing\"."));
244 branch
= branch_get(NULL
);
246 die(_(message_detached_head_die
), remote
->name
);
248 dst
= branch
->refname
;
249 same_remote
= !strcmp(remote
->name
, remote_for_branch(branch
, NULL
));
251 switch (push_default
) {
253 case PUSH_DEFAULT_UNSPECIFIED
:
254 case PUSH_DEFAULT_SIMPLE
:
257 if (strcmp(branch
->refname
, get_upstream_ref(*flags
, branch
, remote
->name
)))
258 die_push_simple(branch
, remote
);
261 case PUSH_DEFAULT_UPSTREAM
:
263 die(_("You are pushing to remote '%s', which is not the upstream of\n"
264 "your current branch '%s', without telling me what to push\n"
265 "to update which remote branch."),
266 remote
->name
, branch
->name
);
267 dst
= get_upstream_ref(*flags
, branch
, remote
->name
);
270 case PUSH_DEFAULT_CURRENT
:
275 * this is a default push - if auto-upstream is enabled and there is
276 * no upstream defined, then set it (with options 'simple', 'upstream',
279 if ((*flags
& TRANSPORT_PUSH_AUTO_UPSTREAM
) && branch
->merge_nr
== 0)
280 *flags
|= TRANSPORT_PUSH_SET_UPSTREAM
;
282 refspec_appendf(&rs
, "%s:%s", branch
->refname
, dst
);
285 static const char message_advice_pull_before_push
[] =
286 N_("Updates were rejected because the tip of your current branch is behind\n"
287 "its remote counterpart. If you want to integrate the remote changes,\n"
288 "use 'git pull' before pushing again.\n"
289 "See the 'Note about fast-forwards' in 'git push --help' for details.");
291 static const char message_advice_checkout_pull_push
[] =
292 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
293 "counterpart. If you want to integrate the remote changes, use 'git pull'\n"
294 "before pushing again.\n"
295 "See the 'Note about fast-forwards' in 'git push --help' for details.");
297 static const char message_advice_ref_fetch_first
[] =
298 N_("Updates were rejected because the remote contains work that you do not\n"
299 "have locally. This is usually caused by another repository pushing to\n"
300 "the same ref. If you want to integrate the remote changes, use\n"
301 "'git pull' before pushing again.\n"
302 "See the 'Note about fast-forwards' in 'git push --help' for details.");
304 static const char message_advice_ref_already_exists
[] =
305 N_("Updates were rejected because the tag already exists in the remote.");
307 static const char message_advice_ref_needs_force
[] =
308 N_("You cannot update a remote ref that points at a non-commit object,\n"
309 "or update a remote ref to make it point at a non-commit object,\n"
310 "without using the '--force' option.\n");
312 static const char message_advice_ref_needs_update
[] =
313 N_("Updates were rejected because the tip of the remote-tracking branch has\n"
314 "been updated since the last checkout. If you want to integrate the\n"
315 "remote changes, use 'git pull' before pushing again.\n"
316 "See the 'Note about fast-forwards' in 'git push --help' for details.");
318 static void advise_pull_before_push(void)
320 if (!advice_enabled(ADVICE_PUSH_NON_FF_CURRENT
) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED
))
322 advise(_(message_advice_pull_before_push
));
325 static void advise_checkout_pull_push(void)
327 if (!advice_enabled(ADVICE_PUSH_NON_FF_MATCHING
) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED
))
329 advise(_(message_advice_checkout_pull_push
));
332 static void advise_ref_already_exists(void)
334 if (!advice_enabled(ADVICE_PUSH_ALREADY_EXISTS
) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED
))
336 advise(_(message_advice_ref_already_exists
));
339 static void advise_ref_fetch_first(void)
341 if (!advice_enabled(ADVICE_PUSH_FETCH_FIRST
) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED
))
343 advise(_(message_advice_ref_fetch_first
));
346 static void advise_ref_needs_force(void)
348 if (!advice_enabled(ADVICE_PUSH_NEEDS_FORCE
) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED
))
350 advise(_(message_advice_ref_needs_force
));
353 static void advise_ref_needs_update(void)
355 if (!advice_enabled(ADVICE_PUSH_REF_NEEDS_UPDATE
) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED
))
357 advise(_(message_advice_ref_needs_update
));
360 static int push_with_options(struct transport
*transport
, struct refspec
*rs
,
364 unsigned int reject_reasons
;
365 char *anon_url
= transport_anonymize_url(transport
->url
);
367 transport_set_verbosity(transport
, verbosity
, progress
);
368 transport
->family
= family
;
371 transport_set_option(transport
,
372 TRANS_OPT_RECEIVEPACK
, receivepack
);
373 transport_set_option(transport
, TRANS_OPT_THIN
, thin
? "yes" : NULL
);
375 if (!is_empty_cas(&cas
)) {
376 if (!transport
->smart_options
)
377 die("underlying transport does not support --%s option",
379 transport
->smart_options
->cas
= &cas
;
383 fprintf(stderr
, _("Pushing to %s\n"), anon_url
);
384 trace2_region_enter("push", "transport_push", the_repository
);
385 err
= transport_push(the_repository
, transport
,
386 rs
, flags
, &reject_reasons
);
387 trace2_region_leave("push", "transport_push", the_repository
);
389 fprintf(stderr
, "%s", push_get_color(PUSH_COLOR_ERROR
));
390 error(_("failed to push some refs to '%s'"), anon_url
);
391 fprintf(stderr
, "%s", push_get_color(PUSH_COLOR_RESET
));
394 err
|= transport_disconnect(transport
);
399 if (reject_reasons
& REJECT_NON_FF_HEAD
) {
400 advise_pull_before_push();
401 } else if (reject_reasons
& REJECT_NON_FF_OTHER
) {
402 advise_checkout_pull_push();
403 } else if (reject_reasons
& REJECT_ALREADY_EXISTS
) {
404 advise_ref_already_exists();
405 } else if (reject_reasons
& REJECT_FETCH_FIRST
) {
406 advise_ref_fetch_first();
407 } else if (reject_reasons
& REJECT_NEEDS_FORCE
) {
408 advise_ref_needs_force();
409 } else if (reject_reasons
& REJECT_REF_NEEDS_UPDATE
) {
410 advise_ref_needs_update();
416 static int do_push(int flags
,
417 const struct string_list
*push_options
,
418 struct remote
*remote
)
422 struct refspec
*push_refspec
= &rs
;
424 if (push_options
->nr
)
425 flags
|= TRANSPORT_PUSH_OPTIONS
;
427 if (!push_refspec
->nr
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
428 if (remote
->push
.nr
) {
429 push_refspec
= &remote
->push
;
430 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
431 setup_default_push_refspecs(&flags
, remote
);
434 url
= push_url_of_remote(remote
);
435 for (i
= 0; i
< url
->nr
; i
++) {
436 struct transport
*transport
=
437 transport_get(remote
, url
->v
[i
]);
438 if (flags
& TRANSPORT_PUSH_OPTIONS
)
439 transport
->push_options
= push_options
;
440 if (push_with_options(transport
, push_refspec
, flags
))
446 static int option_parse_recurse_submodules(const struct option
*opt
,
447 const char *arg
, int unset
)
449 int *recurse_submodules
= opt
->value
;
452 *recurse_submodules
= RECURSE_SUBMODULES_OFF
;
454 if (!strcmp(arg
, "only-is-on-demand")) {
455 if (*recurse_submodules
== RECURSE_SUBMODULES_ONLY
) {
456 warning(_("recursing into submodule with push.recurseSubmodules=only; using on-demand instead"));
457 *recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
460 *recurse_submodules
= parse_push_recurse_submodules_arg(opt
->long_name
, arg
);
467 static void set_push_cert_flags(int *flags
, int v
)
470 case SEND_PACK_PUSH_CERT_NEVER
:
471 *flags
&= ~(TRANSPORT_PUSH_CERT_ALWAYS
| TRANSPORT_PUSH_CERT_IF_ASKED
);
473 case SEND_PACK_PUSH_CERT_ALWAYS
:
474 *flags
|= TRANSPORT_PUSH_CERT_ALWAYS
;
475 *flags
&= ~TRANSPORT_PUSH_CERT_IF_ASKED
;
477 case SEND_PACK_PUSH_CERT_IF_ASKED
:
478 *flags
|= TRANSPORT_PUSH_CERT_IF_ASKED
;
479 *flags
&= ~TRANSPORT_PUSH_CERT_ALWAYS
;
485 static int git_push_config(const char *k
, const char *v
,
486 const struct config_context
*ctx
, void *cb
)
488 const char *slot_name
;
491 if (!strcmp(k
, "push.followtags")) {
492 if (git_config_bool(k
, v
))
493 *flags
|= TRANSPORT_PUSH_FOLLOW_TAGS
;
495 *flags
&= ~TRANSPORT_PUSH_FOLLOW_TAGS
;
497 } else if (!strcmp(k
, "push.autosetupremote")) {
498 if (git_config_bool(k
, v
))
499 *flags
|= TRANSPORT_PUSH_AUTO_UPSTREAM
;
501 } else if (!strcmp(k
, "push.gpgsign")) {
502 switch (git_parse_maybe_bool(v
)) {
504 set_push_cert_flags(flags
, SEND_PACK_PUSH_CERT_NEVER
);
507 set_push_cert_flags(flags
, SEND_PACK_PUSH_CERT_ALWAYS
);
510 if (!strcasecmp(v
, "if-asked"))
511 set_push_cert_flags(flags
, SEND_PACK_PUSH_CERT_IF_ASKED
);
513 return error(_("invalid value for '%s'"), k
);
515 } else if (!strcmp(k
, "push.recursesubmodules")) {
516 recurse_submodules
= parse_push_recurse_submodules_arg(k
, v
);
517 } else if (!strcmp(k
, "submodule.recurse")) {
518 int val
= git_config_bool(k
, v
) ?
519 RECURSE_SUBMODULES_ON_DEMAND
: RECURSE_SUBMODULES_OFF
;
520 recurse_submodules
= val
;
521 } else if (!strcmp(k
, "push.pushoption")) {
522 return parse_transport_option(k
, v
, &push_options_config
);
523 } else if (!strcmp(k
, "color.push")) {
524 push_use_color
= git_config_colorbool(k
, v
);
526 } else if (skip_prefix(k
, "color.push.", &slot_name
)) {
527 int slot
= parse_push_color_slot(slot_name
);
531 return config_error_nonbool(k
);
532 return color_parse(v
, push_colors
[slot
]);
533 } else if (!strcmp(k
, "push.useforceifincludes")) {
534 if (git_config_bool(k
, v
))
535 *flags
|= TRANSPORT_PUSH_FORCE_IF_INCLUDES
;
537 *flags
&= ~TRANSPORT_PUSH_FORCE_IF_INCLUDES
;
541 return git_default_config(k
, v
, ctx
, NULL
);
544 int cmd_push(int argc
,
547 struct repository
*repository UNUSED
)
553 const char *repo
= NULL
; /* default repository */
554 struct string_list push_options_cmdline
= STRING_LIST_INIT_DUP
;
555 struct string_list
*push_options
;
556 const struct string_list_item
*item
;
557 struct remote
*remote
;
559 struct option options
[] = {
560 OPT__VERBOSITY(&verbosity
),
561 OPT_STRING( 0 , "repo", &repo
, N_("repository"), N_("repository")),
562 OPT_BIT( 0 , "all", &flags
, N_("push all branches"), TRANSPORT_PUSH_ALL
),
563 OPT_ALIAS( 0 , "branches", "all"),
564 OPT_BIT( 0 , "mirror", &flags
, N_("mirror all refs"),
565 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
566 OPT_BOOL('d', "delete", &deleterefs
, N_("delete refs")),
567 OPT_BOOL( 0 , "tags", &tags
, N_("push tags (can't be used with --all or --branches or --mirror)")),
568 OPT_BIT('n' , "dry-run", &flags
, N_("dry run"), TRANSPORT_PUSH_DRY_RUN
),
569 OPT_BIT( 0, "porcelain", &flags
, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN
),
570 OPT_BIT('f', "force", &flags
, N_("force updates"), TRANSPORT_PUSH_FORCE
),
571 OPT_CALLBACK_F(0, "force-with-lease", &cas
, N_("<refname>:<expect>"),
572 N_("require old value of ref to be at this value"),
573 PARSE_OPT_OPTARG
| PARSE_OPT_LITERAL_ARGHELP
, parseopt_push_cas_option
),
574 OPT_BIT(0, TRANS_OPT_FORCE_IF_INCLUDES
, &flags
,
575 N_("require remote updates to be integrated locally"),
576 TRANSPORT_PUSH_FORCE_IF_INCLUDES
),
577 OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules
, "(check|on-demand|no)",
578 N_("control recursive pushing of submodules"), option_parse_recurse_submodules
),
579 OPT_BOOL_F( 0 , "thin", &thin
, N_("use thin pack"), PARSE_OPT_NOCOMPLETE
),
580 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
581 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", N_("receive pack program")),
582 OPT_BIT('u', "set-upstream", &flags
, N_("set upstream for git pull/status"),
583 TRANSPORT_PUSH_SET_UPSTREAM
),
584 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
585 OPT_BIT(0, "prune", &flags
, N_("prune locally removed refs"),
586 TRANSPORT_PUSH_PRUNE
),
587 OPT_BIT(0, "no-verify", &flags
, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK
),
588 OPT_BIT(0, "follow-tags", &flags
, N_("push missing but relevant tags"),
589 TRANSPORT_PUSH_FOLLOW_TAGS
),
590 OPT_CALLBACK_F(0, "signed", &push_cert
, "(yes|no|if-asked)", N_("GPG sign the push"),
591 PARSE_OPT_OPTARG
, option_parse_push_signed
),
592 OPT_BIT(0, "atomic", &flags
, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC
),
593 OPT_STRING_LIST('o', "push-option", &push_options_cmdline
, N_("server-specific"), N_("option to transmit")),
594 OPT_IPVERSION(&family
),
598 packet_trace_identity("push");
599 git_config(git_push_config
, &flags
);
600 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
601 push_options
= (push_options_cmdline
.nr
602 ? &push_options_cmdline
603 : &push_options_config
);
604 set_push_cert_flags(&flags
, push_cert
);
606 die_for_incompatible_opt4(deleterefs
, "--delete",
608 flags
& TRANSPORT_PUSH_ALL
, "--all/--branches",
609 flags
& TRANSPORT_PUSH_MIRROR
, "--mirror");
610 if (deleterefs
&& argc
< 2)
611 die(_("--delete doesn't make sense without any refs"));
613 if (recurse_submodules
== RECURSE_SUBMODULES_CHECK
)
614 flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
615 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
616 flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
617 else if (recurse_submodules
== RECURSE_SUBMODULES_ONLY
)
618 flags
|= TRANSPORT_RECURSE_SUBMODULES_ONLY
;
621 refspec_append(&rs
, "refs/tags/*");
626 remote
= pushremote_get(repo
);
629 die(_("bad repository '%s'"), repo
);
630 die(_("No configured push destination.\n"
631 "Either specify the URL from the command-line or configure a remote repository using\n"
633 " git remote add <name> <url>\n"
635 "and then push using the remote name\n"
637 " git push <name>\n"));
641 set_refspecs(argv
+ 1, argc
- 1, remote
);
644 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
646 if (flags
& TRANSPORT_PUSH_ALL
) {
648 die(_("--all can't be combined with refspecs"));
650 if (flags
& TRANSPORT_PUSH_MIRROR
) {
652 die(_("--mirror can't be combined with refspecs"));
655 if (!is_empty_cas(&cas
) && (flags
& TRANSPORT_PUSH_FORCE_IF_INCLUDES
))
656 cas
.use_force_if_includes
= 1;
658 for_each_string_list_item(item
, push_options
)
659 if (strchr(item
->string
, '\n'))
660 die(_("push options must not have new line characters"));
662 rc
= do_push(flags
, push_options
, remote
);
663 string_list_clear(&push_options_cmdline
, 0);
664 string_list_clear(&push_options_config
, 0);
665 clear_cas_option(&cas
);
667 usage_with_options(push_usage
, options
);