travis: add a test for everything
[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
13 static const char * const push_usage[] = {
14         N_("git push [<options>] [<repository> [<refspec>...]]"),
15         NULL,
16 };
17
18 static int thin = 1;
19 static int deleterefs;
20 static const char *receivepack;
21 static int verbosity;
22 static int progress = -1;
23
24 static struct push_cas_option cas;
25
26 static const char **refspec;
27 static int refspec_nr;
28 static int refspec_alloc;
29 static int default_matching_used;
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             !prefixcmp(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 =
132                 skip_prefix(branch->merge[0]->src, "refs/heads/");
133
134         if (!short_upstream)
135                 short_upstream = branch->merge[0]->src;
136         /*
137          * Don't show advice for people who explicitly set
138          * push.default.
139          */
140         if (push_default == PUSH_DEFAULT_UNSPECIFIED)
141                 advice_maybe = _("\n"
142                                  "To choose either option permanently, "
143                                  "see push.default in 'git help config'.");
144         die(_("The upstream branch of your current branch does not match\n"
145               "the name of your current branch.  To push to the upstream branch\n"
146               "on the remote, use\n"
147               "\n"
148               "    git push %s HEAD:%s\n"
149               "\n"
150               "To push to the branch of the same name on the remote, use\n"
151               "\n"
152               "    git push %s %s\n"
153               "%s"),
154             remote->name, short_upstream,
155             remote->name, branch->name, advice_maybe);
156 }
157
158 static void setup_push_upstream(struct remote *remote, struct branch *branch,
159                                 int triangular)
160 {
161         struct strbuf refspec = STRBUF_INIT;
162
163         if (!branch->merge_nr || !branch->merge || !branch->remote_name)
164                 die(_("The current branch %s has no upstream branch.\n"
165                     "To push the current branch and set the remote as upstream, use\n"
166                     "\n"
167                     "    git push --set-upstream %s %s\n"),
168                     branch->name,
169                     remote->name,
170                     branch->name);
171         if (branch->merge_nr != 1)
172                 die(_("The current branch %s has multiple upstream branches, "
173                     "refusing to push."), branch->name);
174         if (triangular)
175                 die(_("You are pushing to remote '%s', which is not the upstream of\n"
176                       "your current branch '%s', without telling me what to push\n"
177                       "to update which remote branch."),
178                     remote->name, branch->name);
179
180         if (push_default == PUSH_DEFAULT_SIMPLE) {
181                 /* Additional safety */
182                 if (strcmp(branch->refname, branch->merge[0]->src))
183                         die_push_simple(branch, remote);
184         }
185
186         strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
187         add_refspec(refspec.buf);
188 }
189
190 static void setup_push_current(struct remote *remote, struct branch *branch)
191 {
192         add_refspec(branch->name);
193 }
194
195 static void setup_push_simple(struct remote *remote, struct branch *branch,
196                 int triangular)
197 {
198         if (branch->push_name) {
199                 struct strbuf refspec = STRBUF_INIT;
200                 strbuf_addf(&refspec, "%s:%s", branch->name, branch->push_name);
201                 add_refspec(refspec.buf);
202         } else if (triangular) {
203                 setup_push_current(remote, branch);
204         } else {
205                 setup_push_upstream(remote, branch, triangular);
206         }
207 }
208
209 static char warn_unspecified_push_default_msg[] =
210 N_("push.default is unset; its implicit value is changing in\n"
211    "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
212    "and maintain the current behavior after the default changes, use:\n"
213    "\n"
214    "  git config --global push.default matching\n"
215    "\n"
216    "To squelch this message and adopt the new behavior now, use:\n"
217    "\n"
218    "  git config --global push.default simple\n"
219    "\n"
220    "When push.default is set to 'matching', git will push local branches\n"
221    "to the remote branches that already exist with the same name.\n"
222    "\n"
223    "In Git 2.0, Git will default to the more conservative 'simple'\n"
224    "behavior, which only pushes the current branch to the corresponding\n"
225    "remote branch that 'git pull' uses to update the current branch.\n"
226    "\n"
227    "See 'git help config' and search for 'push.default' for further information.\n"
228    "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
229    "'current' instead of 'simple' if you sometimes use older versions of Git)");
230
231 static void warn_unspecified_push_default_configuration(void)
232 {
233         static int warn_once;
234
235         if (warn_once++)
236                 return;
237         warning("%s\n", _(warn_unspecified_push_default_msg));
238 }
239
240 static int is_workflow_triangular(struct remote *remote)
241 {
242         struct remote *fetch_remote = remote_get(NULL);
243         return (fetch_remote && fetch_remote != remote);
244 }
245
246 static const char message_detached_head_die[] =
247         N_("You are not currently on a branch.\n"
248            "To push the history leading to the current (detached HEAD)\n"
249            "state now, use\n"
250            "\n"
251            "    git push %s HEAD:<name-of-remote-branch>\n");
252
253 static struct branch *get_current_branch(struct remote *remote)
254 {
255         struct branch *branch = branch_get(NULL);
256         if (!branch)
257                 die(_(message_detached_head_die), remote->name);
258         return branch;
259 }
260
261 static void setup_default_push_refspecs(struct remote *remote)
262 {
263         int triangular = is_workflow_triangular(remote);
264
265         switch (push_default) {
266         default:
267         case PUSH_DEFAULT_UNSPECIFIED:
268                 default_matching_used = 1;
269                 warn_unspecified_push_default_configuration();
270                 /* fallthru */
271         case PUSH_DEFAULT_MATCHING:
272                 add_refspec(":");
273                 break;
274
275         case PUSH_DEFAULT_SIMPLE:
276                 setup_push_simple(remote, get_current_branch(remote), triangular);
277                 break;
278
279         case PUSH_DEFAULT_UPSTREAM:
280                 setup_push_upstream(remote, get_current_branch(remote), triangular);
281                 break;
282
283         case PUSH_DEFAULT_CURRENT:
284                 setup_push_current(remote, get_current_branch(remote));
285                 break;
286
287         case PUSH_DEFAULT_NOTHING:
288                 die(_("You didn't specify any refspecs to push, and "
289                     "push.default is \"nothing\"."));
290                 break;
291         }
292 }
293
294 static const char message_advice_pull_before_push[] =
295         N_("Updates were rejected because the tip of your current branch is behind\n"
296            "its remote counterpart. Integrate the remote changes (e.g.\n"
297            "'git pull ...') before pushing again.\n"
298            "See the 'Note about fast-forwards' in 'git push --help' for details.");
299
300 static const char message_advice_use_upstream[] =
301         N_("Updates were rejected because a pushed branch tip is behind its remote\n"
302            "counterpart. If you did not intend to push that branch, you may want to\n"
303            "specify branches to push or set the 'push.default' configuration variable\n"
304            "to 'simple', 'current' or 'upstream' to push only the current branch.");
305
306 static const char message_advice_checkout_pull_push[] =
307         N_("Updates were rejected because a pushed branch tip is behind its remote\n"
308            "counterpart. Check out this branch and integrate the remote changes\n"
309            "(e.g. 'git pull ...') before pushing again.\n"
310            "See the 'Note about fast-forwards' in 'git push --help' for details.");
311
312 static const char message_advice_ref_fetch_first[] =
313         N_("Updates were rejected because the remote contains work that you do\n"
314            "not have locally. This is usually caused by another repository pushing\n"
315            "to the same ref. You may want to first integrate the remote changes\n"
316            "(e.g., 'git pull ...') before pushing again.\n"
317            "See the 'Note about fast-forwards' in 'git push --help' for details.");
318
319 static const char message_advice_ref_already_exists[] =
320         N_("Updates were rejected because the tag already exists in the remote.");
321
322 static const char message_advice_ref_needs_force[] =
323         N_("You cannot update a remote ref that points at a non-commit object,\n"
324            "or update a remote ref to make it point at a non-commit object,\n"
325            "without using the '--force' option.\n");
326
327 static void advise_pull_before_push(void)
328 {
329         if (!advice_push_non_ff_current || !advice_push_update_rejected)
330                 return;
331         advise(_(message_advice_pull_before_push));
332 }
333
334 static void advise_use_upstream(void)
335 {
336         if (!advice_push_non_ff_default || !advice_push_update_rejected)
337                 return;
338         advise(_(message_advice_use_upstream));
339 }
340
341 static void advise_checkout_pull_push(void)
342 {
343         if (!advice_push_non_ff_matching || !advice_push_update_rejected)
344                 return;
345         advise(_(message_advice_checkout_pull_push));
346 }
347
348 static void advise_ref_already_exists(void)
349 {
350         if (!advice_push_already_exists || !advice_push_update_rejected)
351                 return;
352         advise(_(message_advice_ref_already_exists));
353 }
354
355 static void advise_ref_fetch_first(void)
356 {
357         if (!advice_push_fetch_first || !advice_push_update_rejected)
358                 return;
359         advise(_(message_advice_ref_fetch_first));
360 }
361
362 static void advise_ref_needs_force(void)
363 {
364         if (!advice_push_needs_force || !advice_push_update_rejected)
365                 return;
366         advise(_(message_advice_ref_needs_force));
367 }
368
369 static int push_with_options(struct transport *transport, int flags)
370 {
371         int err;
372         unsigned int reject_reasons;
373
374         transport_set_verbosity(transport, verbosity, progress);
375
376         if (receivepack)
377                 transport_set_option(transport,
378                                      TRANS_OPT_RECEIVEPACK, receivepack);
379         transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
380
381         if (!is_empty_cas(&cas)) {
382                 if (!transport->smart_options)
383                         die("underlying transport does not support --%s option",
384                             CAS_OPT_NAME);
385                 transport->smart_options->cas = &cas;
386         }
387
388         if (verbosity > 0)
389                 fprintf(stderr, _("Pushing to %s\n"), transport->url);
390         err = transport_push(transport, refspec_nr, refspec, flags,
391                              &reject_reasons);
392         if (err != 0)
393                 error(_("failed to push some refs to '%s'"), transport->url);
394
395         err |= transport_disconnect(transport);
396         if (!err)
397                 return 0;
398
399         if (reject_reasons & REJECT_NON_FF_HEAD) {
400                 advise_pull_before_push();
401         } else if (reject_reasons & REJECT_NON_FF_OTHER) {
402                 if (default_matching_used)
403                         advise_use_upstream();
404                 else
405                         advise_checkout_pull_push();
406         } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
407                 advise_ref_already_exists();
408         } else if (reject_reasons & REJECT_FETCH_FIRST) {
409                 advise_ref_fetch_first();
410         } else if (reject_reasons & REJECT_NEEDS_FORCE) {
411                 advise_ref_needs_force();
412         }
413
414         return 1;
415 }
416
417 static int do_push(const char *repo, int flags)
418 {
419         int i, errs;
420         struct remote *remote = pushremote_get(repo);
421         const char **url;
422         int url_nr;
423
424         if (!remote) {
425                 if (repo)
426                         die(_("bad repository '%s'"), repo);
427                 die(_("No configured push destination.\n"
428                     "Either specify the URL from the command-line or configure a remote repository using\n"
429                     "\n"
430                     "    git remote add <name> <url>\n"
431                     "\n"
432                     "and then push using the remote name\n"
433                     "\n"
434                     "    git push <name>\n"));
435         }
436
437         if (remote->mirror)
438                 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
439
440         if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
441                 if (!strcmp(*refspec, "refs/tags/*"))
442                         return error(_("--all and --tags are incompatible"));
443                 return error(_("--all can't be combined with refspecs"));
444         }
445
446         if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
447                 if (!strcmp(*refspec, "refs/tags/*"))
448                         return error(_("--mirror and --tags are incompatible"));
449                 return error(_("--mirror can't be combined with refspecs"));
450         }
451
452         if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
453                                 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
454                 return error(_("--all and --mirror are incompatible"));
455         }
456
457         if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
458                 if (remote->push_refspec_nr) {
459                         refspec = remote->push_refspec;
460                         refspec_nr = remote->push_refspec_nr;
461                 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
462                         setup_default_push_refspecs(remote);
463         }
464         errs = 0;
465         url_nr = push_url_of_remote(remote, &url);
466         if (url_nr) {
467                 for (i = 0; i < url_nr; i++) {
468                         struct transport *transport =
469                                 transport_get(remote, url[i]);
470                         if (push_with_options(transport, flags))
471                                 errs++;
472                 }
473         } else {
474                 struct transport *transport =
475                         transport_get(remote, NULL);
476
477                 if (push_with_options(transport, flags))
478                         errs++;
479         }
480         return !!errs;
481 }
482
483 static int option_parse_recurse_submodules(const struct option *opt,
484                                    const char *arg, int unset)
485 {
486         int *flags = opt->value;
487
488         if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
489                       TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
490                 die("%s can only be used once.", opt->long_name);
491
492         if (arg) {
493                 if (!strcmp(arg, "check"))
494                         *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
495                 else if (!strcmp(arg, "on-demand"))
496                         *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
497                 else
498                         die("bad %s argument: %s", opt->long_name, arg);
499         } else
500                 die("option %s needs an argument (check|on-demand)",
501                                 opt->long_name);
502
503         return 0;
504 }
505
506 int cmd_push(int argc, const char **argv, const char *prefix)
507 {
508         int flags = 0;
509         int tags = 0;
510         int rc;
511         const char *repo = NULL;        /* default repository */
512         struct option options[] = {
513                 OPT__VERBOSITY(&verbosity),
514                 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
515                 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
516                 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
517                             (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
518                 OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
519                 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
520                 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
521                 OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
522                 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
523                 { OPTION_CALLBACK,
524                   0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
525                   N_("require old value of ref to be at this value"),
526                   PARSE_OPT_OPTARG, parseopt_push_cas_option },
527                 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
528                         N_("control recursive pushing of submodules"),
529                         PARSE_OPT_OPTARG, option_parse_recurse_submodules },
530                 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
531                 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
532                 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
533                 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
534                         TRANSPORT_PUSH_SET_UPSTREAM),
535                 OPT_BIT('p', "set-publish", &flags, N_("set publish for git pull/status"),
536                         TRANSPORT_PUSH_SET_PUBLISH),
537                 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
538                 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
539                         TRANSPORT_PUSH_PRUNE),
540                 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
541                 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
542                         TRANSPORT_PUSH_FOLLOW_TAGS),
543                 OPT_END()
544         };
545
546         packet_trace_identity("push");
547         git_config(git_default_config, NULL);
548         argc = parse_options(argc, argv, prefix, options, push_usage, 0);
549
550         if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
551                 die(_("--delete is incompatible with --all, --mirror and --tags"));
552         if (deleterefs && argc < 2)
553                 die(_("--delete doesn't make sense without any refs"));
554
555         if (tags)
556                 add_refspec("refs/tags/*");
557
558         if (argc > 0) {
559                 repo = argv[0];
560                 set_refspecs(argv + 1, argc - 1, repo);
561         }
562
563         rc = do_push(repo, flags);
564         if (rc == -1)
565                 usage_with_options(push_usage, options);
566         else
567                 return rc;
568 }