8 #include "run-command.h"
11 #include "transport.h"
12 #include "parse-options.h"
13 #include "submodule.h"
14 #include "submodule-config.h"
15 #include "send-pack.h"
18 static const char * const push_usage[] = {
19 N_("git push [<options>] [<repository> [<refspec>...]]"),
23 static int push_use_color = -1;
24 static char push_colors[][COLOR_MAXLEN] = {
26 GIT_COLOR_RED, /* ERROR */
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;
43 static const char *push_get_color(enum color_push ix)
45 if (want_color_stderr(push_use_color))
46 return push_colors[ix];
51 static int deleterefs;
52 static const char *receivepack;
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 const char **refspec;
61 static int refspec_nr;
62 static int refspec_alloc;
64 static struct string_list push_options_config = STRING_LIST_INIT_DUP;
66 static void add_refspec(const char *ref)
69 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
70 refspec[refspec_nr-1] = ref;
73 static const char *map_refspec(const char *ref,
74 struct remote *remote, struct ref *local_refs)
76 struct ref *matched = NULL;
78 /* Does "ref" uniquely name our ref? */
79 if (count_refspec_match(ref, local_refs, &matched) != 1)
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.items, remote->push.nr, &query) &&
88 struct strbuf buf = STRBUF_INIT;
89 strbuf_addf(&buf, "%s%s:%s",
90 query.force ? "+" : "",
91 query.src, query.dst);
92 return strbuf_detach(&buf, NULL);
96 if (push_default == PUSH_DEFAULT_UPSTREAM &&
97 starts_with(matched->name, "refs/heads/")) {
98 struct branch *branch = branch_get(matched->name + 11);
99 if (branch->merge_nr == 1 && branch->merge[0]->src) {
100 struct strbuf buf = STRBUF_INIT;
101 strbuf_addf(&buf, "%s:%s",
102 ref, branch->merge[0]->src);
103 return strbuf_detach(&buf, NULL);
110 static void set_refspecs(const char **refs, int nr, const char *repo)
112 struct remote *remote = NULL;
113 struct ref *local_refs = NULL;
116 for (i = 0; i < nr; i++) {
117 const char *ref = refs[i];
118 if (!strcmp("tag", ref)) {
119 struct strbuf tagref = STRBUF_INIT;
121 die(_("tag shorthand without <tag>"));
124 strbuf_addf(&tagref, ":refs/tags/%s", ref);
126 strbuf_addf(&tagref, "refs/tags/%s", ref);
127 ref = strbuf_detach(&tagref, NULL);
128 } else if (deleterefs) {
129 struct strbuf delref = STRBUF_INIT;
130 if (strchr(ref, ':'))
131 die(_("--delete only accepts plain target ref names"));
132 strbuf_addf(&delref, ":%s", ref);
133 ref = strbuf_detach(&delref, NULL);
134 } else if (!strchr(ref, ':')) {
136 /* lazily grab remote and local_refs */
137 remote = remote_get(repo);
138 local_refs = get_local_heads();
140 ref = map_refspec(ref, remote, local_refs);
146 static int push_url_of_remote(struct remote *remote, const char ***url_p)
148 if (remote->pushurl_nr) {
149 *url_p = remote->pushurl;
150 return remote->pushurl_nr;
152 *url_p = remote->url;
153 return remote->url_nr;
156 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
158 * There's no point in using shorten_unambiguous_ref here,
159 * as the ambiguity would be on the remote side, not what
160 * we have locally. Plus, this is supposed to be the simple
161 * mode. If the user is doing something crazy like setting
162 * upstream to a non-branch, we should probably be showing
163 * them the big ugly fully qualified ref.
165 const char *advice_maybe = "";
166 const char *short_upstream = branch->merge[0]->src;
168 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
171 * Don't show advice for people who explicitly set
174 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
175 advice_maybe = _("\n"
176 "To choose either option permanently, "
177 "see push.default in 'git help config'.");
178 die(_("The upstream branch of your current branch does not match\n"
179 "the name of your current branch. To push to the upstream branch\n"
180 "on the remote, use\n"
182 " git push %s HEAD:%s\n"
184 "To push to the branch of the same name on the remote, use\n"
188 remote->name, short_upstream,
189 remote->name, branch->name, advice_maybe);
192 static const char message_detached_head_die[] =
193 N_("You are not currently on a branch.\n"
194 "To push the history leading to the current (detached HEAD)\n"
197 " git push %s HEAD:<name-of-remote-branch>\n");
199 static void setup_push_upstream(struct remote *remote, struct branch *branch,
200 int triangular, int simple)
202 struct strbuf refspec = STRBUF_INIT;
205 die(_(message_detached_head_die), remote->name);
206 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
207 die(_("The current branch %s has no upstream branch.\n"
208 "To push the current branch and set the remote as upstream, use\n"
210 " git push --set-upstream %s %s\n"),
214 if (branch->merge_nr != 1)
215 die(_("The current branch %s has multiple upstream branches, "
216 "refusing to push."), branch->name);
218 die(_("You are pushing to remote '%s', which is not the upstream of\n"
219 "your current branch '%s', without telling me what to push\n"
220 "to update which remote branch."),
221 remote->name, branch->name);
224 /* Additional safety */
225 if (strcmp(branch->refname, branch->merge[0]->src))
226 die_push_simple(branch, remote);
229 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
230 add_refspec(refspec.buf);
233 static void setup_push_current(struct remote *remote, struct branch *branch)
235 struct strbuf refspec = STRBUF_INIT;
238 die(_(message_detached_head_die), remote->name);
239 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
240 add_refspec(refspec.buf);
243 static int is_workflow_triangular(struct remote *remote)
245 struct remote *fetch_remote = remote_get(NULL);
246 return (fetch_remote && fetch_remote != remote);
249 static void setup_default_push_refspecs(struct remote *remote)
251 struct branch *branch = branch_get(NULL);
252 int triangular = is_workflow_triangular(remote);
254 switch (push_default) {
256 case PUSH_DEFAULT_MATCHING:
260 case PUSH_DEFAULT_UNSPECIFIED:
261 case PUSH_DEFAULT_SIMPLE:
263 setup_push_current(remote, branch);
265 setup_push_upstream(remote, branch, triangular, 1);
268 case PUSH_DEFAULT_UPSTREAM:
269 setup_push_upstream(remote, branch, triangular, 0);
272 case PUSH_DEFAULT_CURRENT:
273 setup_push_current(remote, branch);
276 case PUSH_DEFAULT_NOTHING:
277 die(_("You didn't specify any refspecs to push, and "
278 "push.default is \"nothing\"."));
283 static const char message_advice_pull_before_push[] =
284 N_("Updates were rejected because the tip of your current branch is behind\n"
285 "its remote counterpart. Integrate the remote changes (e.g.\n"
286 "'git pull ...') before pushing again.\n"
287 "See the 'Note about fast-forwards' in 'git push --help' for details.");
289 static const char message_advice_checkout_pull_push[] =
290 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
291 "counterpart. Check out this branch and integrate the remote changes\n"
292 "(e.g. 'git pull ...') before pushing again.\n"
293 "See the 'Note about fast-forwards' in 'git push --help' for details.");
295 static const char message_advice_ref_fetch_first[] =
296 N_("Updates were rejected because the remote contains work that you do\n"
297 "not have locally. This is usually caused by another repository pushing\n"
298 "to the same ref. You may want to first integrate the remote changes\n"
299 "(e.g., 'git pull ...') before pushing again.\n"
300 "See the 'Note about fast-forwards' in 'git push --help' for details.");
302 static const char message_advice_ref_already_exists[] =
303 N_("Updates were rejected because the tag already exists in the remote.");
305 static const char message_advice_ref_needs_force[] =
306 N_("You cannot update a remote ref that points at a non-commit object,\n"
307 "or update a remote ref to make it point at a non-commit object,\n"
308 "without using the '--force' option.\n");
310 static void advise_pull_before_push(void)
312 if (!advice_push_non_ff_current || !advice_push_update_rejected)
314 advise(_(message_advice_pull_before_push));
317 static void advise_checkout_pull_push(void)
319 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
321 advise(_(message_advice_checkout_pull_push));
324 static void advise_ref_already_exists(void)
326 if (!advice_push_already_exists || !advice_push_update_rejected)
328 advise(_(message_advice_ref_already_exists));
331 static void advise_ref_fetch_first(void)
333 if (!advice_push_fetch_first || !advice_push_update_rejected)
335 advise(_(message_advice_ref_fetch_first));
338 static void advise_ref_needs_force(void)
340 if (!advice_push_needs_force || !advice_push_update_rejected)
342 advise(_(message_advice_ref_needs_force));
345 static int push_with_options(struct transport *transport, int flags)
348 unsigned int reject_reasons;
350 transport_set_verbosity(transport, verbosity, progress);
351 transport->family = family;
354 transport_set_option(transport,
355 TRANS_OPT_RECEIVEPACK, receivepack);
356 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
358 if (!is_empty_cas(&cas)) {
359 if (!transport->smart_options)
360 die("underlying transport does not support --%s option",
362 transport->smart_options->cas = &cas;
366 fprintf(stderr, _("Pushing to %s\n"), transport->url);
367 err = transport_push(transport, refspec_nr, refspec, flags,
370 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
371 error(_("failed to push some refs to '%s'"), transport->url);
372 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
375 err |= transport_disconnect(transport);
379 if (reject_reasons & REJECT_NON_FF_HEAD) {
380 advise_pull_before_push();
381 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
382 advise_checkout_pull_push();
383 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
384 advise_ref_already_exists();
385 } else if (reject_reasons & REJECT_FETCH_FIRST) {
386 advise_ref_fetch_first();
387 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
388 advise_ref_needs_force();
394 static int do_push(const char *repo, int flags,
395 const struct string_list *push_options)
398 struct remote *remote = pushremote_get(repo);
404 die(_("bad repository '%s'"), repo);
405 die(_("No configured push destination.\n"
406 "Either specify the URL from the command-line or configure a remote repository using\n"
408 " git remote add <name> <url>\n"
410 "and then push using the remote name\n"
412 " git push <name>\n"));
416 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
418 if (push_options->nr)
419 flags |= TRANSPORT_PUSH_OPTIONS;
421 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
422 if (!strcmp(*refspec, "refs/tags/*"))
423 return error(_("--all and --tags are incompatible"));
424 return error(_("--all can't be combined with refspecs"));
427 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
428 if (!strcmp(*refspec, "refs/tags/*"))
429 return error(_("--mirror and --tags are incompatible"));
430 return error(_("--mirror can't be combined with refspecs"));
433 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
434 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
435 return error(_("--all and --mirror are incompatible"));
438 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
439 if (remote->push.raw_nr) {
440 refspec = remote->push.raw;
441 refspec_nr = remote->push.raw_nr;
442 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
443 setup_default_push_refspecs(remote);
446 url_nr = push_url_of_remote(remote, &url);
448 for (i = 0; i < url_nr; i++) {
449 struct transport *transport =
450 transport_get(remote, url[i]);
451 if (flags & TRANSPORT_PUSH_OPTIONS)
452 transport->push_options = push_options;
453 if (push_with_options(transport, flags))
457 struct transport *transport =
458 transport_get(remote, NULL);
459 if (flags & TRANSPORT_PUSH_OPTIONS)
460 transport->push_options = push_options;
461 if (push_with_options(transport, flags))
467 static int option_parse_recurse_submodules(const struct option *opt,
468 const char *arg, int unset)
470 int *recurse_submodules = opt->value;
473 *recurse_submodules = RECURSE_SUBMODULES_OFF;
475 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
477 die("%s missing parameter", opt->long_name);
482 static void set_push_cert_flags(int *flags, int v)
485 case SEND_PACK_PUSH_CERT_NEVER:
486 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
488 case SEND_PACK_PUSH_CERT_ALWAYS:
489 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
490 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
492 case SEND_PACK_PUSH_CERT_IF_ASKED:
493 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
494 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
500 static int git_push_config(const char *k, const char *v, void *cb)
502 const char *slot_name;
506 status = git_gpg_config(k, v, NULL);
510 if (!strcmp(k, "push.followtags")) {
511 if (git_config_bool(k, v))
512 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
514 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
516 } else if (!strcmp(k, "push.gpgsign")) {
518 if (!git_config_get_value("push.gpgsign", &value)) {
519 switch (git_parse_maybe_bool(value)) {
521 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
524 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
527 if (value && !strcasecmp(value, "if-asked"))
528 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
530 return error("Invalid value for '%s'", k);
533 } else if (!strcmp(k, "push.recursesubmodules")) {
535 if (!git_config_get_value("push.recursesubmodules", &value))
536 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
537 } else if (!strcmp(k, "submodule.recurse")) {
538 int val = git_config_bool(k, v) ?
539 RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
540 recurse_submodules = val;
541 } else if (!strcmp(k, "push.pushoption")) {
543 return config_error_nonbool(k);
546 string_list_clear(&push_options_config, 0);
548 string_list_append(&push_options_config, v);
550 } else if (!strcmp(k, "color.push")) {
551 push_use_color = git_config_colorbool(k, v);
553 } else if (skip_prefix(k, "color.push.", &slot_name)) {
554 int slot = parse_push_color_slot(slot_name);
558 return config_error_nonbool(k);
559 return color_parse(v, push_colors[slot]);
562 return git_default_config(k, v, NULL);
565 int cmd_push(int argc, const char **argv, const char *prefix)
571 const char *repo = NULL; /* default repository */
572 struct string_list push_options_cmdline = STRING_LIST_INIT_DUP;
573 struct string_list *push_options;
574 const struct string_list_item *item;
576 struct option options[] = {
577 OPT__VERBOSITY(&verbosity),
578 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
579 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
580 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
581 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
582 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
583 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
584 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
585 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
586 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
588 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
589 N_("require old value of ref to be at this value"),
590 PARSE_OPT_OPTARG, parseopt_push_cas_option },
591 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "check|on-demand|no",
592 N_("control recursive pushing of submodules"),
593 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
594 OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
595 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
596 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
597 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
598 TRANSPORT_PUSH_SET_UPSTREAM),
599 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
600 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
601 TRANSPORT_PUSH_PRUNE),
602 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
603 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
604 TRANSPORT_PUSH_FOLLOW_TAGS),
606 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
607 PARSE_OPT_OPTARG, option_parse_push_signed },
608 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
609 OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")),
610 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
611 TRANSPORT_FAMILY_IPV4),
612 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
613 TRANSPORT_FAMILY_IPV6),
617 packet_trace_identity("push");
618 git_config(git_push_config, &flags);
619 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
620 push_options = (push_options_cmdline.nr
621 ? &push_options_cmdline
622 : &push_options_config);
623 set_push_cert_flags(&flags, push_cert);
625 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
626 die(_("--delete is incompatible with --all, --mirror and --tags"));
627 if (deleterefs && argc < 2)
628 die(_("--delete doesn't make sense without any refs"));
630 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
631 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
632 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
633 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
634 else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
635 flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
638 add_refspec("refs/tags/*");
642 set_refspecs(argv + 1, argc - 1, repo);
645 for_each_string_list_item(item, push_options)
646 if (strchr(item->string, '\n'))
647 die(_("push options must not have new line characters"));
649 rc = do_push(repo, flags, push_options);
650 string_list_clear(&push_options_cmdline, 0);
651 string_list_clear(&push_options_config, 0);
653 usage_with_options(push_usage, options);