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