blob: cc6666f75e7db8a546d4a1589335589190e414fe [file] [log] [blame]
Linus Torvalds755225d2006-04-29 21:22:49 -07001/*
2 * "git push"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "run-command.h"
7#include "builtin.h"
Daniel Barkalow5751f492007-05-12 11:45:53 -04008#include "remote.h"
Daniel Barkalow9b288512007-09-10 23:03:04 -04009#include "transport.h"
Daniel Barkalow378c4832007-11-04 22:35:37 -050010#include "parse-options.h"
Linus Torvalds755225d2006-04-29 21:22:49 -070011
Daniel Barkalow378c4832007-11-04 22:35:37 -050012static const char * const push_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020013 "git push [--all | --mirror] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]",
Daniel Barkalow378c4832007-11-04 22:35:37 -050014 NULL,
15};
Linus Torvalds755225d2006-04-29 21:22:49 -070016
Michele Ballabioc29c1b42008-07-20 14:02:20 +020017static int thin;
Uwe Kleine-Königd23842f2007-01-19 13:49:27 +010018static const char *receivepack;
Linus Torvalds755225d2006-04-29 21:22:49 -070019
David Rientjes96f1e582006-08-15 10:23:48 -070020static const char **refspec;
21static int refspec_nr;
Linus Torvalds755225d2006-04-29 21:22:49 -070022
23static void add_refspec(const char *ref)
24{
25 int nr = refspec_nr + 1;
26 refspec = xrealloc(refspec, nr * sizeof(char *));
27 refspec[nr-1] = ref;
28 refspec_nr = nr;
29}
30
Linus Torvalds755225d2006-04-29 21:22:49 -070031static void set_refspecs(const char **refs, int nr)
32{
Daniel Barkalow8558fd92007-05-25 01:20:56 -040033 int i;
34 for (i = 0; i < nr; i++) {
35 const char *ref = refs[i];
36 if (!strcmp("tag", ref)) {
37 char *tag;
38 int len;
39 if (nr <= ++i)
40 die("tag shorthand without <tag>");
41 len = strlen(refs[i]) + 11;
42 tag = xmalloc(len);
43 strcpy(tag, "refs/tags/");
44 strcat(tag, refs[i]);
45 ref = tag;
Junio C Hamano411fb8b2006-12-13 10:03:39 -080046 }
Daniel Barkalow8558fd92007-05-25 01:20:56 -040047 add_refspec(ref);
Linus Torvalds755225d2006-04-29 21:22:49 -070048 }
Linus Torvalds755225d2006-04-29 21:22:49 -070049}
50
Daniel Barkalow9b288512007-09-10 23:03:04 -040051static int do_push(const char *repo, int flags)
Linus Torvalds755225d2006-04-29 21:22:49 -070052{
Daniel Barkalow5751f492007-05-12 11:45:53 -040053 int i, errs;
Daniel Barkalow5751f492007-05-12 11:45:53 -040054 struct remote *remote = remote_get(repo);
Linus Torvalds755225d2006-04-29 21:22:49 -070055
Daniel Barkalow5751f492007-05-12 11:45:53 -040056 if (!remote)
Linus Torvalds755225d2006-04-29 21:22:49 -070057 die("bad repository '%s'", repo);
58
Paolo Bonzini84bb2df2008-04-17 13:17:20 +020059 if (remote->mirror)
60 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
61
Marek Zawirskib259f092008-08-16 19:58:32 +020062 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
63 if (!strcmp(*refspec, "refs/tags/*"))
64 return error("--all and --tags are incompatible");
65 return error("--all can't be combined with refspecs");
66 }
67
68 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
69 if (!strcmp(*refspec, "refs/tags/*"))
70 return error("--mirror and --tags are incompatible");
71 return error("--mirror can't be combined with refspecs");
72 }
Paolo Bonzini84bb2df2008-04-17 13:17:20 +020073
74 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
75 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
76 return error("--all and --mirror are incompatible");
77 }
78
Shawn O. Pearce18184f72007-10-16 00:25:34 -040079 if (!refspec
80 && !(flags & TRANSPORT_PUSH_ALL)
81 && remote->push_refspec_nr) {
Daniel Barkalow8558fd92007-05-25 01:20:56 -040082 refspec = remote->push_refspec;
83 refspec_nr = remote->push_refspec_nr;
Daniel Barkalow5751f492007-05-12 11:45:53 -040084 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -070085 errs = 0;
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040086 for (i = 0; i < remote->url_nr; i++) {
Daniel Barkalow9b288512007-09-10 23:03:04 -040087 struct transport *transport =
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040088 transport_get(remote, remote->url[i]);
Pierre Habouzit60b7f382006-08-23 12:39:10 +020089 int err;
Daniel Barkalow9b288512007-09-10 23:03:04 -040090 if (receivepack)
91 transport_set_option(transport,
92 TRANS_OPT_RECEIVEPACK, receivepack);
93 if (thin)
94 transport_set_option(transport, TRANS_OPT_THIN, "yes");
95
Michele Ballabioc29c1b42008-07-20 14:02:20 +020096 if (flags & TRANSPORT_PUSH_VERBOSE)
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040097 fprintf(stderr, "Pushing to %s\n", remote->url[i]);
Daniel Barkalow9b288512007-09-10 23:03:04 -040098 err = transport_push(transport, refspec_nr, refspec, flags);
99 err |= transport_disconnect(transport);
100
Pierre Habouzit60b7f382006-08-23 12:39:10 +0200101 if (!err)
Linus Torvalds755225d2006-04-29 21:22:49 -0700102 continue;
Junio C Hamano39878b02007-04-06 23:04:55 -0700103
Jeff King2b8130c2008-02-19 11:25:01 -0500104 error("failed to push some refs to '%s'", remote->url[i]);
Junio C Hamanofd1d1b02007-04-06 23:04:53 -0700105 errs++;
Linus Torvalds755225d2006-04-29 21:22:49 -0700106 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -0700107 return !!errs;
Linus Torvalds755225d2006-04-29 21:22:49 -0700108}
109
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700110int cmd_push(int argc, const char **argv, const char *prefix)
Linus Torvalds755225d2006-04-29 21:22:49 -0700111{
Daniel Barkalow9b288512007-09-10 23:03:04 -0400112 int flags = 0;
Daniel Barkalow378c4832007-11-04 22:35:37 -0500113 int tags = 0;
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200114 int rc;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400115 const char *repo = NULL; /* default repository */
Linus Torvalds755225d2006-04-29 21:22:49 -0700116
Daniel Barkalow378c4832007-11-04 22:35:37 -0500117 struct option options[] = {
Michele Ballabioc29c1b42008-07-20 14:02:20 +0200118 OPT_BIT('v', "verbose", &flags, "be verbose", TRANSPORT_PUSH_VERBOSE),
Daniel Barkalow378c4832007-11-04 22:35:37 -0500119 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
Michele Ballabioc29c1b42008-07-20 14:02:20 +0200120 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
121 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
122 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
Daniel Barkalow378c4832007-11-04 22:35:37 -0500123 OPT_BOOLEAN( 0 , "tags", &tags, "push tags"),
Michele Ballabioc29c1b42008-07-20 14:02:20 +0200124 OPT_BIT( 0 , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
125 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
Daniel Barkalow378c4832007-11-04 22:35:37 -0500126 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
127 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
128 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
129 OPT_END()
130 };
Linus Torvalds755225d2006-04-29 21:22:49 -0700131
Daniel Barkalow378c4832007-11-04 22:35:37 -0500132 argc = parse_options(argc, argv, options, push_usage, 0);
133
Daniel Barkalow378c4832007-11-04 22:35:37 -0500134 if (tags)
135 add_refspec("refs/tags/*");
Daniel Barkalow378c4832007-11-04 22:35:37 -0500136
137 if (argc > 0) {
138 repo = argv[0];
139 set_refspecs(argv + 1, argc - 1);
Linus Torvalds755225d2006-04-29 21:22:49 -0700140 }
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400141
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200142 rc = do_push(repo, flags);
143 if (rc == -1)
Andy Whitcroft94c89ba2007-11-09 23:32:25 +0000144 usage_with_options(push_usage, options);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200145 else
146 return rc;
Linus Torvalds755225d2006-04-29 21:22:49 -0700147}