Merge branch 'ah/fix-http-push' into maint
[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
30 static void add_refspec(const char *ref)
31 {
32         refspec_nr++;
33         ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
34         refspec[refspec_nr-1] = ref;
35 }
36
37 static const char *map_refspec(const char *ref,
38                                struct remote *remote, struct ref *local_refs)
39 {
40         struct ref *matched = NULL;
41
42         /* Does "ref" uniquely name our ref? */
43         if (count_refspec_match(ref, local_refs, &matched) != 1)
44                 return ref;
45
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);
57                 }
58         }
59
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);
68                 }
69         }
70
71         return ref;
72 }
73
74 static void set_refspecs(const char **refs, int nr, const char *repo)
75 {
76         struct remote *remote = NULL;
77         struct ref *local_refs = NULL;
78         int i;
79
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();
103                         }
104                         ref = map_refspec(ref, remote, local_refs);
105                 }
106                 add_refspec(ref);
107         }
108 }
109
110 static int push_url_of_remote(struct remote *remote, const char ***url_p)
111 {
112         if (remote->pushurl_nr) {
113                 *url_p = remote->pushurl;
114                 return remote->pushurl_nr;
115         }
116         *url_p = remote->url;
117         return remote->url_nr;
118 }
119
120 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
121         /*
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.
128          */
129         const char *advice_maybe = "";
130         const char *short_upstream =
131                 skip_prefix(branch->merge[0]->src, "refs/heads/");
132
133         if (!short_upstream)
134                 short_upstream = branch->merge[0]->src;
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)
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 (push_default == PUSH_DEFAULT_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);
262                 break;
263
264         case PUSH_DEFAULT_UPSTREAM:
265                 setup_push_upstream(remote, branch, triangular);
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                 if (remote->push_refspec_nr) {
428                         refspec = remote->push_refspec;
429                         refspec_nr = remote->push_refspec_nr;
430                 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
431                         setup_default_push_refspecs(remote);
432         }
433         errs = 0;
434         url_nr = push_url_of_remote(remote, &url);
435         if (url_nr) {
436                 for (i = 0; i < url_nr; i++) {
437                         struct transport *transport =
438                                 transport_get(remote, url[i]);
439                         if (push_with_options(transport, flags))
440                                 errs++;
441                 }
442         } else {
443                 struct transport *transport =
444                         transport_get(remote, NULL);
445
446                 if (push_with_options(transport, flags))
447                         errs++;
448         }
449         return !!errs;
450 }
451
452 static int option_parse_recurse_submodules(const struct option *opt,
453                                    const char *arg, int unset)
454 {
455         int *flags = opt->value;
456
457         if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
458                       TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
459                 die("%s can only be used once.", opt->long_name);
460
461         if (arg) {
462                 if (!strcmp(arg, "check"))
463                         *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
464                 else if (!strcmp(arg, "on-demand"))
465                         *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
466                 else
467                         die("bad %s argument: %s", opt->long_name, arg);
468         } else
469                 die("option %s needs an argument (check|on-demand)",
470                                 opt->long_name);
471
472         return 0;
473 }
474
475 int cmd_push(int argc, const char **argv, const char *prefix)
476 {
477         int flags = 0;
478         int tags = 0;
479         int rc;
480         const char *repo = NULL;        /* default repository */
481         struct option options[] = {
482                 OPT__VERBOSITY(&verbosity),
483                 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
484                 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
485                 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
486                             (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
487                 OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
488                 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
489                 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
490                 OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
491                 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
492                 { OPTION_CALLBACK,
493                   0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
494                   N_("require old value of ref to be at this value"),
495                   PARSE_OPT_OPTARG, parseopt_push_cas_option },
496                 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
497                         N_("control recursive pushing of submodules"),
498                         PARSE_OPT_OPTARG, option_parse_recurse_submodules },
499                 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
500                 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
501                 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
502                 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
503                         TRANSPORT_PUSH_SET_UPSTREAM),
504                 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
505                 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
506                         TRANSPORT_PUSH_PRUNE),
507                 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
508                 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
509                         TRANSPORT_PUSH_FOLLOW_TAGS),
510                 OPT_END()
511         };
512
513         packet_trace_identity("push");
514         git_config(git_default_config, NULL);
515         argc = parse_options(argc, argv, prefix, options, push_usage, 0);
516
517         if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
518                 die(_("--delete is incompatible with --all, --mirror and --tags"));
519         if (deleterefs && argc < 2)
520                 die(_("--delete doesn't make sense without any refs"));
521
522         if (tags)
523                 add_refspec("refs/tags/*");
524
525         if (argc > 0) {
526                 repo = argv[0];
527                 set_refspecs(argv + 1, argc - 1, repo);
528         }
529
530         rc = do_push(repo, flags);
531         if (rc == -1)
532                 usage_with_options(push_usage, options);
533         else
534                 return rc;
535 }