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