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