7 #include "run-command.h"
10 #include "transport.h"
11 #include "parse-options.h"
12 #include "submodule.h"
13 #include "submodule-config.h"
14 #include "send-pack.h"
16 static const char * const push_usage
[] = {
17 N_("git push [<options>] [<repository> [<refspec>...]]"),
22 static int deleterefs
;
23 static const char *receivepack
;
25 static int progress
= -1;
26 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
27 static enum transport_family family
;
29 static struct push_cas_option cas
;
31 static const char **refspec
;
32 static int refspec_nr
;
33 static int refspec_alloc
;
35 static struct string_list push_options_config
= STRING_LIST_INIT_DUP
;
37 static void add_refspec(const char *ref
)
40 ALLOC_GROW(refspec
, refspec_nr
, refspec_alloc
);
41 refspec
[refspec_nr
-1] = ref
;
44 static const char *map_refspec(const char *ref
,
45 struct remote
*remote
, struct ref
*local_refs
)
47 struct ref
*matched
= NULL
;
49 /* Does "ref" uniquely name our ref? */
50 if (count_refspec_match(ref
, local_refs
, &matched
) != 1)
55 memset(&query
, 0, sizeof(struct refspec
));
56 query
.src
= matched
->name
;
57 if (!query_refspecs(remote
->push
, remote
->push_refspec_nr
, &query
) &&
59 struct strbuf buf
= STRBUF_INIT
;
60 strbuf_addf(&buf
, "%s%s:%s",
61 query
.force
? "+" : "",
62 query
.src
, query
.dst
);
63 return strbuf_detach(&buf
, NULL
);
67 if (push_default
== PUSH_DEFAULT_UPSTREAM
&&
68 starts_with(matched
->name
, "refs/heads/")) {
69 struct branch
*branch
= branch_get(matched
->name
+ 11);
70 if (branch
->merge_nr
== 1 && branch
->merge
[0]->src
) {
71 struct strbuf buf
= STRBUF_INIT
;
72 strbuf_addf(&buf
, "%s:%s",
73 ref
, branch
->merge
[0]->src
);
74 return strbuf_detach(&buf
, NULL
);
81 static void set_refspecs(const char **refs
, int nr
, const char *repo
)
83 struct remote
*remote
= NULL
;
84 struct ref
*local_refs
= NULL
;
87 for (i
= 0; i
< nr
; i
++) {
88 const char *ref
= refs
[i
];
89 if (!strcmp("tag", ref
)) {
90 struct strbuf tagref
= STRBUF_INIT
;
92 die(_("tag shorthand without <tag>"));
95 strbuf_addf(&tagref
, ":refs/tags/%s", ref
);
97 strbuf_addf(&tagref
, "refs/tags/%s", ref
);
98 ref
= strbuf_detach(&tagref
, NULL
);
99 } else if (deleterefs
) {
100 struct strbuf delref
= STRBUF_INIT
;
101 if (strchr(ref
, ':'))
102 die(_("--delete only accepts plain target ref names"));
103 strbuf_addf(&delref
, ":%s", ref
);
104 ref
= strbuf_detach(&delref
, NULL
);
105 } else if (!strchr(ref
, ':')) {
107 /* lazily grab remote and local_refs */
108 remote
= remote_get(repo
);
109 local_refs
= get_local_heads();
111 ref
= map_refspec(ref
, remote
, local_refs
);
117 static int push_url_of_remote(struct remote
*remote
, const char ***url_p
)
119 if (remote
->pushurl_nr
) {
120 *url_p
= remote
->pushurl
;
121 return remote
->pushurl_nr
;
123 *url_p
= remote
->url
;
124 return remote
->url_nr
;
127 static NORETURN
int die_push_simple(struct branch
*branch
, struct remote
*remote
) {
129 * There's no point in using shorten_unambiguous_ref here,
130 * as the ambiguity would be on the remote side, not what
131 * we have locally. Plus, this is supposed to be the simple
132 * mode. If the user is doing something crazy like setting
133 * upstream to a non-branch, we should probably be showing
134 * them the big ugly fully qualified ref.
136 const char *advice_maybe
= "";
137 const char *short_upstream
= branch
->merge
[0]->src
;
139 skip_prefix(short_upstream
, "refs/heads/", &short_upstream
);
142 * Don't show advice for people who explicitly set
145 if (push_default
== PUSH_DEFAULT_UNSPECIFIED
)
146 advice_maybe
= _("\n"
147 "To choose either option permanently, "
148 "see push.default in 'git help config'.");
149 die(_("The upstream branch of your current branch does not match\n"
150 "the name of your current branch. To push to the upstream branch\n"
151 "on the remote, use\n"
153 " git push %s HEAD:%s\n"
155 "To push to the branch of the same name on the remote, use\n"
159 remote
->name
, short_upstream
,
160 remote
->name
, branch
->name
, advice_maybe
);
163 static const char message_detached_head_die
[] =
164 N_("You are not currently on a branch.\n"
165 "To push the history leading to the current (detached HEAD)\n"
168 " git push %s HEAD:<name-of-remote-branch>\n");
170 static void setup_push_upstream(struct remote
*remote
, struct branch
*branch
,
171 int triangular
, int simple
)
173 struct strbuf refspec
= STRBUF_INIT
;
176 die(_(message_detached_head_die
), remote
->name
);
177 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
)
178 die(_("The current branch %s has no upstream branch.\n"
179 "To push the current branch and set the remote as upstream, use\n"
181 " git push --set-upstream %s %s\n"),
185 if (branch
->merge_nr
!= 1)
186 die(_("The current branch %s has multiple upstream branches, "
187 "refusing to push."), branch
->name
);
189 die(_("You are pushing to remote '%s', which is not the upstream of\n"
190 "your current branch '%s', without telling me what to push\n"
191 "to update which remote branch."),
192 remote
->name
, branch
->name
);
195 /* Additional safety */
196 if (strcmp(branch
->refname
, branch
->merge
[0]->src
))
197 die_push_simple(branch
, remote
);
200 strbuf_addf(&refspec
, "%s:%s", branch
->refname
, branch
->merge
[0]->src
);
201 add_refspec(refspec
.buf
);
204 static void setup_push_current(struct remote
*remote
, struct branch
*branch
)
206 struct strbuf refspec
= STRBUF_INIT
;
209 die(_(message_detached_head_die
), remote
->name
);
210 strbuf_addf(&refspec
, "%s:%s", branch
->refname
, branch
->refname
);
211 add_refspec(refspec
.buf
);
214 static int is_workflow_triangular(struct remote
*remote
)
216 struct remote
*fetch_remote
= remote_get(NULL
);
217 return (fetch_remote
&& fetch_remote
!= remote
);
220 static void setup_default_push_refspecs(struct remote
*remote
)
222 struct branch
*branch
= branch_get(NULL
);
223 int triangular
= is_workflow_triangular(remote
);
225 switch (push_default
) {
227 case PUSH_DEFAULT_MATCHING
:
231 case PUSH_DEFAULT_UNSPECIFIED
:
232 case PUSH_DEFAULT_SIMPLE
:
234 setup_push_current(remote
, branch
);
236 setup_push_upstream(remote
, branch
, triangular
, 1);
239 case PUSH_DEFAULT_UPSTREAM
:
240 setup_push_upstream(remote
, branch
, triangular
, 0);
243 case PUSH_DEFAULT_CURRENT
:
244 setup_push_current(remote
, branch
);
247 case PUSH_DEFAULT_NOTHING
:
248 die(_("You didn't specify any refspecs to push, and "
249 "push.default is \"nothing\"."));
254 static const char message_advice_pull_before_push
[] =
255 N_("Updates were rejected because the tip of your current branch is behind\n"
256 "its remote counterpart. Integrate the remote changes (e.g.\n"
257 "'git pull ...') before pushing again.\n"
258 "See the 'Note about fast-forwards' in 'git push --help' for details.");
260 static const char message_advice_checkout_pull_push
[] =
261 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
262 "counterpart. Check out this branch and integrate the remote changes\n"
263 "(e.g. 'git pull ...') before pushing again.\n"
264 "See the 'Note about fast-forwards' in 'git push --help' for details.");
266 static const char message_advice_ref_fetch_first
[] =
267 N_("Updates were rejected because the remote contains work that you do\n"
268 "not have locally. This is usually caused by another repository pushing\n"
269 "to the same ref. You may want to first integrate the remote changes\n"
270 "(e.g., 'git pull ...') before pushing again.\n"
271 "See the 'Note about fast-forwards' in 'git push --help' for details.");
273 static const char message_advice_ref_already_exists
[] =
274 N_("Updates were rejected because the tag already exists in the remote.");
276 static const char message_advice_ref_needs_force
[] =
277 N_("You cannot update a remote ref that points at a non-commit object,\n"
278 "or update a remote ref to make it point at a non-commit object,\n"
279 "without using the '--force' option.\n");
281 static void advise_pull_before_push(void)
283 if (!advice_push_non_ff_current
|| !advice_push_update_rejected
)
285 advise(_(message_advice_pull_before_push
));
288 static void advise_checkout_pull_push(void)
290 if (!advice_push_non_ff_matching
|| !advice_push_update_rejected
)
292 advise(_(message_advice_checkout_pull_push
));
295 static void advise_ref_already_exists(void)
297 if (!advice_push_already_exists
|| !advice_push_update_rejected
)
299 advise(_(message_advice_ref_already_exists
));
302 static void advise_ref_fetch_first(void)
304 if (!advice_push_fetch_first
|| !advice_push_update_rejected
)
306 advise(_(message_advice_ref_fetch_first
));
309 static void advise_ref_needs_force(void)
311 if (!advice_push_needs_force
|| !advice_push_update_rejected
)
313 advise(_(message_advice_ref_needs_force
));
316 static int push_with_options(struct transport
*transport
, int flags
)
319 unsigned int reject_reasons
;
321 transport_set_verbosity(transport
, verbosity
, progress
);
322 transport
->family
= family
;
325 transport_set_option(transport
,
326 TRANS_OPT_RECEIVEPACK
, receivepack
);
327 transport_set_option(transport
, TRANS_OPT_THIN
, thin
? "yes" : NULL
);
329 if (!is_empty_cas(&cas
)) {
330 if (!transport
->smart_options
)
331 die("underlying transport does not support --%s option",
333 transport
->smart_options
->cas
= &cas
;
337 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
338 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
341 error(_("failed to push some refs to '%s'"), transport
->url
);
343 err
|= transport_disconnect(transport
);
347 if (reject_reasons
& REJECT_NON_FF_HEAD
) {
348 advise_pull_before_push();
349 } else if (reject_reasons
& REJECT_NON_FF_OTHER
) {
350 advise_checkout_pull_push();
351 } else if (reject_reasons
& REJECT_ALREADY_EXISTS
) {
352 advise_ref_already_exists();
353 } else if (reject_reasons
& REJECT_FETCH_FIRST
) {
354 advise_ref_fetch_first();
355 } else if (reject_reasons
& REJECT_NEEDS_FORCE
) {
356 advise_ref_needs_force();
362 static int do_push(const char *repo
, int flags
,
363 const struct string_list
*push_options
)
366 struct remote
*remote
= pushremote_get(repo
);
372 die(_("bad repository '%s'"), repo
);
373 die(_("No configured push destination.\n"
374 "Either specify the URL from the command-line or configure a remote repository using\n"
376 " git remote add <name> <url>\n"
378 "and then push using the remote name\n"
380 " git push <name>\n"));
384 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
386 if (push_options
->nr
)
387 flags
|= TRANSPORT_PUSH_OPTIONS
;
389 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
390 if (!strcmp(*refspec
, "refs/tags/*"))
391 return error(_("--all and --tags are incompatible"));
392 return error(_("--all can't be combined with refspecs"));
395 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
396 if (!strcmp(*refspec
, "refs/tags/*"))
397 return error(_("--mirror and --tags are incompatible"));
398 return error(_("--mirror can't be combined with refspecs"));
401 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
402 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
403 return error(_("--all and --mirror are incompatible"));
406 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
407 if (remote
->push_refspec_nr
) {
408 refspec
= remote
->push_refspec
;
409 refspec_nr
= remote
->push_refspec_nr
;
410 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
411 setup_default_push_refspecs(remote
);
414 url_nr
= push_url_of_remote(remote
, &url
);
416 for (i
= 0; i
< url_nr
; i
++) {
417 struct transport
*transport
=
418 transport_get(remote
, url
[i
]);
419 if (flags
& TRANSPORT_PUSH_OPTIONS
)
420 transport
->push_options
= push_options
;
421 if (push_with_options(transport
, flags
))
425 struct transport
*transport
=
426 transport_get(remote
, NULL
);
427 if (flags
& TRANSPORT_PUSH_OPTIONS
)
428 transport
->push_options
= push_options
;
429 if (push_with_options(transport
, flags
))
435 static int option_parse_recurse_submodules(const struct option
*opt
,
436 const char *arg
, int unset
)
438 int *recurse_submodules
= opt
->value
;
441 *recurse_submodules
= RECURSE_SUBMODULES_OFF
;
443 *recurse_submodules
= parse_push_recurse_submodules_arg(opt
->long_name
, arg
);
445 die("%s missing parameter", opt
->long_name
);
450 static void set_push_cert_flags(int *flags
, int v
)
453 case SEND_PACK_PUSH_CERT_NEVER
:
454 *flags
&= ~(TRANSPORT_PUSH_CERT_ALWAYS
| TRANSPORT_PUSH_CERT_IF_ASKED
);
456 case SEND_PACK_PUSH_CERT_ALWAYS
:
457 *flags
|= TRANSPORT_PUSH_CERT_ALWAYS
;
458 *flags
&= ~TRANSPORT_PUSH_CERT_IF_ASKED
;
460 case SEND_PACK_PUSH_CERT_IF_ASKED
:
461 *flags
|= TRANSPORT_PUSH_CERT_IF_ASKED
;
462 *flags
&= ~TRANSPORT_PUSH_CERT_ALWAYS
;
468 static int git_push_config(const char *k
, const char *v
, void *cb
)
473 status
= git_gpg_config(k
, v
, NULL
);
477 if (!strcmp(k
, "push.followtags")) {
478 if (git_config_bool(k
, v
))
479 *flags
|= TRANSPORT_PUSH_FOLLOW_TAGS
;
481 *flags
&= ~TRANSPORT_PUSH_FOLLOW_TAGS
;
483 } else if (!strcmp(k
, "push.gpgsign")) {
485 if (!git_config_get_value("push.gpgsign", &value
)) {
486 switch (git_parse_maybe_bool(value
)) {
488 set_push_cert_flags(flags
, SEND_PACK_PUSH_CERT_NEVER
);
491 set_push_cert_flags(flags
, SEND_PACK_PUSH_CERT_ALWAYS
);
494 if (value
&& !strcasecmp(value
, "if-asked"))
495 set_push_cert_flags(flags
, SEND_PACK_PUSH_CERT_IF_ASKED
);
497 return error("Invalid value for '%s'", k
);
500 } else if (!strcmp(k
, "push.recursesubmodules")) {
502 if (!git_config_get_value("push.recursesubmodules", &value
))
503 recurse_submodules
= parse_push_recurse_submodules_arg(k
, value
);
504 } else if (!strcmp(k
, "submodule.recurse")) {
505 int val
= git_config_bool(k
, v
) ?
506 RECURSE_SUBMODULES_ON_DEMAND
: RECURSE_SUBMODULES_OFF
;
507 recurse_submodules
= val
;
508 } else if (!strcmp(k
, "push.pushoption")) {
510 return config_error_nonbool(k
);
513 string_list_clear(&push_options_config
, 0);
515 string_list_append(&push_options_config
, v
);
519 return git_default_config(k
, v
, NULL
);
522 int cmd_push(int argc
, const char **argv
, const char *prefix
)
528 const char *repo
= NULL
; /* default repository */
529 struct string_list push_options_cmdline
= STRING_LIST_INIT_DUP
;
530 struct string_list
*push_options
;
531 const struct string_list_item
*item
;
533 struct option options
[] = {
534 OPT__VERBOSITY(&verbosity
),
535 OPT_STRING( 0 , "repo", &repo
, N_("repository"), N_("repository")),
536 OPT_BIT( 0 , "all", &flags
, N_("push all refs"), TRANSPORT_PUSH_ALL
),
537 OPT_BIT( 0 , "mirror", &flags
, N_("mirror all refs"),
538 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
539 OPT_BOOL('d', "delete", &deleterefs
, N_("delete refs")),
540 OPT_BOOL( 0 , "tags", &tags
, N_("push tags (can't be used with --all or --mirror)")),
541 OPT_BIT('n' , "dry-run", &flags
, N_("dry run"), TRANSPORT_PUSH_DRY_RUN
),
542 OPT_BIT( 0, "porcelain", &flags
, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN
),
543 OPT_BIT('f', "force", &flags
, N_("force updates"), TRANSPORT_PUSH_FORCE
),
545 0, CAS_OPT_NAME
, &cas
, N_("refname>:<expect"),
546 N_("require old value of ref to be at this value"),
547 PARSE_OPT_OPTARG
, parseopt_push_cas_option
},
548 { OPTION_CALLBACK
, 0, "recurse-submodules", &recurse_submodules
, "check|on-demand|no",
549 N_("control recursive pushing of submodules"),
550 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
551 OPT_BOOL_F( 0 , "thin", &thin
, N_("use thin pack"), PARSE_OPT_NOCOMPLETE
),
552 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
553 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", N_("receive pack program")),
554 OPT_BIT('u', "set-upstream", &flags
, N_("set upstream for git pull/status"),
555 TRANSPORT_PUSH_SET_UPSTREAM
),
556 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
557 OPT_BIT(0, "prune", &flags
, N_("prune locally removed refs"),
558 TRANSPORT_PUSH_PRUNE
),
559 OPT_BIT(0, "no-verify", &flags
, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK
),
560 OPT_BIT(0, "follow-tags", &flags
, N_("push missing but relevant tags"),
561 TRANSPORT_PUSH_FOLLOW_TAGS
),
563 0, "signed", &push_cert
, "yes|no|if-asked", N_("GPG sign the push"),
564 PARSE_OPT_OPTARG
, option_parse_push_signed
},
565 OPT_BIT(0, "atomic", &flags
, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC
),
566 OPT_STRING_LIST('o', "push-option", &push_options_cmdline
, N_("server-specific"), N_("option to transmit")),
567 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
568 TRANSPORT_FAMILY_IPV4
),
569 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
570 TRANSPORT_FAMILY_IPV6
),
574 packet_trace_identity("push");
575 git_config(git_push_config
, &flags
);
576 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
577 push_options
= (push_options_cmdline
.nr
578 ? &push_options_cmdline
579 : &push_options_config
);
580 set_push_cert_flags(&flags
, push_cert
);
582 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
583 die(_("--delete is incompatible with --all, --mirror and --tags"));
584 if (deleterefs
&& argc
< 2)
585 die(_("--delete doesn't make sense without any refs"));
587 if (recurse_submodules
== RECURSE_SUBMODULES_CHECK
)
588 flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
589 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
590 flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
591 else if (recurse_submodules
== RECURSE_SUBMODULES_ONLY
)
592 flags
|= TRANSPORT_RECURSE_SUBMODULES_ONLY
;
595 add_refspec("refs/tags/*");
599 set_refspecs(argv
+ 1, argc
- 1, repo
);
602 for_each_string_list_item(item
, push_options
)
603 if (strchr(item
->string
, '\n'))
604 die(_("push options must not have new line characters"));
606 rc
= do_push(repo
, flags
, push_options
);
607 string_list_clear(&push_options_cmdline
, 0);
608 string_list_clear(&push_options_config
, 0);
610 usage_with_options(push_usage
, options
);