blob: d09a42062c36d930b8147d6b0a4bcfedf34a2321 [file] [log] [blame]
Linus Torvalds755225d2006-04-29 21:22:49 -07001/*
2 * "git push"
3 */
4#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07005#include "config.h"
Linus Torvalds755225d2006-04-29 21:22:49 -07006#include "refs.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -07007#include "refspec.h"
Linus Torvalds755225d2006-04-29 21:22:49 -07008#include "run-command.h"
9#include "builtin.h"
Daniel Barkalow5751f492007-05-12 11:45:53 -040010#include "remote.h"
Daniel Barkalow9b288512007-09-10 23:03:04 -040011#include "transport.h"
Daniel Barkalow378c4832007-11-04 22:35:37 -050012#include "parse-options.h"
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +020013#include "submodule.h"
Mike Croweb33a15b2015-11-17 11:05:56 +000014#include "submodule-config.h"
Dave Borowitz30261092015-08-19 11:26:46 -040015#include "send-pack.h"
Ryan Dammrose960786e2018-04-21 12:10:00 +020016#include "color.h"
Linus Torvalds755225d2006-04-29 21:22:49 -070017
Daniel Barkalow378c4832007-11-04 22:35:37 -050018static const char * const push_usage[] = {
Nguyễn Thái Ngọc Duy78dafaa2012-08-20 19:32:33 +070019 N_("git push [<options>] [<repository> [<refspec>...]]"),
Daniel Barkalow378c4832007-11-04 22:35:37 -050020 NULL,
21};
Linus Torvalds755225d2006-04-29 21:22:49 -070022
Ryan Dammrose960786e2018-04-21 12:10:00 +020023static 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
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +070050static int thin = 1;
Jan Krügerf517f1f2009-12-30 20:57:42 +010051static int deleterefs;
Uwe Kleine-Königd23842f2007-01-19 13:49:27 +010052static const char *receivepack;
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +080053static int verbosity;
Mike Crowed34141c2015-12-03 13:10:35 +000054static int progress = -1;
55static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
Eric Wongc915f112016-02-03 04:09:14 +000056static enum transport_family family;
Linus Torvalds755225d2006-04-29 21:22:49 -070057
Junio C Hamano28f5d172013-07-08 15:34:36 -070058static struct push_cas_option cas;
59
Brandon Williamsaa402892018-05-16 15:58:16 -070060static struct refspec rs = REFSPEC_INIT_PUSH;
Linus Torvalds755225d2006-04-29 21:22:49 -070061
Marius Paligad8052752017-10-23 13:44:49 +020062static struct string_list push_options_config = STRING_LIST_INIT_DUP;
63
Junio C Hamanoca024652013-12-03 15:41:15 -080064static const char *map_refspec(const char *ref,
65 struct remote *remote, struct ref *local_refs)
Linus Torvalds755225d2006-04-29 21:22:49 -070066{
Junio C Hamanoca024652013-12-03 15:41:15 -080067 struct ref *matched = NULL;
68
69 /* Does "ref" uniquely name our ref? */
70 if (count_refspec_match(ref, local_refs, &matched) != 1)
71 return ref;
72
Brandon Williams6bdb3042018-05-16 15:58:00 -070073 if (remote->push.nr) {
Brandon Williams0ad4a5f2018-05-16 15:57:49 -070074 struct refspec_item query;
75 memset(&query, 0, sizeof(struct refspec_item));
Junio C Hamanoca024652013-12-03 15:41:15 -080076 query.src = matched->name;
Brandon Williams86baf822018-05-16 15:58:12 -070077 if (!query_refspecs(&remote->push, &query) && query.dst) {
Junio C Hamanoca024652013-12-03 15:41:15 -080078 struct strbuf buf = STRBUF_INIT;
79 strbuf_addf(&buf, "%s%s:%s",
80 query.force ? "+" : "",
81 query.src, query.dst);
82 return strbuf_detach(&buf, NULL);
83 }
84 }
85
Junio C Hamanofc9261c2013-12-03 16:23:35 -080086 if (push_default == PUSH_DEFAULT_UPSTREAM &&
Junio C Hamanoe3f11852014-03-07 15:18:17 -080087 starts_with(matched->name, "refs/heads/")) {
Junio C Hamanofc9261c2013-12-03 16:23:35 -080088 struct branch *branch = branch_get(matched->name + 11);
89 if (branch->merge_nr == 1 && branch->merge[0]->src) {
90 struct strbuf buf = STRBUF_INIT;
91 strbuf_addf(&buf, "%s:%s",
92 ref, branch->merge[0]->src);
93 return strbuf_detach(&buf, NULL);
94 }
95 }
96
Junio C Hamanoca024652013-12-03 15:41:15 -080097 return ref;
98}
99
100static void set_refspecs(const char **refs, int nr, const char *repo)
101{
102 struct remote *remote = NULL;
103 struct ref *local_refs = NULL;
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400104 int i;
Junio C Hamanoca024652013-12-03 15:41:15 -0800105
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400106 for (i = 0; i < nr; i++) {
107 const char *ref = refs[i];
108 if (!strcmp("tag", ref)) {
Junio C Hamano50d829c2013-12-03 14:33:10 -0800109 struct strbuf tagref = STRBUF_INIT;
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400110 if (nr <= ++i)
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000111 die(_("tag shorthand without <tag>"));
Junio C Hamano50d829c2013-12-03 14:33:10 -0800112 ref = refs[i];
113 if (deleterefs)
114 strbuf_addf(&tagref, ":refs/tags/%s", ref);
115 else
116 strbuf_addf(&tagref, "refs/tags/%s", ref);
117 ref = strbuf_detach(&tagref, NULL);
118 } else if (deleterefs) {
119 struct strbuf delref = STRBUF_INIT;
120 if (strchr(ref, ':'))
121 die(_("--delete only accepts plain target ref names"));
122 strbuf_addf(&delref, ":%s", ref);
123 ref = strbuf_detach(&delref, NULL);
Junio C Hamanoca024652013-12-03 15:41:15 -0800124 } else if (!strchr(ref, ':')) {
125 if (!remote) {
126 /* lazily grab remote and local_refs */
127 remote = remote_get(repo);
128 local_refs = get_local_heads();
Jan Krügerf517f1f2009-12-30 20:57:42 +0100129 }
Junio C Hamanoca024652013-12-03 15:41:15 -0800130 ref = map_refspec(ref, remote, local_refs);
Junio C Hamano50d829c2013-12-03 14:33:10 -0800131 }
Brandon Williamsaa402892018-05-16 15:58:16 -0700132 refspec_append(&rs, ref);
Linus Torvalds755225d2006-04-29 21:22:49 -0700133 }
Linus Torvalds755225d2006-04-29 21:22:49 -0700134}
135
Junio C Hamano135dade2012-03-30 16:07:12 -0700136static int push_url_of_remote(struct remote *remote, const char ***url_p)
137{
138 if (remote->pushurl_nr) {
139 *url_p = remote->pushurl;
140 return remote->pushurl_nr;
141 }
142 *url_p = remote->url;
143 return remote->url_nr;
144}
145
Matthieu Moyb55e6772012-04-24 09:50:03 +0200146static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
147 /*
148 * There's no point in using shorten_unambiguous_ref here,
149 * as the ambiguity would be on the remote side, not what
150 * we have locally. Plus, this is supposed to be the simple
151 * mode. If the user is doing something crazy like setting
152 * upstream to a non-branch, we should probably be showing
153 * them the big ugly fully qualified ref.
154 */
155 const char *advice_maybe = "";
Jeff Kingcf4fff52014-06-18 15:44:19 -0400156 const char *short_upstream = branch->merge[0]->src;
Matthieu Moyb55e6772012-04-24 09:50:03 +0200157
Jeff Kingcf4fff52014-06-18 15:44:19 -0400158 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
159
Matthieu Moyb55e6772012-04-24 09:50:03 +0200160 /*
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200161 * Don't show advice for people who explicitly set
Matthieu Moyb55e6772012-04-24 09:50:03 +0200162 * push.default.
163 */
164 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
165 advice_maybe = _("\n"
166 "To choose either option permanently, "
167 "see push.default in 'git help config'.");
168 die(_("The upstream branch of your current branch does not match\n"
169 "the name of your current branch. To push to the upstream branch\n"
170 "on the remote, use\n"
171 "\n"
172 " git push %s HEAD:%s\n"
173 "\n"
174 "To push to the branch of the same name on the remote, use\n"
175 "\n"
176 " git push %s %s\n"
177 "%s"),
178 remote->name, short_upstream,
179 remote->name, branch->name, advice_maybe);
180}
181
Ramkumar Ramachandra35ee69c2013-05-30 00:51:49 +0530182static const char message_detached_head_die[] =
183 N_("You are not currently on a branch.\n"
184 "To push the history leading to the current (detached HEAD)\n"
185 "state now, use\n"
186 "\n"
187 " git push %s HEAD:<name-of-remote-branch>\n");
188
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530189static void setup_push_upstream(struct remote *remote, struct branch *branch,
Jeff King00a6fa02014-11-26 22:43:06 -0500190 int triangular, int simple)
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100191{
192 struct strbuf refspec = STRBUF_INIT;
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530193
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100194 if (!branch)
Ramkumar Ramachandra35ee69c2013-05-30 00:51:49 +0530195 die(_(message_detached_head_die), remote->name);
Junio C Hamano135dade2012-03-30 16:07:12 -0700196 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700197 die(_("The current branch %s has no upstream branch.\n"
Matthieu Moyec8460b2011-03-02 21:12:10 +0100198 "To push the current branch and set the remote as upstream, use\n"
199 "\n"
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700200 " git push --set-upstream %s %s\n"),
Matthieu Moyec8460b2011-03-02 21:12:10 +0100201 branch->name,
202 remote->name,
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100203 branch->name);
204 if (branch->merge_nr != 1)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700205 die(_("The current branch %s has multiple upstream branches, "
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000206 "refusing to push."), branch->name);
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530207 if (triangular)
Junio C Hamano135dade2012-03-30 16:07:12 -0700208 die(_("You are pushing to remote '%s', which is not the upstream of\n"
209 "your current branch '%s', without telling me what to push\n"
210 "to update which remote branch."),
211 remote->name, branch->name);
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530212
Jeff King00a6fa02014-11-26 22:43:06 -0500213 if (simple) {
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530214 /* Additional safety */
215 if (strcmp(branch->refname, branch->merge[0]->src))
216 die_push_simple(branch, remote);
217 }
Junio C Hamano135dade2012-03-30 16:07:12 -0700218
Junio C Hamanoeef2bda2016-10-28 12:25:30 -0700219 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
Brandon Williamsaa402892018-05-16 15:58:16 -0700220 refspec_append(&rs, refspec.buf);
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100221}
222
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530223static void setup_push_current(struct remote *remote, struct branch *branch)
224{
Junio C Hamanoeef2bda2016-10-28 12:25:30 -0700225 struct strbuf refspec = STRBUF_INIT;
226
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530227 if (!branch)
228 die(_(message_detached_head_die), remote->name);
Junio C Hamanoeef2bda2016-10-28 12:25:30 -0700229 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
Brandon Williamsaa402892018-05-16 15:58:16 -0700230 refspec_append(&rs, refspec.buf);
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530231}
232
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530233static int is_workflow_triangular(struct remote *remote)
234{
235 struct remote *fetch_remote = remote_get(NULL);
236 return (fetch_remote && fetch_remote != remote);
237}
238
Matthieu Moyec8460b2011-03-02 21:12:10 +0100239static void setup_default_push_refspecs(struct remote *remote)
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100240{
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530241 struct branch *branch = branch_get(NULL);
242 int triangular = is_workflow_triangular(remote);
Ramkumar Ramachandra7b2ecd82013-05-30 00:51:50 +0530243
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100244 switch (push_default) {
Junio C Hamanobba0fd22009-07-18 17:19:47 -0700245 default:
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100246 case PUSH_DEFAULT_MATCHING:
Brandon Williamsaa402892018-05-16 15:58:16 -0700247 refspec_append(&rs, ":");
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100248 break;
249
Junio C Hamanob2ed9442013-01-04 16:02:29 -0800250 case PUSH_DEFAULT_UNSPECIFIED:
Matthieu Moyb55e6772012-04-24 09:50:03 +0200251 case PUSH_DEFAULT_SIMPLE:
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530252 if (triangular)
253 setup_push_current(remote, branch);
254 else
Jeff King00a6fa02014-11-26 22:43:06 -0500255 setup_push_upstream(remote, branch, triangular, 1);
Matthieu Moyb55e6772012-04-24 09:50:03 +0200256 break;
257
Johan Herland53c40312011-02-16 01:54:24 +0100258 case PUSH_DEFAULT_UPSTREAM:
Jeff King00a6fa02014-11-26 22:43:06 -0500259 setup_push_upstream(remote, branch, triangular, 0);
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100260 break;
261
262 case PUSH_DEFAULT_CURRENT:
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530263 setup_push_current(remote, branch);
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100264 break;
265
266 case PUSH_DEFAULT_NOTHING:
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000267 die(_("You didn't specify any refspecs to push, and "
268 "push.default is \"nothing\"."));
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100269 break;
270 }
271}
272
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400273static const char message_advice_pull_before_push[] =
274 N_("Updates were rejected because the tip of your current branch is behind\n"
John Keepingfc6c4e92013-07-07 20:02:14 +0100275 "its remote counterpart. Integrate the remote changes (e.g.\n"
276 "'git pull ...') before pushing again.\n"
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400277 "See the 'Note about fast-forwards' in 'git push --help' for details.");
278
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400279static const char message_advice_checkout_pull_push[] =
280 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
John Keepingfc6c4e92013-07-07 20:02:14 +0100281 "counterpart. Check out this branch and integrate the remote changes\n"
282 "(e.g. 'git pull ...') before pushing again.\n"
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400283 "See the 'Note about fast-forwards' in 'git push --help' for details.");
284
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800285static const char message_advice_ref_fetch_first[] =
286 N_("Updates were rejected because the remote contains work that you do\n"
287 "not have locally. This is usually caused by another repository pushing\n"
John Keepingfc6c4e92013-07-07 20:02:14 +0100288 "to the same ref. You may want to first integrate the remote changes\n"
289 "(e.g., 'git pull ...') before pushing again.\n"
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800290 "See the 'Note about fast-forwards' in 'git push --help' for details.");
291
Chris Rorvickb24e6042012-11-29 19:41:34 -0600292static const char message_advice_ref_already_exists[] =
Junio C Hamanob4cf8db2013-01-24 21:09:00 -0800293 N_("Updates were rejected because the tag already exists in the remote.");
Chris Rorvickb24e6042012-11-29 19:41:34 -0600294
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800295static const char message_advice_ref_needs_force[] =
296 N_("You cannot update a remote ref that points at a non-commit object,\n"
297 "or update a remote ref to make it point at a non-commit object,\n"
298 "without using the '--force' option.\n");
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400299
300static void advise_pull_before_push(void)
301{
Chris Rorvick11845642012-12-02 21:27:50 -0600302 if (!advice_push_non_ff_current || !advice_push_update_rejected)
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400303 return;
304 advise(_(message_advice_pull_before_push));
305}
306
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400307static void advise_checkout_pull_push(void)
308{
Chris Rorvick11845642012-12-02 21:27:50 -0600309 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400310 return;
311 advise(_(message_advice_checkout_pull_push));
312}
313
Chris Rorvickb24e6042012-11-29 19:41:34 -0600314static void advise_ref_already_exists(void)
315{
Chris Rorvickb4505682012-12-02 21:27:51 -0600316 if (!advice_push_already_exists || !advice_push_update_rejected)
317 return;
Chris Rorvickb24e6042012-11-29 19:41:34 -0600318 advise(_(message_advice_ref_already_exists));
319}
320
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800321static void advise_ref_fetch_first(void)
322{
323 if (!advice_push_fetch_first || !advice_push_update_rejected)
324 return;
325 advise(_(message_advice_ref_fetch_first));
326}
327
328static void advise_ref_needs_force(void)
329{
330 if (!advice_push_needs_force || !advice_push_update_rejected)
331 return;
332 advise(_(message_advice_ref_needs_force));
333}
334
Brandon Williamsaa402892018-05-16 15:58:16 -0700335static int push_with_options(struct transport *transport, struct refspec *rs,
336 int flags)
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100337{
338 int err;
Chris Rorvick10643d42012-11-29 19:41:33 -0600339 unsigned int reject_reasons;
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800340
Tay Ray Chuan78381062010-02-24 20:50:27 +0800341 transport_set_verbosity(transport, verbosity, progress);
Eric Wongc915f112016-02-03 04:09:14 +0000342 transport->family = family;
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800343
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100344 if (receivepack)
345 transport_set_option(transport,
346 TRANS_OPT_RECEIVEPACK, receivepack);
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +0700347 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100348
Junio C Hamano91048a92013-07-09 11:01:06 -0700349 if (!is_empty_cas(&cas)) {
350 if (!transport->smart_options)
351 die("underlying transport does not support --%s option",
352 CAS_OPT_NAME);
353 transport->smart_options->cas = &cas;
354 }
355
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800356 if (verbosity > 0)
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000357 fprintf(stderr, _("Pushing to %s\n"), transport->url);
Brandon Williams306f22d2018-05-16 15:58:17 -0700358 err = transport_push(transport, rs, flags, &reject_reasons);
Ryan Dammrose960786e2018-04-21 12:10:00 +0200359 if (err != 0) {
360 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000361 error(_("failed to push some refs to '%s'"), transport->url);
Ryan Dammrose960786e2018-04-21 12:10:00 +0200362 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
363 }
Tay Ray Chuan53970b92009-12-04 07:31:44 +0800364
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100365 err |= transport_disconnect(transport);
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100366 if (!err)
367 return 0;
368
Chris Rorvick10643d42012-11-29 19:41:33 -0600369 if (reject_reasons & REJECT_NON_FF_HEAD) {
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400370 advise_pull_before_push();
Chris Rorvick10643d42012-11-29 19:41:33 -0600371 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
Junio C Hamanob2ed9442013-01-04 16:02:29 -0800372 advise_checkout_pull_push();
Chris Rorvickb24e6042012-11-29 19:41:34 -0600373 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
374 advise_ref_already_exists();
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800375 } else if (reject_reasons & REJECT_FETCH_FIRST) {
376 advise_ref_fetch_first();
377 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
378 advise_ref_needs_force();
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100379 }
380
381 return 1;
382}
383
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700384static int do_push(const char *repo, int flags,
385 const struct string_list *push_options)
Linus Torvalds755225d2006-04-29 21:22:49 -0700386{
Daniel Barkalow5751f492007-05-12 11:45:53 -0400387 int i, errs;
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +0530388 struct remote *remote = pushremote_get(repo);
Michael J Gruber20346232009-06-09 18:01:34 +0200389 const char **url;
390 int url_nr;
Brandon Williamsaa402892018-05-16 15:58:16 -0700391 struct refspec *push_refspec = &rs;
Linus Torvalds755225d2006-04-29 21:22:49 -0700392
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400393 if (!remote) {
394 if (repo)
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000395 die(_("bad repository '%s'"), repo);
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700396 die(_("No configured push destination.\n"
Matthieu Moya3f5e7a2011-03-02 21:12:11 +0100397 "Either specify the URL from the command-line or configure a remote repository using\n"
398 "\n"
399 " git remote add <name> <url>\n"
400 "\n"
401 "and then push using the remote name\n"
402 "\n"
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700403 " git push <name>\n"));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400404 }
Linus Torvalds755225d2006-04-29 21:22:49 -0700405
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200406 if (remote->mirror)
407 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
408
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700409 if (push_options->nr)
410 flags |= TRANSPORT_PUSH_OPTIONS;
411
Brandon Williamsaa402892018-05-16 15:58:16 -0700412 if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) {
413 if (remote->push.nr) {
414 push_refspec = &remote->push;
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100415 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
Matthieu Moyec8460b2011-03-02 21:12:10 +0100416 setup_default_push_refspecs(remote);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400417 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -0700418 errs = 0;
Junio C Hamano135dade2012-03-30 16:07:12 -0700419 url_nr = push_url_of_remote(remote, &url);
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100420 if (url_nr) {
421 for (i = 0; i < url_nr; i++) {
422 struct transport *transport =
423 transport_get(remote, url[i]);
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700424 if (flags & TRANSPORT_PUSH_OPTIONS)
425 transport->push_options = push_options;
Brandon Williamsaa402892018-05-16 15:58:16 -0700426 if (push_with_options(transport, push_refspec, flags))
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100427 errs++;
Matthieu Moy07436e42009-08-08 09:51:08 +0200428 }
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100429 } else {
430 struct transport *transport =
431 transport_get(remote, NULL);
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700432 if (flags & TRANSPORT_PUSH_OPTIONS)
433 transport->push_options = push_options;
Brandon Williamsaa402892018-05-16 15:58:16 -0700434 if (push_with_options(transport, push_refspec, flags))
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100435 errs++;
Linus Torvalds755225d2006-04-29 21:22:49 -0700436 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -0700437 return !!errs;
Linus Torvalds755225d2006-04-29 21:22:49 -0700438}
439
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200440static int option_parse_recurse_submodules(const struct option *opt,
441 const char *arg, int unset)
442{
Mike Croweb33a15b2015-11-17 11:05:56 +0000443 int *recurse_submodules = opt->value;
Heiko Voigteb21c732012-03-29 09:21:24 +0200444
Mike Croweb33a15b2015-11-17 11:05:56 +0000445 if (unset)
446 *recurse_submodules = RECURSE_SUBMODULES_OFF;
447 else if (arg)
448 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
449 else
450 die("%s missing parameter", opt->long_name);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200451
452 return 0;
453}
454
Dave Borowitz68c757f2015-08-19 11:26:47 -0400455static void set_push_cert_flags(int *flags, int v)
456{
457 switch (v) {
458 case SEND_PACK_PUSH_CERT_NEVER:
459 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
460 break;
461 case SEND_PACK_PUSH_CERT_ALWAYS:
462 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
463 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
464 break;
465 case SEND_PACK_PUSH_CERT_IF_ASKED:
466 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
467 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
468 break;
469 }
470}
471
472
Michael J Gruberb9459012014-10-22 16:57:49 +0200473static int git_push_config(const char *k, const char *v, void *cb)
474{
Ryan Dammrose960786e2018-04-21 12:10:00 +0200475 const char *slot_name;
Dave Olszewskia8bc2692015-02-16 01:16:19 -0500476 int *flags = cb;
Michael J Gruberb9459012014-10-22 16:57:49 +0200477 int status;
478
479 status = git_gpg_config(k, v, NULL);
480 if (status)
481 return status;
Dave Olszewskia8bc2692015-02-16 01:16:19 -0500482
483 if (!strcmp(k, "push.followtags")) {
484 if (git_config_bool(k, v))
485 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
486 else
487 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
488 return 0;
Dave Borowitz68c757f2015-08-19 11:26:47 -0400489 } else if (!strcmp(k, "push.gpgsign")) {
490 const char *value;
491 if (!git_config_get_value("push.gpgsign", &value)) {
Martin Ågren89576612017-08-07 20:20:49 +0200492 switch (git_parse_maybe_bool(value)) {
Dave Borowitz68c757f2015-08-19 11:26:47 -0400493 case 0:
494 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
495 break;
496 case 1:
497 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
498 break;
499 default:
500 if (value && !strcasecmp(value, "if-asked"))
501 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
502 else
503 return error("Invalid value for '%s'", k);
504 }
505 }
Mike Croweb33a15b2015-11-17 11:05:56 +0000506 } else if (!strcmp(k, "push.recursesubmodules")) {
507 const char *value;
508 if (!git_config_get_value("push.recursesubmodules", &value))
509 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
Stefan Beller4e53d6a2017-05-31 17:30:49 -0700510 } else if (!strcmp(k, "submodule.recurse")) {
511 int val = git_config_bool(k, v) ?
512 RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
513 recurse_submodules = val;
Marius Paligad8052752017-10-23 13:44:49 +0200514 } else if (!strcmp(k, "push.pushoption")) {
515 if (!v)
516 return config_error_nonbool(k);
517 else
518 if (!*v)
519 string_list_clear(&push_options_config, 0);
520 else
521 string_list_append(&push_options_config, v);
522 return 0;
Ryan Dammrose960786e2018-04-21 12:10:00 +0200523 } else if (!strcmp(k, "color.push")) {
524 push_use_color = git_config_colorbool(k, v);
525 return 0;
526 } else if (skip_prefix(k, "color.push.", &slot_name)) {
527 int slot = parse_push_color_slot(slot_name);
528 if (slot < 0)
529 return 0;
530 if (!v)
531 return config_error_nonbool(k);
532 return color_parse(v, push_colors[slot]);
Dave Olszewskia8bc2692015-02-16 01:16:19 -0500533 }
534
Jeff King06038cd2015-02-16 00:46:30 -0500535 return git_default_config(k, v, NULL);
Michael J Gruberb9459012014-10-22 16:57:49 +0200536}
537
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700538int cmd_push(int argc, const char **argv, const char *prefix)
Linus Torvalds755225d2006-04-29 21:22:49 -0700539{
Daniel Barkalow9b288512007-09-10 23:03:04 -0400540 int flags = 0;
Daniel Barkalow378c4832007-11-04 22:35:37 -0500541 int tags = 0;
Dave Borowitz30261092015-08-19 11:26:46 -0400542 int push_cert = -1;
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200543 int rc;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400544 const char *repo = NULL; /* default repository */
Marius Paligad8052752017-10-23 13:44:49 +0200545 struct string_list push_options_cmdline = STRING_LIST_INIT_DUP;
546 struct string_list *push_options;
Brandon Williams54cc8ac2017-03-31 16:56:22 -0700547 const struct string_list_item *item;
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700548
Daniel Barkalow378c4832007-11-04 22:35:37 -0500549 struct option options[] = {
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800550 OPT__VERBOSITY(&verbosity),
Nguyễn Thái Ngọc Duy78dafaa2012-08-20 19:32:33 +0700551 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
552 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
553 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
Michele Ballabioc29c1b42008-07-20 14:02:20 +0200554 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
Patrick Steinhardt38a25592015-12-14 16:23:04 +0100555 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200556 OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
Nguyễn Thái Ngọc Duy78dafaa2012-08-20 19:32:33 +0700557 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
558 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
559 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
Junio C Hamano28f5d172013-07-08 15:34:36 -0700560 { OPTION_CALLBACK,
Ævar Arnfjörð Bjarmasonc67318e2018-08-02 00:31:33 +0200561 0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
Junio C Hamano28f5d172013-07-08 15:34:36 -0700562 N_("require old value of ref to be at this value"),
Ævar Arnfjörð Bjarmasonc67318e2018-08-02 00:31:33 +0200563 PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
René Scharfebbc072f2018-08-19 19:34:48 +0200564 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
Nguyễn Thái Ngọc Duyf63cf8c2012-08-20 19:32:55 +0700565 N_("control recursive pushing of submodules"),
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200566 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
Nguyễn Thái Ngọc Duyf1e1bdd2018-02-09 18:02:10 +0700567 OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
Nguyễn Thái Ngọc Duy78dafaa2012-08-20 19:32:33 +0700568 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
569 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
570 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
Ilari Liusvaarae9fcd1e2010-01-16 23:45:31 +0200571 TRANSPORT_PUSH_SET_UPSTREAM),
Nguyễn Thái Ngọc Duy78dafaa2012-08-20 19:32:33 +0700572 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
573 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
Felipe Contreras6ddba5e2012-02-23 00:43:41 +0200574 TRANSPORT_PUSH_PRUNE),
Aaron Schrabec555592013-01-13 00:17:03 -0500575 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
Junio C Hamanoc2aba152013-03-04 12:09:50 -0800576 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
577 TRANSPORT_PUSH_FOLLOW_TAGS),
Dave Borowitz30261092015-08-19 11:26:46 -0400578 { OPTION_CALLBACK,
René Scharfebbc072f2018-08-19 19:34:48 +0200579 0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
Dave Borowitz30261092015-08-19 11:26:46 -0400580 PARSE_OPT_OPTARG, option_parse_push_signed },
Jeff Kingd16c33b2015-02-16 01:12:04 -0500581 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
Marius Paligad8052752017-10-23 13:44:49 +0200582 OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")),
Eric Wongc915f112016-02-03 04:09:14 +0000583 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
584 TRANSPORT_FAMILY_IPV4),
585 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
586 TRANSPORT_FAMILY_IPV6),
Daniel Barkalow378c4832007-11-04 22:35:37 -0500587 OPT_END()
588 };
Linus Torvalds755225d2006-04-29 21:22:49 -0700589
Jeff Kingbbc30f92011-02-24 09:30:19 -0500590 packet_trace_identity("push");
Jeff King06c21e182015-02-16 01:13:25 -0500591 git_config(git_push_config, &flags);
Stephen Boyd37782922009-05-23 11:53:12 -0700592 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
Marius Paligad8052752017-10-23 13:44:49 +0200593 push_options = (push_options_cmdline.nr
594 ? &push_options_cmdline
595 : &push_options_config);
Dave Borowitz68c757f2015-08-19 11:26:47 -0400596 set_push_cert_flags(&flags, push_cert);
Daniel Barkalow378c4832007-11-04 22:35:37 -0500597
Jan Krügerf517f1f2009-12-30 20:57:42 +0100598 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000599 die(_("--delete is incompatible with --all, --mirror and --tags"));
Jan Krügerf517f1f2009-12-30 20:57:42 +0100600 if (deleterefs && argc < 2)
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000601 die(_("--delete doesn't make sense without any refs"));
Brandon Williams800a4ab2018-05-16 15:58:15 -0700602 if (flags & TRANSPORT_PUSH_ALL) {
603 if (tags)
604 die(_("--all and --tags are incompatible"));
605 if (argc >= 2)
606 die(_("--all can't be combined with refspecs"));
607 }
608 if (flags & TRANSPORT_PUSH_MIRROR) {
609 if (tags)
610 die(_("--mirror and --tags are incompatible"));
611 if (argc >= 2)
612 die(_("--mirror can't be combined with refspecs"));
613 }
614 if ((flags & TRANSPORT_PUSH_ALL) && (flags & TRANSPORT_PUSH_MIRROR))
615 die(_("--all and --mirror are incompatible"));
Jan Krügerf517f1f2009-12-30 20:57:42 +0100616
Mike Croweb33a15b2015-11-17 11:05:56 +0000617 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
618 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
619 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
620 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
Brandon Williams225e8bf2016-12-19 10:25:33 -0800621 else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
622 flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
Mike Croweb33a15b2015-11-17 11:05:56 +0000623
Daniel Barkalow378c4832007-11-04 22:35:37 -0500624 if (tags)
Brandon Williamsaa402892018-05-16 15:58:16 -0700625 refspec_append(&rs, "refs/tags/*");
Daniel Barkalow378c4832007-11-04 22:35:37 -0500626
627 if (argc > 0) {
628 repo = argv[0];
Junio C Hamanoca024652013-12-03 15:41:15 -0800629 set_refspecs(argv + 1, argc - 1, repo);
Linus Torvalds755225d2006-04-29 21:22:49 -0700630 }
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400631
Marius Paligad8052752017-10-23 13:44:49 +0200632 for_each_string_list_item(item, push_options)
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700633 if (strchr(item->string, '\n'))
634 die(_("push options must not have new line characters"));
635
Marius Paligad8052752017-10-23 13:44:49 +0200636 rc = do_push(repo, flags, push_options);
637 string_list_clear(&push_options_cmdline, 0);
638 string_list_clear(&push_options_config, 0);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200639 if (rc == -1)
Andy Whitcroft94c89ba2007-11-09 23:32:25 +0000640 usage_with_options(push_usage, options);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200641 else
642 return rc;
Linus Torvalds755225d2006-04-29 21:22:49 -0700643}