]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/push.c
config doc: rewrite push.default section
[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
b55e6775 123static void setup_push_upstream(struct remote *remote, int simple)
52153747
FAG
124{
125 struct strbuf refspec = STRBUF_INIT;
126 struct branch *branch = branch_get(NULL);
127 if (!branch)
35ee69c0 128 die(_(message_detached_head_die), remote->name);
135dadef 129 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
6c80cd29 130 die(_("The current branch %s has no upstream branch.\n"
ec8460bd
MM
131 "To push the current branch and set the remote as upstream, use\n"
132 "\n"
6c80cd29 133 " git push --set-upstream %s %s\n"),
ec8460bd
MM
134 branch->name,
135 remote->name,
52153747
FAG
136 branch->name);
137 if (branch->merge_nr != 1)
6c80cd29 138 die(_("The current branch %s has multiple upstream branches, "
8352d29e 139 "refusing to push."), branch->name);
135dadef
JH
140 if (strcmp(branch->remote_name, remote->name))
141 die(_("You are pushing to remote '%s', which is not the upstream of\n"
142 "your current branch '%s', without telling me what to push\n"
143 "to update which remote branch."),
144 remote->name, branch->name);
b55e6775
MM
145 if (simple && strcmp(branch->refname, branch->merge[0]->src))
146 die_push_simple(branch, remote);
135dadef 147
52153747
FAG
148 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
149 add_refspec(refspec.buf);
150}
151
f2c2c901
MM
152static char warn_unspecified_push_default_msg[] =
153N_("push.default is unset; its implicit value is changing in\n"
154 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
155 "and maintain the current behavior after the default changes, use:\n"
156 "\n"
157 " git config --global push.default matching\n"
158 "\n"
159 "To squelch this message and adopt the new behavior now, use:\n"
160 "\n"
161 " git config --global push.default simple\n"
162 "\n"
163 "See 'git help config' and search for 'push.default' for further information.\n"
164 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
165 "'current' instead of 'simple' if you sometimes use older versions of Git)");
166
167static void warn_unspecified_push_default_configuration(void)
168{
169 static int warn_once;
170
171 if (warn_once++)
172 return;
173 warning("%s\n", _(warn_unspecified_push_default_msg));
174}
175
ec8460bd 176static void setup_default_push_refspecs(struct remote *remote)
52153747 177{
7b2ecd81
RR
178 struct branch *branch;
179
52153747 180 switch (push_default) {
bba0fd22 181 default:
f25950f3
CT
182 case PUSH_DEFAULT_UNSPECIFIED:
183 default_matching_used = 1;
f2c2c901 184 warn_unspecified_push_default_configuration();
f25950f3 185 /* fallthru */
52153747
FAG
186 case PUSH_DEFAULT_MATCHING:
187 add_refspec(":");
188 break;
189
b55e6775
MM
190 case PUSH_DEFAULT_SIMPLE:
191 setup_push_upstream(remote, 1);
192 break;
193
53c40311 194 case PUSH_DEFAULT_UPSTREAM:
b55e6775 195 setup_push_upstream(remote, 0);
52153747
FAG
196 break;
197
198 case PUSH_DEFAULT_CURRENT:
7b2ecd81
RR
199 branch = branch_get(NULL);
200 if (!branch)
201 die(_(message_detached_head_die), remote->name);
0f075b22 202 add_refspec(branch->name);
52153747
FAG
203 break;
204
205 case PUSH_DEFAULT_NOTHING:
8352d29e
ÆAB
206 die(_("You didn't specify any refspecs to push, and "
207 "push.default is \"nothing\"."));
52153747
FAG
208 break;
209 }
210}
211
f25950f3
CT
212static const char message_advice_pull_before_push[] =
213 N_("Updates were rejected because the tip of your current branch is behind\n"
214 "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
215 "before pushing again.\n"
216 "See the 'Note about fast-forwards' in 'git push --help' for details.");
217
218static const char message_advice_use_upstream[] =
219 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
220 "counterpart. If you did not intend to push that branch, you may want to\n"
f2c2c901
MM
221 "specify branches to push or set the 'push.default' configuration variable\n"
222 "to 'simple', 'current' or 'upstream' to push only the current branch.");
f25950f3
CT
223
224static const char message_advice_checkout_pull_push[] =
225 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
226 "counterpart. Check out this branch and merge the remote changes\n"
227 "(e.g. 'git pull') before pushing again.\n"
228 "See the 'Note about fast-forwards' in 'git push --help' for details.");
229
75e5c0dc
JH
230static const char message_advice_ref_fetch_first[] =
231 N_("Updates were rejected because the remote contains work that you do\n"
232 "not have locally. This is usually caused by another repository pushing\n"
233 "to the same ref. You may want to first merge the remote changes (e.g.,\n"
234 "'git pull') before pushing again.\n"
235 "See the 'Note about fast-forwards' in 'git push --help' for details.");
236
b24e6047 237static const char message_advice_ref_already_exists[] =
b4cf8db2 238 N_("Updates were rejected because the tag already exists in the remote.");
b24e6047 239
75e5c0dc
JH
240static const char message_advice_ref_needs_force[] =
241 N_("You cannot update a remote ref that points at a non-commit object,\n"
242 "or update a remote ref to make it point at a non-commit object,\n"
243 "without using the '--force' option.\n");
b24e6047 244
f25950f3
CT
245static void advise_pull_before_push(void)
246{
1184564e 247 if (!advice_push_non_ff_current || !advice_push_update_rejected)
f25950f3
CT
248 return;
249 advise(_(message_advice_pull_before_push));
250}
251
252static void advise_use_upstream(void)
253{
1184564e 254 if (!advice_push_non_ff_default || !advice_push_update_rejected)
f25950f3
CT
255 return;
256 advise(_(message_advice_use_upstream));
257}
258
259static void advise_checkout_pull_push(void)
260{
1184564e 261 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
f25950f3
CT
262 return;
263 advise(_(message_advice_checkout_pull_push));
264}
265
b24e6047
CR
266static void advise_ref_already_exists(void)
267{
b4505682
CR
268 if (!advice_push_already_exists || !advice_push_update_rejected)
269 return;
b24e6047
CR
270 advise(_(message_advice_ref_already_exists));
271}
272
75e5c0dc
JH
273static void advise_ref_fetch_first(void)
274{
275 if (!advice_push_fetch_first || !advice_push_update_rejected)
276 return;
277 advise(_(message_advice_ref_fetch_first));
278}
279
280static void advise_ref_needs_force(void)
281{
282 if (!advice_push_needs_force || !advice_push_update_rejected)
283 return;
284 advise(_(message_advice_ref_needs_force));
285}
286
fb0cc87e
DB
287static int push_with_options(struct transport *transport, int flags)
288{
289 int err;
10643d4e 290 unsigned int reject_reasons;
8afd8dc0 291
78381069 292 transport_set_verbosity(transport, verbosity, progress);
8afd8dc0 293
fb0cc87e
DB
294 if (receivepack)
295 transport_set_option(transport,
296 TRANS_OPT_RECEIVEPACK, receivepack);
297 if (thin)
298 transport_set_option(transport, TRANS_OPT_THIN, "yes");
299
8afd8dc0 300 if (verbosity > 0)
8352d29e 301 fprintf(stderr, _("Pushing to %s\n"), transport->url);
fb0cc87e 302 err = transport_push(transport, refspec_nr, refspec, flags,
10643d4e 303 &reject_reasons);
53970b92 304 if (err != 0)
8352d29e 305 error(_("failed to push some refs to '%s'"), transport->url);
53970b92 306
fb0cc87e 307 err |= transport_disconnect(transport);
fb0cc87e
DB
308 if (!err)
309 return 0;
310
10643d4e 311 if (reject_reasons & REJECT_NON_FF_HEAD) {
f25950f3 312 advise_pull_before_push();
10643d4e 313 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
f25950f3
CT
314 if (default_matching_used)
315 advise_use_upstream();
316 else
317 advise_checkout_pull_push();
b24e6047
CR
318 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
319 advise_ref_already_exists();
75e5c0dc
JH
320 } else if (reject_reasons & REJECT_FETCH_FIRST) {
321 advise_ref_fetch_first();
322 } else if (reject_reasons & REJECT_NEEDS_FORCE) {
323 advise_ref_needs_force();
fb0cc87e
DB
324 }
325
326 return 1;
327}
328
9b288516 329static int do_push(const char *repo, int flags)
755225de 330{
5751f490 331 int i, errs;
f24f715e 332 struct remote *remote = pushremote_get(repo);
20346234
MG
333 const char **url;
334 int url_nr;
755225de 335
fa685bdf
DB
336 if (!remote) {
337 if (repo)
8352d29e 338 die(_("bad repository '%s'"), repo);
6c80cd29 339 die(_("No configured push destination.\n"
a3f5e7a3
MM
340 "Either specify the URL from the command-line or configure a remote repository using\n"
341 "\n"
342 " git remote add <name> <url>\n"
343 "\n"
344 "and then push using the remote name\n"
345 "\n"
6c80cd29 346 " git push <name>\n"));
fa685bdf 347 }
755225de 348
84bb2dfd
PB
349 if (remote->mirror)
350 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
351
b259f09b
MZ
352 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
353 if (!strcmp(*refspec, "refs/tags/*"))
8352d29e
ÆAB
354 return error(_("--all and --tags are incompatible"));
355 return error(_("--all can't be combined with refspecs"));
b259f09b
MZ
356 }
357
358 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
359 if (!strcmp(*refspec, "refs/tags/*"))
8352d29e
ÆAB
360 return error(_("--mirror and --tags are incompatible"));
361 return error(_("--mirror can't be combined with refspecs"));
b259f09b 362 }
84bb2dfd
PB
363
364 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
365 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
8352d29e 366 return error(_("--all and --mirror are incompatible"));
84bb2dfd
PB
367 }
368
52153747
FAG
369 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
370 if (remote->push_refspec_nr) {
371 refspec = remote->push_refspec;
372 refspec_nr = remote->push_refspec_nr;
373 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
ec8460bd 374 setup_default_push_refspecs(remote);
5751f490 375 }
fd1d1b05 376 errs = 0;
135dadef 377 url_nr = push_url_of_remote(remote, &url);
fb0cc87e
DB
378 if (url_nr) {
379 for (i = 0; i < url_nr; i++) {
380 struct transport *transport =
381 transport_get(remote, url[i]);
382 if (push_with_options(transport, flags))
383 errs++;
07436e43 384 }
fb0cc87e
DB
385 } else {
386 struct transport *transport =
387 transport_get(remote, NULL);
388
389 if (push_with_options(transport, flags))
390 errs++;
755225de 391 }
fd1d1b05 392 return !!errs;
755225de
LT
393}
394
d2b17b32
FG
395static int option_parse_recurse_submodules(const struct option *opt,
396 const char *arg, int unset)
397{
398 int *flags = opt->value;
eb21c732
HV
399
400 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
401 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
402 die("%s can only be used once.", opt->long_name);
403
d2b17b32
FG
404 if (arg) {
405 if (!strcmp(arg, "check"))
406 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
eb21c732
HV
407 else if (!strcmp(arg, "on-demand"))
408 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
d2b17b32
FG
409 else
410 die("bad %s argument: %s", opt->long_name, arg);
411 } else
eb21c732
HV
412 die("option %s needs an argument (check|on-demand)",
413 opt->long_name);
d2b17b32
FG
414
415 return 0;
416}
417
a633fca0 418int cmd_push(int argc, const char **argv, const char *prefix)
755225de 419{
9b288516 420 int flags = 0;
378c4832 421 int tags = 0;
84bb2dfd 422 int rc;
5751f490 423 const char *repo = NULL; /* default repository */
378c4832 424 struct option options[] = {
8afd8dc0 425 OPT__VERBOSITY(&verbosity),
78dafaa5
NTND
426 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
427 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
428 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
c29c1b40 429 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
78dafaa5
NTND
430 OPT_BOOLEAN( 0, "delete", &deleterefs, N_("delete refs")),
431 OPT_BOOLEAN( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
432 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
433 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
434 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
435 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
f63cf8c9 436 N_("control recursive pushing of submodules"),
d2b17b32 437 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
78dafaa5
NTND
438 OPT_BOOLEAN( 0 , "thin", &thin, N_("use thin pack")),
439 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
440 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
441 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
e9fcd1e2 442 TRANSPORT_PUSH_SET_UPSTREAM),
78dafaa5
NTND
443 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
444 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
6ddba5e2 445 TRANSPORT_PUSH_PRUNE),
ec55559f 446 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
c2aba155
JH
447 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
448 TRANSPORT_PUSH_FOLLOW_TAGS),
378c4832
DB
449 OPT_END()
450 };
755225de 451
bbc30f99 452 packet_trace_identity("push");
2aae905f 453 git_config(git_default_config, NULL);
37782920 454 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
378c4832 455
f517f1f2 456 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
8352d29e 457 die(_("--delete is incompatible with --all, --mirror and --tags"));
f517f1f2 458 if (deleterefs && argc < 2)
8352d29e 459 die(_("--delete doesn't make sense without any refs"));
f517f1f2 460
378c4832
DB
461 if (tags)
462 add_refspec("refs/tags/*");
378c4832
DB
463
464 if (argc > 0) {
465 repo = argv[0];
466 set_refspecs(argv + 1, argc - 1);
755225de 467 }
8558fd9e 468
84bb2dfd
PB
469 rc = do_push(repo, flags);
470 if (rc == -1)
94c89ba6 471 usage_with_options(push_usage, options);
84bb2dfd
PB
472 else
473 return rc;
755225de 474}