7 #include "run-command.h"
10 #include "transport.h"
11 #include "parse-options.h"
12 #include "submodule.h"
13 #include "submodule-config.h"
14 #include "send-pack.h"
16 static const char * const push_usage[] = {
17 N_("git push [<options>] [<repository> [<refspec>...]]"),
22 static int deleterefs;
23 static const char *receivepack;
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;
30 static struct push_cas_option cas;
32 static const char **refspec;
33 static int refspec_nr;
34 static int refspec_alloc;
36 static void preset_submodule_default(void)
38 if (has_submodules_configured || file_exists(git_path("modules")) ||
39 (!is_bare_repository() && file_exists(".gitmodules")))
40 recurse_submodules = RECURSE_SUBMODULES_CHECK;
42 recurse_submodules = RECURSE_SUBMODULES_OFF;
45 static void add_refspec(const char *ref)
48 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
49 refspec[refspec_nr-1] = ref;
52 static const char *map_refspec(const char *ref,
53 struct remote *remote, struct ref *local_refs)
55 struct ref *matched = NULL;
57 /* Does "ref" uniquely name our ref? */
58 if (count_refspec_match(ref, local_refs, &matched) != 1)
63 memset(&query, 0, sizeof(struct refspec));
64 query.src = matched->name;
65 if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
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);
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);
89 static void set_refspecs(const char **refs, int nr, const char *repo)
91 struct remote *remote = NULL;
92 struct ref *local_refs = NULL;
95 for (i = 0; i < nr; i++) {
96 const char *ref = refs[i];
97 if (!strcmp("tag", ref)) {
98 struct strbuf tagref = STRBUF_INIT;
100 die(_("tag shorthand without <tag>"));
103 strbuf_addf(&tagref, ":refs/tags/%s", ref);
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, ':')) {
115 /* lazily grab remote and local_refs */
116 remote = remote_get(repo);
117 local_refs = get_local_heads();
119 ref = map_refspec(ref, remote, local_refs);
125 static int push_url_of_remote(struct remote *remote, const char ***url_p)
127 if (remote->pushurl_nr) {
128 *url_p = remote->pushurl;
129 return remote->pushurl_nr;
131 *url_p = remote->url;
132 return remote->url_nr;
135 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
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.
144 const char *advice_maybe = "";
145 const char *short_upstream = branch->merge[0]->src;
147 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
150 * Don't show advice for people who explicitly set
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"
161 " git push %s HEAD:%s\n"
163 "To push to the branch of the same name on the remote, use\n"
167 remote->name, short_upstream,
168 remote->name, branch->name, advice_maybe);
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"
176 " git push %s HEAD:<name-of-remote-branch>\n");
178 static void setup_push_upstream(struct remote *remote, struct branch *branch,
179 int triangular, int simple)
181 struct strbuf refspec = STRBUF_INIT;
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"
189 " git push --set-upstream %s %s\n"),
193 if (branch->merge_nr != 1)
194 die(_("The current branch %s has multiple upstream branches, "
195 "refusing to push."), branch->name);
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);
203 /* Additional safety */
204 if (strcmp(branch->refname, branch->merge[0]->src))
205 die_push_simple(branch, remote);
208 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
209 add_refspec(refspec.buf);
212 static void setup_push_current(struct remote *remote, struct branch *branch)
214 struct strbuf refspec = STRBUF_INIT;
217 die(_(message_detached_head_die), remote->name);
218 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
219 add_refspec(refspec.buf);
222 static int is_workflow_triangular(struct remote *remote)
224 struct remote *fetch_remote = remote_get(NULL);
225 return (fetch_remote && fetch_remote != remote);
228 static void setup_default_push_refspecs(struct remote *remote)
230 struct branch *branch = branch_get(NULL);
231 int triangular = is_workflow_triangular(remote);
233 switch (push_default) {
235 case PUSH_DEFAULT_MATCHING:
239 case PUSH_DEFAULT_UNSPECIFIED:
240 case PUSH_DEFAULT_SIMPLE:
242 setup_push_current(remote, branch);
244 setup_push_upstream(remote, branch, triangular, 1);
247 case PUSH_DEFAULT_UPSTREAM:
248 setup_push_upstream(remote, branch, triangular, 0);
251 case PUSH_DEFAULT_CURRENT:
252 setup_push_current(remote, branch);
255 case PUSH_DEFAULT_NOTHING:
256 die(_("You didn't specify any refspecs to push, and "
257 "push.default is \"nothing\"."));
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.");
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.");
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.");
281 static const char message_advice_ref_already_exists[] =
282 N_("Updates were rejected because the tag already exists in the remote.");
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");
289 static void advise_pull_before_push(void)
291 if (!advice_push_non_ff_current || !advice_push_update_rejected)
293 advise(_(message_advice_pull_before_push));
296 static void advise_checkout_pull_push(void)
298 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
300 advise(_(message_advice_checkout_pull_push));
303 static void advise_ref_already_exists(void)
305 if (!advice_push_already_exists || !advice_push_update_rejected)
307 advise(_(message_advice_ref_already_exists));
310 static void advise_ref_fetch_first(void)
312 if (!advice_push_fetch_first || !advice_push_update_rejected)
314 advise(_(message_advice_ref_fetch_first));
317 static void advise_ref_needs_force(void)
319 if (!advice_push_needs_force || !advice_push_update_rejected)
321 advise(_(message_advice_ref_needs_force));
324 static int push_with_options(struct transport *transport, int flags)
327 unsigned int reject_reasons;
329 transport_set_verbosity(transport, verbosity, progress);
330 transport->family = family;
333 transport_set_option(transport,
334 TRANS_OPT_RECEIVEPACK, receivepack);
335 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
337 if (!is_empty_cas(&cas)) {
338 if (!transport->smart_options)
339 die("underlying transport does not support --%s option",
341 transport->smart_options->cas = &cas;
345 fprintf(stderr, _("Pushing to %s\n"), transport->url);
346 err = transport_push(transport, refspec_nr, refspec, flags,
349 error(_("failed to push some refs to '%s'"), transport->url);
351 err |= transport_disconnect(transport);
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();
370 static int do_push(const char *repo, int flags,
371 const struct string_list *push_options)
374 struct remote *remote = pushremote_get(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"
384 " git remote add <name> <url>\n"
386 "and then push using the remote name\n"
388 " git push <name>\n"));
392 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
394 if (push_options->nr)
395 flags |= TRANSPORT_PUSH_OPTIONS;
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"));
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"));
409 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
410 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
411 return error(_("--all and --mirror are incompatible"));
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);
422 url_nr = push_url_of_remote(remote, &url);
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))
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))
443 static int option_parse_recurse_submodules(const struct option *opt,
444 const char *arg, int unset)
446 int *recurse_submodules = opt->value;
449 *recurse_submodules = RECURSE_SUBMODULES_OFF;
451 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
453 die("%s missing parameter", opt->long_name);
458 static void set_push_cert_flags(int *flags, int v)
461 case SEND_PACK_PUSH_CERT_NEVER:
462 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
464 case SEND_PACK_PUSH_CERT_ALWAYS:
465 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
466 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
468 case SEND_PACK_PUSH_CERT_IF_ASKED:
469 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
470 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
476 static int git_push_config(const char *k, const char *v, void *cb)
481 status = git_gpg_config(k, v, NULL);
485 if (!strcmp(k, "push.followtags")) {
486 if (git_config_bool(k, v))
487 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
489 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
491 } else if (!strcmp(k, "push.gpgsign")) {
493 if (!git_config_get_value("push.gpgsign", &value)) {
494 switch (git_config_maybe_bool("push.gpgsign", value)) {
496 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
499 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
502 if (value && !strcasecmp(value, "if-asked"))
503 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
505 return error("Invalid value for '%s'", k);
508 } else if (!strcmp(k, "push.recursesubmodules")) {
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;
516 return git_default_config(k, v, NULL);
519 int cmd_push(int argc, const char **argv, const char *prefix)
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;
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),
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),
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),
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);
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"));
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;
587 add_refspec("refs/tags/*");
591 set_refspecs(argv + 1, argc - 1, repo);
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"));
598 rc = do_push(repo, flags, &push_options);
600 usage_with_options(push_usage, options);