blob: 6dbf0f0bb713f167947e8d3d3aa0b8ecae0dc44e [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{
René Scharfe1768aaf2019-11-26 16:18:28 +010067 const char *branch_name;
Junio C Hamanoca024652013-12-03 15:41:15 -080068 struct ref *matched = NULL;
69
70 /* Does "ref" uniquely name our ref? */
71 if (count_refspec_match(ref, local_refs, &matched) != 1)
72 return ref;
73
Brandon Williams6bdb3042018-05-16 15:58:00 -070074 if (remote->push.nr) {
Brandon Williams0ad4a5f2018-05-16 15:57:49 -070075 struct refspec_item query;
76 memset(&query, 0, sizeof(struct refspec_item));
Junio C Hamanoca024652013-12-03 15:41:15 -080077 query.src = matched->name;
Brandon Williams86baf822018-05-16 15:58:12 -070078 if (!query_refspecs(&remote->push, &query) && query.dst) {
Junio C Hamanoca024652013-12-03 15:41:15 -080079 struct strbuf buf = STRBUF_INIT;
80 strbuf_addf(&buf, "%s%s:%s",
81 query.force ? "+" : "",
82 query.src, query.dst);
83 return strbuf_detach(&buf, NULL);
84 }
85 }
86
Junio C Hamanofc9261c2013-12-03 16:23:35 -080087 if (push_default == PUSH_DEFAULT_UPSTREAM &&
René Scharfe1768aaf2019-11-26 16:18:28 +010088 skip_prefix(matched->name, "refs/heads/", &branch_name)) {
89 struct branch *branch = branch_get(branch_name);
Junio C Hamanofc9261c2013-12-03 16:23:35 -080090 if (branch->merge_nr == 1 && branch->merge[0]->src) {
91 struct strbuf buf = STRBUF_INIT;
92 strbuf_addf(&buf, "%s:%s",
93 ref, branch->merge[0]->src);
94 return strbuf_detach(&buf, NULL);
95 }
96 }
97
Junio C Hamanoca024652013-12-03 15:41:15 -080098 return ref;
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;
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400105 int i;
Junio C Hamanoca024652013-12-03 15:41:15 -0800106
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400107 for (i = 0; i < nr; i++) {
108 const char *ref = refs[i];
109 if (!strcmp("tag", ref)) {
Junio C Hamano50d829c2013-12-03 14:33:10 -0800110 struct strbuf tagref = STRBUF_INIT;
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400111 if (nr <= ++i)
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000112 die(_("tag shorthand without <tag>"));
Junio C Hamano50d829c2013-12-03 14:33:10 -0800113 ref = refs[i];
114 if (deleterefs)
115 strbuf_addf(&tagref, ":refs/tags/%s", ref);
116 else
117 strbuf_addf(&tagref, "refs/tags/%s", ref);
118 ref = strbuf_detach(&tagref, NULL);
119 } else if (deleterefs) {
120 struct strbuf delref = STRBUF_INIT;
121 if (strchr(ref, ':'))
122 die(_("--delete only accepts plain target ref names"));
123 strbuf_addf(&delref, ":%s", ref);
124 ref = strbuf_detach(&delref, NULL);
Junio C Hamanoca024652013-12-03 15:41:15 -0800125 } else if (!strchr(ref, ':')) {
126 if (!remote) {
127 /* lazily grab remote and local_refs */
128 remote = remote_get(repo);
129 local_refs = get_local_heads();
Jan Krügerf517f1f2009-12-30 20:57:42 +0100130 }
Junio C Hamanoca024652013-12-03 15:41:15 -0800131 ref = map_refspec(ref, remote, local_refs);
Junio C Hamano50d829c2013-12-03 14:33:10 -0800132 }
Brandon Williamsaa402892018-05-16 15:58:16 -0700133 refspec_append(&rs, ref);
Linus Torvalds755225d2006-04-29 21:22:49 -0700134 }
Linus Torvalds755225d2006-04-29 21:22:49 -0700135}
136
Junio C Hamano135dade2012-03-30 16:07:12 -0700137static int push_url_of_remote(struct remote *remote, const char ***url_p)
138{
139 if (remote->pushurl_nr) {
140 *url_p = remote->pushurl;
141 return remote->pushurl_nr;
142 }
143 *url_p = remote->url;
144 return remote->url_nr;
145}
146
Johannes Schindelindbcd9702019-09-30 02:55:31 -0700147static NORETURN void die_push_simple(struct branch *branch,
148 struct remote *remote)
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100149{
Matthieu Moyb55e6772012-04-24 09:50:03 +0200150 /*
151 * There's no point in using shorten_unambiguous_ref here,
152 * as the ambiguity would be on the remote side, not what
153 * we have locally. Plus, this is supposed to be the simple
154 * mode. If the user is doing something crazy like setting
155 * upstream to a non-branch, we should probably be showing
156 * them the big ugly fully qualified ref.
157 */
158 const char *advice_maybe = "";
Jeff Kingcf4fff52014-06-18 15:44:19 -0400159 const char *short_upstream = branch->merge[0]->src;
Matthieu Moyb55e6772012-04-24 09:50:03 +0200160
Jeff Kingcf4fff52014-06-18 15:44:19 -0400161 skip_prefix(short_upstream, "refs/heads/", &short_upstream);
162
Matthieu Moyb55e6772012-04-24 09:50:03 +0200163 /*
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200164 * Don't show advice for people who explicitly set
Matthieu Moyb55e6772012-04-24 09:50:03 +0200165 * push.default.
166 */
167 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
168 advice_maybe = _("\n"
169 "To choose either option permanently, "
170 "see push.default in 'git help config'.");
171 die(_("The upstream branch of your current branch does not match\n"
172 "the name of your current branch. To push to the upstream branch\n"
173 "on the remote, use\n"
174 "\n"
175 " git push %s HEAD:%s\n"
176 "\n"
177 "To push to the branch of the same name on the remote, use\n"
178 "\n"
Ævar Arnfjörð Bjarmason82471662018-11-13 20:39:09 +0000179 " git push %s HEAD\n"
Matthieu Moyb55e6772012-04-24 09:50:03 +0200180 "%s"),
181 remote->name, short_upstream,
Ævar Arnfjörð Bjarmason82471662018-11-13 20:39:09 +0000182 remote->name, advice_maybe);
Matthieu Moyb55e6772012-04-24 09:50:03 +0200183}
184
Ramkumar Ramachandra35ee69c2013-05-30 00:51:49 +0530185static const char message_detached_head_die[] =
186 N_("You are not currently on a branch.\n"
187 "To push the history leading to the current (detached HEAD)\n"
188 "state now, use\n"
189 "\n"
190 " git push %s HEAD:<name-of-remote-branch>\n");
191
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530192static void setup_push_upstream(struct remote *remote, struct branch *branch,
Jeff King00a6fa02014-11-26 22:43:06 -0500193 int triangular, int simple)
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100194{
195 struct strbuf refspec = STRBUF_INIT;
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530196
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100197 if (!branch)
Ramkumar Ramachandra35ee69c2013-05-30 00:51:49 +0530198 die(_(message_detached_head_die), remote->name);
Junio C Hamano135dade2012-03-30 16:07:12 -0700199 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700200 die(_("The current branch %s has no upstream branch.\n"
Matthieu Moyec8460b2011-03-02 21:12:10 +0100201 "To push the current branch and set the remote as upstream, use\n"
202 "\n"
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700203 " git push --set-upstream %s %s\n"),
Matthieu Moyec8460b2011-03-02 21:12:10 +0100204 branch->name,
205 remote->name,
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100206 branch->name);
207 if (branch->merge_nr != 1)
Junio C Hamano6c80cd22011-04-01 17:55:55 -0700208 die(_("The current branch %s has multiple upstream branches, "
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000209 "refusing to push."), branch->name);
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530210 if (triangular)
Junio C Hamano135dade2012-03-30 16:07:12 -0700211 die(_("You are pushing to remote '%s', which is not the upstream of\n"
212 "your current branch '%s', without telling me what to push\n"
213 "to update which remote branch."),
214 remote->name, branch->name);
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530215
Jeff King00a6fa02014-11-26 22:43:06 -0500216 if (simple) {
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530217 /* Additional safety */
218 if (strcmp(branch->refname, branch->merge[0]->src))
219 die_push_simple(branch, remote);
220 }
Junio C Hamano135dade2012-03-30 16:07:12 -0700221
Junio C Hamanoeef2bda2016-10-28 12:25:30 -0700222 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
Brandon Williamsaa402892018-05-16 15:58:16 -0700223 refspec_append(&rs, refspec.buf);
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100224}
225
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530226static void setup_push_current(struct remote *remote, struct branch *branch)
227{
Junio C Hamanoeef2bda2016-10-28 12:25:30 -0700228 struct strbuf refspec = STRBUF_INIT;
229
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530230 if (!branch)
231 die(_(message_detached_head_die), remote->name);
Junio C Hamanoeef2bda2016-10-28 12:25:30 -0700232 strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
Brandon Williamsaa402892018-05-16 15:58:16 -0700233 refspec_append(&rs, refspec.buf);
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530234}
235
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530236static int is_workflow_triangular(struct remote *remote)
237{
238 struct remote *fetch_remote = remote_get(NULL);
239 return (fetch_remote && fetch_remote != remote);
240}
241
Matthieu Moyec8460b2011-03-02 21:12:10 +0100242static void setup_default_push_refspecs(struct remote *remote)
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100243{
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530244 struct branch *branch = branch_get(NULL);
245 int triangular = is_workflow_triangular(remote);
Ramkumar Ramachandra7b2ecd82013-05-30 00:51:50 +0530246
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100247 switch (push_default) {
Junio C Hamanobba0fd22009-07-18 17:19:47 -0700248 default:
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100249 case PUSH_DEFAULT_MATCHING:
Brandon Williamsaa402892018-05-16 15:58:16 -0700250 refspec_append(&rs, ":");
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100251 break;
252
Junio C Hamanob2ed9442013-01-04 16:02:29 -0800253 case PUSH_DEFAULT_UNSPECIFIED:
Matthieu Moyb55e6772012-04-24 09:50:03 +0200254 case PUSH_DEFAULT_SIMPLE:
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530255 if (triangular)
256 setup_push_current(remote, branch);
257 else
Jeff King00a6fa02014-11-26 22:43:06 -0500258 setup_push_upstream(remote, branch, triangular, 1);
Matthieu Moyb55e6772012-04-24 09:50:03 +0200259 break;
260
Johan Herland53c40312011-02-16 01:54:24 +0100261 case PUSH_DEFAULT_UPSTREAM:
Jeff King00a6fa02014-11-26 22:43:06 -0500262 setup_push_upstream(remote, branch, triangular, 0);
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100263 break;
264
265 case PUSH_DEFAULT_CURRENT:
Ramkumar Ramachandraed2b1822013-06-19 16:41:41 +0530266 setup_push_current(remote, branch);
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100267 break;
268
269 case PUSH_DEFAULT_NOTHING:
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000270 die(_("You didn't specify any refspecs to push, and "
271 "push.default is \"nothing\"."));
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100272 break;
273 }
274}
275
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400276static const char message_advice_pull_before_push[] =
277 N_("Updates were rejected because the tip of your current branch is behind\n"
John Keepingfc6c4e92013-07-07 20:02:14 +0100278 "its remote counterpart. Integrate the remote changes (e.g.\n"
279 "'git pull ...') before pushing again.\n"
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400280 "See the 'Note about fast-forwards' in 'git push --help' for details.");
281
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400282static const char message_advice_checkout_pull_push[] =
283 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
John Keepingfc6c4e92013-07-07 20:02:14 +0100284 "counterpart. Check out this branch and integrate the remote changes\n"
285 "(e.g. 'git pull ...') before pushing again.\n"
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400286 "See the 'Note about fast-forwards' in 'git push --help' for details.");
287
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800288static const char message_advice_ref_fetch_first[] =
289 N_("Updates were rejected because the remote contains work that you do\n"
290 "not have locally. This is usually caused by another repository pushing\n"
John Keepingfc6c4e92013-07-07 20:02:14 +0100291 "to the same ref. You may want to first integrate the remote changes\n"
292 "(e.g., 'git pull ...') before pushing again.\n"
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800293 "See the 'Note about fast-forwards' in 'git push --help' for details.");
294
Chris Rorvickb24e6042012-11-29 19:41:34 -0600295static const char message_advice_ref_already_exists[] =
Junio C Hamanob4cf8db2013-01-24 21:09:00 -0800296 N_("Updates were rejected because the tag already exists in the remote.");
Chris Rorvickb24e6042012-11-29 19:41:34 -0600297
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800298static const char message_advice_ref_needs_force[] =
299 N_("You cannot update a remote ref that points at a non-commit object,\n"
300 "or update a remote ref to make it point at a non-commit object,\n"
301 "without using the '--force' option.\n");
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400302
303static void advise_pull_before_push(void)
304{
Chris Rorvick11845642012-12-02 21:27:50 -0600305 if (!advice_push_non_ff_current || !advice_push_update_rejected)
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400306 return;
307 advise(_(message_advice_pull_before_push));
308}
309
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400310static void advise_checkout_pull_push(void)
311{
Chris Rorvick11845642012-12-02 21:27:50 -0600312 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400313 return;
314 advise(_(message_advice_checkout_pull_push));
315}
316
Chris Rorvickb24e6042012-11-29 19:41:34 -0600317static void advise_ref_already_exists(void)
318{
Chris Rorvickb4505682012-12-02 21:27:51 -0600319 if (!advice_push_already_exists || !advice_push_update_rejected)
320 return;
Chris Rorvickb24e6042012-11-29 19:41:34 -0600321 advise(_(message_advice_ref_already_exists));
322}
323
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800324static void advise_ref_fetch_first(void)
325{
326 if (!advice_push_fetch_first || !advice_push_update_rejected)
327 return;
328 advise(_(message_advice_ref_fetch_first));
329}
330
331static void advise_ref_needs_force(void)
332{
333 if (!advice_push_needs_force || !advice_push_update_rejected)
334 return;
335 advise(_(message_advice_ref_needs_force));
336}
337
Brandon Williamsaa402892018-05-16 15:58:16 -0700338static int push_with_options(struct transport *transport, struct refspec *rs,
339 int flags)
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100340{
341 int err;
Chris Rorvick10643d42012-11-29 19:41:33 -0600342 unsigned int reject_reasons;
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800343
Tay Ray Chuan78381062010-02-24 20:50:27 +0800344 transport_set_verbosity(transport, verbosity, progress);
Eric Wongc915f112016-02-03 04:09:14 +0000345 transport->family = family;
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800346
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100347 if (receivepack)
348 transport_set_option(transport,
349 TRANS_OPT_RECEIVEPACK, receivepack);
Nguyễn Thái Ngọc Duyf7c815c2013-08-12 20:55:55 +0700350 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100351
Junio C Hamano91048a92013-07-09 11:01:06 -0700352 if (!is_empty_cas(&cas)) {
353 if (!transport->smart_options)
354 die("underlying transport does not support --%s option",
355 CAS_OPT_NAME);
356 transport->smart_options->cas = &cas;
357 }
358
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800359 if (verbosity > 0)
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000360 fprintf(stderr, _("Pushing to %s\n"), transport->url);
Josh Steadmon25e4b802019-10-02 16:49:29 -0700361 trace2_region_enter("push", "transport_push", the_repository);
Nguyễn Thái Ngọc Duy6c6d5d02018-11-10 06:48:55 +0100362 err = transport_push(the_repository, transport,
363 rs, flags, &reject_reasons);
Josh Steadmon25e4b802019-10-02 16:49:29 -0700364 trace2_region_leave("push", "transport_push", the_repository);
Ryan Dammrose960786e2018-04-21 12:10:00 +0200365 if (err != 0) {
366 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000367 error(_("failed to push some refs to '%s'"), transport->url);
Ryan Dammrose960786e2018-04-21 12:10:00 +0200368 fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
369 }
Tay Ray Chuan53970b92009-12-04 07:31:44 +0800370
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100371 err |= transport_disconnect(transport);
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100372 if (!err)
373 return 0;
374
Chris Rorvick10643d42012-11-29 19:41:33 -0600375 if (reject_reasons & REJECT_NON_FF_HEAD) {
Christopher Tiwaldf25950f2012-03-20 00:31:33 -0400376 advise_pull_before_push();
Chris Rorvick10643d42012-11-29 19:41:33 -0600377 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
Junio C Hamanob2ed9442013-01-04 16:02:29 -0800378 advise_checkout_pull_push();
Chris Rorvickb24e6042012-11-29 19:41:34 -0600379 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
380 advise_ref_already_exists();
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800381 } else if (reject_reasons & REJECT_FETCH_FIRST) {
382 advise_ref_fetch_first();
383 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
384 advise_ref_needs_force();
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100385 }
386
387 return 1;
388}
389
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700390static int do_push(const char *repo, int flags,
Thomas Gummerer8e4c8af2019-09-02 19:08:28 +0100391 const struct string_list *push_options,
392 struct remote *remote)
Linus Torvalds755225d2006-04-29 21:22:49 -0700393{
Daniel Barkalow5751f492007-05-12 11:45:53 -0400394 int i, errs;
Michael J Gruber20346232009-06-09 18:01:34 +0200395 const char **url;
396 int url_nr;
Brandon Williamsaa402892018-05-16 15:58:16 -0700397 struct refspec *push_refspec = &rs;
Linus Torvalds755225d2006-04-29 21:22:49 -0700398
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700399 if (push_options->nr)
400 flags |= TRANSPORT_PUSH_OPTIONS;
401
Brandon Williamsaa402892018-05-16 15:58:16 -0700402 if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) {
403 if (remote->push.nr) {
404 push_refspec = &remote->push;
Finn Arne Gangstad52153742009-03-16 16:42:51 +0100405 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
Matthieu Moyec8460b2011-03-02 21:12:10 +0100406 setup_default_push_refspecs(remote);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400407 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -0700408 errs = 0;
Junio C Hamano135dade2012-03-30 16:07:12 -0700409 url_nr = push_url_of_remote(remote, &url);
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100410 if (url_nr) {
411 for (i = 0; i < url_nr; i++) {
412 struct transport *transport =
413 transport_get(remote, url[i]);
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700414 if (flags & TRANSPORT_PUSH_OPTIONS)
415 transport->push_options = push_options;
Brandon Williamsaa402892018-05-16 15:58:16 -0700416 if (push_with_options(transport, push_refspec, flags))
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100417 errs++;
Matthieu Moy07436e42009-08-08 09:51:08 +0200418 }
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100419 } else {
420 struct transport *transport =
421 transport_get(remote, NULL);
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700422 if (flags & TRANSPORT_PUSH_OPTIONS)
423 transport->push_options = push_options;
Brandon Williamsaa402892018-05-16 15:58:16 -0700424 if (push_with_options(transport, push_refspec, flags))
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100425 errs++;
Linus Torvalds755225d2006-04-29 21:22:49 -0700426 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -0700427 return !!errs;
Linus Torvalds755225d2006-04-29 21:22:49 -0700428}
429
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200430static int option_parse_recurse_submodules(const struct option *opt,
431 const char *arg, int unset)
432{
Mike Croweb33a15b2015-11-17 11:05:56 +0000433 int *recurse_submodules = opt->value;
Heiko Voigteb21c732012-03-29 09:21:24 +0200434
Mike Croweb33a15b2015-11-17 11:05:56 +0000435 if (unset)
436 *recurse_submodules = RECURSE_SUBMODULES_OFF;
437 else if (arg)
438 *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg);
439 else
440 die("%s missing parameter", opt->long_name);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200441
442 return 0;
443}
444
Dave Borowitz68c757f2015-08-19 11:26:47 -0400445static void set_push_cert_flags(int *flags, int v)
446{
447 switch (v) {
448 case SEND_PACK_PUSH_CERT_NEVER:
449 *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED);
450 break;
451 case SEND_PACK_PUSH_CERT_ALWAYS:
452 *flags |= TRANSPORT_PUSH_CERT_ALWAYS;
453 *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED;
454 break;
455 case SEND_PACK_PUSH_CERT_IF_ASKED:
456 *flags |= TRANSPORT_PUSH_CERT_IF_ASKED;
457 *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS;
458 break;
459 }
460}
461
462
Michael J Gruberb9459012014-10-22 16:57:49 +0200463static int git_push_config(const char *k, const char *v, void *cb)
464{
Ryan Dammrose960786e2018-04-21 12:10:00 +0200465 const char *slot_name;
Dave Olszewskia8bc2692015-02-16 01:16:19 -0500466 int *flags = cb;
Michael J Gruberb9459012014-10-22 16:57:49 +0200467 int status;
468
469 status = git_gpg_config(k, v, NULL);
470 if (status)
471 return status;
Dave Olszewskia8bc2692015-02-16 01:16:19 -0500472
473 if (!strcmp(k, "push.followtags")) {
474 if (git_config_bool(k, v))
475 *flags |= TRANSPORT_PUSH_FOLLOW_TAGS;
476 else
477 *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
478 return 0;
Dave Borowitz68c757f2015-08-19 11:26:47 -0400479 } else if (!strcmp(k, "push.gpgsign")) {
480 const char *value;
481 if (!git_config_get_value("push.gpgsign", &value)) {
Martin Ågren89576612017-08-07 20:20:49 +0200482 switch (git_parse_maybe_bool(value)) {
Dave Borowitz68c757f2015-08-19 11:26:47 -0400483 case 0:
484 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
485 break;
486 case 1:
487 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS);
488 break;
489 default:
490 if (value && !strcasecmp(value, "if-asked"))
491 set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED);
492 else
493 return error("Invalid value for '%s'", k);
494 }
495 }
Mike Croweb33a15b2015-11-17 11:05:56 +0000496 } else if (!strcmp(k, "push.recursesubmodules")) {
497 const char *value;
498 if (!git_config_get_value("push.recursesubmodules", &value))
499 recurse_submodules = parse_push_recurse_submodules_arg(k, value);
Stefan Beller4e53d6a2017-05-31 17:30:49 -0700500 } else if (!strcmp(k, "submodule.recurse")) {
501 int val = git_config_bool(k, v) ?
502 RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF;
503 recurse_submodules = val;
Marius Paligad8052752017-10-23 13:44:49 +0200504 } else if (!strcmp(k, "push.pushoption")) {
505 if (!v)
506 return config_error_nonbool(k);
507 else
508 if (!*v)
509 string_list_clear(&push_options_config, 0);
510 else
511 string_list_append(&push_options_config, v);
512 return 0;
Ryan Dammrose960786e2018-04-21 12:10:00 +0200513 } else if (!strcmp(k, "color.push")) {
514 push_use_color = git_config_colorbool(k, v);
515 return 0;
516 } else if (skip_prefix(k, "color.push.", &slot_name)) {
517 int slot = parse_push_color_slot(slot_name);
518 if (slot < 0)
519 return 0;
520 if (!v)
521 return config_error_nonbool(k);
522 return color_parse(v, push_colors[slot]);
Dave Olszewskia8bc2692015-02-16 01:16:19 -0500523 }
524
Jeff King06038cd2015-02-16 00:46:30 -0500525 return git_default_config(k, v, NULL);
Michael J Gruberb9459012014-10-22 16:57:49 +0200526}
527
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700528int cmd_push(int argc, const char **argv, const char *prefix)
Linus Torvalds755225d2006-04-29 21:22:49 -0700529{
Daniel Barkalow9b288512007-09-10 23:03:04 -0400530 int flags = 0;
Daniel Barkalow378c4832007-11-04 22:35:37 -0500531 int tags = 0;
Dave Borowitz30261092015-08-19 11:26:46 -0400532 int push_cert = -1;
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200533 int rc;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400534 const char *repo = NULL; /* default repository */
Marius Paligad8052752017-10-23 13:44:49 +0200535 struct string_list push_options_cmdline = STRING_LIST_INIT_DUP;
536 struct string_list *push_options;
Brandon Williams54cc8ac2017-03-31 16:56:22 -0700537 const struct string_list_item *item;
Thomas Gummerer8e4c8af2019-09-02 19:08:28 +0100538 struct remote *remote;
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700539
Daniel Barkalow378c4832007-11-04 22:35:37 -0500540 struct option options[] = {
Tay Ray Chuan8afd8dc2010-02-24 20:50:24 +0800541 OPT__VERBOSITY(&verbosity),
Nguyễn Thái Ngọc Duy78dafaa2012-08-20 19:32:33 +0700542 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
543 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
544 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
Michele Ballabioc29c1b42008-07-20 14:02:20 +0200545 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
Patrick Steinhardt38a25592015-12-14 16:23:04 +0100546 OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200547 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 +0700548 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
549 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
550 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
Junio C Hamano28f5d172013-07-08 15:34:36 -0700551 { OPTION_CALLBACK,
Ævar Arnfjörð Bjarmasonc67318e2018-08-02 00:31:33 +0200552 0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
Junio C Hamano28f5d172013-07-08 15:34:36 -0700553 N_("require old value of ref to be at this value"),
Ævar Arnfjörð Bjarmasonc67318e2018-08-02 00:31:33 +0200554 PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option },
René Scharfebbc072f2018-08-19 19:34:48 +0200555 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
Nguyễn Thái Ngọc Duyf63cf8c2012-08-20 19:32:55 +0700556 N_("control recursive pushing of submodules"),
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200557 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
Nguyễn Thái Ngọc Duyf1e1bdd2018-02-09 18:02:10 +0700558 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 +0700559 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
560 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
561 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
Ilari Liusvaarae9fcd1e2010-01-16 23:45:31 +0200562 TRANSPORT_PUSH_SET_UPSTREAM),
Nguyễn Thái Ngọc Duy78dafaa2012-08-20 19:32:33 +0700563 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
564 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
Felipe Contreras6ddba5e2012-02-23 00:43:41 +0200565 TRANSPORT_PUSH_PRUNE),
Aaron Schrabec555592013-01-13 00:17:03 -0500566 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
Junio C Hamanoc2aba152013-03-04 12:09:50 -0800567 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
568 TRANSPORT_PUSH_FOLLOW_TAGS),
Dave Borowitz30261092015-08-19 11:26:46 -0400569 { OPTION_CALLBACK,
René Scharfebbc072f2018-08-19 19:34:48 +0200570 0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
Dave Borowitz30261092015-08-19 11:26:46 -0400571 PARSE_OPT_OPTARG, option_parse_push_signed },
Jeff Kingd16c33b2015-02-16 01:12:04 -0500572 OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
Marius Paligad8052752017-10-23 13:44:49 +0200573 OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")),
Eric Wongc915f112016-02-03 04:09:14 +0000574 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
575 TRANSPORT_FAMILY_IPV4),
576 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
577 TRANSPORT_FAMILY_IPV6),
Daniel Barkalow378c4832007-11-04 22:35:37 -0500578 OPT_END()
579 };
Linus Torvalds755225d2006-04-29 21:22:49 -0700580
Jeff Kingbbc30f92011-02-24 09:30:19 -0500581 packet_trace_identity("push");
Jeff King06c21e182015-02-16 01:13:25 -0500582 git_config(git_push_config, &flags);
Stephen Boyd37782922009-05-23 11:53:12 -0700583 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
Marius Paligad8052752017-10-23 13:44:49 +0200584 push_options = (push_options_cmdline.nr
585 ? &push_options_cmdline
586 : &push_options_config);
Dave Borowitz68c757f2015-08-19 11:26:47 -0400587 set_push_cert_flags(&flags, push_cert);
Daniel Barkalow378c4832007-11-04 22:35:37 -0500588
Jan Krügerf517f1f2009-12-30 20:57:42 +0100589 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000590 die(_("--delete is incompatible with --all, --mirror and --tags"));
Jan Krügerf517f1f2009-12-30 20:57:42 +0100591 if (deleterefs && argc < 2)
Ævar Arnfjörð Bjarmason8352d292011-02-22 23:42:11 +0000592 die(_("--delete doesn't make sense without any refs"));
Jan Krügerf517f1f2009-12-30 20:57:42 +0100593
Mike Croweb33a15b2015-11-17 11:05:56 +0000594 if (recurse_submodules == RECURSE_SUBMODULES_CHECK)
595 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
596 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
597 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
Brandon Williams225e8bf2016-12-19 10:25:33 -0800598 else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
599 flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
Mike Croweb33a15b2015-11-17 11:05:56 +0000600
Daniel Barkalow378c4832007-11-04 22:35:37 -0500601 if (tags)
Brandon Williamsaa402892018-05-16 15:58:16 -0700602 refspec_append(&rs, "refs/tags/*");
Daniel Barkalow378c4832007-11-04 22:35:37 -0500603
604 if (argc > 0) {
605 repo = argv[0];
Junio C Hamanoca024652013-12-03 15:41:15 -0800606 set_refspecs(argv + 1, argc - 1, repo);
Linus Torvalds755225d2006-04-29 21:22:49 -0700607 }
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400608
Thomas Gummerer8e4c8af2019-09-02 19:08:28 +0100609 remote = pushremote_get(repo);
610 if (!remote) {
611 if (repo)
612 die(_("bad repository '%s'"), repo);
613 die(_("No configured push destination.\n"
614 "Either specify the URL from the command-line or configure a remote repository using\n"
615 "\n"
616 " git remote add <name> <url>\n"
617 "\n"
618 "and then push using the remote name\n"
619 "\n"
620 " git push <name>\n"));
621 }
622
623 if (remote->mirror)
624 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
625
626 if (flags & TRANSPORT_PUSH_ALL) {
627 if (tags)
628 die(_("--all and --tags are incompatible"));
629 if (argc >= 2)
630 die(_("--all can't be combined with refspecs"));
631 }
632 if (flags & TRANSPORT_PUSH_MIRROR) {
633 if (tags)
634 die(_("--mirror and --tags are incompatible"));
635 if (argc >= 2)
636 die(_("--mirror can't be combined with refspecs"));
637 }
638 if ((flags & TRANSPORT_PUSH_ALL) && (flags & TRANSPORT_PUSH_MIRROR))
639 die(_("--all and --mirror are incompatible"));
640
Marius Paligad8052752017-10-23 13:44:49 +0200641 for_each_string_list_item(item, push_options)
Stefan Bellerf6a4e612016-07-14 14:49:47 -0700642 if (strchr(item->string, '\n'))
643 die(_("push options must not have new line characters"));
644
Thomas Gummerer8e4c8af2019-09-02 19:08:28 +0100645 rc = do_push(repo, flags, push_options, remote);
Marius Paligad8052752017-10-23 13:44:49 +0200646 string_list_clear(&push_options_cmdline, 0);
647 string_list_clear(&push_options_config, 0);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200648 if (rc == -1)
Andy Whitcroft94c89ba2007-11-09 23:32:25 +0000649 usage_with_options(push_usage, options);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200650 else
651 return rc;
Linus Torvalds755225d2006-04-29 21:22:49 -0700652}