blob: 6d1da07c46704f3de837044652f0a1fd149327ad [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[] = {
13 "git-push [--all] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]",
14 NULL,
15};
Linus Torvalds755225d2006-04-29 21:22:49 -070016
Shawn O. Pearce18184f72007-10-16 00:25:34 -040017static int thin, verbose;
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
Shawn O. Pearce18184f72007-10-16 00:25:34 -040059 if (!refspec
60 && !(flags & TRANSPORT_PUSH_ALL)
61 && remote->push_refspec_nr) {
Daniel Barkalow8558fd92007-05-25 01:20:56 -040062 refspec = remote->push_refspec;
63 refspec_nr = remote->push_refspec_nr;
Daniel Barkalow5751f492007-05-12 11:45:53 -040064 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -070065 errs = 0;
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040066 for (i = 0; i < remote->url_nr; i++) {
Daniel Barkalow9b288512007-09-10 23:03:04 -040067 struct transport *transport =
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040068 transport_get(remote, remote->url[i]);
Pierre Habouzit60b7f382006-08-23 12:39:10 +020069 int err;
Daniel Barkalow9b288512007-09-10 23:03:04 -040070 if (receivepack)
71 transport_set_option(transport,
72 TRANS_OPT_RECEIVEPACK, receivepack);
73 if (thin)
74 transport_set_option(transport, TRANS_OPT_THIN, "yes");
75
Linus Torvaldsbcc785f2006-10-30 08:28:59 -080076 if (verbose)
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040077 fprintf(stderr, "Pushing to %s\n", remote->url[i]);
Daniel Barkalow9b288512007-09-10 23:03:04 -040078 err = transport_push(transport, refspec_nr, refspec, flags);
79 err |= transport_disconnect(transport);
80
Pierre Habouzit60b7f382006-08-23 12:39:10 +020081 if (!err)
Linus Torvalds755225d2006-04-29 21:22:49 -070082 continue;
Junio C Hamano39878b02007-04-06 23:04:55 -070083
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040084 error("failed to push to '%s'", remote->url[i]);
Junio C Hamanofd1d1b02007-04-06 23:04:53 -070085 errs++;
Linus Torvalds755225d2006-04-29 21:22:49 -070086 }
Junio C Hamanofd1d1b02007-04-06 23:04:53 -070087 return !!errs;
Linus Torvalds755225d2006-04-29 21:22:49 -070088}
89
Linus Torvaldsa633fca2006-07-28 22:44:25 -070090int cmd_push(int argc, const char **argv, const char *prefix)
Linus Torvalds755225d2006-04-29 21:22:49 -070091{
Daniel Barkalow9b288512007-09-10 23:03:04 -040092 int flags = 0;
Daniel Barkalow378c4832007-11-04 22:35:37 -050093 int all = 0;
94 int dry_run = 0;
95 int force = 0;
96 int tags = 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -040097 const char *repo = NULL; /* default repository */
Linus Torvalds755225d2006-04-29 21:22:49 -070098
Daniel Barkalow378c4832007-11-04 22:35:37 -050099 struct option options[] = {
100 OPT__VERBOSE(&verbose),
101 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
102 OPT_BOOLEAN( 0 , "all", &all, "push all refs"),
103 OPT_BOOLEAN( 0 , "tags", &tags, "push tags"),
104 OPT_BOOLEAN( 0 , "dry-run", &dry_run, "dry run"),
105 OPT_BOOLEAN('f', "force", &force, "force updates"),
106 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
107 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
108 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
109 OPT_END()
110 };
Linus Torvalds755225d2006-04-29 21:22:49 -0700111
Daniel Barkalow378c4832007-11-04 22:35:37 -0500112 argc = parse_options(argc, argv, options, push_usage, 0);
113
114 if (force)
115 flags |= TRANSPORT_PUSH_FORCE;
116 if (dry_run)
117 flags |= TRANSPORT_PUSH_DRY_RUN;
Steffen Prohaska1b2486d2007-11-11 15:01:44 +0100118 if (verbose)
119 flags |= TRANSPORT_PUSH_VERBOSE;
Daniel Barkalow378c4832007-11-04 22:35:37 -0500120 if (tags)
121 add_refspec("refs/tags/*");
122 if (all)
123 flags |= TRANSPORT_PUSH_ALL;
124
125 if (argc > 0) {
126 repo = argv[0];
127 set_refspecs(argv + 1, argc - 1);
Linus Torvalds755225d2006-04-29 21:22:49 -0700128 }
Shawn O. Pearce18184f72007-10-16 00:25:34 -0400129 if ((flags & TRANSPORT_PUSH_ALL) && refspec)
Daniel Barkalow378c4832007-11-04 22:35:37 -0500130 usage_with_options(push_usage, options);
Daniel Barkalow8558fd92007-05-25 01:20:56 -0400131
Daniel Barkalow9b288512007-09-10 23:03:04 -0400132 return do_push(repo, flags);
Linus Torvalds755225d2006-04-29 21:22:49 -0700133}