sequencer: allow to --skip current commit
[git] / builtin / push.c
1 /*
2  * "git push"
3  */
4 #include "cache.h"
5 #include "refs.h"
6 #include "dir.h"
7 #include "run-command.h"
8 #include "builtin.h"
9 #include "remote.h"
10 #include "transport.h"
11 #include "parse-options.h"
12 #include "submodule.h"
13 #include "submodule-config.h"
14 #include "send-pack.h"
15
16 static const char * const push_usage[] = {
17         N_("git push [<options>] [<repository> [<refspec>...]]"),
18         NULL,
19 };
20
21 static int thin = 1;
22 static int deleterefs;
23 static const char *receivepack;
24 static int verbosity;
25 static int progress = -1;
26 static int has_submodules_configured;
27 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
28 static enum transport_family family;
29
30 static struct push_cas_option cas;
31
32 static const char **refspec;
33 static int refspec_nr;
34 static int refspec_alloc;
35
36 static void preset_submodule_default(void)
37 {
38         if (has_submodules_configured || file_exists(git_path("modules")) ||
39             (!is_bare_repository() && file_exists(".gitmodules")))
40                 recurse_submodules = RECURSE_SUBMODULES_CHECK;
41         else
42                 recurse_submodules = RECURSE_SUBMODULES_OFF;
43 }
44
45 static void add_refspec(const char *ref)
46 {
47         refspec_nr++;
48         ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
49         refspec[refspec_nr-1] = ref;
50 }
51
52 static const char *map_refspec(const char *ref,
53                                struct remote *remote, struct ref *local_refs)
54 {
55         struct ref *matched = NULL;
56
57         /* Does "ref" uniquely name our ref? */
58         if (count_refspec_match(ref, local_refs, &matched) != 1)
59                 return ref;
60
61         if (remote->push) {
62                 struct refspec query;
63                 memset(&query, 0, sizeof(struct refspec));
64                 query.src = matched->name;
65                 if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
66                     query.dst) {
67                         struct strbuf buf = STRBUF_INIT;
68                         strbuf_addf(&buf, "%s%s:%s",
69                                     query.force ? "+" : "",
70                                     query.src, query.dst);
71                         return strbuf_detach(&buf, NULL);
72                 }
73         }
74
75         if (push_default == PUSH_DEFAULT_UPSTREAM &&
76             starts_with(matched->name, "refs/heads/")) {
77                 struct branch *branch = branch_get(matched->name + 11);
78                 if (branch->merge_nr == 1 && branch->merge[0]->src) {
79                         struct strbuf buf = STRBUF_INIT;
80                         strbuf_addf(&buf, "%s:%s",
81                                     ref, branch->merge[0]->src);
82                         return strbuf_detach(&buf, NULL);
83                 }
84         }
85
86         return ref;
87 }
88
89 static void set_refspecs(const char **refs, int nr, const char *repo)
90 {
91         struct remote *remote = NULL;
92         struct ref *local_refs = NULL;
93         int i;
94
95         for (i = 0; i < nr; i++) {
96                 const char *ref = refs[i];
97                 if (!strcmp("tag", ref)) {
98                         struct strbuf tagref = STRBUF_INIT;
99                         if (nr <= ++i)
100                                 die(_("tag shorthand without <tag>"));
101                         ref = refs[i];
102                         if (deleterefs)
103                                 strbuf_addf(&tagref, ":refs/tags/%s", ref);
104                         else
105                                 strbuf_addf(&tagref, "refs/tags/%s", ref);
106                         ref = strbuf_detach(&tagref, NULL);
107                 } else if (deleterefs) {
108                         struct strbuf delref = STRBUF_INIT;
109                         if (strchr(ref, ':'))
110                                 die(_("--delete only accepts plain target ref names"));
111                         strbuf_addf(&delref, ":%s", ref);
112                         ref = strbuf_detach(&delref, NULL);
113                 } else if (!strchr(ref, ':')) {
114                         if (!remote) {
115                                 /* lazily grab remote and local_refs */
116                                 remote = remote_get(repo);
117                                 local_refs = get_local_heads();
118                         }
119                         ref = map_refspec(ref, remote, local_refs);
120                 }
121                 add_refspec(ref);
122         }
123 }
124
125 static int push_url_of_remote(struct remote *remote, const char ***url_p)
126 {
127         if (remote->pushurl_nr) {
128                 *url_p = remote->pushurl;
129                 return remote->pushurl_nr;
130         }
131         *url_p = remote->url;
132         return remote->url_nr;
133 }
134
135 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
136         /*
137          * There's no point in using shorten_unambiguous_ref here,
138          * as the ambiguity would be on the remote side, not what
139          * we have locally. Plus, this is supposed to be the simple
140          * mode. If the user is doing something crazy like setting
141          * upstream to a non-branch, we should probably be showing
142          * them the big ugly fully qualified ref.
143          */
144         const char *advice_maybe = "";
145         const char *short_upstream = branch->merge[0]->src;
146
147         skip_prefix(short_upstream, "refs/heads/", &short_upstream);
148
149         /*
150          * Don't show advice for people who explicitly set
151          * push.default.
152          */
153         if (push_default == PUSH_DEFAULT_UNSPECIFIED)
154                 advice_maybe = _("\n"
155                                  "To choose either option permanently, "
156                                  "see push.default in 'git help config'.");
157         die(_("The upstream branch of your current branch does not match\n"
158               "the name of your current branch.  To push to the upstream branch\n"
159               "on the remote, use\n"
160               "\n"
161               "    git push %s HEAD:%s\n"
162               "\n"
163               "To push to the branch of the same name on the remote, use\n"
164               "\n"
165               "    git push %s %s\n"
166               "%s"),
167             remote->name, short_upstream,
168             remote->name, branch->name, advice_maybe);
169 }
170
171 static const char message_detached_head_die[] =
172         N_("You are not currently on a branch.\n"
173            "To push the history leading to the current (detached HEAD)\n"
174            "state now, use\n"
175            "\n"
176            "    git push %s HEAD:<name-of-remote-branch>\n");
177
178 static void setup_push_upstream(struct remote *remote, struct branch *branch,
179                                 int triangular, int simple)
180 {
181         struct strbuf refspec = STRBUF_INIT;
182
183         if (!branch)
184                 die(_(message_detached_head_die), remote->name);
185         if (!branch->merge_nr || !branch->merge || !branch->remote_name)
186                 die(_("The current branch %s has no upstream branch.\n"
187                     "To push the current branch and set the remote as upstream, use\n"
188                     "\n"
189                     "    git push --set-upstream %s %s\n"),
190                     branch->name,
191                     remote->name,
192                     branch->name);
193         if (branch->merge_nr != 1)
194                 die(_("The current branch %s has multiple upstream branches, "
195                     "refusing to push."), branch->name);
196         if (triangular)
197                 die(_("You are pushing to remote '%s', which is not the upstream of\n"
198                       "your current branch '%s', without telling me what to push\n"
199                       "to update which remote branch."),
200                     remote->name, branch->name);
201
202         if (simple) {
203                 /* Additional safety */
204                 if (strcmp(branch->refname, branch->merge[0]->src))
205                         die_push_simple(branch, remote);
206         }
207
208         strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
209         add_refspec(refspec.buf);
210 }
211
212 static void setup_push_current(struct remote *remote, struct branch *branch)
213 {
214         struct strbuf refspec = STRBUF_INIT;
215
216         if (!branch)
217                 die(_(message_detached_head_die), remote->name);
218         strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
219         add_refspec(refspec.buf);
220 }
221
222 static int is_workflow_triangular(struct remote *remote)
223 {
224         struct remote *fetch_remote = remote_get(NULL);
225         return (fetch_remote && fetch_remote != remote);
226 }
227
228 static void setup_default_push_refspecs(struct remote *remote)
229 {
230         struct branch *branch = branch_get(NULL);
231         int triangular = is_workflow_triangular(remote);
232
233         switch (push_default) {
234         default:
235         case PUSH_DEFAULT_MATCHING:
236                 add_refspec(":");
237                 break;
238
239         case PUSH_DEFAULT_UNSPECIFIED:
240         case PUSH_DEFAULT_SIMPLE:
241                 if (triangular)
242                         setup_push_current(remote, branch);
243                 else
244                         setup_push_upstream(remote, branch, triangular, 1);
245                 break;
246
247         case PUSH_DEFAULT_UPSTREAM:
248                 setup_push_upstream(remote, branch, triangular, 0);
249                 break;
250
251         case PUSH_DEFAULT_CURRENT:
252                 setup_push_current(remote, branch);
253                 break;
254
255         case PUSH_DEFAULT_NOTHING:
256                 die(_("You didn't specify any refspecs to push, and "
257                     "push.default is \"nothing\"."));
258                 break;
259         }
260 }
261
262 static const char message_advice_pull_before_push[] =
263         N_("Updates were rejected because the tip of your current branch is behind\n"
264            "its remote counterpart. Integrate the remote changes (e.g.\n"
265            "'git pull ...') before pushing again.\n"
266            "See the 'Note about fast-forwards' in 'git push --help' for details.");
267
268 static const char message_advice_checkout_pull_push[] =
269         N_("Updates were rejected because a pushed branch tip is behind its remote\n"
270            "counterpart. Check out this branch and integrate the remote changes\n"
271            "(e.g. 'git pull ...') before pushing again.\n"
272            "See the 'Note about fast-forwards' in 'git push --help' for details.");
273
274 static const char message_advice_ref_fetch_first[] =
275         N_("Updates were rejected because the remote contains work that you do\n"
276            "not have locally. This is usually caused by another repository pushing\n"
277            "to the same ref. You may want to first integrate the remote changes\n"
278            "(e.g., 'git pull ...') before pushing again.\n"
279            "See the 'Note about fast-forwards' in 'git push --help' for details.");
280
281 static const char message_advice_ref_already_exists[] =
282         N_("Updates were rejected because the tag already exists in the remote.");
283
284 static const char message_advice_ref_needs_force[] =
285         N_("You cannot update a remote ref that points at a non-commit object,\n"
286            "or update a remote ref to make it point at a non-commit object,\n"
287            "without using the '--force' option.\n");
288
289 static void advise_pull_before_push(void)
290 {
291         if (!advice_push_non_ff_current || !advice_push_update_rejected)
292                 return;
293         advise(_(message_advice_pull_before_push));
294 }
295
296 static void advise_checkout_pull_push(void)
297 {
298         if (!advice_push_non_ff_matching || !advice_push_update_rejected)
299                 return;
300         advise(_(message_advice_checkout_pull_push));
301 }
302
303 static void advise_ref_already_exists(void)
304 {
305         if (!advice_push_already_exists || !advice_push_update_rejected)
306                 return;
307         advise(_(message_advice_ref_already_exists));
308 }
309
310 static void advise_ref_fetch_first(void)
311 {
312         if (!advice_push_fetch_first || !advice_push_update_rejected)
313                 return;
314         advise(_(message_advice_ref_fetch_first));
315 }
316
317 static void advise_ref_needs_force(void)
318 {
319         if (!advice_push_needs_force || !advice_push_update_rejected)
320                 return;
321         advise(_(message_advice_ref_needs_force));
322 }
323
324 static int push_with_options(struct transport *transport, int flags)
325 {
326         int err;
327         unsigned int reject_reasons;
328
329         transport_set_verbosity(transport, verbosity, progress);
330         transport->family = family;
331
332         if (receivepack)
333                 transport_set_option(transport,
334                                      TRANS_OPT_RECEIVEPACK, receivepack);
335         transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
336
337         if (!is_empty_cas(&cas)) {
338                 if (!transport->smart_options)
339                         die("underlying transport does not support --%s option",
340                             CAS_OPT_NAME);
341                 transport->smart_options->cas = &cas;
342         }
343
344         if (verbosity > 0)
345                 fprintf(stderr, _("Pushing to %s\n"), transport->url);
346         err = transport_push(transport, refspec_nr, refspec, flags,
347                              &reject_reasons);
348         if (err != 0)
349                 error(_("failed to push some refs to '%s'"), transport->url);
350
351         err |= transport_disconnect(transport);
352         if (!err)
353                 return 0;
354
355         if (reject_reasons & REJECT_NON_FF_HEAD) {
356                 advise_pull_before_push();
357         } else if (reject_reasons & REJECT_NON_FF_OTHER) {
358                 advise_checkout_pull_push();
359         } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
360                 advise_ref_already_exists();
361         } else if (reject_reasons & REJECT_FETCH_FIRST) {
362                 advise_ref_fetch_first();
363         } else if (reject_reasons & REJECT_NEEDS_FORCE) {
364                 advise_ref_needs_force();
365         }
366
367         return 1;
368 }
369
370 static int do_push(const char *repo, int flags,
371                    const struct string_list *push_options)
372 {
373         int i, errs;
374         struct remote *remote = pushremote_get(repo);
375         const char **url;
376         int url_nr;
377
378         if (!remote) {
379                 if (repo)
380                         die(_("bad repository '%s'"), repo);
381                 die(_("No configured push destination.\n"
382                     "Either specify the URL from the command-line or configure a remote repository using\n"
383                     "\n"
384                     "    git remote add <name> <url>\n"
385                     "\n"
386                     "and then push using the remote name\n"
387                     "\n"
388                     "    git push <name>\n"));
389         }
390
391         if (remote->mirror)
392                 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
393
394         if (push_options->nr)
395                 flags |= TRANSPORT_PUSH_OPTIONS;
396
397         if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
398                 if (!strcmp(*refspec, "refs/tags/*"))
399                         return error(_("--all and --tags are incompatible"));
400                 return error(_("--all can't be combined with refspecs"));
401         }
402
403         if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
404                 if (!strcmp(*refspec, "refs/tags/*"))
405                         return error(_("--mirror and --tags are incompatible"));
406                 return error(_("--mirror can't be combined with refspecs"));
407         }
408
409         if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
410                                 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
411                 return error(_("--all and --mirror are incompatible"));
412         }
413
414         if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
415                 if (remote->push_refspec_nr) {
416                         refspec = remote->push_refspec;
417                         refspec_nr = remote->push_refspec_nr;
418                 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
419                         setup_default_push_refspecs(remote);
420         }
421         errs = 0;
422         url_nr = push_url_of_remote(remote, &url);
423         if (url_nr) {
424                 for (i = 0; i < url_nr; i++) {
425                         struct transport *transport =
426                                 transport_get(remote, url[i]);
427                         if (flags & TRANSPORT_PUSH_OPTIONS)
428                                 transport->push_options = push_options;
429                         if (push_with_options(transport, flags))
430                                 errs++;
431                 }
432         } else {
433                 struct transport *transport =
434                         transport_get(remote, NULL);
435                 if (flags & TRANSPORT_PUSH_OPTIONS)
436                         transport->push_options = push_options;
437                 if (push_with_options(transport, flags))
438                         errs++;
439         }
440         return !!errs;
441 }
442
443 static int option_parse_recurse_submodules(const struct option *opt,
444                                    const char *arg, int unset)
445 {
446         int *recurse_submodules = opt->value;
447
448         if (unset)
449                 *recurse_submodules = RECURSE_SUBMODULES_OFF;
450         else if (arg)
451                 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
452         else
453                 die("%s missing parameter", opt->long_name);
454
455         return 0;
456 }
457
458 static void set_push_cert_flags(int *flags, int v)
459 {
460         switch (v) {
461         case SEND_PACK_PUSH_CERT_NEVER:
462                 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
463                 break;
464         case SEND_PACK_PUSH_CERT_ALWAYS:
465                 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
466                 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
467                 break;
468         case SEND_PACK_PUSH_CERT_IF_ASKED:
469                 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
470                 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
471                 break;
472         }
473 }
474
475
476 static int git_push_config(const char *k, const char *v, void *cb)
477 {
478         int *flags = cb;
479         int status;
480
481         status = git_gpg_config(k, v, NULL);
482         if (status)
483                 return status;
484
485         if (!strcmp(k, "push.followtags")) {
486                 if (git_config_bool(k, v))
487                         *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
488                 else
489                         *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
490                 return 0;
491         } else if (!strcmp(k, "push.gpgsign")) {
492                 const char *value;
493                 if (!git_config_get_value("push.gpgsign", &value)) {
494                         switch (git_config_maybe_bool("push.gpgsign", value)) {
495                         case 0:
496                                 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
497                                 break;
498                         case 1:
499                                 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
500                                 break;
501                         default:
502                                 if (value && !strcasecmp(value, "if-asked"))
503                                         set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
504                                 else
505                                         return error("Invalid value for '%s'", k);
506                         }
507                 }
508         } else if (!strcmp(k, "push.recursesubmodules")) {
509                 const char *value;
510                 if (!git_config_get_value("push.recursesubmodules", &value))
511                         recurse_submodules = parse_push_recurse_submodules_arg(k, value);
512         } else if (starts_with(k, "submodule.") && ends_with(k, ".url"))
513                 /* The submodule.<name>.url is used as a bit to indicate existence */
514                 has_submodules_configured = 1;
515
516         return git_default_config(k, v, NULL);
517 }
518
519 int cmd_push(int argc, const char **argv, const char *prefix)
520 {
521         int flags = 0;
522         int tags = 0;
523         int push_cert = -1;
524         int rc;
525         const char *repo = NULL;        /* default repository */
526         static struct string_list push_options = STRING_LIST_INIT_DUP;
527         static struct string_list_item *item;
528
529         struct option options[] = {
530                 OPT__VERBOSITY(&verbosity),
531                 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
532                 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
533                 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
534                             (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
535                 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
536                 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
537                 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
538                 OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
539                 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
540                 { OPTION_CALLBACK,
541                   0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
542                   N_("require old value of ref to be at this value"),
543                   PARSE_OPT_OPTARG, parseopt_push_cas_option },
544                 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "check|on-demand|no",
545                         N_("control recursive pushing of submodules"),
546                         PARSE_OPT_OPTARG, option_parse_recurse_submodules },
547                 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
548                 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
549                 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
550                 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
551                         TRANSPORT_PUSH_SET_UPSTREAM),
552                 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
553                 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
554                         TRANSPORT_PUSH_PRUNE),
555                 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
556                 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
557                         TRANSPORT_PUSH_FOLLOW_TAGS),
558                 { OPTION_CALLBACK,
559                   0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
560                   PARSE_OPT_OPTARG, option_parse_push_signed },
561                 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
562                 OPT_STRING_LIST('o', "push-option", &push_options, N_("server-specific"), N_("option to transmit")),
563                 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
564                                 TRANSPORT_FAMILY_IPV4),
565                 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
566                                 TRANSPORT_FAMILY_IPV6),
567                 OPT_END()
568         };
569
570         packet_trace_identity("push");
571         preset_submodule_default();
572         git_config(git_push_config, &flags);
573         argc = parse_options(argc, argv, prefix, options, push_usage, 0);
574         set_push_cert_flags(&flags, push_cert);
575
576         if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
577                 die(_("--delete is incompatible with --all, --mirror and --tags"));
578         if (deleterefs && argc < 2)
579                 die(_("--delete doesn't make sense without any refs"));
580
581         if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
582                 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
583         else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
584                 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
585
586         if (tags)
587                 add_refspec("refs/tags/*");
588
589         if (argc > 0) {
590                 repo = argv[0];
591                 set_refspecs(argv + 1, argc - 1, repo);
592         }
593
594         for_each_string_list_item(item, &push_options)
595                 if (strchr(item->string, '\n'))
596                         die(_("push options must not have new line characters"));
597
598         rc = do_push(repo, flags, &push_options);
599         if (rc == -1)
600                 usage_with_options(push_usage, options);
601         else
602                 return rc;
603 }