push: respect --no-thin
[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 const char **refspec;
25 static int refspec_nr;
26 static int refspec_alloc;
27 static int default_matching_used;
28
29 static void add_refspec(const char *ref)
30 {
31         refspec_nr++;
32         ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
33         refspec[refspec_nr-1] = ref;
34 }
35
36 static void set_refspecs(const char **refs, int nr)
37 {
38         int i;
39         for (i = 0; i < nr; i++) {
40                 const char *ref = refs[i];
41                 if (!strcmp("tag", ref)) {
42                         char *tag;
43                         int len;
44                         if (nr <= ++i)
45                                 die(_("tag shorthand without <tag>"));
46                         len = strlen(refs[i]) + 11;
47                         if (deleterefs) {
48                                 tag = xmalloc(len+1);
49                                 strcpy(tag, ":refs/tags/");
50                         } else {
51                                 tag = xmalloc(len);
52                                 strcpy(tag, "refs/tags/");
53                         }
54                         strcat(tag, refs[i]);
55                         ref = tag;
56                 } else if (deleterefs && !strchr(ref, ':')) {
57                         char *delref;
58                         int len = strlen(ref)+1;
59                         delref = xmalloc(len+1);
60                         strcpy(delref, ":");
61                         strcat(delref, ref);
62                         ref = delref;
63                 } else if (deleterefs)
64                         die(_("--delete only accepts plain target ref names"));
65                 add_refspec(ref);
66         }
67 }
68
69 static int push_url_of_remote(struct remote *remote, const char ***url_p)
70 {
71         if (remote->pushurl_nr) {
72                 *url_p = remote->pushurl;
73                 return remote->pushurl_nr;
74         }
75         *url_p = remote->url;
76         return remote->url_nr;
77 }
78
79 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
80         /*
81          * There's no point in using shorten_unambiguous_ref here,
82          * as the ambiguity would be on the remote side, not what
83          * we have locally. Plus, this is supposed to be the simple
84          * mode. If the user is doing something crazy like setting
85          * upstream to a non-branch, we should probably be showing
86          * them the big ugly fully qualified ref.
87          */
88         const char *advice_maybe = "";
89         const char *short_upstream =
90                 skip_prefix(branch->merge[0]->src, "refs/heads/");
91
92         if (!short_upstream)
93                 short_upstream = branch->merge[0]->src;
94         /*
95          * Don't show advice for people who explicitly set
96          * push.default.
97          */
98         if (push_default == PUSH_DEFAULT_UNSPECIFIED)
99                 advice_maybe = _("\n"
100                                  "To choose either option permanently, "
101                                  "see push.default in 'git help config'.");
102         die(_("The upstream branch of your current branch does not match\n"
103               "the name of your current branch.  To push to the upstream branch\n"
104               "on the remote, use\n"
105               "\n"
106               "    git push %s HEAD:%s\n"
107               "\n"
108               "To push to the branch of the same name on the remote, use\n"
109               "\n"
110               "    git push %s %s\n"
111               "%s"),
112             remote->name, short_upstream,
113             remote->name, branch->name, advice_maybe);
114 }
115
116 static const char message_detached_head_die[] =
117         N_("You are not currently on a branch.\n"
118            "To push the history leading to the current (detached HEAD)\n"
119            "state now, use\n"
120            "\n"
121            "    git push %s HEAD:<name-of-remote-branch>\n");
122
123 static void setup_push_upstream(struct remote *remote, struct branch *branch,
124                                 int triangular)
125 {
126         struct strbuf refspec = STRBUF_INIT;
127
128         if (!branch)
129                 die(_(message_detached_head_die), remote->name);
130         if (!branch->merge_nr || !branch->merge || !branch->remote_name)
131                 die(_("The current branch %s has no upstream branch.\n"
132                     "To push the current branch and set the remote as upstream, use\n"
133                     "\n"
134                     "    git push --set-upstream %s %s\n"),
135                     branch->name,
136                     remote->name,
137                     branch->name);
138         if (branch->merge_nr != 1)
139                 die(_("The current branch %s has multiple upstream branches, "
140                     "refusing to push."), branch->name);
141         if (triangular)
142                 die(_("You are pushing to remote '%s', which is not the upstream of\n"
143                       "your current branch '%s', without telling me what to push\n"
144                       "to update which remote branch."),
145                     remote->name, branch->name);
146
147         if (push_default == PUSH_DEFAULT_SIMPLE) {
148                 /* Additional safety */
149                 if (strcmp(branch->refname, branch->merge[0]->src))
150                         die_push_simple(branch, remote);
151         }
152
153         strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
154         add_refspec(refspec.buf);
155 }
156
157 static void setup_push_current(struct remote *remote, struct branch *branch)
158 {
159         if (!branch)
160                 die(_(message_detached_head_die), remote->name);
161         add_refspec(branch->name);
162 }
163
164 static char warn_unspecified_push_default_msg[] =
165 N_("push.default is unset; its implicit value is changing in\n"
166    "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
167    "and maintain the current behavior after the default changes, use:\n"
168    "\n"
169    "  git config --global push.default matching\n"
170    "\n"
171    "To squelch this message and adopt the new behavior now, use:\n"
172    "\n"
173    "  git config --global push.default simple\n"
174    "\n"
175    "See 'git help config' and search for 'push.default' for further information.\n"
176    "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
177    "'current' instead of 'simple' if you sometimes use older versions of Git)");
178
179 static void warn_unspecified_push_default_configuration(void)
180 {
181         static int warn_once;
182
183         if (warn_once++)
184                 return;
185         warning("%s\n", _(warn_unspecified_push_default_msg));
186 }
187
188 static int is_workflow_triangular(struct remote *remote)
189 {
190         struct remote *fetch_remote = remote_get(NULL);
191         return (fetch_remote && fetch_remote != remote);
192 }
193
194 static void setup_default_push_refspecs(struct remote *remote)
195 {
196         struct branch *branch = branch_get(NULL);
197         int triangular = is_workflow_triangular(remote);
198
199         switch (push_default) {
200         default:
201         case PUSH_DEFAULT_UNSPECIFIED:
202                 default_matching_used = 1;
203                 warn_unspecified_push_default_configuration();
204                 /* fallthru */
205         case PUSH_DEFAULT_MATCHING:
206                 add_refspec(":");
207                 break;
208
209         case PUSH_DEFAULT_SIMPLE:
210                 if (triangular)
211                         setup_push_current(remote, branch);
212                 else
213                         setup_push_upstream(remote, branch, triangular);
214                 break;
215
216         case PUSH_DEFAULT_UPSTREAM:
217                 setup_push_upstream(remote, branch, triangular);
218                 break;
219
220         case PUSH_DEFAULT_CURRENT:
221                 setup_push_current(remote, branch);
222                 break;
223
224         case PUSH_DEFAULT_NOTHING:
225                 die(_("You didn't specify any refspecs to push, and "
226                     "push.default is \"nothing\"."));
227                 break;
228         }
229 }
230
231 static const char message_advice_pull_before_push[] =
232         N_("Updates were rejected because the tip of your current branch is behind\n"
233            "its remote counterpart. Integrate the remote changes (e.g.\n"
234            "'git pull ...') before pushing again.\n"
235            "See the 'Note about fast-forwards' in 'git push --help' for details.");
236
237 static const char message_advice_use_upstream[] =
238         N_("Updates were rejected because a pushed branch tip is behind its remote\n"
239            "counterpart. If you did not intend to push that branch, you may want to\n"
240            "specify branches to push or set the 'push.default' configuration variable\n"
241            "to 'simple', 'current' or 'upstream' to push only the current branch.");
242
243 static const char message_advice_checkout_pull_push[] =
244         N_("Updates were rejected because a pushed branch tip is behind its remote\n"
245            "counterpart. Check out this branch and integrate the remote changes\n"
246            "(e.g. 'git pull ...') before pushing again.\n"
247            "See the 'Note about fast-forwards' in 'git push --help' for details.");
248
249 static const char message_advice_ref_fetch_first[] =
250         N_("Updates were rejected because the remote contains work that you do\n"
251            "not have locally. This is usually caused by another repository pushing\n"
252            "to the same ref. You may want to first integrate the remote changes\n"
253            "(e.g., 'git pull ...') before pushing again.\n"
254            "See the 'Note about fast-forwards' in 'git push --help' for details.");
255
256 static const char message_advice_ref_already_exists[] =
257         N_("Updates were rejected because the tag already exists in the remote.");
258
259 static const char message_advice_ref_needs_force[] =
260         N_("You cannot update a remote ref that points at a non-commit object,\n"
261            "or update a remote ref to make it point at a non-commit object,\n"
262            "without using the '--force' option.\n");
263
264 static void advise_pull_before_push(void)
265 {
266         if (!advice_push_non_ff_current || !advice_push_update_rejected)
267                 return;
268         advise(_(message_advice_pull_before_push));
269 }
270
271 static void advise_use_upstream(void)
272 {
273         if (!advice_push_non_ff_default || !advice_push_update_rejected)
274                 return;
275         advise(_(message_advice_use_upstream));
276 }
277
278 static void advise_checkout_pull_push(void)
279 {
280         if (!advice_push_non_ff_matching || !advice_push_update_rejected)
281                 return;
282         advise(_(message_advice_checkout_pull_push));
283 }
284
285 static void advise_ref_already_exists(void)
286 {
287         if (!advice_push_already_exists || !advice_push_update_rejected)
288                 return;
289         advise(_(message_advice_ref_already_exists));
290 }
291
292 static void advise_ref_fetch_first(void)
293 {
294         if (!advice_push_fetch_first || !advice_push_update_rejected)
295                 return;
296         advise(_(message_advice_ref_fetch_first));
297 }
298
299 static void advise_ref_needs_force(void)
300 {
301         if (!advice_push_needs_force || !advice_push_update_rejected)
302                 return;
303         advise(_(message_advice_ref_needs_force));
304 }
305
306 static int push_with_options(struct transport *transport, int flags)
307 {
308         int err;
309         unsigned int reject_reasons;
310
311         transport_set_verbosity(transport, verbosity, progress);
312
313         if (receivepack)
314                 transport_set_option(transport,
315                                      TRANS_OPT_RECEIVEPACK, receivepack);
316         transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
317
318         if (verbosity > 0)
319                 fprintf(stderr, _("Pushing to %s\n"), transport->url);
320         err = transport_push(transport, refspec_nr, refspec, flags,
321                              &reject_reasons);
322         if (err != 0)
323                 error(_("failed to push some refs to '%s'"), transport->url);
324
325         err |= transport_disconnect(transport);
326         if (!err)
327                 return 0;
328
329         if (reject_reasons & REJECT_NON_FF_HEAD) {
330                 advise_pull_before_push();
331         } else if (reject_reasons & REJECT_NON_FF_OTHER) {
332                 if (default_matching_used)
333                         advise_use_upstream();
334                 else
335                         advise_checkout_pull_push();
336         } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
337                 advise_ref_already_exists();
338         } else if (reject_reasons & REJECT_FETCH_FIRST) {
339                 advise_ref_fetch_first();
340         } else if (reject_reasons & REJECT_NEEDS_FORCE) {
341                 advise_ref_needs_force();
342         }
343
344         return 1;
345 }
346
347 static int do_push(const char *repo, int flags)
348 {
349         int i, errs;
350         struct remote *remote = pushremote_get(repo);
351         const char **url;
352         int url_nr;
353
354         if (!remote) {
355                 if (repo)
356                         die(_("bad repository '%s'"), repo);
357                 die(_("No configured push destination.\n"
358                     "Either specify the URL from the command-line or configure a remote repository using\n"
359                     "\n"
360                     "    git remote add <name> <url>\n"
361                     "\n"
362                     "and then push using the remote name\n"
363                     "\n"
364                     "    git push <name>\n"));
365         }
366
367         if (remote->mirror)
368                 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
369
370         if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
371                 if (!strcmp(*refspec, "refs/tags/*"))
372                         return error(_("--all and --tags are incompatible"));
373                 return error(_("--all can't be combined with refspecs"));
374         }
375
376         if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
377                 if (!strcmp(*refspec, "refs/tags/*"))
378                         return error(_("--mirror and --tags are incompatible"));
379                 return error(_("--mirror can't be combined with refspecs"));
380         }
381
382         if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
383                                 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
384                 return error(_("--all and --mirror are incompatible"));
385         }
386
387         if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
388                 if (remote->push_refspec_nr) {
389                         refspec = remote->push_refspec;
390                         refspec_nr = remote->push_refspec_nr;
391                 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
392                         setup_default_push_refspecs(remote);
393         }
394         errs = 0;
395         url_nr = push_url_of_remote(remote, &url);
396         if (url_nr) {
397                 for (i = 0; i < url_nr; i++) {
398                         struct transport *transport =
399                                 transport_get(remote, url[i]);
400                         if (push_with_options(transport, flags))
401                                 errs++;
402                 }
403         } else {
404                 struct transport *transport =
405                         transport_get(remote, NULL);
406
407                 if (push_with_options(transport, flags))
408                         errs++;
409         }
410         return !!errs;
411 }
412
413 static int option_parse_recurse_submodules(const struct option *opt,
414                                    const char *arg, int unset)
415 {
416         int *flags = opt->value;
417
418         if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
419                       TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
420                 die("%s can only be used once.", opt->long_name);
421
422         if (arg) {
423                 if (!strcmp(arg, "check"))
424                         *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
425                 else if (!strcmp(arg, "on-demand"))
426                         *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
427                 else
428                         die("bad %s argument: %s", opt->long_name, arg);
429         } else
430                 die("option %s needs an argument (check|on-demand)",
431                                 opt->long_name);
432
433         return 0;
434 }
435
436 int cmd_push(int argc, const char **argv, const char *prefix)
437 {
438         int flags = 0;
439         int tags = 0;
440         int rc;
441         const char *repo = NULL;        /* default repository */
442         struct option options[] = {
443                 OPT__VERBOSITY(&verbosity),
444                 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
445                 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
446                 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
447                             (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
448                 OPT_BOOLEAN( 0, "delete", &deleterefs, N_("delete refs")),
449                 OPT_BOOLEAN( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
450                 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
451                 OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
452                 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
453                 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
454                         N_("control recursive pushing of submodules"),
455                         PARSE_OPT_OPTARG, option_parse_recurse_submodules },
456                 OPT_BOOLEAN( 0 , "thin", &thin, N_("use thin pack")),
457                 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
458                 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
459                 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
460                         TRANSPORT_PUSH_SET_UPSTREAM),
461                 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
462                 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
463                         TRANSPORT_PUSH_PRUNE),
464                 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
465                 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
466                         TRANSPORT_PUSH_FOLLOW_TAGS),
467                 OPT_END()
468         };
469
470         packet_trace_identity("push");
471         git_config(git_default_config, NULL);
472         argc = parse_options(argc, argv, prefix, options, push_usage, 0);
473
474         if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
475                 die(_("--delete is incompatible with --all, --mirror and --tags"));
476         if (deleterefs && argc < 2)
477                 die(_("--delete doesn't make sense without any refs"));
478
479         if (tags)
480                 add_refspec("refs/tags/*");
481
482         if (argc > 0) {
483                 repo = argv[0];
484                 set_refspecs(argv + 1, argc - 1);
485         }
486
487         rc = do_push(repo, flags);
488         if (rc == -1)
489                 usage_with_options(push_usage, options);
490         else
491                 return rc;
492 }