Raw
1 /*
2 * "git push"
3 */
4
5 #define USE_THE_REPOSITORY_VARIABLE
6
7 #include "builtin.h"
8 #include "advice.h"
9 #include "branch.h"
10 #include "config.h"
11 #include "environment.h"
12 #include "gettext.h"
13 #include "hex.h"
14 #include "refspec.h"
15 #include "run-command.h"
16 #include "remote.h"
17 #include "transport.h"
18 #include "parse-options.h"
19 #include "pkt-line.h"
20 #include "submodule.h"
21 #include "submodule-config.h"
22 #include "send-pack.h"
23 #include "trace2.h"
24 #include "color.h"
25
26 static const char * const push_usage[] = {
27 N_("git push [<options>] [<repository> [<refspec>...]]"),
28 NULL,
29 };
30
31 static enum git_colorbool push_use_color = GIT_COLOR_UNKNOWN;
32 static char push_colors[][COLOR_MAXLEN] = {
33 GIT_COLOR_RESET,
34 GIT_COLOR_RED, /* ERROR */
35 };
36
37 enum color_push {
38 PUSH_COLOR_RESET = 0,
39 PUSH_COLOR_ERROR = 1
40 };
41
42 static int parse_push_color_slot(const char *slot)
43 {
44 if (!strcasecmp(slot, "reset"))
45 return PUSH_COLOR_RESET;
46 if (!strcasecmp(slot, "error"))
47 return PUSH_COLOR_ERROR;
48 return -1;
49 }
50
51 static const char *push_get_color(enum color_push ix)
52 {
53 if (want_color_stderr(push_use_color))
54 return push_colors[ix];
55 return "";
56 }
57
58 static int thin = 1;
59 static int deleterefs;
60 static const char *receivepack;
61 static int verbosity;
62 static int progress = -1;
63 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
64 static enum transport_family family;
65
66 static struct push_cas_option cas;
67
68 static struct refspec rs = REFSPEC_INIT_PUSH;
69
70 static struct string_list push_options_config = STRING_LIST_INIT_DUP;
71
72 static void refspec_append_mapped(struct refspec *refspec, const char *ref,
73 struct remote *remote, struct ref *matched)
74 {
75 const char *branch_name;
76
77 if (remote->push.nr) {
78 struct refspec_item query = {
79 .src = matched->name,
80 };
81
82 if (!refspec_find_match(&remote->push, &query) && query.dst) {
83 refspec_appendf(refspec, "%s%s:%s",
84 query.force ? "+" : "",
85 query.src, query.dst);
86 free(query.dst);
87 return;
88 }
89 }
90
91 if (push_default == PUSH_DEFAULT_UPSTREAM &&
92 skip_prefix(matched->name, "refs/heads/", &branch_name)) {
93 struct branch *branch = branch_get(branch_name);
94 if (branch->merge_nr == 1 && branch->merge[0]->src) {
95 refspec_appendf(refspec, "%s:%s",
96 ref, branch->merge[0]->src);
97 return;
98 }
99 }
100
101 refspec_append(refspec, ref);
102 }
103
104 static void set_refspecs(const char **refs, int nr, struct remote *remote)
105 {
106 struct ref *local_refs = NULL;
107 int i;
108
109 for (i = 0; i < nr; i++) {
110 const char *ref = refs[i];
111 if (!strcmp("tag", ref)) {
112 if (nr <= ++i)
113 die(_("tag shorthand without <tag>"));
114 ref = refs[i];
115 if (deleterefs)
116 refspec_appendf(&rs, ":refs/tags/%s", ref);
117 else
118 refspec_appendf(&rs, "refs/tags/%s", ref);
119 } else if (deleterefs) {
120 if (strchr(ref, ':') || !*ref)
121 die(_("--delete only accepts plain target ref names"));
122 refspec_appendf(&rs, ":%s", ref);
123 } else if (!strchr(ref, ':')) {
124 struct ref *matched = NULL;
125
126 /* lazily grab local_refs */
127 if (!local_refs)
128 local_refs = get_local_heads();
129
130 /* Does "ref" uniquely name our ref? */
131 if (count_refspec_match(ref, local_refs, &matched) != 1)
132 refspec_append(&rs, ref);
133 else
134 refspec_append_mapped(&rs, ref, remote, matched);
135 } else
136 refspec_append(&rs, ref);
137 }
138 free_refs(local_refs);
139 }
140
141 static NORETURN void die_push_simple(struct branch *branch,
142 struct remote *remote)
143 {
144 /*
145 * There's no point in using shorten_unambiguous_ref here,
146 * as the ambiguity would be on the remote side, not what
147 * we have locally. Plus, this is supposed to be the simple
148 * mode. If the user is doing something crazy like setting
149 * upstream to a non-branch, we should probably be showing
150 * them the big ugly fully qualified ref.
151 */
152 const char *advice_pushdefault_maybe = "";
153 const char *advice_automergesimple_maybe = "";
154 const char *short_upstream = branch->merge[0]->src;
155 struct repo_config_values *cfg = repo_config_values(the_repository);
156
157 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
158
159 /*
160 * Don't show advice for people who explicitly set
161 * push.default.
162 */
163 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
164 advice_pushdefault_maybe = _("\n"
165 "To choose either option permanently, "
166 "see push.default in 'git help config'.\n");
167 if (cfg->branch_track != BRANCH_TRACK_SIMPLE)
168 advice_automergesimple_maybe = _("\n"
169 "To avoid automatically configuring "
170 "an upstream branch when its name\n"
171 "won't match the local branch, see option "
172 "'simple' of branch.autoSetupMerge\n"
173 "in 'git help config'.\n");
174 die(_("The upstream branch of your current branch does not match\n"
175 "the name of your current branch. To push to the upstream branch\n"
176 "on the remote, use\n"
177 "\n"
178 " git push %s HEAD:%s\n"
179 "\n"
180 "To push to the branch of the same name on the remote, use\n"
181 "\n"
182 " git push %s HEAD\n"
183 "%s%s"),
184 remote->name, short_upstream,
185 remote->name, advice_pushdefault_maybe,
186 advice_automergesimple_maybe);
187 }
188
189 static const char message_detached_head_die[] =
190 N_("You are not currently on a branch.\n"
191 "To push the history leading to the current (detached HEAD)\n"
192 "state now, use\n"
193 "\n"
194 " git push %s HEAD:<name-of-remote-branch>\n");
195
196 static const char *get_upstream_ref(int flags, struct branch *branch, const char *remote_name)
197 {
198 if (branch->merge_nr == 0 && (flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
199 /* if missing, assume same; set_upstream will be defined later */
200 return branch->refname;
201 }
202
203 if (!branch->merge_nr || !branch->merge || !branch->remote_name) {
204 const char *advice_autosetup_maybe = "";
205 if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
206 advice_autosetup_maybe = _("\n"
207 "To have this happen automatically for "
208 "branches without a tracking\n"
209 "upstream, see 'push.autoSetupRemote' "
210 "in 'git help config'.\n");
211 }
212 die(_("The current branch %s has no upstream branch.\n"
213 "To push the current branch and set the remote as upstream, use\n"
214 "\n"
215 " git push --set-upstream %s %s\n"
216 "%s"),
217 branch->name,
218 remote_name,
219 branch->name,
220 advice_autosetup_maybe);
221 }
222 if (branch->merge_nr != 1)
223 die(_("The current branch %s has multiple upstream branches, "
224 "refusing to push."), branch->name);
225
226 return branch->merge[0]->src;
227 }
228
229 static void setup_default_push_refspecs(int *flags, struct remote *remote)
230 {
231 struct branch *branch;
232 const char *dst;
233 int same_remote;
234
235 switch (push_default) {
236 case PUSH_DEFAULT_MATCHING:
237 refspec_append(&rs, ":");
238 return;
239
240 case PUSH_DEFAULT_NOTHING:
241 die(_("You didn't specify any refspecs to push, and "
242 "push.default is \"nothing\"."));
243 return;
244 default:
245 break;
246 }
247
248 branch = branch_get(NULL);
249 if (!branch)
250 die(_(message_detached_head_die), remote->name);
251
252 dst = branch->refname;
253 same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
254
255 switch (push_default) {
256 default:
257 case PUSH_DEFAULT_UNSPECIFIED:
258 case PUSH_DEFAULT_SIMPLE:
259 if (!same_remote)
260 break;
261 if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name)))
262 die_push_simple(branch, remote);
263 break;
264
265 case PUSH_DEFAULT_UPSTREAM:
266 if (!same_remote)
267 die(_("You are pushing to remote '%s', which is not the upstream of\n"
268 "your current branch '%s', without telling me what to push\n"
269 "to update which remote branch."),
270 remote->name, branch->name);
271 dst = get_upstream_ref(*flags, branch, remote->name);
272 break;
273
274 case PUSH_DEFAULT_CURRENT:
275 break;
276 }
277
278 /*
279 * this is a default push - if auto-upstream is enabled and there is
280 * no upstream defined, then set it (with options 'simple', 'upstream',
281 * and 'current').
282 */
283 if ((*flags & TRANSPORT_PUSH_AUTO_UPSTREAM) && branch->merge_nr == 0)
284 *flags |= TRANSPORT_PUSH_SET_UPSTREAM;
285
286 refspec_appendf(&rs, "%s:%s", branch->refname, dst);
287 }
288
289 static const char message_advice_pull_before_push[] =
290 N_("Updates were rejected because the tip of your current branch is behind\n"
291 "its remote counterpart. If you want to integrate the remote changes,\n"
292 "use 'git pull' before pushing again.\n"
293 "See the 'Note about fast-forwards' in 'git push --help' for details.");
294
295 static const char message_advice_checkout_pull_push[] =
296 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
297 "counterpart. If you want to integrate the remote changes, use 'git pull'\n"
298 "before pushing again.\n"
299 "See the 'Note about fast-forwards' in 'git push --help' for details.");
300
301 static const char message_advice_ref_fetch_first[] =
302 N_("Updates were rejected because the remote contains work that you do not\n"
303 "have locally. This is usually caused by another repository pushing to\n"
304 "the same ref. If you want to integrate the remote changes, use\n"
305 "'git pull' before pushing again.\n"
306 "See the 'Note about fast-forwards' in 'git push --help' for details.");
307
308 static const char message_advice_ref_already_exists[] =
309 N_("Updates were rejected because the tag already exists in the remote.");
310
311 static const char message_advice_ref_needs_force[] =
312 N_("You cannot update a remote ref that points at a non-commit object,\n"
313 "or update a remote ref to make it point at a non-commit object,\n"
314 "without using the '--force' option.\n");
315
316 static const char message_advice_ref_needs_update[] =
317 N_("Updates were rejected because the tip of the remote-tracking branch has\n"
318 "been updated since the last checkout. If you want to integrate the\n"
319 "remote changes, use 'git pull' before pushing again.\n"
320 "See the 'Note about fast-forwards' in 'git push --help' for details.");
321
322 static void advise_pull_before_push(void)
323 {
324 if (!advice_enabled(ADVICE_PUSH_NON_FF_CURRENT) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
325 return;
326 advise(_(message_advice_pull_before_push));
327 }
328
329 static void advise_checkout_pull_push(void)
330 {
331 if (!advice_enabled(ADVICE_PUSH_NON_FF_MATCHING) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
332 return;
333 advise(_(message_advice_checkout_pull_push));
334 }
335
336 static void advise_ref_already_exists(void)
337 {
338 if (!advice_enabled(ADVICE_PUSH_ALREADY_EXISTS) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
339 return;
340 advise(_(message_advice_ref_already_exists));
341 }
342
343 static void advise_ref_fetch_first(void)
344 {
345 if (!advice_enabled(ADVICE_PUSH_FETCH_FIRST) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
346 return;
347 advise(_(message_advice_ref_fetch_first));
348 }
349
350 static void advise_ref_needs_force(void)
351 {
352 if (!advice_enabled(ADVICE_PUSH_NEEDS_FORCE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
353 return;
354 advise(_(message_advice_ref_needs_force));
355 }
356
357 static void advise_ref_needs_update(void)
358 {
359 if (!advice_enabled(ADVICE_PUSH_REF_NEEDS_UPDATE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED))
360 return;
361 advise(_(message_advice_ref_needs_update));
362 }
363
364 static int push_with_options(struct transport *transport, struct refspec *rs,
365 int flags)
366 {
367 int err;
368 unsigned int reject_reasons;
369 char *anon_url = transport_anonymize_url(transport->url);
370
371 transport_set_verbosity(transport, verbosity, progress);
372 transport->family = family;
373
374 if (receivepack)
375 transport_set_option(transport,
376 TRANS_OPT_RECEIVEPACK, receivepack);
377 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
378
379 if (!is_empty_cas(&cas)) {
380 if (!transport->smart_options)
381 die("underlying transport does not support --%s option",
382 "force-with-lease");
383 transport->smart_options->cas = &cas;
384 }
385
386 if (verbosity > 0)
387 fprintf(stderr, _("Pushing to %s\n"), anon_url);
388 trace2_region_enter("push", "transport_push", the_repository);
389 err = transport_push(the_repository, transport,
390 rs, flags, &reject_reasons);
391 trace2_region_leave("push", "transport_push", the_repository);
392 if (err != 0) {
393 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
394 error(_("failed to push some refs to '%s'"), anon_url);
395 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
396 }
397
398 err |= transport_disconnect(transport);
399 free(anon_url);
400 if (!err)
401 return 0;
402
403 if (reject_reasons & REJECT_NON_FF_HEAD) {
404 advise_pull_before_push();
405 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
406 advise_checkout_pull_push();
407 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
408 advise_ref_already_exists();
409 } else if (reject_reasons & REJECT_FETCH_FIRST) {
410 advise_ref_fetch_first();
411 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
412 advise_ref_needs_force();
413 } else if (reject_reasons & REJECT_REF_NEEDS_UPDATE) {
414 advise_ref_needs_update();
415 }
416
417 return 1;
418 }
419
420 static int do_push(int flags,
421 const struct string_list *push_options,
422 struct remote *remote)
423 {
424 int errs;
425 struct strvec *url;
426 struct refspec *push_refspec = &rs;
427
428 if (push_options->nr)
429 flags |= TRANSPORT_PUSH_OPTIONS;
430
431 if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) {
432 if (remote->push.nr) {
433 push_refspec = &remote->push;
434 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
435 setup_default_push_refspecs(&flags, remote);
436 }
437 errs = 0;
438 url = push_url_of_remote(remote);
439 for (size_t i = 0; i < url->nr; i++) {
440 struct transport *transport =
441 transport_get(remote, url->v[i]);
442 if (flags & TRANSPORT_PUSH_OPTIONS)
443 transport->push_options = push_options;
444 if (push_with_options(transport, push_refspec, flags))
445 errs++;
446 }
447 return !!errs;
448 }
449
450 static int option_parse_recurse_submodules(const struct option *opt,
451 const char *arg, int unset)
452 {
453 int *recurse_submodules = opt->value;
454
455 if (unset)
456 *recurse_submodules = RECURSE_SUBMODULES_OFF;
457 else {
458 if (!strcmp(arg, "only-is-on-demand")) {
459 if (*recurse_submodules == RECURSE_SUBMODULES_ONLY) {
460 warning(_("recursing into submodule with push.recurseSubmodules=only; using on-demand instead"));
461 *recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
462 }
463 } else {
464 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
465 }
466 }
467
468 return 0;
469 }
470
471 static void set_push_cert_flags(int *flags, int v)
472 {
473 switch (v) {
474 case SEND_PACK_PUSH_CERT_NEVER:
475 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
476 break;
477 case SEND_PACK_PUSH_CERT_ALWAYS:
478 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
479 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
480 break;
481 case SEND_PACK_PUSH_CERT_IF_ASKED:
482 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
483 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
484 break;
485 }
486 }
487
488
489 static int git_push_config(const char *k, const char *v,
490 const struct config_context *ctx, void *cb)
491 {
492 const char *slot_name;
493 int *flags = cb;
494
495 if (!strcmp(k, "push.followtags")) {
496 if (git_config_bool(k, v))
497 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
498 else
499 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
500 return 0;
501 } else if (!strcmp(k, "push.autosetupremote")) {
502 if (git_config_bool(k, v))
503 *flags |= TRANSPORT_PUSH_AUTO_UPSTREAM;
504 return 0;
505 } else if (!strcmp(k, "push.gpgsign")) {
506 switch (git_parse_maybe_bool(v)) {
507 case 0:
508 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
509 break;
510 case 1:
511 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
512 break;
513 default:
514 if (!strcasecmp(v, "if-asked"))
515 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
516 else
517 return error(_("invalid value for '%s'"), k);
518 }
519 } else if (!strcmp(k, "push.recursesubmodules")) {
520 recurse_submodules = parse_push_recurse_submodules_arg(k, v);
521 } else if (!strcmp(k, "submodule.recurse")) {
522 int val = git_config_bool(k, v) ?
523 RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
524 recurse_submodules = val;
525 } else if (!strcmp(k, "push.pushoption")) {
526 return parse_transport_option(k, v, &push_options_config);
527 } else if (!strcmp(k, "color.push")) {
528 push_use_color = git_config_colorbool(k, v);
529 return 0;
530 } else if (skip_prefix(k, "color.push.", &slot_name)) {
531 int slot = parse_push_color_slot(slot_name);
532 if (slot < 0)
533 return 0;
534 if (!v)
535 return config_error_nonbool(k);
536 return color_parse(v, push_colors[slot]);
537 } else if (!strcmp(k, "push.useforceifincludes")) {
538 if (git_config_bool(k, v))
539 *flags |= TRANSPORT_PUSH_FORCE_IF_INCLUDES;
540 else
541 *flags &= ~TRANSPORT_PUSH_FORCE_IF_INCLUDES;
542 return 0;
543 }
544
545 return git_default_config(k, v, ctx, NULL);
546 }
547
548 static int push_multiple(struct string_list *list,
549 const struct string_list *push_options,
550 int flags,
551 int tags,
552 const char **refspecs,
553 int refspec_nr)
554 {
555 int result = 0;
556 size_t i;
557 struct strvec argv = STRVEC_INIT;
558
559 strvec_push(&argv, "push");
560
561 if (flags & TRANSPORT_PUSH_FORCE)
562 strvec_push(&argv, "--force");
563 if (flags & TRANSPORT_PUSH_DRY_RUN)
564 strvec_push(&argv, "--dry-run");
565 if (flags & TRANSPORT_PUSH_PORCELAIN)
566 strvec_push(&argv, "--porcelain");
567 if (flags & TRANSPORT_PUSH_PRUNE)
568 strvec_push(&argv, "--prune");
569 if (flags & TRANSPORT_PUSH_NO_HOOK)
570 strvec_push(&argv, "--no-verify");
571 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
572 strvec_push(&argv, "--follow-tags");
573 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
574 strvec_push(&argv, "--set-upstream");
575 if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)
576 strvec_push(&argv, "--force-if-includes");
577 if (flags & TRANSPORT_PUSH_ALL)
578 strvec_push(&argv, "--all");
579 if (flags & TRANSPORT_PUSH_MIRROR)
580 strvec_push(&argv, "--mirror");
581
582 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
583 strvec_push(&argv, "--signed=yes");
584 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
585 strvec_push(&argv, "--signed=if-asked");
586 if (!thin)
587 strvec_push(&argv, "--no-thin");
588
589 if (deleterefs)
590 strvec_push(&argv, "--delete");
591
592 if (receivepack)
593 strvec_pushf(&argv, "--receive-pack=%s", receivepack);
594 if (verbosity >= 2)
595 strvec_push(&argv, "-v");
596 if (verbosity >= 1)
597 strvec_push(&argv, "-v");
598 else if (verbosity < 0)
599 strvec_push(&argv, "-q");
600 if (progress > 0)
601 strvec_push(&argv, "--progress");
602 else if (progress == 0)
603 strvec_push(&argv, "--no-progress");
604
605 if (family == TRANSPORT_FAMILY_IPV4)
606 strvec_push(&argv, "--ipv4");
607 else if (family == TRANSPORT_FAMILY_IPV6)
608 strvec_push(&argv, "--ipv6");
609
610 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
611 strvec_push(&argv, "--recurse-submodules=check");
612 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
613 strvec_push(&argv, "--recurse-submodules=on-demand");
614 else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
615 strvec_push(&argv, "--recurse-submodules=only");
616 else if (recurse_submodules == RECURSE_SUBMODULES_OFF)
617 strvec_push(&argv, "--recurse-submodules=no");
618
619
620 if (tags)
621 strvec_push(&argv, "--tags");
622
623 for (i = 0; i < push_options->nr; i++)
624 strvec_pushf(&argv, "--push-option=%s",
625 push_options->items[i].string);
626
627 for (i = 0; i < cas.nr; i++) {
628 if (cas.entry[i].use_tracking) {
629 strvec_pushf(&argv, "--force-with-lease=%s",
630 cas.entry[i].refname);
631 } else if (!is_null_oid(&cas.entry[i].expect)) {
632 strvec_pushf(&argv, "--force-with-lease=%s:%s",
633 cas.entry[i].refname,
634 oid_to_hex(&cas.entry[i].expect));
635 } else {
636 strvec_push(&argv, "--force-with-lease");
637 }
638 }
639
640 for (i = 0; i < list->nr; i++) {
641 const char *name = list->items[i].string;
642 struct child_process cmd = CHILD_PROCESS_INIT;
643 int j;
644
645 strvec_pushv(&cmd.args, argv.v);
646 strvec_push(&cmd.args, name);
647
648 for (j = 0; j < refspec_nr; j++)
649 strvec_push(&cmd.args, refspecs[j]);
650
651 if (verbosity >= 0)
652 printf(_("Pushing to %s\n"), name);
653
654 cmd.git_cmd = 1;
655 if (run_command(&cmd)) {
656 error(_("could not push to %s"), name);
657 result = 1;
658 }
659 }
660
661 strvec_clear(&argv);
662 return result;
663 }
664
665 int cmd_push(int argc,
666 const char **argv,
667 const char *prefix,
668 struct repository *repository UNUSED)
669 {
670 int flags = 0;
671 int tags = 0;
672 int push_cert = -1;
673 int rc = 0;
674 int base_flags;
675 const char *repo = NULL; /* default repository */
676 struct string_list push_options_cmdline = STRING_LIST_INIT_DUP;
677 struct string_list remote_group = STRING_LIST_INIT_DUP;
678 struct string_list *push_options;
679 const struct string_list_item *item;
680
681 struct option options[] = {
682 OPT__VERBOSITY(&verbosity),
683 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
684 OPT_BIT( 0 , "all", &flags, N_("push all branches"), TRANSPORT_PUSH_ALL),
685 OPT_ALIAS( 0 , "branches", "all"),
686 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
687 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
688 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
689 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --branches or --mirror)")),
690 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
691 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
692 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
693 OPT_CALLBACK_F(0, "force-with-lease", &cas, N_("<refname>:<expect>"),
694 N_("require old value of ref to be at this value"),
695 PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option),
696 OPT_BIT(0, TRANS_OPT_FORCE_IF_INCLUDES, &flags,
697 N_("require remote updates to be integrated locally"),
698 TRANSPORT_PUSH_FORCE_IF_INCLUDES),
699 OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
700 N_("control recursive pushing of submodules"), option_parse_recurse_submodules),
701 OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
702 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
703 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
704 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
705 TRANSPORT_PUSH_SET_UPSTREAM),
706 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
707 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
708 TRANSPORT_PUSH_PRUNE),
709 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
710 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
711 TRANSPORT_PUSH_FOLLOW_TAGS),
712 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
713 PARSE_OPT_OPTARG, option_parse_push_signed),
714 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
715 OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")),
716 OPT_IPVERSION(&family),
717 OPT_END()
718 };
719
720 packet_trace_identity("push");
721 repo_config(the_repository, git_push_config, &flags);
722 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
723 push_options = (push_options_cmdline.nr
724 ? &push_options_cmdline
725 : &push_options_config);
726 set_push_cert_flags(&flags, push_cert);
727
728 die_for_incompatible_opt4(deleterefs, "--delete",
729 tags, "--tags",
730 flags & TRANSPORT_PUSH_ALL, "--all/--branches",
731 flags & TRANSPORT_PUSH_MIRROR, "--mirror");
732 if (deleterefs && argc < 2)
733 die(_("--delete doesn't make sense without any refs"));
734
735 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
736 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
737 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
738 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
739 else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
740 flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
741
742 if (argc > 0)
743 repo = argv[0];
744
745 if (repo) {
746 if (!add_remote_or_group(repo, &remote_group)) {
747 /*
748 * Not a configured remote name or group name.
749 * Try treating it as a direct URL or path, e.g.
750 * git push /tmp/foo.git
751 * git push https://github.com/user/repo.git
752 * pushremote_get() creates an anonymous remote
753 * from the URL so the loop below can handle it
754 * identically to a named remote.
755 */
756 struct remote *r = pushremote_get(repo);
757 if (!r)
758 die(_("bad repository '%s'"), repo);
759 string_list_append(&remote_group, r->name);
760 }
761 } else {
762 struct remote *r = pushremote_get(NULL);
763 if (!r)
764 die(_("No configured push destination.\n"
765 "Either specify the URL from the command-line or configure a remote repository using\n"
766 "\n"
767 " git remote add <name> <url>\n"
768 "\n"
769 "and then push using the remote name\n"
770 "\n"
771 " git push <name>\n"
772 "\n"
773 "To push to multiple remotes at once, configure a remote group using\n"
774 "\n"
775 " git config remotes.<groupname> \"<remote1> <remote2>\"\n"
776 "\n"
777 "and then push using the group name\n"
778 "\n"
779 " git push <groupname>\n"));
780 string_list_append(&remote_group, r->name);
781 }
782
783 if (!is_empty_cas(&cas) && (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES))
784 cas.use_force_if_includes = 1;
785
786 for_each_string_list_item(item, push_options)
787 if (strchr(item->string, '\n'))
788 die(_("push options must not have new line characters"));
789
790 if (remote_group.nr == 1) {
791 /*
792 * Single remote (the common case): run do_push() directly
793 * in this process. The loop runs exactly once.
794 *
795 * Mirror detection and the --mirror/--all + refspec conflict
796 * checks are done here. rs is rebuilt so that per-remote push
797 * mappings (remote.NAME.push config) are resolved against the
798 * correct remote. inner_flags is a snapshot of flags so that a
799 * mirror remote cannot bleed TRANSPORT_PUSH_FORCE into any
800 * subsequent call.
801 */
802 base_flags = flags;
803 {
804 int inner_flags = base_flags;
805 struct remote *r = pushremote_get(remote_group.items[0].string);
806 if (!r)
807 die(_("no such remote or remote group: %s"),
808 remote_group.items[0].string);
809
810 if (r->mirror)
811 inner_flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
812
813 if (inner_flags & TRANSPORT_PUSH_ALL) {
814 if (argc >= 2)
815 die(_("--all can't be combined with refspecs"));
816 }
817 if (inner_flags & TRANSPORT_PUSH_MIRROR) {
818 if (argc >= 2)
819 die(_("--mirror can't be combined with refspecs"));
820 }
821
822 refspec_clear(&rs);
823 rs = (struct refspec) REFSPEC_INIT_PUSH;
824
825 if (tags)
826 refspec_append(&rs, "refs/tags/*");
827 if (argc > 0)
828 set_refspecs(argv + 1, argc - 1, r);
829
830 rc = do_push(inner_flags, push_options, r);
831 }
832 } else {
833 /*
834 * Multiple remotes: spawn one "git push <remote> [<refspecs>]"
835 * subprocess per remote, sequentially.
836 *
837 * Options that only make sense for a single transport connection
838 * are rejected here.
839 */
840 if (flags & TRANSPORT_PUSH_ATOMIC)
841 die(_("--atomic can only be used when pushing to one remote"));
842
843 rc = push_multiple(&remote_group, push_options, flags,
844 tags,
845 argc > 1 ? argv + 1 : NULL,
846 argc > 1 ? argc - 1 : 0);
847 }
848
849 string_list_clear(&push_options_cmdline, 0);
850 string_list_clear(&push_options_config, 0);
851 string_list_clear(&remote_group, 0);
852 clear_cas_option(&cas);
853
854 if (rc == -1)
855 usage_with_options(push_usage, options);
856 else
857 return rc;
858 }