]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/push.c
typofix: in-code comments
[thirdparty/git.git] / builtin / push.c
CommitLineData
755225de
LT
1/*
2 * "git push"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "run-command.h"
7#include "builtin.h"
5751f490 8#include "remote.h"
9b288516 9#include "transport.h"
378c4832 10#include "parse-options.h"
d2b17b32 11#include "submodule.h"
755225de 12
378c4832 13static const char * const push_usage[] = {
78dafaa5 14 N_("git push [<options>] [<repository> [<refspec>...]]"),
378c4832
DB
15 NULL,
16};
755225de 17
c29c1b40 18static int thin;
f517f1f2 19static int deleterefs;
d23842fd 20static const char *receivepack;
8afd8dc0 21static int verbosity;
01fdc21f 22static int progress = -1;
755225de 23
96f1e58f
DR
24static const char **refspec;
25static int refspec_nr;
8a883b02 26static int refspec_alloc;
f25950f3 27static int default_matching_used;
755225de
LT
28
29static void add_refspec(const char *ref)
30{
8a883b02
JH
31 refspec_nr++;
32 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
33 refspec[refspec_nr-1] = ref;
755225de
LT
34}
35
755225de
LT
36static void set_refspecs(const char **refs, int nr)
37{
8558fd9e
DB
38 int i;
39 for (i = 0; i < nr; i++) {
40 const char *ref = refs[i];
41 if (!strcmp("tag", ref)) {
42 char *tag;
43 int len;
44 if (nr <= ++i)
8352d29e 45 die(_("tag shorthand without <tag>"));
8558fd9e 46 len = strlen(refs[i]) + 11;
f517f1f2
JK
47 if (deleterefs) {
48 tag = xmalloc(len+1);
49 strcpy(tag, ":refs/tags/");
50 } else {
51 tag = xmalloc(len);
52 strcpy(tag, "refs/tags/");
53 }
8558fd9e
DB
54 strcat(tag, refs[i]);
55 ref = tag;
f517f1f2
JK
56 } else if (deleterefs && !strchr(ref, ':')) {
57 char *delref;
58 int len = strlen(ref)+1;
7b48c170 59 delref = xmalloc(len+1);
f517f1f2
JK
60 strcpy(delref, ":");
61 strcat(delref, ref);
62 ref = delref;
63 } else if (deleterefs)
8352d29e 64 die(_("--delete only accepts plain target ref names"));
8558fd9e 65 add_refspec(ref);
755225de 66 }
755225de
LT
67}
68
135dadef
JH
69static int push_url_of_remote(struct remote *remote, const char ***url_p)
70{
71 if (remote->pushurl_nr) {
72 *url_p = remote->pushurl;
73 return remote->pushurl_nr;
74 }
75 *url_p = remote->url;
76 return remote->url_nr;
77}
78
b55e6775
MM
79static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
80 /*
81 * There's no point in using shorten_unambiguous_ref here,
82 * as the ambiguity would be on the remote side, not what
83 * we have locally. Plus, this is supposed to be the simple
84 * mode. If the user is doing something crazy like setting
85 * upstream to a non-branch, we should probably be showing
86 * them the big ugly fully qualified ref.
87 */
88 const char *advice_maybe = "";
89 const char *short_upstream =
90 skip_prefix(branch->merge[0]->src, "refs/heads/");
91
92 if (!short_upstream)
93 short_upstream = branch->merge[0]->src;
94 /*
95 * Don't show advice for people who explicitely set
96 * push.default.
97 */
98 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
99 advice_maybe = _("\n"
100 "To choose either option permanently, "
101 "see push.default in 'git help config'.");
102 die(_("The upstream branch of your current branch does not match\n"
103 "the name of your current branch. To push to the upstream branch\n"
104 "on the remote, use\n"
105 "\n"
106 " git push %s HEAD:%s\n"
107 "\n"
108 "To push to the branch of the same name on the remote, use\n"
109 "\n"
110 " git push %s %s\n"
111 "%s"),
112 remote->name, short_upstream,
113 remote->name, branch->name, advice_maybe);
114}
115
35ee69c0
RR
116static const char message_detached_head_die[] =
117 N_("You are not currently on a branch.\n"
118 "To push the history leading to the current (detached HEAD)\n"
119 "state now, use\n"
120 "\n"
121 " git push %s HEAD:<name-of-remote-branch>\n");
122
ed2b1829
RR
123static void setup_push_upstream(struct remote *remote, struct branch *branch,
124 int triangular)
52153747
FAG
125{
126 struct strbuf refspec = STRBUF_INIT;
ed2b1829 127
52153747 128 if (!branch)
35ee69c0 129 die(_(message_detached_head_die), remote->name);
135dadef 130 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
6c80cd29 131 die(_("The current branch %s has no upstream branch.\n"
ec8460bd
MM
132 "To push the current branch and set the remote as upstream, use\n"
133 "\n"
6c80cd29 134 " git push --set-upstream %s %s\n"),
ec8460bd
MM
135 branch->name,
136 remote->name,
52153747
FAG
137 branch->name);
138 if (branch->merge_nr != 1)
6c80cd29 139 die(_("The current branch %s has multiple upstream branches, "
8352d29e 140 "refusing to push."), branch->name);
ed2b1829 141 if (triangular)
135dadef
JH
142 die(_("You are pushing to remote '%s', which is not the upstream of\n"
143 "your current branch '%s', without telling me what to push\n"
144 "to update which remote branch."),
145 remote->name, branch->name);
ed2b1829
RR
146
147 if (push_default == PUSH_DEFAULT_SIMPLE) {
148 /* Additional safety */
149 if (strcmp(branch->refname, branch->merge[0]->src))
150 die_push_simple(branch, remote);
151 }
135dadef 152
52153747
FAG
153 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
154 add_refspec(refspec.buf);
155}
156
ed2b1829
RR
157static void setup_push_current(struct remote *remote, struct branch *branch)
158{
159 if (!branch)
160 die(_(message_detached_head_die), remote->name);
161 add_refspec(branch->name);
162}
163
f2c2c901
MM
164static char warn_unspecified_push_default_msg[] =
165N_("push.default is unset; its implicit value is changing in\n"
166 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
167 "and maintain the current behavior after the default changes, use:\n"
168 "\n"
169 " git config --global push.default matching\n"
170 "\n"
171 "To squelch this message and adopt the new behavior now, use:\n"
172 "\n"
173 " git config --global push.default simple\n"
174 "\n"
175 "See 'git help config' and search for 'push.default' for further information.\n"
176 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
177 "'current' instead of 'simple' if you sometimes use older versions of Git)");
178
179static void warn_unspecified_push_default_configuration(void)
180{
181 static int warn_once;
182
183 if (warn_once++)
184 return;
185 warning("%s\n", _(warn_unspecified_push_default_msg));
186}
187
ed2b1829
RR
188static int is_workflow_triangular(struct remote *remote)
189{
190 struct remote *fetch_remote = remote_get(NULL);
191 return (fetch_remote && fetch_remote != remote);
192}
193
ec8460bd 194static void setup_default_push_refspecs(struct remote *remote)
52153747 195{
ed2b1829
RR
196 struct branch *branch = branch_get(NULL);
197 int triangular = is_workflow_triangular(remote);
7b2ecd81 198
52153747 199 switch (push_default) {
bba0fd22 200 default:
f25950f3
CT
201 case PUSH_DEFAULT_UNSPECIFIED:
202 default_matching_used = 1;
f2c2c901 203 warn_unspecified_push_default_configuration();
f25950f3 204 /* fallthru */
52153747
FAG
205 case PUSH_DEFAULT_MATCHING:
206 add_refspec(":");
207 break;
208
b55e6775 209 case PUSH_DEFAULT_SIMPLE:
ed2b1829
RR
210 if (triangular)
211 setup_push_current(remote, branch);
212 else
213 setup_push_upstream(remote, branch, triangular);
b55e6775
MM
214 break;
215
53c40311 216 case PUSH_DEFAULT_UPSTREAM:
ed2b1829 217 setup_push_upstream(remote, branch, triangular);
52153747
FAG
218 break;
219
220 case PUSH_DEFAULT_CURRENT:
ed2b1829 221 setup_push_current(remote, branch);
52153747
FAG
222 break;
223
224 case PUSH_DEFAULT_NOTHING:
8352d29e
ÆAB
225 die(_("You didn't specify any refspecs to push, and "
226 "push.default is \"nothing\"."));
52153747
FAG
227 break;
228 }
229}
230
f25950f3
CT
231static const char message_advice_pull_before_push[] =
232 N_("Updates were rejected because the tip of your current branch is behind\n"
fc6c4e96
JK
233 "its remote counterpart. Integrate the remote changes (e.g.\n"
234 "'git pull ...') before pushing again.\n"
f25950f3
CT
235 "See the 'Note about fast-forwards' in 'git push --help' for details.");
236
237static const char message_advice_use_upstream[] =
238 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
239 "counterpart. If you did not intend to push that branch, you may want to\n"
f2c2c901
MM
240 "specify branches to push or set the 'push.default' configuration variable\n"
241 "to 'simple', 'current' or 'upstream' to push only the current branch.");
f25950f3
CT
242
243static const char message_advice_checkout_pull_push[] =
244 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
fc6c4e96
JK
245 "counterpart. Check out this branch and integrate the remote changes\n"
246 "(e.g. 'git pull ...') before pushing again.\n"
f25950f3
CT
247 "See the 'Note about fast-forwards' in 'git push --help' for details.");
248
75e5c0dc
JH
249static const char message_advice_ref_fetch_first[] =
250 N_("Updates were rejected because the remote contains work that you do\n"
251 "not have locally. This is usually caused by another repository pushing\n"
fc6c4e96
JK
252 "to the same ref. You may want to first integrate the remote changes\n"
253 "(e.g., 'git pull ...') before pushing again.\n"
75e5c0dc
JH
254 "See the 'Note about fast-forwards' in 'git push --help' for details.");
255
b24e6047 256static const char message_advice_ref_already_exists[] =
b4cf8db2 257 N_("Updates were rejected because the tag already exists in the remote.");
b24e6047 258
75e5c0dc
JH
259static const char message_advice_ref_needs_force[] =
260 N_("You cannot update a remote ref that points at a non-commit object,\n"
261 "or update a remote ref to make it point at a non-commit object,\n"
262 "without using the '--force' option.\n");
b24e6047 263
f25950f3
CT
264static void advise_pull_before_push(void)
265{
1184564e 266 if (!advice_push_non_ff_current || !advice_push_update_rejected)
f25950f3
CT
267 return;
268 advise(_(message_advice_pull_before_push));
269}
270
271static void advise_use_upstream(void)
272{
1184564e 273 if (!advice_push_non_ff_default || !advice_push_update_rejected)
f25950f3
CT
274 return;
275 advise(_(message_advice_use_upstream));
276}
277
278static void advise_checkout_pull_push(void)
279{
1184564e 280 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
f25950f3
CT
281 return;
282 advise(_(message_advice_checkout_pull_push));
283}
284
b24e6047
CR
285static void advise_ref_already_exists(void)
286{
b4505682
CR
287 if (!advice_push_already_exists || !advice_push_update_rejected)
288 return;
b24e6047
CR
289 advise(_(message_advice_ref_already_exists));
290}
291
75e5c0dc
JH
292static void advise_ref_fetch_first(void)
293{
294 if (!advice_push_fetch_first || !advice_push_update_rejected)
295 return;
296 advise(_(message_advice_ref_fetch_first));
297}
298
299static void advise_ref_needs_force(void)
300{
301 if (!advice_push_needs_force || !advice_push_update_rejected)
302 return;
303 advise(_(message_advice_ref_needs_force));
304}
305
fb0cc87e
DB
306static int push_with_options(struct transport *transport, int flags)
307{
308 int err;
10643d4e 309 unsigned int reject_reasons;
8afd8dc0 310
78381069 311 transport_set_verbosity(transport, verbosity, progress);
8afd8dc0 312
fb0cc87e
DB
313 if (receivepack)
314 transport_set_option(transport,
315 TRANS_OPT_RECEIVEPACK, receivepack);
316 if (thin)
317 transport_set_option(transport, TRANS_OPT_THIN, "yes");
318
8afd8dc0 319 if (verbosity > 0)
8352d29e 320 fprintf(stderr, _("Pushing to %s\n"), transport->url);
fb0cc87e 321 err = transport_push(transport, refspec_nr, refspec, flags,
10643d4e 322 &reject_reasons);
53970b92 323 if (err != 0)
8352d29e 324 error(_("failed to push some refs to '%s'"), transport->url);
53970b92 325
fb0cc87e 326 err |= transport_disconnect(transport);
fb0cc87e
DB
327 if (!err)
328 return 0;
329
10643d4e 330 if (reject_reasons & REJECT_NON_FF_HEAD) {
f25950f3 331 advise_pull_before_push();
10643d4e 332 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
f25950f3
CT
333 if (default_matching_used)
334 advise_use_upstream();
335 else
336 advise_checkout_pull_push();
b24e6047
CR
337 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
338 advise_ref_already_exists();
75e5c0dc
JH
339 } else if (reject_reasons & REJECT_FETCH_FIRST) {
340 advise_ref_fetch_first();
341 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
342 advise_ref_needs_force();
fb0cc87e
DB
343 }
344
345 return 1;
346}
347
9b288516 348static int do_push(const char *repo, int flags)
755225de 349{
5751f490 350 int i, errs;
f24f715e 351 struct remote *remote = pushremote_get(repo);
20346234
MG
352 const char **url;
353 int url_nr;
755225de 354
fa685bdf
DB
355 if (!remote) {
356 if (repo)
8352d29e 357 die(_("bad repository '%s'"), repo);
6c80cd29 358 die(_("No configured push destination.\n"
a3f5e7a3
MM
359 "Either specify the URL from the command-line or configure a remote repository using\n"
360 "\n"
361 " git remote add <name> <url>\n"
362 "\n"
363 "and then push using the remote name\n"
364 "\n"
6c80cd29 365 " git push <name>\n"));
fa685bdf 366 }
755225de 367
84bb2dfd
PB
368 if (remote->mirror)
369 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
370
b259f09b
MZ
371 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
372 if (!strcmp(*refspec, "refs/tags/*"))
8352d29e
ÆAB
373 return error(_("--all and --tags are incompatible"));
374 return error(_("--all can't be combined with refspecs"));
b259f09b
MZ
375 }
376
377 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
378 if (!strcmp(*refspec, "refs/tags/*"))
8352d29e
ÆAB
379 return error(_("--mirror and --tags are incompatible"));
380 return error(_("--mirror can't be combined with refspecs"));
b259f09b 381 }
84bb2dfd
PB
382
383 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
384 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
8352d29e 385 return error(_("--all and --mirror are incompatible"));
84bb2dfd
PB
386 }
387
52153747
FAG
388 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
389 if (remote->push_refspec_nr) {
390 refspec = remote->push_refspec;
391 refspec_nr = remote->push_refspec_nr;
392 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
ec8460bd 393 setup_default_push_refspecs(remote);
5751f490 394 }
fd1d1b05 395 errs = 0;
135dadef 396 url_nr = push_url_of_remote(remote, &url);
fb0cc87e
DB
397 if (url_nr) {
398 for (i = 0; i < url_nr; i++) {
399 struct transport *transport =
400 transport_get(remote, url[i]);
401 if (push_with_options(transport, flags))
402 errs++;
07436e43 403 }
fb0cc87e
DB
404 } else {
405 struct transport *transport =
406 transport_get(remote, NULL);
407
408 if (push_with_options(transport, flags))
409 errs++;
755225de 410 }
fd1d1b05 411 return !!errs;
755225de
LT
412}
413
d2b17b32
FG
414static int option_parse_recurse_submodules(const struct option *opt,
415 const char *arg, int unset)
416{
417 int *flags = opt->value;
eb21c732
HV
418
419 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
420 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
421 die("%s can only be used once.", opt->long_name);
422
d2b17b32
FG
423 if (arg) {
424 if (!strcmp(arg, "check"))
425 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
eb21c732
HV
426 else if (!strcmp(arg, "on-demand"))
427 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
d2b17b32
FG
428 else
429 die("bad %s argument: %s", opt->long_name, arg);
430 } else
eb21c732
HV
431 die("option %s needs an argument (check|on-demand)",
432 opt->long_name);
d2b17b32
FG
433
434 return 0;
435}
436
a633fca0 437int cmd_push(int argc, const char **argv, const char *prefix)
755225de 438{
9b288516 439 int flags = 0;
378c4832 440 int tags = 0;
84bb2dfd 441 int rc;
5751f490 442 const char *repo = NULL; /* default repository */
378c4832 443 struct option options[] = {
8afd8dc0 444 OPT__VERBOSITY(&verbosity),
78dafaa5
NTND
445 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
446 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
447 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
c29c1b40 448 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
78dafaa5
NTND
449 OPT_BOOLEAN( 0, "delete", &deleterefs, N_("delete refs")),
450 OPT_BOOLEAN( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
451 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
452 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
453 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
454 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
f63cf8c9 455 N_("control recursive pushing of submodules"),
d2b17b32 456 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
78dafaa5
NTND
457 OPT_BOOLEAN( 0 , "thin", &thin, N_("use thin pack")),
458 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
459 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
460 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
e9fcd1e2 461 TRANSPORT_PUSH_SET_UPSTREAM),
78dafaa5
NTND
462 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
463 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
6ddba5e2 464 TRANSPORT_PUSH_PRUNE),
ec55559f 465 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
c2aba155
JH
466 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
467 TRANSPORT_PUSH_FOLLOW_TAGS),
378c4832
DB
468 OPT_END()
469 };
755225de 470
bbc30f99 471 packet_trace_identity("push");
2aae905f 472 git_config(git_default_config, NULL);
37782920 473 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
378c4832 474
f517f1f2 475 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
8352d29e 476 die(_("--delete is incompatible with --all, --mirror and --tags"));
f517f1f2 477 if (deleterefs && argc < 2)
8352d29e 478 die(_("--delete doesn't make sense without any refs"));
f517f1f2 479
378c4832
DB
480 if (tags)
481 add_refspec("refs/tags/*");
378c4832
DB
482
483 if (argc > 0) {
484 repo = argv[0];
485 set_refspecs(argv + 1, argc - 1);
755225de 486 }
8558fd9e 487
84bb2dfd
PB
488 rc = do_push(repo, flags);
489 if (rc == -1)
94c89ba6 490 usage_with_options(push_usage, options);
84bb2dfd
PB
491 else
492 return rc;
755225de 493}