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
12static const char * const push_usage[] = {
13 "git push [<options>] [<repository> <refspec>...]",
14 NULL,
15};
16
17static int thin;
18static int deleterefs;
19static const char *receivepack;
20
21static const char **refspec;
22static int refspec_nr;
23
24static void add_refspec(const char *ref)
25{
26 int nr = refspec_nr + 1;
27 refspec = xrealloc(refspec, nr * sizeof(char *));
28 refspec[nr-1] = ref;
29 refspec_nr = nr;
30}
31
32static void set_refspecs(const char **refs, int nr)
33{
34 int i;
35 for (i = 0; i < nr; i++) {
36 const char *ref = refs[i];
37 if (!strcmp("tag", ref)) {
38 char *tag;
39 int len;
40 if (nr <= ++i)
41 die("tag shorthand without <tag>");
42 len = strlen(refs[i]) + 11;
43 if (deleterefs) {
44 tag = xmalloc(len+1);
45 strcpy(tag, ":refs/tags/");
46 } else {
47 tag = xmalloc(len);
48 strcpy(tag, "refs/tags/");
49 }
50 strcat(tag, refs[i]);
51 ref = tag;
52 } else if (deleterefs && !strchr(ref, ':')) {
53 char *delref;
54 int len = strlen(ref)+1;
55 delref = xmalloc(len);
56 strcpy(delref, ":");
57 strcat(delref, ref);
58 ref = delref;
59 } else if (deleterefs)
60 die("--delete only accepts plain target ref names");
61 add_refspec(ref);
62 }
63}
64
65static void setup_push_tracking(void)
66{
67 struct strbuf refspec = STRBUF_INIT;
68 struct branch *branch = branch_get(NULL);
69 if (!branch)
70 die("You are not currently on a branch.");
71 if (!branch->merge_nr)
72 die("The current branch %s is not tracking anything.",
73 branch->name);
74 if (branch->merge_nr != 1)
75 die("The current branch %s is tracking multiple branches, "
76 "refusing to push.", branch->name);
77 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
78 add_refspec(refspec.buf);
79}
80
81static void setup_default_push_refspecs(void)
82{
83 switch (push_default) {
84 default:
85 case PUSH_DEFAULT_MATCHING:
86 add_refspec(":");
87 break;
88
89 case PUSH_DEFAULT_TRACKING:
90 setup_push_tracking();
91 break;
92
93 case PUSH_DEFAULT_CURRENT:
94 add_refspec("HEAD");
95 break;
96
97 case PUSH_DEFAULT_NOTHING:
98 die("You didn't specify any refspecs to push, and "
99 "push.default is \"nothing\".");
100 break;
101 }
102}
103
104static int do_push(const char *repo, int flags)
105{
106 int i, errs;
107 struct remote *remote = remote_get(repo);
108 const char **url;
109 int url_nr;
110
111 if (!remote) {
112 if (repo)
113 die("bad repository '%s'", repo);
114 die("No destination configured to push to.");
115 }
116
117 if (remote->mirror)
118 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
119
120 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
121 if (!strcmp(*refspec, "refs/tags/*"))
122 return error("--all and --tags are incompatible");
123 return error("--all can't be combined with refspecs");
124 }
125
126 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
127 if (!strcmp(*refspec, "refs/tags/*"))
128 return error("--mirror and --tags are incompatible");
129 return error("--mirror can't be combined with refspecs");
130 }
131
132 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
133 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
134 return error("--all and --mirror are incompatible");
135 }
136
137 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
138 if (remote->push_refspec_nr) {
139 refspec = remote->push_refspec;
140 refspec_nr = remote->push_refspec_nr;
141 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
142 setup_default_push_refspecs();
143 }
144 errs = 0;
145 if (remote->pushurl_nr) {
146 url = remote->pushurl;
147 url_nr = remote->pushurl_nr;
148 } else {
149 url = remote->url;
150 url_nr = remote->url_nr;
151 }
152 for (i = 0; i < url_nr; i++) {
153 struct transport *transport =
154 transport_get(remote, url[i]);
155 int err;
156 int nonfastforward;
157 if (receivepack)
158 transport_set_option(transport,
159 TRANS_OPT_RECEIVEPACK, receivepack);
160 if (thin)
161 transport_set_option(transport, TRANS_OPT_THIN, "yes");
162
163 if (flags & TRANSPORT_PUSH_VERBOSE)
164 fprintf(stderr, "Pushing to %s\n", url[i]);
165 err = transport_push(transport, refspec_nr, refspec, flags,
166 &nonfastforward);
167 err |= transport_disconnect(transport);
168
169 if (!err)
170 continue;
171
172 error("failed to push some refs to '%s'", url[i]);
173 if (nonfastforward && advice_push_nonfastforward) {
174 printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
175 "Merge the remote changes before pushing again. See the 'non-fast-forward'\n"
176 "section of 'git push --help' for details.\n");
177 }
178 errs++;
179 }
180 return !!errs;
181}
182
183int cmd_push(int argc, const char **argv, const char *prefix)
184{
185 int flags = 0;
186 int tags = 0;
187 int rc;
188 const char *repo = NULL; /* default repository */
189 struct option options[] = {
190 OPT_BIT('q', "quiet", &flags, "be quiet", TRANSPORT_PUSH_QUIET),
191 OPT_BIT('v', "verbose", &flags, "be verbose", TRANSPORT_PUSH_VERBOSE),
192 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
193 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
194 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
195 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
196 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
197 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
198 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
199 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
200 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
201 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
202 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
203 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
204 OPT_END()
205 };
206
207 git_config(git_default_config, NULL);
208 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
209
210 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
211 die("--delete is incompatible with --all, --mirror and --tags");
212 if (deleterefs && argc < 2)
213 die("--delete doesn't make sense without any refs");
214
215 if (tags)
216 add_refspec("refs/tags/*");
217
218 if (argc > 0) {
219 repo = argv[0];
220 set_refspecs(argv + 1, argc - 1);
221 }
222
223 rc = do_push(repo, flags);
224 if (rc == -1)
225 usage_with_options(push_usage, options);
226 else
227 return rc;
228}