Merge branch 'ls/p4-keep-empty-commits'
[git/git-svn.git] / builtin / push.c
blob3bda430b6b1cf455986afae537ee8429dae5a001
1 /*
2 * "git push"
3 */
4 #include "cache.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "builtin.h"
8 #include "remote.h"
9 #include "transport.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>...]]"),
16 NULL,
19 static int thin = 1;
20 static int deleterefs;
21 static const char *receivepack;
22 static int verbosity;
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)
33 refspec_nr++;
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)
45 return ref;
47 if (remote->push) {
48 struct refspec query;
49 memset(&query, 0, sizeof(struct refspec));
50 query.src = matched->name;
51 if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
52 query.dst) {
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);
72 return ref;
75 static void set_refspecs(const char **refs, int nr, const char *repo)
77 struct remote *remote = NULL;
78 struct ref *local_refs = NULL;
79 int i;
81 for (i = 0; i < nr; i++) {
82 const char *ref = refs[i];
83 if (!strcmp("tag", ref)) {
84 struct strbuf tagref = STRBUF_INIT;
85 if (nr <= ++i)
86 die(_("tag shorthand without <tag>"));
87 ref = refs[i];
88 if (deleterefs)
89 strbuf_addf(&tagref, ":refs/tags/%s", ref);
90 else
91 strbuf_addf(&tagref, "refs/tags/%s", ref);
92 ref = strbuf_detach(&tagref, NULL);
93 } else if (deleterefs) {
94 struct strbuf delref = STRBUF_INIT;
95 if (strchr(ref, ':'))
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, ':')) {
100 if (!remote) {
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);
107 add_refspec(ref);
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
137 * push.default.
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"
146 "\n"
147 " git push %s HEAD:%s\n"
148 "\n"
149 "To push to the branch of the same name on the remote, use\n"
150 "\n"
151 " git push %s %s\n"
152 "%s"),
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"
160 "state now, use\n"
161 "\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;
169 if (!branch)
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"
174 "\n"
175 " git push --set-upstream %s %s\n"),
176 branch->name,
177 remote->name,
178 branch->name);
179 if (branch->merge_nr != 1)
180 die(_("The current branch %s has multiple upstream branches, "
181 "refusing to push."), branch->name);
182 if (triangular)
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);
188 if (simple) {
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)
200 if (!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"
209 "\n"
210 " git config --global push.default matching\n"
211 "\n"
212 "To squelch this message and adopt the new behavior now, use:\n"
213 "\n"
214 " git config --global push.default simple\n"
215 "\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"
218 "\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"
222 "\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;
231 if (warn_once++)
232 return;
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) {
248 default:
249 case PUSH_DEFAULT_MATCHING:
250 add_refspec(":");
251 break;
253 case PUSH_DEFAULT_UNSPECIFIED:
254 warn_unspecified_push_default_configuration();
255 /* fallthru */
257 case PUSH_DEFAULT_SIMPLE:
258 if (triangular)
259 setup_push_current(remote, branch);
260 else
261 setup_push_upstream(remote, branch, triangular, 1);
262 break;
264 case PUSH_DEFAULT_UPSTREAM:
265 setup_push_upstream(remote, branch, triangular, 0);
266 break;
268 case PUSH_DEFAULT_CURRENT:
269 setup_push_current(remote, branch);
270 break;
272 case PUSH_DEFAULT_NOTHING:
273 die(_("You didn't specify any refspecs to push, and "
274 "push.default is \"nothing\"."));
275 break;
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)
309 return;
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)
316 return;
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)
323 return;
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)
330 return;
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)
337 return;
338 advise(_(message_advice_ref_needs_force));
341 static int push_with_options(struct transport *transport, int flags)
343 int err;
344 unsigned int reject_reasons;
346 transport_set_verbosity(transport, verbosity, progress);
348 if (receivepack)
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",
356 CAS_OPT_NAME);
357 transport->smart_options->cas = &cas;
360 if (verbosity > 0)
361 fprintf(stderr, _("Pushing to %s\n"), transport->url);
362 err = transport_push(transport, refspec_nr, refspec, flags,
363 &reject_reasons);
364 if (err != 0)
365 error(_("failed to push some refs to '%s'"), transport->url);
367 err |= transport_disconnect(transport);
368 if (!err)
369 return 0;
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();
383 return 1;
386 static int do_push(const char *repo, int flags)
388 int i, errs;
389 struct remote *remote = pushremote_get(repo);
390 const char **url;
391 int url_nr;
393 if (!remote) {
394 if (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"
398 "\n"
399 " git remote add <name> <url>\n"
400 "\n"
401 "and then push using the remote name\n"
402 "\n"
403 " git push <name>\n"));
406 if (remote->mirror)
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 if (remote->push_refspec_nr) {
428 refspec = remote->push_refspec;
429 refspec_nr = remote->push_refspec_nr;
430 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
431 setup_default_push_refspecs(remote);
433 errs = 0;
434 url_nr = push_url_of_remote(remote, &url);
435 if (url_nr) {
436 for (i = 0; i < url_nr; i++) {
437 struct transport *transport =
438 transport_get(remote, url[i]);
439 if (push_with_options(transport, flags))
440 errs++;
442 } else {
443 struct transport *transport =
444 transport_get(remote, NULL);
446 if (push_with_options(transport, flags))
447 errs++;
449 return !!errs;
452 static int option_parse_recurse_submodules(const struct option *opt,
453 const char *arg, int unset)
455 int *flags = opt->value;
457 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
458 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
459 die("%s can only be used once.", opt->long_name);
461 if (arg) {
462 if (!strcmp(arg, "check"))
463 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
464 else if (!strcmp(arg, "on-demand"))
465 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
466 else
467 die("bad %s argument: %s", opt->long_name, arg);
468 } else
469 die("option %s needs an argument (check|on-demand)",
470 opt->long_name);
472 return 0;
475 static void set_push_cert_flags(int *flags, int v)
477 switch (v) {
478 case SEND_PACK_PUSH_CERT_NEVER:
479 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
480 break;
481 case SEND_PACK_PUSH_CERT_ALWAYS:
482 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
483 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
484 break;
485 case SEND_PACK_PUSH_CERT_IF_ASKED:
486 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
487 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
488 break;
493 static int git_push_config(const char *k, const char *v, void *cb)
495 int *flags = cb;
496 int status;
498 status = git_gpg_config(k, v, NULL);
499 if (status)
500 return status;
502 if (!strcmp(k, "push.followtags")) {
503 if (git_config_bool(k, v))
504 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
505 else
506 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
507 return 0;
508 } else if (!strcmp(k, "push.gpgsign")) {
509 const char *value;
510 if (!git_config_get_value("push.gpgsign", &value)) {
511 switch (git_config_maybe_bool("push.gpgsign", value)) {
512 case 0:
513 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
514 break;
515 case 1:
516 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
517 break;
518 default:
519 if (value && !strcasecmp(value, "if-asked"))
520 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
521 else
522 return error("Invalid value for '%s'", k);
527 return git_default_config(k, v, NULL);
530 int cmd_push(int argc, const char **argv, const char *prefix)
532 int flags = 0;
533 int tags = 0;
534 int push_cert = -1;
535 int rc;
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( 0, "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),
548 { OPTION_CALLBACK,
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", &flags, "check|on-demand",
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),
566 { OPTION_CALLBACK,
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_END()
573 packet_trace_identity("push");
574 git_config(git_push_config, &flags);
575 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
576 set_push_cert_flags(&flags, push_cert);
578 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
579 die(_("--delete is incompatible with --all, --mirror and --tags"));
580 if (deleterefs && argc < 2)
581 die(_("--delete doesn't make sense without any refs"));
583 if (tags)
584 add_refspec("refs/tags/*");
586 if (argc > 0) {
587 repo = argv[0];
588 set_refspecs(argv + 1, argc - 1, repo);
591 rc = do_push(repo, flags);
592 if (rc == -1)
593 usage_with_options(push_usage, options);
594 else
595 return rc;