6 #include "run-command.h"
10 #include "parse-options.h"
11 #include "submodule.h"
13 static const char * const push_usage[] = {
14 N_("git push [<options>] [<repository> [<refspec>...]]"),
19 static int deleterefs;
20 static const char *receivepack;
22 static int progress = -1;
24 static struct push_cas_option cas;
26 static const char **refspec;
27 static int refspec_nr;
28 static int refspec_alloc;
29 static int default_matching_used;
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 !prefixcmp(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 =
132 skip_prefix(branch->merge[0]->src, "refs/heads/");
135 short_upstream = branch->merge[0]->src;
137 * Don't show advice for people who explicitly set
140 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
141 advice_maybe = _("\n"
142 "To choose either option permanently, "
143 "see push.default in 'git help config'.");
144 die(_("The upstream branch of your current branch does not match\n"
145 "the name of your current branch. To push to the upstream branch\n"
146 "on the remote, use\n"
148 " git push %s HEAD:%s\n"
150 "To push to the branch of the same name on the remote, use\n"
154 remote->name, short_upstream,
155 remote->name, branch->name, advice_maybe);
158 static void setup_push_upstream(struct remote *remote, struct branch *branch,
161 struct strbuf refspec = STRBUF_INIT;
163 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
164 die(_("The current branch %s has no upstream branch.\n"
165 "To push the current branch and set the remote as upstream, use\n"
167 " git push --set-upstream %s %s\n"),
171 if (branch->merge_nr != 1)
172 die(_("The current branch %s has multiple upstream branches, "
173 "refusing to push."), branch->name);
175 die(_("You are pushing to remote '%s', which is not the upstream of\n"
176 "your current branch '%s', without telling me what to push\n"
177 "to update which remote branch."),
178 remote->name, branch->name);
180 if (push_default == PUSH_DEFAULT_SIMPLE) {
181 /* Additional safety */
182 if (strcmp(branch->refname, branch->merge[0]->src))
183 die_push_simple(branch, remote);
186 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
187 add_refspec(refspec.buf);
190 static void setup_push_current(struct remote *remote, struct branch *branch)
192 add_refspec(branch->name);
195 static void setup_push_simple(struct remote *remote, struct branch *branch,
198 if (branch->push_name) {
199 struct strbuf refspec = STRBUF_INIT;
200 strbuf_addf(&refspec, "%s:%s", branch->name, branch->push_name);
201 add_refspec(refspec.buf);
202 } else if (triangular) {
203 setup_push_current(remote, branch);
205 setup_push_upstream(remote, branch, triangular);
209 static char warn_unspecified_push_default_msg[] =
210 N_("push.default is unset; its implicit value is changing in\n"
211 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
212 "and maintain the current behavior after the default changes, use:\n"
214 " git config --global push.default matching\n"
216 "To squelch this message and adopt the new behavior now, use:\n"
218 " git config --global push.default simple\n"
220 "When push.default is set to 'matching', git will push local branches\n"
221 "to the remote branches that already exist with the same name.\n"
223 "In Git 2.0, Git will default to the more conservative 'simple'\n"
224 "behavior, which only pushes the current branch to the corresponding\n"
225 "remote branch that 'git pull' uses to update the current branch.\n"
227 "See 'git help config' and search for 'push.default' for further information.\n"
228 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
229 "'current' instead of 'simple' if you sometimes use older versions of Git)");
231 static void warn_unspecified_push_default_configuration(void)
233 static int warn_once;
237 warning("%s\n", _(warn_unspecified_push_default_msg));
240 static int is_workflow_triangular(struct remote *remote)
242 struct remote *fetch_remote = remote_get(NULL);
243 return (fetch_remote && fetch_remote != remote);
246 static const char message_detached_head_die[] =
247 N_("You are not currently on a branch.\n"
248 "To push the history leading to the current (detached HEAD)\n"
251 " git push %s HEAD:<name-of-remote-branch>\n");
253 static struct branch *get_current_branch(struct remote *remote)
255 struct branch *branch = branch_get(NULL);
257 die(_(message_detached_head_die), remote->name);
261 static void setup_default_push_refspecs(struct remote *remote)
263 int triangular = is_workflow_triangular(remote);
265 switch (push_default) {
267 case PUSH_DEFAULT_UNSPECIFIED:
268 default_matching_used = 1;
269 warn_unspecified_push_default_configuration();
271 case PUSH_DEFAULT_MATCHING:
275 case PUSH_DEFAULT_SIMPLE:
276 setup_push_simple(remote, get_current_branch(remote), triangular);
279 case PUSH_DEFAULT_UPSTREAM:
280 setup_push_upstream(remote, get_current_branch(remote), triangular);
283 case PUSH_DEFAULT_CURRENT:
284 setup_push_current(remote, get_current_branch(remote));
287 case PUSH_DEFAULT_NOTHING:
288 die(_("You didn't specify any refspecs to push, and "
289 "push.default is \"nothing\"."));
294 static const char message_advice_pull_before_push[] =
295 N_("Updates were rejected because the tip of your current branch is behind\n"
296 "its remote counterpart. Integrate the remote changes (e.g.\n"
297 "'git pull ...') before pushing again.\n"
298 "See the 'Note about fast-forwards' in 'git push --help' for details.");
300 static const char message_advice_use_upstream[] =
301 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
302 "counterpart. If you did not intend to push that branch, you may want to\n"
303 "specify branches to push or set the 'push.default' configuration variable\n"
304 "to 'simple', 'current' or 'upstream' to push only the current branch.");
306 static const char message_advice_checkout_pull_push[] =
307 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
308 "counterpart. Check out this branch and integrate the remote changes\n"
309 "(e.g. 'git pull ...') before pushing again.\n"
310 "See the 'Note about fast-forwards' in 'git push --help' for details.");
312 static const char message_advice_ref_fetch_first[] =
313 N_("Updates were rejected because the remote contains work that you do\n"
314 "not have locally. This is usually caused by another repository pushing\n"
315 "to the same ref. You may want to first integrate the remote changes\n"
316 "(e.g., 'git pull ...') before pushing again.\n"
317 "See the 'Note about fast-forwards' in 'git push --help' for details.");
319 static const char message_advice_ref_already_exists[] =
320 N_("Updates were rejected because the tag already exists in the remote.");
322 static const char message_advice_ref_needs_force[] =
323 N_("You cannot update a remote ref that points at a non-commit object,\n"
324 "or update a remote ref to make it point at a non-commit object,\n"
325 "without using the '--force' option.\n");
327 static void advise_pull_before_push(void)
329 if (!advice_push_non_ff_current || !advice_push_update_rejected)
331 advise(_(message_advice_pull_before_push));
334 static void advise_use_upstream(void)
336 if (!advice_push_non_ff_default || !advice_push_update_rejected)
338 advise(_(message_advice_use_upstream));
341 static void advise_checkout_pull_push(void)
343 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
345 advise(_(message_advice_checkout_pull_push));
348 static void advise_ref_already_exists(void)
350 if (!advice_push_already_exists || !advice_push_update_rejected)
352 advise(_(message_advice_ref_already_exists));
355 static void advise_ref_fetch_first(void)
357 if (!advice_push_fetch_first || !advice_push_update_rejected)
359 advise(_(message_advice_ref_fetch_first));
362 static void advise_ref_needs_force(void)
364 if (!advice_push_needs_force || !advice_push_update_rejected)
366 advise(_(message_advice_ref_needs_force));
369 static int push_with_options(struct transport *transport, int flags)
372 unsigned int reject_reasons;
374 transport_set_verbosity(transport, verbosity, progress);
377 transport_set_option(transport,
378 TRANS_OPT_RECEIVEPACK, receivepack);
379 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
381 if (!is_empty_cas(&cas)) {
382 if (!transport->smart_options)
383 die("underlying transport does not support --%s option",
385 transport->smart_options->cas = &cas;
389 fprintf(stderr, _("Pushing to %s\n"), transport->url);
390 err = transport_push(transport, refspec_nr, refspec, flags,
393 error(_("failed to push some refs to '%s'"), transport->url);
395 err |= transport_disconnect(transport);
399 if (reject_reasons & REJECT_NON_FF_HEAD) {
400 advise_pull_before_push();
401 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
402 if (default_matching_used)
403 advise_use_upstream();
405 advise_checkout_pull_push();
406 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
407 advise_ref_already_exists();
408 } else if (reject_reasons & REJECT_FETCH_FIRST) {
409 advise_ref_fetch_first();
410 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
411 advise_ref_needs_force();
417 static int do_push(const char *repo, int flags)
420 struct remote *remote = pushremote_get(repo);
426 die(_("bad repository '%s'"), repo);
427 die(_("No configured push destination.\n"
428 "Either specify the URL from the command-line or configure a remote repository using\n"
430 " git remote add <name> <url>\n"
432 "and then push using the remote name\n"
434 " git push <name>\n"));
438 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
440 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
441 if (!strcmp(*refspec, "refs/tags/*"))
442 return error(_("--all and --tags are incompatible"));
443 return error(_("--all can't be combined with refspecs"));
446 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
447 if (!strcmp(*refspec, "refs/tags/*"))
448 return error(_("--mirror and --tags are incompatible"));
449 return error(_("--mirror can't be combined with refspecs"));
452 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
453 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
454 return error(_("--all and --mirror are incompatible"));
457 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
458 if (remote->push_refspec_nr) {
459 refspec = remote->push_refspec;
460 refspec_nr = remote->push_refspec_nr;
461 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
462 setup_default_push_refspecs(remote);
465 url_nr = push_url_of_remote(remote, &url);
467 for (i = 0; i < url_nr; i++) {
468 struct transport *transport =
469 transport_get(remote, url[i]);
470 if (push_with_options(transport, flags))
474 struct transport *transport =
475 transport_get(remote, NULL);
477 if (push_with_options(transport, flags))
483 static int option_parse_recurse_submodules(const struct option *opt,
484 const char *arg, int unset)
486 int *flags = opt->value;
488 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
489 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
490 die("%s can only be used once.", opt->long_name);
493 if (!strcmp(arg, "check"))
494 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
495 else if (!strcmp(arg, "on-demand"))
496 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
498 die("bad %s argument: %s", opt->long_name, arg);
500 die("option %s needs an argument (check|on-demand)",
506 int cmd_push(int argc, const char **argv, const char *prefix)
511 const char *repo = NULL; /* default repository */
512 struct option options[] = {
513 OPT__VERBOSITY(&verbosity),
514 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
515 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
516 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
517 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
518 OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
519 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
520 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
521 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
522 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
524 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
525 N_("require old value of ref to be at this value"),
526 PARSE_OPT_OPTARG, parseopt_push_cas_option },
527 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
528 N_("control recursive pushing of submodules"),
529 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
530 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
531 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
532 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
533 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
534 TRANSPORT_PUSH_SET_UPSTREAM),
535 OPT_BIT('p', "set-publish", &flags, N_("set publish for git pull/status"),
536 TRANSPORT_PUSH_SET_PUBLISH),
537 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
538 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
539 TRANSPORT_PUSH_PRUNE),
540 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
541 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
542 TRANSPORT_PUSH_FOLLOW_TAGS),
546 packet_trace_identity("push");
547 git_config(git_default_config, NULL);
548 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
550 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
551 die(_("--delete is incompatible with --all, --mirror and --tags"));
552 if (deleterefs && argc < 2)
553 die(_("--delete doesn't make sense without any refs"));
556 add_refspec("refs/tags/*");
560 set_refspecs(argv + 1, argc - 1, repo);
563 rc = do_push(repo, flags);
565 usage_with_options(push_usage, options);