6 #include "run-command.h"
10 #include "parse-options.h"
11 #include "submodule.h"
12 #include "send-pack.h"
14 static const char * const push_usage[] = {
15 N_("git push [<options>] [<repository> [<refspec>...]]"),
20 static int deleterefs;
21 static const char *receivepack;
23 static int progress = -1;
25 static struct push_cas_option cas;
27 static const char **refspec;
28 static int refspec_nr;
29 static int refspec_alloc;
31 static void add_refspec(const char *ref)
34 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
35 refspec[refspec_nr-1] = ref;
38 static const char *map_refspec(const char *ref,
39 struct remote *remote, struct ref *local_refs)
41 struct ref *matched = NULL;
43 /* Does "ref" uniquely name our ref? */
44 if (count_refspec_match(ref, local_refs, &matched) != 1)
49 memset(&query, 0, sizeof(struct refspec));
50 query.src = matched->name;
51 if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
53 struct strbuf buf = STRBUF_INIT;
54 strbuf_addf(&buf, "%s%s:%s",
55 query.force ? "+" : "",
56 query.src, query.dst);
57 return strbuf_detach(&buf, NULL);
61 if (push_default == PUSH_DEFAULT_UPSTREAM &&
62 starts_with(matched->name, "refs/heads/")) {
63 struct branch *branch = branch_get(matched->name + 11);
64 if (branch->merge_nr == 1 && branch->merge[0]->src) {
65 struct strbuf buf = STRBUF_INIT;
66 strbuf_addf(&buf, "%s:%s",
67 ref, branch->merge[0]->src);
68 return strbuf_detach(&buf, NULL);
75 static void set_refspecs(const char **refs, int nr, const char *repo)
77 struct remote *remote = NULL;
78 struct ref *local_refs = NULL;
81 for (i = 0; i < nr; i++) {
82 const char *ref = refs[i];
83 if (!strcmp("tag", ref)) {
84 struct strbuf tagref = STRBUF_INIT;
86 die(_("tag shorthand without <tag>"));
89 strbuf_addf(&tagref, ":refs/tags/%s", ref);
91 strbuf_addf(&tagref, "refs/tags/%s", ref);
92 ref = strbuf_detach(&tagref, NULL);
93 } else if (deleterefs) {
94 struct strbuf delref = STRBUF_INIT;
96 die(_("--delete only accepts plain target ref names"));
97 strbuf_addf(&delref, ":%s", ref);
98 ref = strbuf_detach(&delref, NULL);
99 } else if (!strchr(ref, ':')) {
101 /* lazily grab remote and local_refs */
102 remote = remote_get(repo);
103 local_refs = get_local_heads();
105 ref = map_refspec(ref, remote, local_refs);
111 static int push_url_of_remote(struct remote *remote, const char ***url_p)
113 if (remote->pushurl_nr) {
114 *url_p = remote->pushurl;
115 return remote->pushurl_nr;
117 *url_p = remote->url;
118 return remote->url_nr;
121 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
123 * There's no point in using shorten_unambiguous_ref here,
124 * as the ambiguity would be on the remote side, not what
125 * we have locally. Plus, this is supposed to be the simple
126 * mode. If the user is doing something crazy like setting
127 * upstream to a non-branch, we should probably be showing
128 * them the big ugly fully qualified ref.
130 const char *advice_maybe = "";
131 const char *short_upstream = branch->merge[0]->src;
133 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
136 * Don't show advice for people who explicitly set
139 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
140 advice_maybe = _("\n"
141 "To choose either option permanently, "
142 "see push.default in 'git help config'.");
143 die(_("The upstream branch of your current branch does not match\n"
144 "the name of your current branch. To push to the upstream branch\n"
145 "on the remote, use\n"
147 " git push %s HEAD:%s\n"
149 "To push to the branch of the same name on the remote, use\n"
153 remote->name, short_upstream,
154 remote->name, branch->name, advice_maybe);
157 static const char message_detached_head_die[] =
158 N_("You are not currently on a branch.\n"
159 "To push the history leading to the current (detached HEAD)\n"
162 " git push %s HEAD:<name-of-remote-branch>\n");
164 static void setup_push_upstream(struct remote *remote, struct branch *branch,
165 int triangular, int simple)
167 struct strbuf refspec = STRBUF_INIT;
170 die(_(message_detached_head_die), remote->name);
171 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
172 die(_("The current branch %s has no upstream branch.\n"
173 "To push the current branch and set the remote as upstream, use\n"
175 " git push --set-upstream %s %s\n"),
179 if (branch->merge_nr != 1)
180 die(_("The current branch %s has multiple upstream branches, "
181 "refusing to push."), branch->name);
183 die(_("You are pushing to remote '%s', which is not the upstream of\n"
184 "your current branch '%s', without telling me what to push\n"
185 "to update which remote branch."),
186 remote->name, branch->name);
189 /* Additional safety */
190 if (strcmp(branch->refname, branch->merge[0]->src))
191 die_push_simple(branch, remote);
194 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
195 add_refspec(refspec.buf);
198 static void setup_push_current(struct remote *remote, struct branch *branch)
201 die(_(message_detached_head_die), remote->name);
202 add_refspec(branch->name);
205 static char warn_unspecified_push_default_msg[] =
206 N_("push.default is unset; its implicit value has changed in\n"
207 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
208 "and maintain the traditional behavior, use:\n"
210 " git config --global push.default matching\n"
212 "To squelch this message and adopt the new behavior now, use:\n"
214 " git config --global push.default simple\n"
216 "When push.default is set to 'matching', git will push local branches\n"
217 "to the remote branches that already exist with the same name.\n"
219 "Since Git 2.0, Git defaults to the more conservative 'simple'\n"
220 "behavior, which only pushes the current branch to the corresponding\n"
221 "remote branch that 'git pull' uses to update the current branch.\n"
223 "See 'git help config' and search for 'push.default' for further information.\n"
224 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
225 "'current' instead of 'simple' if you sometimes use older versions of Git)");
227 static void warn_unspecified_push_default_configuration(void)
229 static int warn_once;
233 warning("%s\n", _(warn_unspecified_push_default_msg));
236 static int is_workflow_triangular(struct remote *remote)
238 struct remote *fetch_remote = remote_get(NULL);
239 return (fetch_remote && fetch_remote != remote);
242 static void setup_default_push_refspecs(struct remote *remote)
244 struct branch *branch = branch_get(NULL);
245 int triangular = is_workflow_triangular(remote);
247 switch (push_default) {
249 case PUSH_DEFAULT_MATCHING:
253 case PUSH_DEFAULT_UNSPECIFIED:
254 warn_unspecified_push_default_configuration();
257 case PUSH_DEFAULT_SIMPLE:
259 setup_push_current(remote, branch);
261 setup_push_upstream(remote, branch, triangular, 1);
264 case PUSH_DEFAULT_UPSTREAM:
265 setup_push_upstream(remote, branch, triangular, 0);
268 case PUSH_DEFAULT_CURRENT:
269 setup_push_current(remote, branch);
272 case PUSH_DEFAULT_NOTHING:
273 die(_("You didn't specify any refspecs to push, and "
274 "push.default is \"nothing\"."));
279 static const char message_advice_pull_before_push[] =
280 N_("Updates were rejected because the tip of your current branch is behind\n"
281 "its remote counterpart. Integrate the remote changes (e.g.\n"
282 "'git pull ...') before pushing again.\n"
283 "See the 'Note about fast-forwards' in 'git push --help' for details.");
285 static const char message_advice_checkout_pull_push[] =
286 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
287 "counterpart. Check out this branch and integrate the remote changes\n"
288 "(e.g. 'git pull ...') before pushing again.\n"
289 "See the 'Note about fast-forwards' in 'git push --help' for details.");
291 static const char message_advice_ref_fetch_first[] =
292 N_("Updates were rejected because the remote contains work that you do\n"
293 "not have locally. This is usually caused by another repository pushing\n"
294 "to the same ref. You may want to first integrate the remote changes\n"
295 "(e.g., 'git pull ...') before pushing again.\n"
296 "See the 'Note about fast-forwards' in 'git push --help' for details.");
298 static const char message_advice_ref_already_exists[] =
299 N_("Updates were rejected because the tag already exists in the remote.");
301 static const char message_advice_ref_needs_force[] =
302 N_("You cannot update a remote ref that points at a non-commit object,\n"
303 "or update a remote ref to make it point at a non-commit object,\n"
304 "without using the '--force' option.\n");
306 static void advise_pull_before_push(void)
308 if (!advice_push_non_ff_current || !advice_push_update_rejected)
310 advise(_(message_advice_pull_before_push));
313 static void advise_checkout_pull_push(void)
315 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
317 advise(_(message_advice_checkout_pull_push));
320 static void advise_ref_already_exists(void)
322 if (!advice_push_already_exists || !advice_push_update_rejected)
324 advise(_(message_advice_ref_already_exists));
327 static void advise_ref_fetch_first(void)
329 if (!advice_push_fetch_first || !advice_push_update_rejected)
331 advise(_(message_advice_ref_fetch_first));
334 static void advise_ref_needs_force(void)
336 if (!advice_push_needs_force || !advice_push_update_rejected)
338 advise(_(message_advice_ref_needs_force));
341 static int push_with_options(struct transport *transport, int flags)
344 unsigned int reject_reasons;
346 transport_set_verbosity(transport, verbosity, progress);
349 transport_set_option(transport,
350 TRANS_OPT_RECEIVEPACK, receivepack);
351 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
353 if (!is_empty_cas(&cas)) {
354 if (!transport->smart_options)
355 die("underlying transport does not support --%s option",
357 transport->smart_options->cas = &cas;
361 fprintf(stderr, _("Pushing to %s\n"), transport->url);
362 err = transport_push(transport, refspec_nr, refspec, flags,
365 error(_("failed to push some refs to '%s'"), transport->url);
367 err |= transport_disconnect(transport);
371 if (reject_reasons & REJECT_NON_FF_HEAD) {
372 advise_pull_before_push();
373 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
374 advise_checkout_pull_push();
375 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
376 advise_ref_already_exists();
377 } else if (reject_reasons & REJECT_FETCH_FIRST) {
378 advise_ref_fetch_first();
379 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
380 advise_ref_needs_force();
386 static int do_push(const char *repo, int flags)
389 struct remote *remote = pushremote_get(repo);
395 die(_("bad repository '%s'"), repo);
396 die(_("No configured push destination.\n"
397 "Either specify the URL from the command-line or configure a remote repository using\n"
399 " git remote add <name> <url>\n"
401 "and then push using the remote name\n"
403 " git push <name>\n"));
407 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
409 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
410 if (!strcmp(*refspec, "refs/tags/*"))
411 return error(_("--all and --tags are incompatible"));
412 return error(_("--all can't be combined with refspecs"));
415 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
416 if (!strcmp(*refspec, "refs/tags/*"))
417 return error(_("--mirror and --tags are incompatible"));
418 return error(_("--mirror can't be combined with refspecs"));
421 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
422 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
423 return error(_("--all and --mirror are incompatible"));
426 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
427 struct branch *branch = branch_get(NULL);
428 /* Is there a publish branch */
429 if (branch && branch->pushremote_name && !strcmp(remote->name, branch->pushremote_name) &&
431 struct strbuf refspec = STRBUF_INIT;
432 strbuf_addf(&refspec, "%s:%s", branch->name, branch->push_name);
433 add_refspec(refspec.buf);
434 } else if (remote->push_refspec_nr) {
435 refspec = remote->push_refspec;
436 refspec_nr = remote->push_refspec_nr;
437 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
438 setup_default_push_refspecs(remote);
441 url_nr = push_url_of_remote(remote, &url);
443 for (i = 0; i < url_nr; i++) {
444 struct transport *transport =
445 transport_get(remote, url[i]);
446 if (push_with_options(transport, flags))
450 struct transport *transport =
451 transport_get(remote, NULL);
453 if (push_with_options(transport, flags))
459 static int option_parse_recurse_submodules(const struct option *opt,
460 const char *arg, int unset)
462 int *flags = opt->value;
464 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
465 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
466 die("%s can only be used once.", opt->long_name);
469 if (!strcmp(arg, "check"))
470 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
471 else if (!strcmp(arg, "on-demand"))
472 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
474 die("bad %s argument: %s", opt->long_name, arg);
476 die("option %s needs an argument (check|on-demand)",
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)
505 status = git_gpg_config(k, v, NULL);
509 if (!strcmp(k, "push.followtags")) {
510 if (git_config_bool(k, v))
511 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
513 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
515 } else if (!strcmp(k, "push.gpgsign")) {
517 if (!git_config_get_value("push.gpgsign", &value)) {
518 switch (git_config_maybe_bool("push.gpgsign", value)) {
520 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
523 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
526 if (value && !strcasecmp(value, "if-asked"))
527 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
529 return error("Invalid value for '%s'", k);
534 return git_default_config(k, v, NULL);
537 int cmd_push(int argc, const char **argv, const char *prefix)
543 const char *repo = NULL; /* default repository */
544 struct option options[] = {
545 OPT__VERBOSITY(&verbosity),
546 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
547 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
548 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
549 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
550 OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
551 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
552 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
553 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
554 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
556 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
557 N_("require old value of ref to be at this value"),
558 PARSE_OPT_OPTARG, parseopt_push_cas_option },
559 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check|on-demand",
560 N_("control recursive pushing of submodules"),
561 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
562 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
563 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
564 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
565 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
566 TRANSPORT_PUSH_SET_UPSTREAM),
567 OPT_BIT('p', "set-publish", &flags, N_("set publish for git pull/status"),
568 TRANSPORT_PUSH_SET_PUBLISH),
569 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
570 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
571 TRANSPORT_PUSH_PRUNE),
572 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
573 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
574 TRANSPORT_PUSH_FOLLOW_TAGS),
576 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
577 PARSE_OPT_OPTARG, option_parse_push_signed },
578 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
582 packet_trace_identity("push");
583 git_config(git_push_config, &flags);
584 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
585 set_push_cert_flags(&flags, push_cert);
587 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
588 die(_("--delete is incompatible with --all, --mirror and --tags"));
589 if (deleterefs && argc < 2)
590 die(_("--delete doesn't make sense without any refs"));
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);