git-push reports the URL after failing.
[git/git.git] / builtin-push.c
CommitLineData
755225de
LT
1/*
2 * "git push"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "run-command.h"
7#include "builtin.h"
8
9#define MAX_URI (16)
10
d23842fd 11static const char push_usage[] = "git-push [--all] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]";
755225de 12
bcc785f6 13static int all, tags, force, thin = 1, verbose;
d23842fd 14static const char *receivepack;
755225de
LT
15
16#define BUF_SIZE (2084)
17static char buffer[BUF_SIZE];
18
96f1e58f
DR
19static const char **refspec;
20static int refspec_nr;
755225de
LT
21
22static void add_refspec(const char *ref)
23{
24 int nr = refspec_nr + 1;
25 refspec = xrealloc(refspec, nr * sizeof(char *));
26 refspec[nr-1] = ref;
27 refspec_nr = nr;
28}
29
8da19775 30static int expand_one_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data)
755225de
LT
31{
32 /* Ignore the "refs/" at the beginning of the refname */
33 ref += 5;
34
cc44c765 35 if (!prefixcmp(ref, "tags/"))
9befac47 36 add_refspec(xstrdup(ref));
755225de
LT
37 return 0;
38}
39
40static void expand_refspecs(void)
41{
42 if (all) {
43 if (refspec_nr)
44 die("cannot mix '--all' and a refspec");
45
46 /*
47 * No need to expand "--all" - we'll just use
48 * the "--all" flag to send-pack
49 */
50 return;
51 }
52 if (!tags)
53 return;
cb5d709f 54 for_each_ref(expand_one_ref, NULL);
755225de
LT
55}
56
d46ae3f0
JH
57struct wildcard_cb {
58 const char *from_prefix;
59 int from_prefix_len;
60 const char *to_prefix;
61 int to_prefix_len;
62 int force;
63};
64
65static int expand_wildcard_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data)
66{
67 struct wildcard_cb *cb = cb_data;
68 int len = strlen(ref);
69 char *expanded, *newref;
70
71 if (len < cb->from_prefix_len ||
72 memcmp(cb->from_prefix, ref, cb->from_prefix_len))
73 return 0;
74 expanded = xmalloc(len * 2 + cb->force +
75 (cb->to_prefix_len - cb->from_prefix_len) + 2);
76 newref = expanded + cb->force;
77 if (cb->force)
78 expanded[0] = '+';
79 memcpy(newref, ref, len);
80 newref[len] = ':';
81 memcpy(newref + len + 1, cb->to_prefix, cb->to_prefix_len);
82 strcpy(newref + len + 1 + cb->to_prefix_len,
83 ref + cb->from_prefix_len);
84 add_refspec(expanded);
85 return 0;
86}
87
88static int wildcard_ref(const char *ref)
89{
90 int len;
91 const char *colon;
92 struct wildcard_cb cb;
93
94 memset(&cb, 0, sizeof(cb));
95 if (ref[0] == '+') {
96 cb.force = 1;
97 ref++;
98 }
99 len = strlen(ref);
100 colon = strchr(ref, ':');
101 if (! (colon && ref < colon &&
102 colon[-2] == '/' && colon[-1] == '*' &&
103 /* "<mine>/<asterisk>:<yours>/<asterisk>" is at least 7 bytes */
104 7 <= len &&
105 ref[len-2] == '/' && ref[len-1] == '*') )
106 return 0 ;
107 cb.from_prefix = ref;
108 cb.from_prefix_len = colon - ref - 1;
109 cb.to_prefix = colon + 1;
110 cb.to_prefix_len = len - (colon - ref) - 2;
111 for_each_ref(expand_wildcard_ref, &cb);
112 return 1;
113}
114
755225de
LT
115static void set_refspecs(const char **refs, int nr)
116{
117 if (nr) {
d46ae3f0
JH
118 int i;
119 for (i = 0; i < nr; i++) {
120 const char *ref = refs[i];
121 if (!strcmp("tag", ref)) {
122 char *tag;
123 int len;
124 if (nr <= ++i)
125 die("tag shorthand without <tag>");
126 len = strlen(refs[i]) + 11;
127 tag = xmalloc(len);
128 strcpy(tag, "refs/tags/");
129 strcat(tag, refs[i]);
130 ref = tag;
411fb8ba 131 }
d46ae3f0
JH
132 else if (wildcard_ref(ref))
133 continue;
134 add_refspec(ref);
411fb8ba 135 }
755225de
LT
136 }
137 expand_refspecs();
138}
139
140static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
141{
142 int n = 0;
143 FILE *f = fopen(git_path("remotes/%s", repo), "r");
5edbcd8d 144 int has_explicit_refspec = refspec_nr || all || tags;
755225de
LT
145
146 if (!f)
147 return -1;
148 while (fgets(buffer, BUF_SIZE, f)) {
5c477b97 149 int is_refspec;
755225de
LT
150 char *s, *p;
151
599065a3 152 if (!prefixcmp(buffer, "URL:")) {
5c477b97 153 is_refspec = 0;
6fe5b7ff 154 s = buffer + 4;
599065a3 155 } else if (!prefixcmp(buffer, "Push:")) {
5c477b97 156 is_refspec = 1;
6fe5b7ff 157 s = buffer + 5;
5c477b97 158 } else
755225de 159 continue;
755225de
LT
160
161 /* Remove whitespace at the head.. */
162 while (isspace(*s))
163 s++;
164 if (!*s)
165 continue;
166
167 /* ..and at the end */
168 p = s + strlen(s);
169 while (isspace(p[-1]))
170 *--p = 0;
171
7aaf83da
JH
172 if (!is_refspec) {
173 if (n < MAX_URI)
9befac47 174 uri[n++] = xstrdup(s);
7aaf83da 175 else
82e5a82f 176 error("more than %d URL's specified, ignoring the rest", MAX_URI);
7aaf83da 177 }
d46ae3f0
JH
178 else if (is_refspec && !has_explicit_refspec) {
179 if (!wildcard_ref(s))
180 add_refspec(xstrdup(s));
181 }
755225de
LT
182 }
183 fclose(f);
184 if (!n)
185 die("remote '%s' has no URL", repo);
186 return n;
187}
188
97d4df0b
JS
189static const char **config_uri;
190static const char *config_repo;
191static int config_repo_len;
192static int config_current_uri;
193static int config_get_refspecs;
060aafc1 194static int config_get_receivepack;
97d4df0b
JS
195
196static int get_remote_config(const char* key, const char* value)
197{
cc44c765 198 if (!prefixcmp(key, "remote.") &&
97d4df0b
JS
199 !strncmp(key + 7, config_repo, config_repo_len)) {
200 if (!strcmp(key + 7 + config_repo_len, ".url")) {
201 if (config_current_uri < MAX_URI)
9befac47 202 config_uri[config_current_uri++] = xstrdup(value);
97d4df0b
JS
203 else
204 error("more than %d URL's specified, ignoring the rest", MAX_URI);
205 }
206 else if (config_get_refspecs &&
d46ae3f0
JH
207 !strcmp(key + 7 + config_repo_len, ".push")) {
208 if (!wildcard_ref(value))
209 add_refspec(xstrdup(value));
210 }
060aafc1
UKK
211 else if (config_get_receivepack &&
212 !strcmp(key + 7 + config_repo_len, ".receivepack")) {
d23842fd
UKK
213 if (!receivepack) {
214 char *rp = xmalloc(strlen(value) + 16);
215 sprintf(rp, "--receive-pack=%s", value);
216 receivepack = rp;
060aafc1
UKK
217 } else
218 error("more than one receivepack given, using the first");
219 }
97d4df0b
JS
220 }
221 return 0;
222}
223
224static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI])
225{
226 config_repo_len = strlen(repo);
227 config_repo = repo;
228 config_current_uri = 0;
229 config_uri = uri;
5edbcd8d 230 config_get_refspecs = !(refspec_nr || all || tags);
d23842fd 231 config_get_receivepack = (receivepack == NULL);
97d4df0b
JS
232
233 git_config(get_remote_config);
234 return config_current_uri;
235}
236
755225de
LT
237static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
238{
239 const char *slash = strchr(repo, '/');
240 int n = slash ? slash - repo : 1000;
241 FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
242 char *s, *p;
243 int len;
244
245 if (!f)
246 return 0;
247 s = fgets(buffer, BUF_SIZE, f);
248 fclose(f);
249 if (!s)
250 return 0;
251 while (isspace(*s))
252 s++;
253 if (!*s)
254 return 0;
255 p = s + strlen(s);
256 while (isspace(p[-1]))
257 *--p = 0;
258 len = p - s;
259 if (slash)
260 len += strlen(slash);
261 p = xmalloc(len + 1);
262 strcpy(p, s);
263 if (slash)
264 strcat(p, slash);
265 uri[0] = p;
266 return 1;
267}
268
7aaf83da
JH
269/*
270 * Read remotes and branches file, fill the push target URI
271 * list. If there is no command line refspecs, read Push: lines
272 * to set up the *refspec list as well.
273 * return the number of push target URIs
274 */
275static int read_config(const char *repo, const char *uri[MAX_URI])
755225de
LT
276{
277 int n;
278
279 if (*repo != '/') {
280 n = get_remotes_uri(repo, uri);
281 if (n > 0)
282 return n;
283
97d4df0b
JS
284 n = get_config_remotes_uri(repo, uri);
285 if (n > 0)
286 return n;
287
755225de
LT
288 n = get_branches_uri(repo, uri);
289 if (n > 0)
290 return n;
291 }
292
293 uri[0] = repo;
294 return 1;
295}
296
297static int do_push(const char *repo)
298{
299 const char *uri[MAX_URI];
7aaf83da 300 int i, n;
441c823e 301 int common_argc;
755225de
LT
302 const char **argv;
303 int argc;
304
7aaf83da 305 n = read_config(repo, uri);
755225de
LT
306 if (n <= 0)
307 die("bad repository '%s'", repo);
308
309 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
310 argv[0] = "dummy-send-pack";
311 argc = 1;
312 if (all)
313 argv[argc++] = "--all";
314 if (force)
315 argv[argc++] = "--force";
d23842fd
UKK
316 if (receivepack)
317 argv[argc++] = receivepack;
441c823e 318 common_argc = argc;
755225de
LT
319
320 for (i = 0; i < n; i++) {
60b7f38e 321 int err;
441c823e
NH
322 int dest_argc = common_argc;
323 int dest_refspec_nr = refspec_nr;
324 const char **dest_refspec = refspec;
755225de 325 const char *dest = uri[i];
df91ba36 326 const char *sender = "send-pack";
cc44c765
JH
327 if (!prefixcmp(dest, "http://") ||
328 !prefixcmp(dest, "https://"))
df91ba36 329 sender = "http-push";
441c823e
NH
330 else if (thin)
331 argv[dest_argc++] = "--thin";
755225de 332 argv[0] = sender;
441c823e
NH
333 argv[dest_argc++] = dest;
334 while (dest_refspec_nr--)
335 argv[dest_argc++] = *dest_refspec++;
336 argv[dest_argc] = NULL;
bcc785f6
LT
337 if (verbose)
338 fprintf(stderr, "Pushing to %s\n", dest);
df91ba36 339 err = run_command_v_opt(argv, RUN_GIT_CMD);
60b7f38e 340 if (!err)
755225de 341 continue;
39878b0c
JH
342
343 error("failed to push to '%s'", uri[i]);
60b7f38e 344 switch (err) {
755225de
LT
345 case -ERR_RUN_COMMAND_FORK:
346 die("unable to fork for %s", sender);
347 case -ERR_RUN_COMMAND_EXEC:
348 die("unable to exec %s", sender);
349 case -ERR_RUN_COMMAND_WAITPID:
350 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
351 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
352 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
353 die("%s died with strange error", sender);
354 default:
60b7f38e 355 return -err;
755225de
LT
356 }
357 }
358 return 0;
359}
360
a633fca0 361int cmd_push(int argc, const char **argv, const char *prefix)
755225de
LT
362{
363 int i;
a9486b02 364 const char *repo = "origin"; /* default repository */
755225de
LT
365
366 for (i = 1; i < argc; i++) {
367 const char *arg = argv[i];
368
369 if (arg[0] != '-') {
370 repo = arg;
371 i++;
372 break;
373 }
bcc785f6
LT
374 if (!strcmp(arg, "-v")) {
375 verbose=1;
376 continue;
377 }
cc44c765 378 if (!prefixcmp(arg, "--repo=")) {
bcc785f6
LT
379 repo = arg+7;
380 continue;
381 }
755225de
LT
382 if (!strcmp(arg, "--all")) {
383 all = 1;
384 continue;
385 }
386 if (!strcmp(arg, "--tags")) {
387 tags = 1;
388 continue;
389 }
8f615493 390 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
755225de
LT
391 force = 1;
392 continue;
393 }
394 if (!strcmp(arg, "--thin")) {
395 thin = 1;
396 continue;
397 }
398 if (!strcmp(arg, "--no-thin")) {
399 thin = 0;
400 continue;
401 }
cc44c765 402 if (!prefixcmp(arg, "--receive-pack=")) {
d23842fd
UKK
403 receivepack = arg;
404 continue;
405 }
cc44c765 406 if (!prefixcmp(arg, "--exec=")) {
d23842fd 407 receivepack = arg;
755225de
LT
408 continue;
409 }
410 usage(push_usage);
411 }
412 set_refspecs(argv + i, argc - i);
413 return do_push(repo);
414}