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