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