push: remove "push.default is unset" warning message
[git.git] / builtin / push.c
blob270db40196cfc1e92a2650aedae5b792819bb959
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 "submodule-config.h"
13 #include "send-pack.h"
15 static const char * const push_usage[] = {
16 N_("git push [<options>] [<repository> [<refspec>...]]"),
17 NULL,
20 static int thin = 1;
21 static int deleterefs;
22 static const char *receivepack;
23 static int verbosity;
24 static int progress = -1;
25 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
27 static struct push_cas_option cas;
29 static const char **refspec;
30 static int refspec_nr;
31 static int refspec_alloc;
33 static void add_refspec(const char *ref)
35 refspec_nr++;
36 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
37 refspec[refspec_nr-1] = ref;
40 static const char *map_refspec(const char *ref,
41 struct remote *remote, struct ref *local_refs)
43 struct ref *matched = NULL;
45 /* Does "ref" uniquely name our ref? */
46 if (count_refspec_match(ref, local_refs, &matched) != 1)
47 return ref;
49 if (remote->push) {
50 struct refspec query;
51 memset(&query, 0, sizeof(struct refspec));
52 query.src = matched->name;
53 if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
54 query.dst) {
55 struct strbuf buf = STRBUF_INIT;
56 strbuf_addf(&buf, "%s%s:%s",
57 query.force ? "+" : "",
58 query.src, query.dst);
59 return strbuf_detach(&buf, NULL);
63 if (push_default == PUSH_DEFAULT_UPSTREAM &&
64 starts_with(matched->name, "refs/heads/")) {
65 struct branch *branch = branch_get(matched->name + 11);
66 if (branch->merge_nr == 1 && branch->merge[0]->src) {
67 struct strbuf buf = STRBUF_INIT;
68 strbuf_addf(&buf, "%s:%s",
69 ref, branch->merge[0]->src);
70 return strbuf_detach(&buf, NULL);
74 return ref;
77 static void set_refspecs(const char **refs, int nr, const char *repo)
79 struct remote *remote = NULL;
80 struct ref *local_refs = NULL;
81 int i;
83 for (i = 0; i < nr; i++) {
84 const char *ref = refs[i];
85 if (!strcmp("tag", ref)) {
86 struct strbuf tagref = STRBUF_INIT;
87 if (nr <= ++i)
88 die(_("tag shorthand without <tag>"));
89 ref = refs[i];
90 if (deleterefs)
91 strbuf_addf(&tagref, ":refs/tags/%s", ref);
92 else
93 strbuf_addf(&tagref, "refs/tags/%s", ref);
94 ref = strbuf_detach(&tagref, NULL);
95 } else if (deleterefs) {
96 struct strbuf delref = STRBUF_INIT;
97 if (strchr(ref, ':'))
98 die(_("--delete only accepts plain target ref names"));
99 strbuf_addf(&delref, ":%s", ref);
100 ref = strbuf_detach(&delref, NULL);
101 } else if (!strchr(ref, ':')) {
102 if (!remote) {
103 /* lazily grab remote and local_refs */
104 remote = remote_get(repo);
105 local_refs = get_local_heads();
107 ref = map_refspec(ref, remote, local_refs);
109 add_refspec(ref);
113 static int push_url_of_remote(struct remote *remote, const char ***url_p)
115 if (remote->pushurl_nr) {
116 *url_p = remote->pushurl;
117 return remote->pushurl_nr;
119 *url_p = remote->url;
120 return remote->url_nr;
123 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
125 * There's no point in using shorten_unambiguous_ref here,
126 * as the ambiguity would be on the remote side, not what
127 * we have locally. Plus, this is supposed to be the simple
128 * mode. If the user is doing something crazy like setting
129 * upstream to a non-branch, we should probably be showing
130 * them the big ugly fully qualified ref.
132 const char *advice_maybe = "";
133 const char *short_upstream = branch->merge[0]->src;
135 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
138 * Don't show advice for people who explicitly set
139 * push.default.
141 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
142 advice_maybe = _("\n"
143 "To choose either option permanently, "
144 "see push.default in 'git help config'.");
145 die(_("The upstream branch of your current branch does not match\n"
146 "the name of your current branch. To push to the upstream branch\n"
147 "on the remote, use\n"
148 "\n"
149 " git push %s HEAD:%s\n"
150 "\n"
151 "To push to the branch of the same name on the remote, use\n"
152 "\n"
153 " git push %s %s\n"
154 "%s"),
155 remote->name, short_upstream,
156 remote->name, branch->name, advice_maybe);
159 static const char message_detached_head_die[] =
160 N_("You are not currently on a branch.\n"
161 "To push the history leading to the current (detached HEAD)\n"
162 "state now, use\n"
163 "\n"
164 " git push %s HEAD:<name-of-remote-branch>\n");
166 static void setup_push_upstream(struct remote *remote, struct branch *branch,
167 int triangular, int simple)
169 struct strbuf refspec = STRBUF_INIT;
171 if (!branch)
172 die(_(message_detached_head_die), remote->name);
173 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
174 die(_("The current branch %s has no upstream branch.\n"
175 "To push the current branch and set the remote as upstream, use\n"
176 "\n"
177 " git push --set-upstream %s %s\n"),
178 branch->name,
179 remote->name,
180 branch->name);
181 if (branch->merge_nr != 1)
182 die(_("The current branch %s has multiple upstream branches, "
183 "refusing to push."), branch->name);
184 if (triangular)
185 die(_("You are pushing to remote '%s', which is not the upstream of\n"
186 "your current branch '%s', without telling me what to push\n"
187 "to update which remote branch."),
188 remote->name, branch->name);
190 if (simple) {
191 /* Additional safety */
192 if (strcmp(branch->refname, branch->merge[0]->src))
193 die_push_simple(branch, remote);
196 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
197 add_refspec(refspec.buf);
200 static void setup_push_current(struct remote *remote, struct branch *branch)
202 if (!branch)
203 die(_(message_detached_head_die), remote->name);
204 add_refspec(branch->name);
207 static int is_workflow_triangular(struct remote *remote)
209 struct remote *fetch_remote = remote_get(NULL);
210 return (fetch_remote && fetch_remote != remote);
213 static void setup_default_push_refspecs(struct remote *remote)
215 struct branch *branch = branch_get(NULL);
216 int triangular = is_workflow_triangular(remote);
218 switch (push_default) {
219 default:
220 case PUSH_DEFAULT_MATCHING:
221 add_refspec(":");
222 break;
224 case PUSH_DEFAULT_UNSPECIFIED:
225 case PUSH_DEFAULT_SIMPLE:
226 if (triangular)
227 setup_push_current(remote, branch);
228 else
229 setup_push_upstream(remote, branch, triangular, 1);
230 break;
232 case PUSH_DEFAULT_UPSTREAM:
233 setup_push_upstream(remote, branch, triangular, 0);
234 break;
236 case PUSH_DEFAULT_CURRENT:
237 setup_push_current(remote, branch);
238 break;
240 case PUSH_DEFAULT_NOTHING:
241 die(_("You didn't specify any refspecs to push, and "
242 "push.default is \"nothing\"."));
243 break;
247 static const char message_advice_pull_before_push[] =
248 N_("Updates were rejected because the tip of your current branch is behind\n"
249 "its remote counterpart. Integrate the remote changes (e.g.\n"
250 "'git pull ...') before pushing again.\n"
251 "See the 'Note about fast-forwards' in 'git push --help' for details.");
253 static const char message_advice_checkout_pull_push[] =
254 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
255 "counterpart. Check out this branch and integrate the remote changes\n"
256 "(e.g. 'git pull ...') before pushing again.\n"
257 "See the 'Note about fast-forwards' in 'git push --help' for details.");
259 static const char message_advice_ref_fetch_first[] =
260 N_("Updates were rejected because the remote contains work that you do\n"
261 "not have locally. This is usually caused by another repository pushing\n"
262 "to the same ref. You may want to first integrate the remote changes\n"
263 "(e.g., 'git pull ...') before pushing again.\n"
264 "See the 'Note about fast-forwards' in 'git push --help' for details.");
266 static const char message_advice_ref_already_exists[] =
267 N_("Updates were rejected because the tag already exists in the remote.");
269 static const char message_advice_ref_needs_force[] =
270 N_("You cannot update a remote ref that points at a non-commit object,\n"
271 "or update a remote ref to make it point at a non-commit object,\n"
272 "without using the '--force' option.\n");
274 static void advise_pull_before_push(void)
276 if (!advice_push_non_ff_current || !advice_push_update_rejected)
277 return;
278 advise(_(message_advice_pull_before_push));
281 static void advise_checkout_pull_push(void)
283 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
284 return;
285 advise(_(message_advice_checkout_pull_push));
288 static void advise_ref_already_exists(void)
290 if (!advice_push_already_exists || !advice_push_update_rejected)
291 return;
292 advise(_(message_advice_ref_already_exists));
295 static void advise_ref_fetch_first(void)
297 if (!advice_push_fetch_first || !advice_push_update_rejected)
298 return;
299 advise(_(message_advice_ref_fetch_first));
302 static void advise_ref_needs_force(void)
304 if (!advice_push_needs_force || !advice_push_update_rejected)
305 return;
306 advise(_(message_advice_ref_needs_force));
309 static int push_with_options(struct transport *transport, int flags)
311 int err;
312 unsigned int reject_reasons;
314 transport_set_verbosity(transport, verbosity, progress);
316 if (receivepack)
317 transport_set_option(transport,
318 TRANS_OPT_RECEIVEPACK, receivepack);
319 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
321 if (!is_empty_cas(&cas)) {
322 if (!transport->smart_options)
323 die("underlying transport does not support --%s option",
324 CAS_OPT_NAME);
325 transport->smart_options->cas = &cas;
328 if (verbosity > 0)
329 fprintf(stderr, _("Pushing to %s\n"), transport->url);
330 err = transport_push(transport, refspec_nr, refspec, flags,
331 &reject_reasons);
332 if (err != 0)
333 error(_("failed to push some refs to '%s'"), transport->url);
335 err |= transport_disconnect(transport);
336 if (!err)
337 return 0;
339 if (reject_reasons & REJECT_NON_FF_HEAD) {
340 advise_pull_before_push();
341 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
342 advise_checkout_pull_push();
343 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
344 advise_ref_already_exists();
345 } else if (reject_reasons & REJECT_FETCH_FIRST) {
346 advise_ref_fetch_first();
347 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
348 advise_ref_needs_force();
351 return 1;
354 static int do_push(const char *repo, int flags)
356 int i, errs;
357 struct remote *remote = pushremote_get(repo);
358 const char **url;
359 int url_nr;
361 if (!remote) {
362 if (repo)
363 die(_("bad repository '%s'"), repo);
364 die(_("No configured push destination.\n"
365 "Either specify the URL from the command-line or configure a remote repository using\n"
366 "\n"
367 " git remote add <name> <url>\n"
368 "\n"
369 "and then push using the remote name\n"
370 "\n"
371 " git push <name>\n"));
374 if (remote->mirror)
375 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
377 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
378 if (!strcmp(*refspec, "refs/tags/*"))
379 return error(_("--all and --tags are incompatible"));
380 return error(_("--all can't be combined with refspecs"));
383 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
384 if (!strcmp(*refspec, "refs/tags/*"))
385 return error(_("--mirror and --tags are incompatible"));
386 return error(_("--mirror can't be combined with refspecs"));
389 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
390 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
391 return error(_("--all and --mirror are incompatible"));
394 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
395 if (remote->push_refspec_nr) {
396 refspec = remote->push_refspec;
397 refspec_nr = remote->push_refspec_nr;
398 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
399 setup_default_push_refspecs(remote);
401 errs = 0;
402 url_nr = push_url_of_remote(remote, &url);
403 if (url_nr) {
404 for (i = 0; i < url_nr; i++) {
405 struct transport *transport =
406 transport_get(remote, url[i]);
407 if (push_with_options(transport, flags))
408 errs++;
410 } else {
411 struct transport *transport =
412 transport_get(remote, NULL);
414 if (push_with_options(transport, flags))
415 errs++;
417 return !!errs;
420 static int option_parse_recurse_submodules(const struct option *opt,
421 const char *arg, int unset)
423 int *recurse_submodules = opt->value;
425 if (unset)
426 *recurse_submodules = RECURSE_SUBMODULES_OFF;
427 else if (arg)
428 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
429 else
430 die("%s missing parameter", opt->long_name);
432 return 0;
435 static void set_push_cert_flags(int *flags, int v)
437 switch (v) {
438 case SEND_PACK_PUSH_CERT_NEVER:
439 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
440 break;
441 case SEND_PACK_PUSH_CERT_ALWAYS:
442 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
443 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
444 break;
445 case SEND_PACK_PUSH_CERT_IF_ASKED:
446 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
447 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
448 break;
453 static int git_push_config(const char *k, const char *v, void *cb)
455 int *flags = cb;
456 int status;
458 status = git_gpg_config(k, v, NULL);
459 if (status)
460 return status;
462 if (!strcmp(k, "push.followtags")) {
463 if (git_config_bool(k, v))
464 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
465 else
466 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
467 return 0;
468 } else if (!strcmp(k, "push.gpgsign")) {
469 const char *value;
470 if (!git_config_get_value("push.gpgsign", &value)) {
471 switch (git_config_maybe_bool("push.gpgsign", value)) {
472 case 0:
473 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
474 break;
475 case 1:
476 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
477 break;
478 default:
479 if (value && !strcasecmp(value, "if-asked"))
480 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
481 else
482 return error("Invalid value for '%s'", k);
485 } else if (!strcmp(k, "push.recursesubmodules")) {
486 const char *value;
487 if (!git_config_get_value("push.recursesubmodules", &value))
488 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
491 return git_default_config(k, v, NULL);
494 int cmd_push(int argc, const char **argv, const char *prefix)
496 int flags = 0;
497 int tags = 0;
498 int push_cert = -1;
499 int rc;
500 const char *repo = NULL; /* default repository */
501 struct option options[] = {
502 OPT__VERBOSITY(&verbosity),
503 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
504 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
505 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
506 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
507 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
508 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
509 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
510 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
511 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
512 { OPTION_CALLBACK,
513 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
514 N_("require old value of ref to be at this value"),
515 PARSE_OPT_OPTARG, parseopt_push_cas_option },
516 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "check|on-demand|no",
517 N_("control recursive pushing of submodules"),
518 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
519 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
520 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
521 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
522 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
523 TRANSPORT_PUSH_SET_UPSTREAM),
524 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
525 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
526 TRANSPORT_PUSH_PRUNE),
527 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
528 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
529 TRANSPORT_PUSH_FOLLOW_TAGS),
530 { OPTION_CALLBACK,
531 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
532 PARSE_OPT_OPTARG, option_parse_push_signed },
533 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
534 OPT_END()
537 packet_trace_identity("push");
538 git_config(git_push_config, &flags);
539 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
540 set_push_cert_flags(&flags, push_cert);
542 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
543 die(_("--delete is incompatible with --all, --mirror and --tags"));
544 if (deleterefs && argc < 2)
545 die(_("--delete doesn't make sense without any refs"));
547 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
548 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
549 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
550 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
552 if (tags)
553 add_refspec("refs/tags/*");
555 if (argc > 0) {
556 repo = argv[0];
557 set_refspecs(argv + 1, argc - 1, repo);
560 rc = do_push(repo, flags);
561 if (rc == -1)
562 usage_with_options(push_usage, options);
563 else
564 return rc;