Merge branch 'jc/plug-fmt-merge-msg-leak' into maint
[git/gitweb.git] / builtin / push.c
blob57c138bd7bc972bfa337de4c615cbfffb99ecc71
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"
13 static const char * const push_usage[] = {
14 N_("git push [<options>] [<repository> [<refspec>...]]"),
15 NULL,
18 static int thin = 1;
19 static int deleterefs;
20 static const char *receivepack;
21 static int verbosity;
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;
30 static void add_refspec(const char *ref)
32 refspec_nr++;
33 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
34 refspec[refspec_nr-1] = ref;
37 static const char *map_refspec(const char *ref,
38 struct remote *remote, struct ref *local_refs)
40 struct ref *matched = NULL;
42 /* Does "ref" uniquely name our ref? */
43 if (count_refspec_match(ref, local_refs, &matched) != 1)
44 return ref;
46 if (remote->push) {
47 struct refspec query;
48 memset(&query, 0, sizeof(struct refspec));
49 query.src = matched->name;
50 if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
51 query.dst) {
52 struct strbuf buf = STRBUF_INIT;
53 strbuf_addf(&buf, "%s%s:%s",
54 query.force ? "+" : "",
55 query.src, query.dst);
56 return strbuf_detach(&buf, NULL);
60 if (push_default == PUSH_DEFAULT_UPSTREAM &&
61 starts_with(matched->name, "refs/heads/")) {
62 struct branch *branch = branch_get(matched->name + 11);
63 if (branch->merge_nr == 1 && branch->merge[0]->src) {
64 struct strbuf buf = STRBUF_INIT;
65 strbuf_addf(&buf, "%s:%s",
66 ref, branch->merge[0]->src);
67 return strbuf_detach(&buf, NULL);
71 return ref;
74 static void set_refspecs(const char **refs, int nr, const char *repo)
76 struct remote *remote = NULL;
77 struct ref *local_refs = NULL;
78 int i;
80 for (i = 0; i < nr; i++) {
81 const char *ref = refs[i];
82 if (!strcmp("tag", ref)) {
83 struct strbuf tagref = STRBUF_INIT;
84 if (nr <= ++i)
85 die(_("tag shorthand without <tag>"));
86 ref = refs[i];
87 if (deleterefs)
88 strbuf_addf(&tagref, ":refs/tags/%s", ref);
89 else
90 strbuf_addf(&tagref, "refs/tags/%s", ref);
91 ref = strbuf_detach(&tagref, NULL);
92 } else if (deleterefs) {
93 struct strbuf delref = STRBUF_INIT;
94 if (strchr(ref, ':'))
95 die(_("--delete only accepts plain target ref names"));
96 strbuf_addf(&delref, ":%s", ref);
97 ref = strbuf_detach(&delref, NULL);
98 } else if (!strchr(ref, ':')) {
99 if (!remote) {
100 /* lazily grab remote and local_refs */
101 remote = remote_get(repo);
102 local_refs = get_local_heads();
104 ref = map_refspec(ref, remote, local_refs);
106 add_refspec(ref);
110 static int push_url_of_remote(struct remote *remote, const char ***url_p)
112 if (remote->pushurl_nr) {
113 *url_p = remote->pushurl;
114 return remote->pushurl_nr;
116 *url_p = remote->url;
117 return remote->url_nr;
120 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
122 * There's no point in using shorten_unambiguous_ref here,
123 * as the ambiguity would be on the remote side, not what
124 * we have locally. Plus, this is supposed to be the simple
125 * mode. If the user is doing something crazy like setting
126 * upstream to a non-branch, we should probably be showing
127 * them the big ugly fully qualified ref.
129 const char *advice_maybe = "";
130 const char *short_upstream = branch->merge[0]->src;
132 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
135 * Don't show advice for people who explicitly set
136 * push.default.
138 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
139 advice_maybe = _("\n"
140 "To choose either option permanently, "
141 "see push.default in 'git help config'.");
142 die(_("The upstream branch of your current branch does not match\n"
143 "the name of your current branch. To push to the upstream branch\n"
144 "on the remote, use\n"
145 "\n"
146 " git push %s HEAD:%s\n"
147 "\n"
148 "To push to the branch of the same name on the remote, use\n"
149 "\n"
150 " git push %s %s\n"
151 "%s"),
152 remote->name, short_upstream,
153 remote->name, branch->name, advice_maybe);
156 static const char message_detached_head_die[] =
157 N_("You are not currently on a branch.\n"
158 "To push the history leading to the current (detached HEAD)\n"
159 "state now, use\n"
160 "\n"
161 " git push %s HEAD:<name-of-remote-branch>\n");
163 static void setup_push_upstream(struct remote *remote, struct branch *branch,
164 int triangular, int simple)
166 struct strbuf refspec = STRBUF_INIT;
168 if (!branch)
169 die(_(message_detached_head_die), remote->name);
170 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
171 die(_("The current branch %s has no upstream branch.\n"
172 "To push the current branch and set the remote as upstream, use\n"
173 "\n"
174 " git push --set-upstream %s %s\n"),
175 branch->name,
176 remote->name,
177 branch->name);
178 if (branch->merge_nr != 1)
179 die(_("The current branch %s has multiple upstream branches, "
180 "refusing to push."), branch->name);
181 if (triangular)
182 die(_("You are pushing to remote '%s', which is not the upstream of\n"
183 "your current branch '%s', without telling me what to push\n"
184 "to update which remote branch."),
185 remote->name, branch->name);
187 if (simple) {
188 /* Additional safety */
189 if (strcmp(branch->refname, branch->merge[0]->src))
190 die_push_simple(branch, remote);
193 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
194 add_refspec(refspec.buf);
197 static void setup_push_current(struct remote *remote, struct branch *branch)
199 if (!branch)
200 die(_(message_detached_head_die), remote->name);
201 add_refspec(branch->name);
204 static char warn_unspecified_push_default_msg[] =
205 N_("push.default is unset; its implicit value has changed in\n"
206 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
207 "and maintain the traditional behavior, use:\n"
208 "\n"
209 " git config --global push.default matching\n"
210 "\n"
211 "To squelch this message and adopt the new behavior now, use:\n"
212 "\n"
213 " git config --global push.default simple\n"
214 "\n"
215 "When push.default is set to 'matching', git will push local branches\n"
216 "to the remote branches that already exist with the same name.\n"
217 "\n"
218 "Since Git 2.0, Git defaults to the more conservative 'simple'\n"
219 "behavior, which only pushes the current branch to the corresponding\n"
220 "remote branch that 'git pull' uses to update the current branch.\n"
221 "\n"
222 "See 'git help config' and search for 'push.default' for further information.\n"
223 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
224 "'current' instead of 'simple' if you sometimes use older versions of Git)");
226 static void warn_unspecified_push_default_configuration(void)
228 static int warn_once;
230 if (warn_once++)
231 return;
232 warning("%s\n", _(warn_unspecified_push_default_msg));
235 static int is_workflow_triangular(struct remote *remote)
237 struct remote *fetch_remote = remote_get(NULL);
238 return (fetch_remote && fetch_remote != remote);
241 static void setup_default_push_refspecs(struct remote *remote)
243 struct branch *branch = branch_get(NULL);
244 int triangular = is_workflow_triangular(remote);
246 switch (push_default) {
247 default:
248 case PUSH_DEFAULT_MATCHING:
249 add_refspec(":");
250 break;
252 case PUSH_DEFAULT_UNSPECIFIED:
253 warn_unspecified_push_default_configuration();
254 /* fallthru */
256 case PUSH_DEFAULT_SIMPLE:
257 if (triangular)
258 setup_push_current(remote, branch);
259 else
260 setup_push_upstream(remote, branch, triangular, 1);
261 break;
263 case PUSH_DEFAULT_UPSTREAM:
264 setup_push_upstream(remote, branch, triangular, 0);
265 break;
267 case PUSH_DEFAULT_CURRENT:
268 setup_push_current(remote, branch);
269 break;
271 case PUSH_DEFAULT_NOTHING:
272 die(_("You didn't specify any refspecs to push, and "
273 "push.default is \"nothing\"."));
274 break;
278 static const char message_advice_pull_before_push[] =
279 N_("Updates were rejected because the tip of your current branch is behind\n"
280 "its remote counterpart. Integrate the remote changes (e.g.\n"
281 "'git pull ...') before pushing again.\n"
282 "See the 'Note about fast-forwards' in 'git push --help' for details.");
284 static const char message_advice_checkout_pull_push[] =
285 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
286 "counterpart. Check out this branch and integrate the remote changes\n"
287 "(e.g. 'git pull ...') before pushing again.\n"
288 "See the 'Note about fast-forwards' in 'git push --help' for details.");
290 static const char message_advice_ref_fetch_first[] =
291 N_("Updates were rejected because the remote contains work that you do\n"
292 "not have locally. This is usually caused by another repository pushing\n"
293 "to the same ref. You may want to first integrate the remote changes\n"
294 "(e.g., 'git pull ...') before pushing again.\n"
295 "See the 'Note about fast-forwards' in 'git push --help' for details.");
297 static const char message_advice_ref_already_exists[] =
298 N_("Updates were rejected because the tag already exists in the remote.");
300 static const char message_advice_ref_needs_force[] =
301 N_("You cannot update a remote ref that points at a non-commit object,\n"
302 "or update a remote ref to make it point at a non-commit object,\n"
303 "without using the '--force' option.\n");
305 static void advise_pull_before_push(void)
307 if (!advice_push_non_ff_current || !advice_push_update_rejected)
308 return;
309 advise(_(message_advice_pull_before_push));
312 static void advise_checkout_pull_push(void)
314 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
315 return;
316 advise(_(message_advice_checkout_pull_push));
319 static void advise_ref_already_exists(void)
321 if (!advice_push_already_exists || !advice_push_update_rejected)
322 return;
323 advise(_(message_advice_ref_already_exists));
326 static void advise_ref_fetch_first(void)
328 if (!advice_push_fetch_first || !advice_push_update_rejected)
329 return;
330 advise(_(message_advice_ref_fetch_first));
333 static void advise_ref_needs_force(void)
335 if (!advice_push_needs_force || !advice_push_update_rejected)
336 return;
337 advise(_(message_advice_ref_needs_force));
340 static int push_with_options(struct transport *transport, int flags)
342 int err;
343 unsigned int reject_reasons;
345 transport_set_verbosity(transport, verbosity, progress);
347 if (receivepack)
348 transport_set_option(transport,
349 TRANS_OPT_RECEIVEPACK, receivepack);
350 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
352 if (!is_empty_cas(&cas)) {
353 if (!transport->smart_options)
354 die("underlying transport does not support --%s option",
355 CAS_OPT_NAME);
356 transport->smart_options->cas = &cas;
359 if (verbosity > 0)
360 fprintf(stderr, _("Pushing to %s\n"), transport->url);
361 err = transport_push(transport, refspec_nr, refspec, flags,
362 &reject_reasons);
363 if (err != 0)
364 error(_("failed to push some refs to '%s'"), transport->url);
366 err |= transport_disconnect(transport);
367 if (!err)
368 return 0;
370 if (reject_reasons & REJECT_NON_FF_HEAD) {
371 advise_pull_before_push();
372 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
373 advise_checkout_pull_push();
374 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
375 advise_ref_already_exists();
376 } else if (reject_reasons & REJECT_FETCH_FIRST) {
377 advise_ref_fetch_first();
378 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
379 advise_ref_needs_force();
382 return 1;
385 static int do_push(const char *repo, int flags)
387 int i, errs;
388 struct remote *remote = pushremote_get(repo);
389 const char **url;
390 int url_nr;
392 if (!remote) {
393 if (repo)
394 die(_("bad repository '%s'"), repo);
395 die(_("No configured push destination.\n"
396 "Either specify the URL from the command-line or configure a remote repository using\n"
397 "\n"
398 " git remote add <name> <url>\n"
399 "\n"
400 "and then push using the remote name\n"
401 "\n"
402 " git push <name>\n"));
405 if (remote->mirror)
406 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
408 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
409 if (!strcmp(*refspec, "refs/tags/*"))
410 return error(_("--all and --tags are incompatible"));
411 return error(_("--all can't be combined with refspecs"));
414 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
415 if (!strcmp(*refspec, "refs/tags/*"))
416 return error(_("--mirror and --tags are incompatible"));
417 return error(_("--mirror can't be combined with refspecs"));
420 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
421 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
422 return error(_("--all and --mirror are incompatible"));
425 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
426 if (remote->push_refspec_nr) {
427 refspec = remote->push_refspec;
428 refspec_nr = remote->push_refspec_nr;
429 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
430 setup_default_push_refspecs(remote);
432 errs = 0;
433 url_nr = push_url_of_remote(remote, &url);
434 if (url_nr) {
435 for (i = 0; i < url_nr; i++) {
436 struct transport *transport =
437 transport_get(remote, url[i]);
438 if (push_with_options(transport, flags))
439 errs++;
441 } else {
442 struct transport *transport =
443 transport_get(remote, NULL);
445 if (push_with_options(transport, flags))
446 errs++;
448 return !!errs;
451 static int option_parse_recurse_submodules(const struct option *opt,
452 const char *arg, int unset)
454 int *flags = opt->value;
456 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
457 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
458 die("%s can only be used once.", opt->long_name);
460 if (arg) {
461 if (!strcmp(arg, "check"))
462 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
463 else if (!strcmp(arg, "on-demand"))
464 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
465 else
466 die("bad %s argument: %s", opt->long_name, arg);
467 } else
468 die("option %s needs an argument (check|on-demand)",
469 opt->long_name);
471 return 0;
474 static int git_push_config(const char *k, const char *v, void *cb)
476 int *flags = cb;
477 int status;
479 status = git_gpg_config(k, v, NULL);
480 if (status)
481 return status;
483 if (!strcmp(k, "push.followtags")) {
484 if (git_config_bool(k, v))
485 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
486 else
487 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
488 return 0;
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 rc;
499 const char *repo = NULL; /* default repository */
500 struct option options[] = {
501 OPT__VERBOSITY(&verbosity),
502 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
503 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
504 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
505 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
506 OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
507 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
508 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
509 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
510 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
511 { OPTION_CALLBACK,
512 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
513 N_("require old value of ref to be at this value"),
514 PARSE_OPT_OPTARG, parseopt_push_cas_option },
515 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check|on-demand",
516 N_("control recursive pushing of submodules"),
517 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
518 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
519 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
520 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
521 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
522 TRANSPORT_PUSH_SET_UPSTREAM),
523 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
524 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
525 TRANSPORT_PUSH_PRUNE),
526 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
527 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
528 TRANSPORT_PUSH_FOLLOW_TAGS),
529 OPT_BIT(0, "signed", &flags, N_("GPG sign the push"), TRANSPORT_PUSH_CERT),
530 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
531 OPT_END()
534 packet_trace_identity("push");
535 git_config(git_push_config, &flags);
536 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
538 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
539 die(_("--delete is incompatible with --all, --mirror and --tags"));
540 if (deleterefs && argc < 2)
541 die(_("--delete doesn't make sense without any refs"));
543 if (tags)
544 add_refspec("refs/tags/*");
546 if (argc > 0) {
547 repo = argv[0];
548 set_refspecs(argv + 1, argc - 1, repo);
551 rc = do_push(repo, flags);
552 if (rc == -1)
553 usage_with_options(push_usage, options);
554 else
555 return rc;