Merge branch 'js/azure-pipelines-msvc'
[git.git] / builtin / push.c
blob8729b031ff5199c8b9c6add8b6cb036b0e5169d6
1 /*
2 * "git push"
3 */
4 #include "cache.h"
5 #include "config.h"
6 #include "refs.h"
7 #include "refspec.h"
8 #include "run-command.h"
9 #include "builtin.h"
10 #include "remote.h"
11 #include "transport.h"
12 #include "parse-options.h"
13 #include "submodule.h"
14 #include "submodule-config.h"
15 #include "send-pack.h"
16 #include "color.h"
18 static const char * const push_usage[] = {
19 N_("git push [<options>] [<repository> [<refspec>...]]"),
20 NULL,
23 static int push_use_color = -1;
24 static char push_colors[][COLOR_MAXLEN] = {
25 GIT_COLOR_RESET,
26 GIT_COLOR_RED, /* ERROR */
29 enum color_push {
30 PUSH_COLOR_RESET = 0,
31 PUSH_COLOR_ERROR = 1
34 static int parse_push_color_slot(const char *slot)
36 if (!strcasecmp(slot, "reset"))
37 return PUSH_COLOR_RESET;
38 if (!strcasecmp(slot, "error"))
39 return PUSH_COLOR_ERROR;
40 return -1;
43 static const char *push_get_color(enum color_push ix)
45 if (want_color_stderr(push_use_color))
46 return push_colors[ix];
47 return "";
50 static int thin = 1;
51 static int deleterefs;
52 static const char *receivepack;
53 static int verbosity;
54 static int progress = -1;
55 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
56 static enum transport_family family;
58 static struct push_cas_option cas;
60 static struct refspec rs = REFSPEC_INIT_PUSH;
62 static struct string_list push_options_config = STRING_LIST_INIT_DUP;
64 static const char *map_refspec(const char *ref,
65 struct remote *remote, struct ref *local_refs)
67 struct ref *matched = NULL;
69 /* Does "ref" uniquely name our ref? */
70 if (count_refspec_match(ref, local_refs, &matched) != 1)
71 return ref;
73 if (remote->push.nr) {
74 struct refspec_item query;
75 memset(&query, 0, sizeof(struct refspec_item));
76 query.src = matched->name;
77 if (!query_refspecs(&remote->push, &query) && query.dst) {
78 struct strbuf buf = STRBUF_INIT;
79 strbuf_addf(&buf, "%s%s:%s",
80 query.force ? "+" : "",
81 query.src, query.dst);
82 return strbuf_detach(&buf, NULL);
86 if (push_default == PUSH_DEFAULT_UPSTREAM &&
87 starts_with(matched->name, "refs/heads/")) {
88 struct branch *branch = branch_get(matched->name + 11);
89 if (branch->merge_nr == 1 && branch->merge[0]->src) {
90 struct strbuf buf = STRBUF_INIT;
91 strbuf_addf(&buf, "%s:%s",
92 ref, branch->merge[0]->src);
93 return strbuf_detach(&buf, NULL);
97 return ref;
100 static void set_refspecs(const char **refs, int nr, const char *repo)
102 struct remote *remote = NULL;
103 struct ref *local_refs = NULL;
104 int i;
106 for (i = 0; i < nr; i++) {
107 const char *ref = refs[i];
108 if (!strcmp("tag", ref)) {
109 struct strbuf tagref = STRBUF_INIT;
110 if (nr <= ++i)
111 die(_("tag shorthand without <tag>"));
112 ref = refs[i];
113 if (deleterefs)
114 strbuf_addf(&tagref, ":refs/tags/%s", ref);
115 else
116 strbuf_addf(&tagref, "refs/tags/%s", ref);
117 ref = strbuf_detach(&tagref, NULL);
118 } else if (deleterefs) {
119 struct strbuf delref = STRBUF_INIT;
120 if (strchr(ref, ':'))
121 die(_("--delete only accepts plain target ref names"));
122 strbuf_addf(&delref, ":%s", ref);
123 ref = strbuf_detach(&delref, NULL);
124 } else if (!strchr(ref, ':')) {
125 if (!remote) {
126 /* lazily grab remote and local_refs */
127 remote = remote_get(repo);
128 local_refs = get_local_heads();
130 ref = map_refspec(ref, remote, local_refs);
132 refspec_append(&rs, ref);
136 static int push_url_of_remote(struct remote *remote, const char ***url_p)
138 if (remote->pushurl_nr) {
139 *url_p = remote->pushurl;
140 return remote->pushurl_nr;
142 *url_p = remote->url;
143 return remote->url_nr;
146 static NORETURN void die_push_simple(struct branch *branch,
147 struct remote *remote)
150 * There's no point in using shorten_unambiguous_ref here,
151 * as the ambiguity would be on the remote side, not what
152 * we have locally. Plus, this is supposed to be the simple
153 * mode. If the user is doing something crazy like setting
154 * upstream to a non-branch, we should probably be showing
155 * them the big ugly fully qualified ref.
157 const char *advice_maybe = "";
158 const char *short_upstream = branch->merge[0]->src;
160 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
163 * Don't show advice for people who explicitly set
164 * push.default.
166 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
167 advice_maybe = _("\n"
168 "To choose either option permanently, "
169 "see push.default in 'git help config'.");
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"
173 "\n"
174 " git push %s HEAD:%s\n"
175 "\n"
176 "To push to the branch of the same name on the remote, use\n"
177 "\n"
178 " git push %s HEAD\n"
179 "%s"),
180 remote->name, short_upstream,
181 remote->name, advice_maybe);
184 static const char message_detached_head_die[] =
185 N_("You are not currently on a branch.\n"
186 "To push the history leading to the current (detached HEAD)\n"
187 "state now, use\n"
188 "\n"
189 " git push %s HEAD:<name-of-remote-branch>\n");
191 static void setup_push_upstream(struct remote *remote, struct branch *branch,
192 int triangular, int simple)
194 struct strbuf refspec = STRBUF_INIT;
196 if (!branch)
197 die(_(message_detached_head_die), remote->name);
198 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
199 die(_("The current branch %s has no upstream branch.\n"
200 "To push the current branch and set the remote as upstream, use\n"
201 "\n"
202 " git push --set-upstream %s %s\n"),
203 branch->name,
204 remote->name,
205 branch->name);
206 if (branch->merge_nr != 1)
207 die(_("The current branch %s has multiple upstream branches, "
208 "refusing to push."), branch->name);
209 if (triangular)
210 die(_("You are pushing to remote '%s', which is not the upstream of\n"
211 "your current branch '%s', without telling me what to push\n"
212 "to update which remote branch."),
213 remote->name, branch->name);
215 if (simple) {
216 /* Additional safety */
217 if (strcmp(branch->refname, branch->merge[0]->src))
218 die_push_simple(branch, remote);
221 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
222 refspec_append(&rs, refspec.buf);
225 static void setup_push_current(struct remote *remote, struct branch *branch)
227 struct strbuf refspec = STRBUF_INIT;
229 if (!branch)
230 die(_(message_detached_head_die), remote->name);
231 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
232 refspec_append(&rs, refspec.buf);
235 static int is_workflow_triangular(struct remote *remote)
237 struct remote *fetch_remote = remote_get(NULL);
238 return (fetch_remote && fetch_remote != remote);
241 static void setup_default_push_refspecs(struct remote *remote)
243 struct branch *branch = branch_get(NULL);
244 int triangular = is_workflow_triangular(remote);
246 switch (push_default) {
247 default:
248 case PUSH_DEFAULT_MATCHING:
249 refspec_append(&rs, ":");
250 break;
252 case PUSH_DEFAULT_UNSPECIFIED:
253 case PUSH_DEFAULT_SIMPLE:
254 if (triangular)
255 setup_push_current(remote, branch);
256 else
257 setup_push_upstream(remote, branch, triangular, 1);
258 break;
260 case PUSH_DEFAULT_UPSTREAM:
261 setup_push_upstream(remote, branch, triangular, 0);
262 break;
264 case PUSH_DEFAULT_CURRENT:
265 setup_push_current(remote, branch);
266 break;
268 case PUSH_DEFAULT_NOTHING:
269 die(_("You didn't specify any refspecs to push, and "
270 "push.default is \"nothing\"."));
271 break;
275 static const char message_advice_pull_before_push[] =
276 N_("Updates were rejected because the tip of your current branch is behind\n"
277 "its remote counterpart. Integrate the remote changes (e.g.\n"
278 "'git pull ...') before pushing again.\n"
279 "See the 'Note about fast-forwards' in 'git push --help' for details.");
281 static const char message_advice_checkout_pull_push[] =
282 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
283 "counterpart. Check out this branch and integrate the remote changes\n"
284 "(e.g. 'git pull ...') before pushing again.\n"
285 "See the 'Note about fast-forwards' in 'git push --help' for details.");
287 static const char message_advice_ref_fetch_first[] =
288 N_("Updates were rejected because the remote contains work that you do\n"
289 "not have locally. This is usually caused by another repository pushing\n"
290 "to the same ref. You may want to first integrate the remote changes\n"
291 "(e.g., 'git pull ...') before pushing again.\n"
292 "See the 'Note about fast-forwards' in 'git push --help' for details.");
294 static const char message_advice_ref_already_exists[] =
295 N_("Updates were rejected because the tag already exists in the remote.");
297 static const char message_advice_ref_needs_force[] =
298 N_("You cannot update a remote ref that points at a non-commit object,\n"
299 "or update a remote ref to make it point at a non-commit object,\n"
300 "without using the '--force' option.\n");
302 static void advise_pull_before_push(void)
304 if (!advice_push_non_ff_current || !advice_push_update_rejected)
305 return;
306 advise(_(message_advice_pull_before_push));
309 static void advise_checkout_pull_push(void)
311 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
312 return;
313 advise(_(message_advice_checkout_pull_push));
316 static void advise_ref_already_exists(void)
318 if (!advice_push_already_exists || !advice_push_update_rejected)
319 return;
320 advise(_(message_advice_ref_already_exists));
323 static void advise_ref_fetch_first(void)
325 if (!advice_push_fetch_first || !advice_push_update_rejected)
326 return;
327 advise(_(message_advice_ref_fetch_first));
330 static void advise_ref_needs_force(void)
332 if (!advice_push_needs_force || !advice_push_update_rejected)
333 return;
334 advise(_(message_advice_ref_needs_force));
337 static int push_with_options(struct transport *transport, struct refspec *rs,
338 int flags)
340 int err;
341 unsigned int reject_reasons;
343 transport_set_verbosity(transport, verbosity, progress);
344 transport->family = family;
346 if (receivepack)
347 transport_set_option(transport,
348 TRANS_OPT_RECEIVEPACK, receivepack);
349 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
351 if (!is_empty_cas(&cas)) {
352 if (!transport->smart_options)
353 die("underlying transport does not support --%s option",
354 CAS_OPT_NAME);
355 transport->smart_options->cas = &cas;
358 if (verbosity > 0)
359 fprintf(stderr, _("Pushing to %s\n"), transport->url);
360 err = transport_push(the_repository, transport,
361 rs, flags, &reject_reasons);
362 if (err != 0) {
363 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
364 error(_("failed to push some refs to '%s'"), transport->url);
365 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
368 err |= transport_disconnect(transport);
369 if (!err)
370 return 0;
372 if (reject_reasons & REJECT_NON_FF_HEAD) {
373 advise_pull_before_push();
374 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
375 advise_checkout_pull_push();
376 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
377 advise_ref_already_exists();
378 } else if (reject_reasons & REJECT_FETCH_FIRST) {
379 advise_ref_fetch_first();
380 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
381 advise_ref_needs_force();
384 return 1;
387 static int do_push(const char *repo, int flags,
388 const struct string_list *push_options,
389 struct remote *remote)
391 int i, errs;
392 const char **url;
393 int url_nr;
394 struct refspec *push_refspec = &rs;
396 if (push_options->nr)
397 flags |= TRANSPORT_PUSH_OPTIONS;
399 if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) {
400 if (remote->push.nr) {
401 push_refspec = &remote->push;
402 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
403 setup_default_push_refspecs(remote);
405 errs = 0;
406 url_nr = push_url_of_remote(remote, &url);
407 if (url_nr) {
408 for (i = 0; i < url_nr; i++) {
409 struct transport *transport =
410 transport_get(remote, url[i]);
411 if (flags & TRANSPORT_PUSH_OPTIONS)
412 transport->push_options = push_options;
413 if (push_with_options(transport, push_refspec, flags))
414 errs++;
416 } else {
417 struct transport *transport =
418 transport_get(remote, NULL);
419 if (flags & TRANSPORT_PUSH_OPTIONS)
420 transport->push_options = push_options;
421 if (push_with_options(transport, push_refspec, flags))
422 errs++;
424 return !!errs;
427 static int option_parse_recurse_submodules(const struct option *opt,
428 const char *arg, int unset)
430 int *recurse_submodules = opt->value;
432 if (unset)
433 *recurse_submodules = RECURSE_SUBMODULES_OFF;
434 else if (arg)
435 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
436 else
437 die("%s missing parameter", opt->long_name);
439 return 0;
442 static void set_push_cert_flags(int *flags, int v)
444 switch (v) {
445 case SEND_PACK_PUSH_CERT_NEVER:
446 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
447 break;
448 case SEND_PACK_PUSH_CERT_ALWAYS:
449 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
450 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
451 break;
452 case SEND_PACK_PUSH_CERT_IF_ASKED:
453 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
454 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
455 break;
460 static int git_push_config(const char *k, const char *v, void *cb)
462 const char *slot_name;
463 int *flags = cb;
464 int status;
466 status = git_gpg_config(k, v, NULL);
467 if (status)
468 return status;
470 if (!strcmp(k, "push.followtags")) {
471 if (git_config_bool(k, v))
472 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
473 else
474 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
475 return 0;
476 } else if (!strcmp(k, "push.gpgsign")) {
477 const char *value;
478 if (!git_config_get_value("push.gpgsign", &value)) {
479 switch (git_parse_maybe_bool(value)) {
480 case 0:
481 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
482 break;
483 case 1:
484 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
485 break;
486 default:
487 if (value && !strcasecmp(value, "if-asked"))
488 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
489 else
490 return error("Invalid value for '%s'", k);
493 } else if (!strcmp(k, "push.recursesubmodules")) {
494 const char *value;
495 if (!git_config_get_value("push.recursesubmodules", &value))
496 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
497 } else if (!strcmp(k, "submodule.recurse")) {
498 int val = git_config_bool(k, v) ?
499 RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
500 recurse_submodules = val;
501 } else if (!strcmp(k, "push.pushoption")) {
502 if (!v)
503 return config_error_nonbool(k);
504 else
505 if (!*v)
506 string_list_clear(&push_options_config, 0);
507 else
508 string_list_append(&push_options_config, v);
509 return 0;
510 } else if (!strcmp(k, "color.push")) {
511 push_use_color = git_config_colorbool(k, v);
512 return 0;
513 } else if (skip_prefix(k, "color.push.", &slot_name)) {
514 int slot = parse_push_color_slot(slot_name);
515 if (slot < 0)
516 return 0;
517 if (!v)
518 return config_error_nonbool(k);
519 return color_parse(v, push_colors[slot]);
522 return git_default_config(k, v, NULL);
525 int cmd_push(int argc, const char **argv, const char *prefix)
527 int flags = 0;
528 int tags = 0;
529 int push_cert = -1;
530 int rc;
531 const char *repo = NULL; /* default repository */
532 struct string_list push_options_cmdline = STRING_LIST_INIT_DUP;
533 struct string_list *push_options;
534 const struct string_list_item *item;
535 struct remote *remote;
537 struct option options[] = {
538 OPT__VERBOSITY(&verbosity),
539 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
540 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
541 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
542 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
543 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
544 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
545 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
546 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
547 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
548 { OPTION_CALLBACK,
549 0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
550 N_("require old value of ref to be at this value"),
551 PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
552 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
553 N_("control recursive pushing of submodules"),
554 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
555 OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
556 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
557 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
558 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
559 TRANSPORT_PUSH_SET_UPSTREAM),
560 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
561 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
562 TRANSPORT_PUSH_PRUNE),
563 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
564 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
565 TRANSPORT_PUSH_FOLLOW_TAGS),
566 { OPTION_CALLBACK,
567 0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
568 PARSE_OPT_OPTARG, option_parse_push_signed },
569 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
570 OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")),
571 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
572 TRANSPORT_FAMILY_IPV4),
573 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
574 TRANSPORT_FAMILY_IPV6),
575 OPT_END()
578 packet_trace_identity("push");
579 git_config(git_push_config, &flags);
580 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
581 push_options = (push_options_cmdline.nr
582 ? &push_options_cmdline
583 : &push_options_config);
584 set_push_cert_flags(&flags, push_cert);
586 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
587 die(_("--delete is incompatible with --all, --mirror and --tags"));
588 if (deleterefs && argc < 2)
589 die(_("--delete doesn't make sense without any refs"));
591 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
592 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
593 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
594 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
595 else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
596 flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
598 if (tags)
599 refspec_append(&rs, "refs/tags/*");
601 if (argc > 0) {
602 repo = argv[0];
603 set_refspecs(argv + 1, argc - 1, repo);
606 remote = pushremote_get(repo);
607 if (!remote) {
608 if (repo)
609 die(_("bad repository '%s'"), repo);
610 die(_("No configured push destination.\n"
611 "Either specify the URL from the command-line or configure a remote repository using\n"
612 "\n"
613 " git remote add <name> <url>\n"
614 "\n"
615 "and then push using the remote name\n"
616 "\n"
617 " git push <name>\n"));
620 if (remote->mirror)
621 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
623 if (flags & TRANSPORT_PUSH_ALL) {
624 if (tags)
625 die(_("--all and --tags are incompatible"));
626 if (argc >= 2)
627 die(_("--all can't be combined with refspecs"));
629 if (flags & TRANSPORT_PUSH_MIRROR) {
630 if (tags)
631 die(_("--mirror and --tags are incompatible"));
632 if (argc >= 2)
633 die(_("--mirror can't be combined with refspecs"));
635 if ((flags & TRANSPORT_PUSH_ALL) && (flags & TRANSPORT_PUSH_MIRROR))
636 die(_("--all and --mirror are incompatible"));
638 for_each_string_list_item(item, push_options)
639 if (strchr(item->string, '\n'))
640 die(_("push options must not have new line characters"));
642 rc = do_push(repo, flags, push_options, remote);
643 string_list_clear(&push_options_cmdline, 0);
644 string_list_clear(&push_options_config, 0);
645 if (rc == -1)
646 usage_with_options(push_usage, options);
647 else
648 return rc;