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