Merge branch 'fc/cherry-pick' into integration
[git] / builtin / push.c
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"
13
14 static const char * const push_usage[] = {
15         N_("git push [<options>] [<repository> [<refspec>...]]"),
16         NULL,
17 };
18
19 static int thin = 1;
20 static int deleterefs;
21 static const char *receivepack;
22 static int verbosity;
23 static int progress = -1;
24
25 static struct push_cas_option cas;
26
27 static const char **refspec;
28 static int refspec_nr;
29 static int refspec_alloc;
30
31 static void add_refspec(const char *ref)
32 {
33         refspec_nr++;
34         ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
35         refspec[refspec_nr-1] = ref;
36 }
37
38 static const char *map_refspec(const char *ref,
39                                struct remote *remote, struct ref *local_refs)
40 {
41         struct ref *matched = NULL;
42
43         /* Does "ref" uniquely name our ref? */
44         if (count_refspec_match(ref, local_refs, &matched) != 1)
45                 return ref;
46
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);
58                 }
59         }
60
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);
69                 }
70         }
71
72         return ref;
73 }
74
75 static void set_refspecs(const char **refs, int nr, const char *repo)
76 {
77         struct remote *remote = NULL;
78         struct ref *local_refs = NULL;
79         int i;
80
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();
104                         }
105                         ref = map_refspec(ref, remote, local_refs);
106                 }
107                 add_refspec(ref);
108         }
109 }
110
111 static int push_url_of_remote(struct remote *remote, const char ***url_p)
112 {
113         if (remote->pushurl_nr) {
114                 *url_p = remote->pushurl;
115                 return remote->pushurl_nr;
116         }
117         *url_p = remote->url;
118         return remote->url_nr;
119 }
120
121 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
122         /*
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.
129          */
130         const char *advice_maybe = "";
131         const char *short_upstream = branch->merge[0]->src;
132
133         skip_prefix(short_upstream, "refs/heads/", &short_upstream);
134
135         /*
136          * Don't show advice for people who explicitly set
137          * push.default.
138          */
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);
155 }
156
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");
163
164 static void setup_push_upstream(struct remote *remote, struct branch *branch,
165                                 int triangular, int simple)
166 {
167         struct strbuf refspec = STRBUF_INIT;
168
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);
187
188         if (simple) {
189                 /* Additional safety */
190                 if (strcmp(branch->refname, branch->merge[0]->src))
191                         die_push_simple(branch, remote);
192         }
193
194         strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
195         add_refspec(refspec.buf);
196 }
197
198 static void setup_push_current(struct remote *remote, struct branch *branch)
199 {
200         if (!branch)
201                 die(_(message_detached_head_die), remote->name);
202         add_refspec(branch->name);
203 }
204
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)");
226
227 static void warn_unspecified_push_default_configuration(void)
228 {
229         static int warn_once;
230
231         if (warn_once++)
232                 return;
233         warning("%s\n", _(warn_unspecified_push_default_msg));
234 }
235
236 static int is_workflow_triangular(struct remote *remote)
237 {
238         struct remote *fetch_remote = remote_get(NULL);
239         return (fetch_remote && fetch_remote != remote);
240 }
241
242 static void setup_default_push_refspecs(struct remote *remote)
243 {
244         struct branch *branch = branch_get(NULL);
245         int triangular = is_workflow_triangular(remote);
246
247         switch (push_default) {
248         default:
249         case PUSH_DEFAULT_MATCHING:
250                 add_refspec(":");
251                 break;
252
253         case PUSH_DEFAULT_UNSPECIFIED:
254                 warn_unspecified_push_default_configuration();
255                 /* fallthru */
256
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;
263
264         case PUSH_DEFAULT_UPSTREAM:
265                 setup_push_upstream(remote, branch, triangular, 0);
266                 break;
267
268         case PUSH_DEFAULT_CURRENT:
269                 setup_push_current(remote, branch);
270                 break;
271
272         case PUSH_DEFAULT_NOTHING:
273                 die(_("You didn't specify any refspecs to push, and "
274                     "push.default is \"nothing\"."));
275                 break;
276         }
277 }
278
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.");
284
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.");
290
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.");
297
298 static const char message_advice_ref_already_exists[] =
299         N_("Updates were rejected because the tag already exists in the remote.");
300
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");
305
306 static void advise_pull_before_push(void)
307 {
308         if (!advice_push_non_ff_current || !advice_push_update_rejected)
309                 return;
310         advise(_(message_advice_pull_before_push));
311 }
312
313 static void advise_checkout_pull_push(void)
314 {
315         if (!advice_push_non_ff_matching || !advice_push_update_rejected)
316                 return;
317         advise(_(message_advice_checkout_pull_push));
318 }
319
320 static void advise_ref_already_exists(void)
321 {
322         if (!advice_push_already_exists || !advice_push_update_rejected)
323                 return;
324         advise(_(message_advice_ref_already_exists));
325 }
326
327 static void advise_ref_fetch_first(void)
328 {
329         if (!advice_push_fetch_first || !advice_push_update_rejected)
330                 return;
331         advise(_(message_advice_ref_fetch_first));
332 }
333
334 static void advise_ref_needs_force(void)
335 {
336         if (!advice_push_needs_force || !advice_push_update_rejected)
337                 return;
338         advise(_(message_advice_ref_needs_force));
339 }
340
341 static int push_with_options(struct transport *transport, int flags)
342 {
343         int err;
344         unsigned int reject_reasons;
345
346         transport_set_verbosity(transport, verbosity, progress);
347
348         if (receivepack)
349                 transport_set_option(transport,
350                                      TRANS_OPT_RECEIVEPACK, receivepack);
351         transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
352
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;
358         }
359
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);
366
367         err |= transport_disconnect(transport);
368         if (!err)
369                 return 0;
370
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();
381         }
382
383         return 1;
384 }
385
386 static int do_push(const char *repo, int flags)
387 {
388         int i, errs;
389         struct remote *remote = pushremote_get(repo);
390         const char **url;
391         int url_nr;
392
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"));
404         }
405
406         if (remote->mirror)
407                 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
408
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"));
413         }
414
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"));
419         }
420
421         if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
422                                 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
423                 return error(_("--all and --mirror are incompatible"));
424         }
425
426         if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
427                 struct branch *branch = branch_get(NULL);
428                 /* Is there a publish branch */
429                 if (branch && branch->pushremote_name && !strcmp(remote->name, branch->pushremote_name) &&
430                                 branch->push_name) {
431                         struct strbuf refspec = STRBUF_INIT;
432                         strbuf_addf(&refspec, "%s:%s", branch->name, branch->push_name);
433                         add_refspec(refspec.buf);
434                 } else if (remote->push_refspec_nr) {
435                         refspec = remote->push_refspec;
436                         refspec_nr = remote->push_refspec_nr;
437                 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
438                         setup_default_push_refspecs(remote);
439         }
440         errs = 0;
441         url_nr = push_url_of_remote(remote, &url);
442         if (url_nr) {
443                 for (i = 0; i < url_nr; i++) {
444                         struct transport *transport =
445                                 transport_get(remote, url[i]);
446                         if (push_with_options(transport, flags))
447                                 errs++;
448                 }
449         } else {
450                 struct transport *transport =
451                         transport_get(remote, NULL);
452
453                 if (push_with_options(transport, flags))
454                         errs++;
455         }
456         return !!errs;
457 }
458
459 static int option_parse_recurse_submodules(const struct option *opt,
460                                    const char *arg, int unset)
461 {
462         int *flags = opt->value;
463
464         if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
465                       TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
466                 die("%s can only be used once.", opt->long_name);
467
468         if (arg) {
469                 if (!strcmp(arg, "check"))
470                         *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
471                 else if (!strcmp(arg, "on-demand"))
472                         *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
473                 else
474                         die("bad %s argument: %s", opt->long_name, arg);
475         } else
476                 die("option %s needs an argument (check|on-demand)",
477                                 opt->long_name);
478
479         return 0;
480 }
481
482 static void set_push_cert_flags(int *flags, int v)
483 {
484         switch (v) {
485         case SEND_PACK_PUSH_CERT_NEVER:
486                 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
487                 break;
488         case SEND_PACK_PUSH_CERT_ALWAYS:
489                 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
490                 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
491                 break;
492         case SEND_PACK_PUSH_CERT_IF_ASKED:
493                 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
494                 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
495                 break;
496         }
497 }
498
499
500 static int git_push_config(const char *k, const char *v, void *cb)
501 {
502         int *flags = cb;
503         int status;
504
505         status = git_gpg_config(k, v, NULL);
506         if (status)
507                 return status;
508
509         if (!strcmp(k, "push.followtags")) {
510                 if (git_config_bool(k, v))
511                         *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
512                 else
513                         *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
514                 return 0;
515         } else if (!strcmp(k, "push.gpgsign")) {
516                 const char *value;
517                 if (!git_config_get_value("push.gpgsign", &value)) {
518                         switch (git_config_maybe_bool("push.gpgsign", value)) {
519                         case 0:
520                                 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
521                                 break;
522                         case 1:
523                                 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
524                                 break;
525                         default:
526                                 if (value && !strcasecmp(value, "if-asked"))
527                                         set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
528                                 else
529                                         return error("Invalid value for '%s'", k);
530                         }
531                 }
532         }
533
534         return git_default_config(k, v, NULL);
535 }
536
537 int cmd_push(int argc, const char **argv, const char *prefix)
538 {
539         int flags = 0;
540         int tags = 0;
541         int push_cert = -1;
542         int rc;
543         const char *repo = NULL;        /* default repository */
544         struct option options[] = {
545                 OPT__VERBOSITY(&verbosity),
546                 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
547                 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
548                 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
549                             (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
550                 OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
551                 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
552                 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
553                 OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
554                 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
555                 { OPTION_CALLBACK,
556                   0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
557                   N_("require old value of ref to be at this value"),
558                   PARSE_OPT_OPTARG, parseopt_push_cas_option },
559                 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check|on-demand",
560                         N_("control recursive pushing of submodules"),
561                         PARSE_OPT_OPTARG, option_parse_recurse_submodules },
562                 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
563                 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
564                 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
565                 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
566                         TRANSPORT_PUSH_SET_UPSTREAM),
567                 OPT_BIT('p', "set-publish", &flags, N_("set publish for git pull/status"),
568                         TRANSPORT_PUSH_SET_PUBLISH),
569                 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
570                 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
571                         TRANSPORT_PUSH_PRUNE),
572                 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
573                 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
574                         TRANSPORT_PUSH_FOLLOW_TAGS),
575                 { OPTION_CALLBACK,
576                   0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
577                   PARSE_OPT_OPTARG, option_parse_push_signed },
578                 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
579                 OPT_END()
580         };
581
582         packet_trace_identity("push");
583         git_config(git_push_config, &flags);
584         argc = parse_options(argc, argv, prefix, options, push_usage, 0);
585         set_push_cert_flags(&flags, push_cert);
586
587         if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
588                 die(_("--delete is incompatible with --all, --mirror and --tags"));
589         if (deleterefs && argc < 2)
590                 die(_("--delete doesn't make sense without any refs"));
591
592         if (tags)
593                 add_refspec("refs/tags/*");
594
595         if (argc > 0) {
596                 repo = argv[0];
597                 set_refspecs(argv + 1, argc - 1, repo);
598         }
599
600         rc = do_push(repo, flags);
601         if (rc == -1)
602                 usage_with_options(push_usage, options);
603         else
604                 return rc;
605 }