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