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