push: use remote.$name.push as a refmap
[git/git.git] / builtin / push.c
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
13 static const char * const push_usage[] = {
14 N_("git push [<options>] [<repository> [<refspec>...]]"),
15 NULL,
16 };
17
18 static int thin = 1;
19 static int deleterefs;
20 static const char *receivepack;
21 static int verbosity;
22 static int progress = -1;
23
24 static struct push_cas_option cas;
25
26 static const char **refspec;
27 static int refspec_nr;
28 static int refspec_alloc;
29 static int default_matching_used;
30
31 static 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
38 static 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 return ref;
62 }
63
64 static void set_refspecs(const char **refs, int nr, const char *repo)
65 {
66 struct remote *remote = NULL;
67 struct ref *local_refs = NULL;
68 int i;
69
70 for (i = 0; i < nr; i++) {
71 const char *ref = refs[i];
72 if (!strcmp("tag", ref)) {
73 struct strbuf tagref = STRBUF_INIT;
74 if (nr <= ++i)
75 die(_("tag shorthand without <tag>"));
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);
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);
95 }
96 add_refspec(ref);
97 }
98 }
99
100 static 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
110 static 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 /*
126 * Don't show advice for people who explicitly set
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
147 static 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
154 static void setup_push_upstream(struct remote *remote, struct branch *branch,
155 int triangular)
156 {
157 struct strbuf refspec = STRBUF_INIT;
158
159 if (!branch)
160 die(_(message_detached_head_die), remote->name);
161 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
162 die(_("The current branch %s has no upstream branch.\n"
163 "To push the current branch and set the remote as upstream, use\n"
164 "\n"
165 " git push --set-upstream %s %s\n"),
166 branch->name,
167 remote->name,
168 branch->name);
169 if (branch->merge_nr != 1)
170 die(_("The current branch %s has multiple upstream branches, "
171 "refusing to push."), branch->name);
172 if (triangular)
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);
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 }
183
184 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
185 add_refspec(refspec.buf);
186 }
187
188 static 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
195 static char warn_unspecified_push_default_msg[] =
196 N_("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
210 static 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
219 static 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
225 static void setup_default_push_refspecs(struct remote *remote)
226 {
227 struct branch *branch = branch_get(NULL);
228 int triangular = is_workflow_triangular(remote);
229
230 switch (push_default) {
231 default:
232 case PUSH_DEFAULT_UNSPECIFIED:
233 default_matching_used = 1;
234 warn_unspecified_push_default_configuration();
235 /* fallthru */
236 case PUSH_DEFAULT_MATCHING:
237 add_refspec(":");
238 break;
239
240 case PUSH_DEFAULT_SIMPLE:
241 if (triangular)
242 setup_push_current(remote, branch);
243 else
244 setup_push_upstream(remote, branch, triangular);
245 break;
246
247 case PUSH_DEFAULT_UPSTREAM:
248 setup_push_upstream(remote, branch, triangular);
249 break;
250
251 case PUSH_DEFAULT_CURRENT:
252 setup_push_current(remote, branch);
253 break;
254
255 case PUSH_DEFAULT_NOTHING:
256 die(_("You didn't specify any refspecs to push, and "
257 "push.default is \"nothing\"."));
258 break;
259 }
260 }
261
262 static const char message_advice_pull_before_push[] =
263 N_("Updates were rejected because the tip of your current branch is behind\n"
264 "its remote counterpart. Integrate the remote changes (e.g.\n"
265 "'git pull ...') before pushing again.\n"
266 "See the 'Note about fast-forwards' in 'git push --help' for details.");
267
268 static 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"
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.");
273
274 static const char message_advice_checkout_pull_push[] =
275 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
276 "counterpart. Check out this branch and integrate the remote changes\n"
277 "(e.g. 'git pull ...') before pushing again.\n"
278 "See the 'Note about fast-forwards' in 'git push --help' for details.");
279
280 static 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"
283 "to the same ref. You may want to first integrate the remote changes\n"
284 "(e.g., 'git pull ...') before pushing again.\n"
285 "See the 'Note about fast-forwards' in 'git push --help' for details.");
286
287 static const char message_advice_ref_already_exists[] =
288 N_("Updates were rejected because the tag already exists in the remote.");
289
290 static 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");
294
295 static void advise_pull_before_push(void)
296 {
297 if (!advice_push_non_ff_current || !advice_push_update_rejected)
298 return;
299 advise(_(message_advice_pull_before_push));
300 }
301
302 static void advise_use_upstream(void)
303 {
304 if (!advice_push_non_ff_default || !advice_push_update_rejected)
305 return;
306 advise(_(message_advice_use_upstream));
307 }
308
309 static void advise_checkout_pull_push(void)
310 {
311 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
312 return;
313 advise(_(message_advice_checkout_pull_push));
314 }
315
316 static void advise_ref_already_exists(void)
317 {
318 if (!advice_push_already_exists || !advice_push_update_rejected)
319 return;
320 advise(_(message_advice_ref_already_exists));
321 }
322
323 static 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
330 static 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
337 static int push_with_options(struct transport *transport, int flags)
338 {
339 int err;
340 unsigned int reject_reasons;
341
342 transport_set_verbosity(transport, verbosity, progress);
343
344 if (receivepack)
345 transport_set_option(transport,
346 TRANS_OPT_RECEIVEPACK, receivepack);
347 transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
348
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
356 if (verbosity > 0)
357 fprintf(stderr, _("Pushing to %s\n"), transport->url);
358 err = transport_push(transport, refspec_nr, refspec, flags,
359 &reject_reasons);
360 if (err != 0)
361 error(_("failed to push some refs to '%s'"), transport->url);
362
363 err |= transport_disconnect(transport);
364 if (!err)
365 return 0;
366
367 if (reject_reasons & REJECT_NON_FF_HEAD) {
368 advise_pull_before_push();
369 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
370 if (default_matching_used)
371 advise_use_upstream();
372 else
373 advise_checkout_pull_push();
374 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
375 advise_ref_already_exists();
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();
380 }
381
382 return 1;
383 }
384
385 static int do_push(const char *repo, int flags)
386 {
387 int i, errs;
388 struct remote *remote = pushremote_get(repo);
389 const char **url;
390 int url_nr;
391
392 if (!remote) {
393 if (repo)
394 die(_("bad repository '%s'"), repo);
395 die(_("No configured push destination.\n"
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"
402 " git push <name>\n"));
403 }
404
405 if (remote->mirror)
406 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
407
408 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
409 if (!strcmp(*refspec, "refs/tags/*"))
410 return error(_("--all and --tags are incompatible"));
411 return error(_("--all can't be combined with refspecs"));
412 }
413
414 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
415 if (!strcmp(*refspec, "refs/tags/*"))
416 return error(_("--mirror and --tags are incompatible"));
417 return error(_("--mirror can't be combined with refspecs"));
418 }
419
420 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
421 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
422 return error(_("--all and --mirror are incompatible"));
423 }
424
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))
430 setup_default_push_refspecs(remote);
431 }
432 errs = 0;
433 url_nr = push_url_of_remote(remote, &url);
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++;
440 }
441 } else {
442 struct transport *transport =
443 transport_get(remote, NULL);
444
445 if (push_with_options(transport, flags))
446 errs++;
447 }
448 return !!errs;
449 }
450
451 static int option_parse_recurse_submodules(const struct option *opt,
452 const char *arg, int unset)
453 {
454 int *flags = opt->value;
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
460 if (arg) {
461 if (!strcmp(arg, "check"))
462 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
463 else if (!strcmp(arg, "on-demand"))
464 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
465 else
466 die("bad %s argument: %s", opt->long_name, arg);
467 } else
468 die("option %s needs an argument (check|on-demand)",
469 opt->long_name);
470
471 return 0;
472 }
473
474 int cmd_push(int argc, const char **argv, const char *prefix)
475 {
476 int flags = 0;
477 int tags = 0;
478 int rc;
479 const char *repo = NULL; /* default repository */
480 struct option options[] = {
481 OPT__VERBOSITY(&verbosity),
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"),
485 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
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)")),
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),
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 },
495 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
496 N_("control recursive pushing of submodules"),
497 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
498 OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
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"),
502 TRANSPORT_PUSH_SET_UPSTREAM),
503 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
504 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
505 TRANSPORT_PUSH_PRUNE),
506 OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
507 OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
508 TRANSPORT_PUSH_FOLLOW_TAGS),
509 OPT_END()
510 };
511
512 packet_trace_identity("push");
513 git_config(git_default_config, NULL);
514 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
515
516 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
517 die(_("--delete is incompatible with --all, --mirror and --tags"));
518 if (deleterefs && argc < 2)
519 die(_("--delete doesn't make sense without any refs"));
520
521 if (tags)
522 add_refspec("refs/tags/*");
523
524 if (argc > 0) {
525 repo = argv[0];
526 set_refspecs(argv + 1, argc - 1, repo);
527 }
528
529 rc = do_push(repo, flags);
530 if (rc == -1)
531 usage_with_options(push_usage, options);
532 else
533 return rc;
534 }