]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/push.c
Merge branch 'jc/push-delete-nothing'
[thirdparty/git.git] / builtin / push.c
CommitLineData
755225de
LT
1/*
2 * "git push"
3 */
4#include "cache.h"
b2141fc1 5#include "config.h"
755225de 6#include "refs.h"
ec0cb496 7#include "refspec.h"
755225de
LT
8#include "run-command.h"
9#include "builtin.h"
5751f490 10#include "remote.h"
9b288516 11#include "transport.h"
378c4832 12#include "parse-options.h"
d2b17b32 13#include "submodule.h"
b33a15b0 14#include "submodule-config.h"
30261094 15#include "send-pack.h"
960786e7 16#include "color.h"
755225de 17
378c4832 18static const char * const push_usage[] = {
78dafaa5 19 N_("git push [<options>] [<repository> [<refspec>...]]"),
378c4832
DB
20 NULL,
21};
755225de 22
960786e7
RD
23static int push_use_color = -1;
24static char push_colors[][COLOR_MAXLEN] = {
25 GIT_COLOR_RESET,
26 GIT_COLOR_RED, /* ERROR */
27};
28
29enum color_push {
30 PUSH_COLOR_RESET = 0,
31 PUSH_COLOR_ERROR = 1
32};
33
34static int parse_push_color_slot(const char *slot)
35{
36 if (!strcasecmp(slot, "reset"))
37 return PUSH_COLOR_RESET;
38 if (!strcasecmp(slot, "error"))
39 return PUSH_COLOR_ERROR;
40 return -1;
41}
42
43static const char *push_get_color(enum color_push ix)
44{
45 if (want_color_stderr(push_use_color))
46 return push_colors[ix];
47 return "";
48}
49
f7c815c3 50static int thin = 1;
f517f1f2 51static int deleterefs;
d23842fd 52static const char *receivepack;
8afd8dc0 53static int verbosity;
d34141cd
MC
54static int progress = -1;
55static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
c915f11e 56static enum transport_family family;
755225de 57
28f5d176
JH
58static struct push_cas_option cas;
59
aa40289c 60static struct refspec rs = REFSPEC_INIT_PUSH;
755225de 61
d8052750
MP
62static struct string_list push_options_config = STRING_LIST_INIT_DUP;
63
30035d9c
RS
64static void refspec_append_mapped(struct refspec *refspec, const char *ref,
65 struct remote *remote, struct ref *local_refs)
755225de 66{
1768aaf0 67 const char *branch_name;
ca02465b
JH
68 struct ref *matched = NULL;
69
70 /* Does "ref" uniquely name our ref? */
30035d9c
RS
71 if (count_refspec_match(ref, local_refs, &matched) != 1) {
72 refspec_append(refspec, ref);
73 return;
74 }
ca02465b 75
6bdb304b 76 if (remote->push.nr) {
0ad4a5ff
BW
77 struct refspec_item query;
78 memset(&query, 0, sizeof(struct refspec_item));
ca02465b 79 query.src = matched->name;
86baf825 80 if (!query_refspecs(&remote->push, &query) && query.dst) {
1af8b8c0
RS
81 refspec_appendf(refspec, "%s%s:%s",
82 query.force ? "+" : "",
83 query.src, query.dst);
30035d9c 84 return;
ca02465b
JH
85 }
86 }
87
fc9261ca 88 if (push_default == PUSH_DEFAULT_UPSTREAM &&
1768aaf0
RS
89 skip_prefix(matched->name, "refs/heads/", &branch_name)) {
90 struct branch *branch = branch_get(branch_name);
fc9261ca 91 if (branch->merge_nr == 1 && branch->merge[0]->src) {
1af8b8c0
RS
92 refspec_appendf(refspec, "%s:%s",
93 ref, branch->merge[0]->src);
30035d9c 94 return;
fc9261ca
JH
95 }
96 }
97
30035d9c 98 refspec_append(refspec, ref);
ca02465b
JH
99}
100
101static void set_refspecs(const char **refs, int nr, const char *repo)
102{
103 struct remote *remote = NULL;
104 struct ref *local_refs = NULL;
8558fd9e 105 int i;
ca02465b 106
8558fd9e
DB
107 for (i = 0; i < nr; i++) {
108 const char *ref = refs[i];
109 if (!strcmp("tag", ref)) {
8558fd9e 110 if (nr <= ++i)
8352d29e 111 die(_("tag shorthand without <tag>"));
50d829c1
JH
112 ref = refs[i];
113 if (deleterefs)
1af8b8c0 114 refspec_appendf(&rs, ":refs/tags/%s", ref);
50d829c1 115 else
1af8b8c0 116 refspec_appendf(&rs, "refs/tags/%s", ref);
50d829c1 117 } else if (deleterefs) {
20e41640 118 if (strchr(ref, ':') || !*ref)
50d829c1 119 die(_("--delete only accepts plain target ref names"));
1af8b8c0 120 refspec_appendf(&rs, ":%s", ref);
ca02465b
JH
121 } else if (!strchr(ref, ':')) {
122 if (!remote) {
123 /* lazily grab remote and local_refs */
124 remote = remote_get(repo);
125 local_refs = get_local_heads();
f517f1f2 126 }
30035d9c
RS
127 refspec_append_mapped(&rs, ref, remote, local_refs);
128 } else
129 refspec_append(&rs, ref);
755225de 130 }
755225de
LT
131}
132
135dadef
JH
133static int push_url_of_remote(struct remote *remote, const char ***url_p)
134{
135 if (remote->pushurl_nr) {
136 *url_p = remote->pushurl;
137 return remote->pushurl_nr;
138 }
139 *url_p = remote->url;
140 return remote->url_nr;
141}
142
dbcd970c
JS
143static NORETURN void die_push_simple(struct branch *branch,
144 struct remote *remote)
3b335762 145{
b55e6775
MM
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_maybe = "";
cf4fff57
JK
155 const char *short_upstream = branch->merge[0]->src;
156
157 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
b55e6775 158
b55e6775 159 /*
98e023de 160 * Don't show advice for people who explicitly set
b55e6775
MM
161 * push.default.
162 */
163 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
164 advice_maybe = _("\n"
165 "To choose either option permanently, "
166 "see push.default in 'git help config'.");
167 die(_("The upstream branch of your current branch does not match\n"
168 "the name of your current branch. To push to the upstream branch\n"
169 "on the remote, use\n"
170 "\n"
171 " git push %s HEAD:%s\n"
172 "\n"
173 "To push to the branch of the same name on the remote, use\n"
174 "\n"
82471667 175 " git push %s HEAD\n"
b55e6775
MM
176 "%s"),
177 remote->name, short_upstream,
82471667 178 remote->name, advice_maybe);
b55e6775
MM
179}
180
35ee69c0
RR
181static const char message_detached_head_die[] =
182 N_("You are not currently on a branch.\n"
183 "To push the history leading to the current (detached HEAD)\n"
184 "state now, use\n"
185 "\n"
186 " git push %s HEAD:<name-of-remote-branch>\n");
187
ed2b1829 188static void setup_push_upstream(struct remote *remote, struct branch *branch,
00a6fa07 189 int triangular, int simple)
52153747 190{
52153747 191 if (!branch)
35ee69c0 192 die(_(message_detached_head_die), remote->name);
135dadef 193 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
6c80cd29 194 die(_("The current branch %s has no upstream branch.\n"
ec8460bd
MM
195 "To push the current branch and set the remote as upstream, use\n"
196 "\n"
6c80cd29 197 " git push --set-upstream %s %s\n"),
ec8460bd
MM
198 branch->name,
199 remote->name,
52153747
FAG
200 branch->name);
201 if (branch->merge_nr != 1)
6c80cd29 202 die(_("The current branch %s has multiple upstream branches, "
8352d29e 203 "refusing to push."), branch->name);
ed2b1829 204 if (triangular)
135dadef
JH
205 die(_("You are pushing to remote '%s', which is not the upstream of\n"
206 "your current branch '%s', without telling me what to push\n"
207 "to update which remote branch."),
208 remote->name, branch->name);
ed2b1829 209
00a6fa07 210 if (simple) {
ed2b1829
RR
211 /* Additional safety */
212 if (strcmp(branch->refname, branch->merge[0]->src))
213 die_push_simple(branch, remote);
214 }
135dadef 215
1af8b8c0 216 refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
52153747
FAG
217}
218
ed2b1829
RR
219static void setup_push_current(struct remote *remote, struct branch *branch)
220{
221 if (!branch)
222 die(_(message_detached_head_die), remote->name);
1af8b8c0 223 refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
ed2b1829
RR
224}
225
ed2b1829
RR
226static int is_workflow_triangular(struct remote *remote)
227{
228 struct remote *fetch_remote = remote_get(NULL);
229 return (fetch_remote && fetch_remote != remote);
230}
231
ec8460bd 232static void setup_default_push_refspecs(struct remote *remote)
52153747 233{
ed2b1829
RR
234 struct branch *branch = branch_get(NULL);
235 int triangular = is_workflow_triangular(remote);
7b2ecd81 236
52153747 237 switch (push_default) {
bba0fd22 238 default:
52153747 239 case PUSH_DEFAULT_MATCHING:
aa40289c 240 refspec_append(&rs, ":");
52153747
FAG
241 break;
242
b2ed944a 243 case PUSH_DEFAULT_UNSPECIFIED:
b55e6775 244 case PUSH_DEFAULT_SIMPLE:
ed2b1829
RR
245 if (triangular)
246 setup_push_current(remote, branch);
247 else
00a6fa07 248 setup_push_upstream(remote, branch, triangular, 1);
b55e6775
MM
249 break;
250
53c40311 251 case PUSH_DEFAULT_UPSTREAM:
00a6fa07 252 setup_push_upstream(remote, branch, triangular, 0);
52153747
FAG
253 break;
254
255 case PUSH_DEFAULT_CURRENT:
ed2b1829 256 setup_push_current(remote, branch);
52153747
FAG
257 break;
258
259 case PUSH_DEFAULT_NOTHING:
8352d29e
ÆAB
260 die(_("You didn't specify any refspecs to push, and "
261 "push.default is \"nothing\"."));
52153747
FAG
262 break;
263 }
264}
265
f25950f3
CT
266static const char message_advice_pull_before_push[] =
267 N_("Updates were rejected because the tip of your current branch is behind\n"
fc6c4e96
JK
268 "its remote counterpart. Integrate the remote changes (e.g.\n"
269 "'git pull ...') before pushing again.\n"
f25950f3
CT
270 "See the 'Note about fast-forwards' in 'git push --help' for details.");
271
f25950f3
CT
272static const char message_advice_checkout_pull_push[] =
273 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
fc6c4e96
JK
274 "counterpart. Check out this branch and integrate the remote changes\n"
275 "(e.g. 'git pull ...') before pushing again.\n"
f25950f3
CT
276 "See the 'Note about fast-forwards' in 'git push --help' for details.");
277
75e5c0dc
JH
278static const char message_advice_ref_fetch_first[] =
279 N_("Updates were rejected because the remote contains work that you do\n"
280 "not have locally. This is usually caused by another repository pushing\n"
fc6c4e96
JK
281 "to the same ref. You may want to first integrate the remote changes\n"
282 "(e.g., 'git pull ...') before pushing again.\n"
75e5c0dc
JH
283 "See the 'Note about fast-forwards' in 'git push --help' for details.");
284
b24e6047 285static const char message_advice_ref_already_exists[] =
b4cf8db2 286 N_("Updates were rejected because the tag already exists in the remote.");
b24e6047 287
75e5c0dc
JH
288static const char message_advice_ref_needs_force[] =
289 N_("You cannot update a remote ref that points at a non-commit object,\n"
290 "or update a remote ref to make it point at a non-commit object,\n"
291 "without using the '--force' option.\n");
b24e6047 292
3b990aa6
SK
293static const char message_advice_ref_needs_update[] =
294 N_("Updates were rejected because the tip of the remote-tracking\n"
295 "branch has been updated since the last checkout. You may want\n"
296 "to integrate those changes locally (e.g., 'git pull ...')\n"
297 "before forcing an update.\n");
298
f25950f3
CT
299static void advise_pull_before_push(void)
300{
1184564e 301 if (!advice_push_non_ff_current || !advice_push_update_rejected)
f25950f3
CT
302 return;
303 advise(_(message_advice_pull_before_push));
304}
305
f25950f3
CT
306static void advise_checkout_pull_push(void)
307{
1184564e 308 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
f25950f3
CT
309 return;
310 advise(_(message_advice_checkout_pull_push));
311}
312
b24e6047
CR
313static void advise_ref_already_exists(void)
314{
b4505682
CR
315 if (!advice_push_already_exists || !advice_push_update_rejected)
316 return;
b24e6047
CR
317 advise(_(message_advice_ref_already_exists));
318}
319
75e5c0dc
JH
320static void advise_ref_fetch_first(void)
321{
322 if (!advice_push_fetch_first || !advice_push_update_rejected)
323 return;
324 advise(_(message_advice_ref_fetch_first));
325}
326
327static void advise_ref_needs_force(void)
328{
329 if (!advice_push_needs_force || !advice_push_update_rejected)
330 return;
331 advise(_(message_advice_ref_needs_force));
332}
333
3b990aa6
SK
334static void advise_ref_needs_update(void)
335{
336 if (!advice_push_ref_needs_update || !advice_push_update_rejected)
337 return;
338 advise(_(message_advice_ref_needs_update));
339}
340
aa40289c
BW
341static int push_with_options(struct transport *transport, struct refspec *rs,
342 int flags)
fb0cc87e
DB
343{
344 int err;
10643d4e 345 unsigned int reject_reasons;
d192fa50 346 char *anon_url = transport_anonymize_url(transport->url);
8afd8dc0 347
78381069 348 transport_set_verbosity(transport, verbosity, progress);
c915f11e 349 transport->family = family;
8afd8dc0 350
fb0cc87e
DB
351 if (receivepack)
352 transport_set_option(transport,
353 TRANS_OPT_RECEIVEPACK, receivepack);
f7c815c3 354 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
fb0cc87e 355
91048a95
JH
356 if (!is_empty_cas(&cas)) {
357 if (!transport->smart_options)
358 die("underlying transport does not support --%s option",
359 CAS_OPT_NAME);
360 transport->smart_options->cas = &cas;
361 }
362
8afd8dc0 363 if (verbosity > 0)
d192fa50 364 fprintf(stderr, _("Pushing to %s\n"), anon_url);
25e4b809 365 trace2_region_enter("push", "transport_push", the_repository);
6c6d5d07
NTND
366 err = transport_push(the_repository, transport,
367 rs, flags, &reject_reasons);
25e4b809 368 trace2_region_leave("push", "transport_push", the_repository);
960786e7
RD
369 if (err != 0) {
370 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
d192fa50 371 error(_("failed to push some refs to '%s'"), anon_url);
960786e7
RD
372 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
373 }
53970b92 374
fb0cc87e 375 err |= transport_disconnect(transport);
d192fa50 376 free(anon_url);
fb0cc87e
DB
377 if (!err)
378 return 0;
379
10643d4e 380 if (reject_reasons & REJECT_NON_FF_HEAD) {
f25950f3 381 advise_pull_before_push();
10643d4e 382 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
b2ed944a 383 advise_checkout_pull_push();
b24e6047
CR
384 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
385 advise_ref_already_exists();
75e5c0dc
JH
386 } else if (reject_reasons & REJECT_FETCH_FIRST) {
387 advise_ref_fetch_first();
388 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
389 advise_ref_needs_force();
3b990aa6
SK
390 } else if (reject_reasons & REJECT_REF_NEEDS_UPDATE) {
391 advise_ref_needs_update();
fb0cc87e
DB
392 }
393
394 return 1;
395}
396
5b9427e0 397static int do_push(int flags,
8e4c8af0
TG
398 const struct string_list *push_options,
399 struct remote *remote)
755225de 400{
5751f490 401 int i, errs;
20346234
MG
402 const char **url;
403 int url_nr;
aa40289c 404 struct refspec *push_refspec = &rs;
755225de 405
f6a4e61f
SB
406 if (push_options->nr)
407 flags |= TRANSPORT_PUSH_OPTIONS;
408
aa40289c
BW
409 if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) {
410 if (remote->push.nr) {
411 push_refspec = &remote->push;
52153747 412 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
ec8460bd 413 setup_default_push_refspecs(remote);
5751f490 414 }
fd1d1b05 415 errs = 0;
135dadef 416 url_nr = push_url_of_remote(remote, &url);
fb0cc87e
DB
417 if (url_nr) {
418 for (i = 0; i < url_nr; i++) {
419 struct transport *transport =
420 transport_get(remote, url[i]);
f6a4e61f
SB
421 if (flags & TRANSPORT_PUSH_OPTIONS)
422 transport->push_options = push_options;
aa40289c 423 if (push_with_options(transport, push_refspec, flags))
fb0cc87e 424 errs++;
07436e43 425 }
fb0cc87e
DB
426 } else {
427 struct transport *transport =
428 transport_get(remote, NULL);
f6a4e61f
SB
429 if (flags & TRANSPORT_PUSH_OPTIONS)
430 transport->push_options = push_options;
aa40289c 431 if (push_with_options(transport, push_refspec, flags))
fb0cc87e 432 errs++;
755225de 433 }
fd1d1b05 434 return !!errs;
755225de
LT
435}
436
d2b17b32
FG
437static int option_parse_recurse_submodules(const struct option *opt,
438 const char *arg, int unset)
439{
b33a15b0 440 int *recurse_submodules = opt->value;
eb21c732 441
b33a15b0
MC
442 if (unset)
443 *recurse_submodules = RECURSE_SUBMODULES_OFF;
b33a15b0 444 else
ce9baf23 445 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
d2b17b32
FG
446
447 return 0;
448}
449
68c757f2
DB
450static void set_push_cert_flags(int *flags, int v)
451{
452 switch (v) {
453 case SEND_PACK_PUSH_CERT_NEVER:
454 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
455 break;
456 case SEND_PACK_PUSH_CERT_ALWAYS:
457 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
458 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
459 break;
460 case SEND_PACK_PUSH_CERT_IF_ASKED:
461 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
462 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
463 break;
464 }
465}
466
467
b9459019
MG
468static int git_push_config(const char *k, const char *v, void *cb)
469{
960786e7 470 const char *slot_name;
a8bc269f 471 int *flags = cb;
b9459019
MG
472 int status;
473
474 status = git_gpg_config(k, v, NULL);
475 if (status)
476 return status;
a8bc269f
DO
477
478 if (!strcmp(k, "push.followtags")) {
479 if (git_config_bool(k, v))
480 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
481 else
482 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
483 return 0;
68c757f2
DB
484 } else if (!strcmp(k, "push.gpgsign")) {
485 const char *value;
486 if (!git_config_get_value("push.gpgsign", &value)) {
89576613 487 switch (git_parse_maybe_bool(value)) {
68c757f2
DB
488 case 0:
489 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
490 break;
491 case 1:
492 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
493 break;
494 default:
495 if (value && !strcasecmp(value, "if-asked"))
496 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
497 else
498 return error("Invalid value for '%s'", k);
499 }
500 }
b33a15b0
MC
501 } else if (!strcmp(k, "push.recursesubmodules")) {
502 const char *value;
503 if (!git_config_get_value("push.recursesubmodules", &value))
504 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
4e53d6a5
SB
505 } else if (!strcmp(k, "submodule.recurse")) {
506 int val = git_config_bool(k, v) ?
507 RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
508 recurse_submodules = val;
d8052750
MP
509 } else if (!strcmp(k, "push.pushoption")) {
510 if (!v)
511 return config_error_nonbool(k);
512 else
513 if (!*v)
514 string_list_clear(&push_options_config, 0);
515 else
516 string_list_append(&push_options_config, v);
517 return 0;
960786e7
RD
518 } else if (!strcmp(k, "color.push")) {
519 push_use_color = git_config_colorbool(k, v);
520 return 0;
521 } else if (skip_prefix(k, "color.push.", &slot_name)) {
522 int slot = parse_push_color_slot(slot_name);
523 if (slot < 0)
524 return 0;
525 if (!v)
526 return config_error_nonbool(k);
527 return color_parse(v, push_colors[slot]);
3b990aa6
SK
528 } else if (!strcmp(k, "push.useforceifincludes")) {
529 if (git_config_bool(k, v))
530 *flags |= TRANSPORT_PUSH_FORCE_IF_INCLUDES;
531 else
532 *flags &= ~TRANSPORT_PUSH_FORCE_IF_INCLUDES;
533 return 0;
a8bc269f
DO
534 }
535
06038cd7 536 return git_default_config(k, v, NULL);
b9459019
MG
537}
538
a633fca0 539int cmd_push(int argc, const char **argv, const char *prefix)
755225de 540{
9b288516 541 int flags = 0;
378c4832 542 int tags = 0;
30261094 543 int push_cert = -1;
84bb2dfd 544 int rc;
5751f490 545 const char *repo = NULL; /* default repository */
d8052750
MP
546 struct string_list push_options_cmdline = STRING_LIST_INIT_DUP;
547 struct string_list *push_options;
54cc8aca 548 const struct string_list_item *item;
8e4c8af0 549 struct remote *remote;
f6a4e61f 550
378c4832 551 struct option options[] = {
8afd8dc0 552 OPT__VERBOSITY(&verbosity),
78dafaa5
NTND
553 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
554 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
555 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
c29c1b40 556 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
38a25591 557 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
d5d09d47 558 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
78dafaa5
NTND
559 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
560 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
561 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
203c8533
DL
562 OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
563 N_("require old value of ref to be at this value"),
564 PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option),
3b990aa6
SK
565 OPT_BIT(0, TRANS_OPT_FORCE_IF_INCLUDES, &flags,
566 N_("require remote updates to be integrated locally"),
567 TRANSPORT_PUSH_FORCE_IF_INCLUDES),
ce9baf23
DL
568 OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
569 N_("control recursive pushing of submodules"), option_parse_recurse_submodules),
f1e1bdd6 570 OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
78dafaa5
NTND
571 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
572 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
573 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
e9fcd1e2 574 TRANSPORT_PUSH_SET_UPSTREAM),
78dafaa5
NTND
575 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
576 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
6ddba5e2 577 TRANSPORT_PUSH_PRUNE),
ec55559f 578 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
c2aba155
JH
579 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
580 TRANSPORT_PUSH_FOLLOW_TAGS),
203c8533
DL
581 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
582 PARSE_OPT_OPTARG, option_parse_push_signed),
d16c33b4 583 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
d8052750 584 OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")),
c915f11e
EW
585 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
586 TRANSPORT_FAMILY_IPV4),
587 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
588 TRANSPORT_FAMILY_IPV6),
378c4832
DB
589 OPT_END()
590 };
755225de 591
bbc30f99 592 packet_trace_identity("push");
06c21e18 593 git_config(git_push_config, &flags);
37782920 594 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
d8052750
MP
595 push_options = (push_options_cmdline.nr
596 ? &push_options_cmdline
597 : &push_options_config);
68c757f2 598 set_push_cert_flags(&flags, push_cert);
378c4832 599
f517f1f2 600 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
8352d29e 601 die(_("--delete is incompatible with --all, --mirror and --tags"));
f517f1f2 602 if (deleterefs && argc < 2)
8352d29e 603 die(_("--delete doesn't make sense without any refs"));
f517f1f2 604
b33a15b0
MC
605 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
606 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
607 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
608 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
225e8bf7
BW
609 else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
610 flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
b33a15b0 611
378c4832 612 if (tags)
aa40289c 613 refspec_append(&rs, "refs/tags/*");
378c4832
DB
614
615 if (argc > 0) {
616 repo = argv[0];
ca02465b 617 set_refspecs(argv + 1, argc - 1, repo);
755225de 618 }
8558fd9e 619
8e4c8af0
TG
620 remote = pushremote_get(repo);
621 if (!remote) {
622 if (repo)
623 die(_("bad repository '%s'"), repo);
624 die(_("No configured push destination.\n"
625 "Either specify the URL from the command-line or configure a remote repository using\n"
626 "\n"
627 " git remote add <name> <url>\n"
628 "\n"
629 "and then push using the remote name\n"
630 "\n"
631 " git push <name>\n"));
632 }
633
634 if (remote->mirror)
635 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
636
637 if (flags & TRANSPORT_PUSH_ALL) {
638 if (tags)
639 die(_("--all and --tags are incompatible"));
640 if (argc >= 2)
641 die(_("--all can't be combined with refspecs"));
642 }
643 if (flags & TRANSPORT_PUSH_MIRROR) {
644 if (tags)
645 die(_("--mirror and --tags are incompatible"));
646 if (argc >= 2)
647 die(_("--mirror can't be combined with refspecs"));
648 }
649 if ((flags & TRANSPORT_PUSH_ALL) && (flags & TRANSPORT_PUSH_MIRROR))
650 die(_("--all and --mirror are incompatible"));
651
3b990aa6
SK
652 if (!is_empty_cas(&cas) && (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES))
653 cas.use_force_if_includes = 1;
654
d8052750 655 for_each_string_list_item(item, push_options)
f6a4e61f
SB
656 if (strchr(item->string, '\n'))
657 die(_("push options must not have new line characters"));
658
5b9427e0 659 rc = do_push(flags, push_options, remote);
d8052750
MP
660 string_list_clear(&push_options_cmdline, 0);
661 string_list_clear(&push_options_config, 0);
84bb2dfd 662 if (rc == -1)
94c89ba6 663 usage_with_options(push_usage, options);
84bb2dfd
PB
664 else
665 return rc;
755225de 666}