git config: report when trying to modify a non-existing repo config
[alt-git.git] / builtin / push.c
blob8963dbdf3da816bba595f7c4fcfdfd8bcdcd8670
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 char warn_unspecified_push_default_msg[] =
208 N_("push.default is unset; its implicit value has changed in\n"
209 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
210 "and maintain the traditional behavior, use:\n"
211 "\n"
212 " git config --global push.default matching\n"
213 "\n"
214 "To squelch this message and adopt the new behavior now, use:\n"
215 "\n"
216 " git config --global push.default simple\n"
217 "\n"
218 "When push.default is set to 'matching', git will push local branches\n"
219 "to the remote branches that already exist with the same name.\n"
220 "\n"
221 "Since Git 2.0, Git defaults to the more conservative 'simple'\n"
222 "behavior, which only pushes the current branch to the corresponding\n"
223 "remote branch that 'git pull' uses to update the current branch.\n"
224 "\n"
225 "See 'git help config' and search for 'push.default' for further information.\n"
226 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
227 "'current' instead of 'simple' if you sometimes use older versions of Git)");
229 static void warn_unspecified_push_default_configuration(void)
231 static int warn_once;
233 if (warn_once++)
234 return;
235 warning("%s\n", _(warn_unspecified_push_default_msg));
238 static int is_workflow_triangular(struct remote *remote)
240 struct remote *fetch_remote = remote_get(NULL);
241 return (fetch_remote && fetch_remote != remote);
244 static void setup_default_push_refspecs(struct remote *remote)
246 struct branch *branch = branch_get(NULL);
247 int triangular = is_workflow_triangular(remote);
249 switch (push_default) {
250 default:
251 case PUSH_DEFAULT_MATCHING:
252 add_refspec(":");
253 break;
255 case PUSH_DEFAULT_UNSPECIFIED:
256 warn_unspecified_push_default_configuration();
257 /* fallthru */
259 case PUSH_DEFAULT_SIMPLE:
260 if (triangular)
261 setup_push_current(remote, branch);
262 else
263 setup_push_upstream(remote, branch, triangular, 1);
264 break;
266 case PUSH_DEFAULT_UPSTREAM:
267 setup_push_upstream(remote, branch, triangular, 0);
268 break;
270 case PUSH_DEFAULT_CURRENT:
271 setup_push_current(remote, branch);
272 break;
274 case PUSH_DEFAULT_NOTHING:
275 die(_("You didn't specify any refspecs to push, and "
276 "push.default is \"nothing\"."));
277 break;
281 static const char message_advice_pull_before_push[] =
282 N_("Updates were rejected because the tip of your current branch is behind\n"
283 "its remote counterpart. Integrate the remote changes (e.g.\n"
284 "'git pull ...') before pushing again.\n"
285 "See the 'Note about fast-forwards' in 'git push --help' for details.");
287 static const char message_advice_checkout_pull_push[] =
288 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
289 "counterpart. Check out this branch and integrate the remote changes\n"
290 "(e.g. 'git pull ...') before pushing again.\n"
291 "See the 'Note about fast-forwards' in 'git push --help' for details.");
293 static const char message_advice_ref_fetch_first[] =
294 N_("Updates were rejected because the remote contains work that you do\n"
295 "not have locally. This is usually caused by another repository pushing\n"
296 "to the same ref. You may want to first integrate the remote changes\n"
297 "(e.g., '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_ref_already_exists[] =
301 N_("Updates were rejected because the tag already exists in the remote.");
303 static const char message_advice_ref_needs_force[] =
304 N_("You cannot update a remote ref that points at a non-commit object,\n"
305 "or update a remote ref to make it point at a non-commit object,\n"
306 "without using the '--force' option.\n");
308 static void advise_pull_before_push(void)
310 if (!advice_push_non_ff_current || !advice_push_update_rejected)
311 return;
312 advise(_(message_advice_pull_before_push));
315 static void advise_checkout_pull_push(void)
317 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
318 return;
319 advise(_(message_advice_checkout_pull_push));
322 static void advise_ref_already_exists(void)
324 if (!advice_push_already_exists || !advice_push_update_rejected)
325 return;
326 advise(_(message_advice_ref_already_exists));
329 static void advise_ref_fetch_first(void)
331 if (!advice_push_fetch_first || !advice_push_update_rejected)
332 return;
333 advise(_(message_advice_ref_fetch_first));
336 static void advise_ref_needs_force(void)
338 if (!advice_push_needs_force || !advice_push_update_rejected)
339 return;
340 advise(_(message_advice_ref_needs_force));
343 static int push_with_options(struct transport *transport, int flags)
345 int err;
346 unsigned int reject_reasons;
348 transport_set_verbosity(transport, verbosity, progress);
350 if (receivepack)
351 transport_set_option(transport,
352 TRANS_OPT_RECEIVEPACK, receivepack);
353 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
355 if (!is_empty_cas(&cas)) {
356 if (!transport->smart_options)
357 die("underlying transport does not support --%s option",
358 CAS_OPT_NAME);
359 transport->smart_options->cas = &cas;
362 if (verbosity > 0)
363 fprintf(stderr, _("Pushing to %s\n"), transport->url);
364 err = transport_push(transport, refspec_nr, refspec, flags,
365 &reject_reasons);
366 if (err != 0)
367 error(_("failed to push some refs to '%s'"), transport->url);
369 err |= transport_disconnect(transport);
370 if (!err)
371 return 0;
373 if (reject_reasons & REJECT_NON_FF_HEAD) {
374 advise_pull_before_push();
375 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
376 advise_checkout_pull_push();
377 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
378 advise_ref_already_exists();
379 } else if (reject_reasons & REJECT_FETCH_FIRST) {
380 advise_ref_fetch_first();
381 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
382 advise_ref_needs_force();
385 return 1;
388 static int do_push(const char *repo, int flags)
390 int i, errs;
391 struct remote *remote = pushremote_get(repo);
392 const char **url;
393 int url_nr;
395 if (!remote) {
396 if (repo)
397 die(_("bad repository '%s'"), repo);
398 die(_("No configured push destination.\n"
399 "Either specify the URL from the command-line or configure a remote repository using\n"
400 "\n"
401 " git remote add <name> <url>\n"
402 "\n"
403 "and then push using the remote name\n"
404 "\n"
405 " git push <name>\n"));
408 if (remote->mirror)
409 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
411 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
412 if (!strcmp(*refspec, "refs/tags/*"))
413 return error(_("--all and --tags are incompatible"));
414 return error(_("--all can't be combined with refspecs"));
417 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
418 if (!strcmp(*refspec, "refs/tags/*"))
419 return error(_("--mirror and --tags are incompatible"));
420 return error(_("--mirror can't be combined with refspecs"));
423 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
424 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
425 return error(_("--all and --mirror are incompatible"));
428 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
429 if (remote->push_refspec_nr) {
430 refspec = remote->push_refspec;
431 refspec_nr = remote->push_refspec_nr;
432 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
433 setup_default_push_refspecs(remote);
435 errs = 0;
436 url_nr = push_url_of_remote(remote, &url);
437 if (url_nr) {
438 for (i = 0; i < url_nr; i++) {
439 struct transport *transport =
440 transport_get(remote, url[i]);
441 if (push_with_options(transport, flags))
442 errs++;
444 } else {
445 struct transport *transport =
446 transport_get(remote, NULL);
448 if (push_with_options(transport, flags))
449 errs++;
451 return !!errs;
454 static int option_parse_recurse_submodules(const struct option *opt,
455 const char *arg, int unset)
457 int *recurse_submodules = opt->value;
459 if (unset)
460 *recurse_submodules = RECURSE_SUBMODULES_OFF;
461 else if (arg)
462 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
463 else
464 die("%s missing parameter", opt->long_name);
466 return 0;
469 static void set_push_cert_flags(int *flags, int v)
471 switch (v) {
472 case SEND_PACK_PUSH_CERT_NEVER:
473 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
474 break;
475 case SEND_PACK_PUSH_CERT_ALWAYS:
476 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
477 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
478 break;
479 case SEND_PACK_PUSH_CERT_IF_ASKED:
480 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
481 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
482 break;
487 static int git_push_config(const char *k, const char *v, void *cb)
489 int *flags = cb;
490 int status;
492 status = git_gpg_config(k, v, NULL);
493 if (status)
494 return status;
496 if (!strcmp(k, "push.followtags")) {
497 if (git_config_bool(k, v))
498 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
499 else
500 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
501 return 0;
502 } else if (!strcmp(k, "push.gpgsign")) {
503 const char *value;
504 if (!git_config_get_value("push.gpgsign", &value)) {
505 switch (git_config_maybe_bool("push.gpgsign", value)) {
506 case 0:
507 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
508 break;
509 case 1:
510 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
511 break;
512 default:
513 if (value && !strcasecmp(value, "if-asked"))
514 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
515 else
516 return error("Invalid value for '%s'", k);
519 } else if (!strcmp(k, "push.recursesubmodules")) {
520 const char *value;
521 if (!git_config_get_value("push.recursesubmodules", &value))
522 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
525 return git_default_config(k, v, NULL);
528 int cmd_push(int argc, const char **argv, const char *prefix)
530 int flags = 0;
531 int tags = 0;
532 int push_cert = -1;
533 int rc;
534 const char *repo = NULL; /* default repository */
535 struct option options[] = {
536 OPT__VERBOSITY(&verbosity),
537 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
538 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
539 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
540 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
541 OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
542 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
543 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
544 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
545 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
546 { OPTION_CALLBACK,
547 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
548 N_("require old value of ref to be at this value"),
549 PARSE_OPT_OPTARG, parseopt_push_cas_option },
550 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "check|on-demand|no",
551 N_("control recursive pushing of submodules"),
552 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
553 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
554 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
555 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
556 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
557 TRANSPORT_PUSH_SET_UPSTREAM),
558 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
559 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
560 TRANSPORT_PUSH_PRUNE),
561 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
562 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
563 TRANSPORT_PUSH_FOLLOW_TAGS),
564 { OPTION_CALLBACK,
565 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
566 PARSE_OPT_OPTARG, option_parse_push_signed },
567 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
568 OPT_END()
571 packet_trace_identity("push");
572 git_config(git_push_config, &flags);
573 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
574 set_push_cert_flags(&flags, push_cert);
576 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
577 die(_("--delete is incompatible with --all, --mirror and --tags"));
578 if (deleterefs && argc < 2)
579 die(_("--delete doesn't make sense without any refs"));
581 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
582 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
583 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
584 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
586 if (tags)
587 add_refspec("refs/tags/*");
589 if (argc > 0) {
590 repo = argv[0];
591 set_refspecs(argv + 1, argc - 1, repo);
594 rc = do_push(repo, flags);
595 if (rc == -1)
596 usage_with_options(push_usage, options);
597 else
598 return rc;