6 #include "run-command.h"
10 #include "parse-options.h"
11 #include "submodule.h"
12 #include "submodule-config.h"
13 #include "send-pack.h"
15 static const char * const push_usage[] = {
16 N_("git push [<options>] [<repository> [<refspec>...]]"),
21 static int deleterefs;
22 static const char *receivepack;
24 static int progress = -1;
25 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
26 static enum transport_family family;
28 static struct push_cas_option cas;
30 static const char **refspec;
31 static int refspec_nr;
32 static int refspec_alloc;
34 static void add_refspec(const char *ref)
37 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
38 refspec[refspec_nr-1] = ref;
41 static const char *map_refspec(const char *ref,
42 struct remote *remote, struct ref *local_refs)
44 struct ref *matched = NULL;
46 /* Does "ref" uniquely name our ref? */
47 if (count_refspec_match(ref, local_refs, &matched) != 1)
52 memset(&query, 0, sizeof(struct refspec));
53 query.src = matched->name;
54 if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
56 struct strbuf buf = STRBUF_INIT;
57 strbuf_addf(&buf, "%s%s:%s",
58 query.force ? "+" : "",
59 query.src, query.dst);
60 return strbuf_detach(&buf, NULL);
64 if (push_default == PUSH_DEFAULT_UPSTREAM &&
65 starts_with(matched->name, "refs/heads/")) {
66 struct branch *branch = branch_get(matched->name + 11);
67 if (branch->merge_nr == 1 && branch->merge[0]->src) {
68 struct strbuf buf = STRBUF_INIT;
69 strbuf_addf(&buf, "%s:%s",
70 ref, branch->merge[0]->src);
71 return strbuf_detach(&buf, NULL);
78 static void set_refspecs(const char **refs, int nr, const char *repo)
80 struct remote *remote = NULL;
81 struct ref *local_refs = NULL;
84 for (i = 0; i < nr; i++) {
85 const char *ref = refs[i];
86 if (!strcmp("tag", ref)) {
87 struct strbuf tagref = STRBUF_INIT;
89 die(_("tag shorthand without <tag>"));
92 strbuf_addf(&tagref, ":refs/tags/%s", ref);
94 strbuf_addf(&tagref, "refs/tags/%s", ref);
95 ref = strbuf_detach(&tagref, NULL);
96 } else if (deleterefs) {
97 struct strbuf delref = STRBUF_INIT;
99 die(_("--delete only accepts plain target ref names"));
100 strbuf_addf(&delref, ":%s", ref);
101 ref = strbuf_detach(&delref, NULL);
102 } else if (!strchr(ref, ':')) {
104 /* lazily grab remote and local_refs */
105 remote = remote_get(repo);
106 local_refs = get_local_heads();
108 ref = map_refspec(ref, remote, local_refs);
114 static int push_url_of_remote(struct remote *remote, const char ***url_p)
116 if (remote->pushurl_nr) {
117 *url_p = remote->pushurl;
118 return remote->pushurl_nr;
120 *url_p = remote->url;
121 return remote->url_nr;
124 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
126 * There's no point in using shorten_unambiguous_ref here,
127 * as the ambiguity would be on the remote side, not what
128 * we have locally. Plus, this is supposed to be the simple
129 * mode. If the user is doing something crazy like setting
130 * upstream to a non-branch, we should probably be showing
131 * them the big ugly fully qualified ref.
133 const char *advice_maybe = "";
134 const char *short_upstream = branch->merge[0]->src;
136 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
139 * Don't show advice for people who explicitly set
142 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
143 advice_maybe = _("\n"
144 "To choose either option permanently, "
145 "see push.default in 'git help config'.");
146 die(_("The upstream branch of your current branch does not match\n"
147 "the name of your current branch. To push to the upstream branch\n"
148 "on the remote, use\n"
150 " git push %s HEAD:%s\n"
152 "To push to the branch of the same name on the remote, use\n"
156 remote->name, short_upstream,
157 remote->name, branch->name, advice_maybe);
160 static const char message_detached_head_die[] =
161 N_("You are not currently on a branch.\n"
162 "To push the history leading to the current (detached HEAD)\n"
165 " git push %s HEAD:<name-of-remote-branch>\n");
167 static void setup_push_upstream(struct remote *remote, struct branch *branch,
168 int triangular, int simple)
170 struct strbuf refspec = STRBUF_INIT;
173 die(_(message_detached_head_die), remote->name);
174 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
175 die(_("The current branch %s has no upstream branch.\n"
176 "To push the current branch and set the remote as upstream, use\n"
178 " git push --set-upstream %s %s\n"),
182 if (branch->merge_nr != 1)
183 die(_("The current branch %s has multiple upstream branches, "
184 "refusing to push."), branch->name);
186 die(_("You are pushing to remote '%s', which is not the upstream of\n"
187 "your current branch '%s', without telling me what to push\n"
188 "to update which remote branch."),
189 remote->name, branch->name);
192 /* Additional safety */
193 if (strcmp(branch->refname, branch->merge[0]->src))
194 die_push_simple(branch, remote);
197 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
198 add_refspec(refspec.buf);
201 static void setup_push_current(struct remote *remote, struct branch *branch)
204 die(_(message_detached_head_die), remote->name);
205 add_refspec(branch->name);
208 static char warn_unspecified_push_default_msg[] =
209 N_("push.default is unset; its implicit value has changed in\n"
210 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
211 "and maintain the traditional behavior, use:\n"
213 " git config --global push.default matching\n"
215 "To squelch this message and adopt the new behavior now, use:\n"
217 " git config --global push.default simple\n"
219 "When push.default is set to 'matching', git will push local branches\n"
220 "to the remote branches that already exist with the same name.\n"
222 "Since Git 2.0, Git defaults to the more conservative 'simple'\n"
223 "behavior, which only pushes the current branch to the corresponding\n"
224 "remote branch that 'git pull' uses to update the current branch.\n"
226 "See 'git help config' and search for 'push.default' for further information.\n"
227 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
228 "'current' instead of 'simple' if you sometimes use older versions of Git)");
230 static void warn_unspecified_push_default_configuration(void)
232 static int warn_once;
236 warning("%s\n", _(warn_unspecified_push_default_msg));
239 static int is_workflow_triangular(struct remote *remote)
241 struct remote *fetch_remote = remote_get(NULL);
242 return (fetch_remote && fetch_remote != remote);
245 static void setup_default_push_refspecs(struct remote *remote)
247 struct branch *branch = branch_get(NULL);
248 int triangular = is_workflow_triangular(remote);
250 switch (push_default) {
252 case PUSH_DEFAULT_MATCHING:
256 case PUSH_DEFAULT_UNSPECIFIED:
257 warn_unspecified_push_default_configuration();
260 case PUSH_DEFAULT_SIMPLE:
262 setup_push_current(remote, branch);
264 setup_push_upstream(remote, branch, triangular, 1);
267 case PUSH_DEFAULT_UPSTREAM:
268 setup_push_upstream(remote, branch, triangular, 0);
271 case PUSH_DEFAULT_CURRENT:
272 setup_push_current(remote, branch);
275 case PUSH_DEFAULT_NOTHING:
276 die(_("You didn't specify any refspecs to push, and "
277 "push.default is \"nothing\"."));
282 static 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.");
288 static 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.");
294 static 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.");
301 static const char message_advice_ref_already_exists[] =
302 N_("Updates were rejected because the tag already exists in the remote.");
304 static 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");
309 static void advise_pull_before_push(void)
311 if (!advice_push_non_ff_current || !advice_push_update_rejected)
313 advise(_(message_advice_pull_before_push));
316 static void advise_checkout_pull_push(void)
318 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
320 advise(_(message_advice_checkout_pull_push));
323 static void advise_ref_already_exists(void)
325 if (!advice_push_already_exists || !advice_push_update_rejected)
327 advise(_(message_advice_ref_already_exists));
330 static void advise_ref_fetch_first(void)
332 if (!advice_push_fetch_first || !advice_push_update_rejected)
334 advise(_(message_advice_ref_fetch_first));
337 static void advise_ref_needs_force(void)
339 if (!advice_push_needs_force || !advice_push_update_rejected)
341 advise(_(message_advice_ref_needs_force));
344 static int push_with_options(struct transport *transport, int flags)
347 unsigned int reject_reasons;
349 transport_set_verbosity(transport, verbosity, progress);
350 transport->family = family;
353 transport_set_option(transport,
354 TRANS_OPT_RECEIVEPACK, receivepack);
355 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
357 if (!is_empty_cas(&cas)) {
358 if (!transport->smart_options)
359 die("underlying transport does not support --%s option",
361 transport->smart_options->cas = &cas;
365 fprintf(stderr, _("Pushing to %s\n"), transport->url);
366 err = transport_push(transport, refspec_nr, refspec, flags,
369 error(_("failed to push some refs to '%s'"), transport->url);
371 err |= transport_disconnect(transport);
375 if (reject_reasons & REJECT_NON_FF_HEAD) {
376 advise_pull_before_push();
377 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
378 advise_checkout_pull_push();
379 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
380 advise_ref_already_exists();
381 } else if (reject_reasons & REJECT_FETCH_FIRST) {
382 advise_ref_fetch_first();
383 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
384 advise_ref_needs_force();
390 static int do_push(const char *repo, int flags)
393 struct remote *remote = pushremote_get(repo);
399 die(_("bad repository '%s'"), repo);
400 die(_("No configured push destination.\n"
401 "Either specify the URL from the command-line or configure a remote repository using\n"
403 " git remote add <name> <url>\n"
405 "and then push using the remote name\n"
407 " git push <name>\n"));
411 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
413 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
414 if (!strcmp(*refspec, "refs/tags/*"))
415 return error(_("--all and --tags are incompatible"));
416 return error(_("--all can't be combined with refspecs"));
419 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
420 if (!strcmp(*refspec, "refs/tags/*"))
421 return error(_("--mirror and --tags are incompatible"));
422 return error(_("--mirror can't be combined with refspecs"));
425 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
426 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
427 return error(_("--all and --mirror are incompatible"));
430 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
431 if (remote->push_refspec_nr) {
432 refspec = remote->push_refspec;
433 refspec_nr = remote->push_refspec_nr;
434 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
435 setup_default_push_refspecs(remote);
438 url_nr = push_url_of_remote(remote, &url);
440 for (i = 0; i < url_nr; i++) {
441 struct transport *transport =
442 transport_get(remote, url[i]);
443 if (push_with_options(transport, flags))
447 struct transport *transport =
448 transport_get(remote, NULL);
450 if (push_with_options(transport, flags))
456 static int option_parse_recurse_submodules(const struct option *opt,
457 const char *arg, int unset)
459 int *recurse_submodules = opt->value;
462 *recurse_submodules = RECURSE_SUBMODULES_OFF;
464 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
466 die("%s missing parameter", opt->long_name);
471 static void set_push_cert_flags(int *flags, int v)
474 case SEND_PACK_PUSH_CERT_NEVER:
475 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
477 case SEND_PACK_PUSH_CERT_ALWAYS:
478 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
479 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
481 case SEND_PACK_PUSH_CERT_IF_ASKED:
482 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
483 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
489 static int git_push_config(const char *k, const char *v, void *cb)
494 status = git_gpg_config(k, v, NULL);
498 if (!strcmp(k, "push.followtags")) {
499 if (git_config_bool(k, v))
500 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
502 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
504 } else if (!strcmp(k, "push.gpgsign")) {
506 if (!git_config_get_value("push.gpgsign", &value)) {
507 switch (git_config_maybe_bool("push.gpgsign", value)) {
509 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
512 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
515 if (value && !strcasecmp(value, "if-asked"))
516 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
518 return error("Invalid value for '%s'", k);
521 } else if (!strcmp(k, "push.recursesubmodules")) {
523 if (!git_config_get_value("push.recursesubmodules", &value))
524 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
527 return git_default_config(k, v, NULL);
530 int cmd_push(int argc, const char **argv, const char *prefix)
536 const char *repo = NULL; /* default repository */
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),
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, 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( 0 , "thin", &thin, N_("use thin pack")),
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),
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_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
571 TRANSPORT_FAMILY_IPV4),
572 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
573 TRANSPORT_FAMILY_IPV6),
577 packet_trace_identity("push");
578 git_config(git_push_config, &flags);
579 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
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;
593 add_refspec("refs/tags/*");
597 set_refspecs(argv + 1, argc - 1, repo);
600 rc = do_push(repo, flags);
602 usage_with_options(push_usage, options);