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