http: update base URLs when we see redirects
[git/git.git] / remote-curl.c
CommitLineData
a2d725b7
DB
1#include "cache.h"
2#include "remote.h"
3#include "strbuf.h"
4#include "walker.h"
5#include "http.h"
d01a8e32 6#include "exec_cmd.h"
ae4efe19 7#include "run-command.h"
97cc7bc4 8#include "pkt-line.h"
de1a2fdd 9#include "sideband.h"
222b1212 10#include "argv-array.h"
2501aff8 11#include "credential.h"
a2d725b7 12
37a8768f 13static struct remote *remote;
d8fab072 14static const char *url; /* always ends with a trailing slash */
37a8768f 15
ef08ef9e
SP
16struct options {
17 int verbosity;
18 unsigned long depth;
19 unsigned progress : 1,
ae4efe19 20 followtags : 1,
de1a2fdd
SP
21 dry_run : 1,
22 thin : 1;
ef08ef9e
SP
23};
24static struct options options;
25
ef08ef9e
SP
26static int set_option(const char *name, const char *value)
27{
28 if (!strcmp(name, "verbosity")) {
29 char *end;
30 int v = strtol(value, &end, 10);
31 if (value == end || *end)
32 return -1;
33 options.verbosity = v;
34 return 0;
35 }
36 else if (!strcmp(name, "progress")) {
37 if (!strcmp(value, "true"))
38 options.progress = 1;
39 else if (!strcmp(value, "false"))
40 options.progress = 0;
41 else
42 return -1;
249b2004 43 return 0;
ef08ef9e
SP
44 }
45 else if (!strcmp(name, "depth")) {
46 char *end;
47 unsigned long v = strtoul(value, &end, 10);
48 if (value == end || *end)
49 return -1;
50 options.depth = v;
249b2004 51 return 0;
ef08ef9e
SP
52 }
53 else if (!strcmp(name, "followtags")) {
54 if (!strcmp(value, "true"))
55 options.followtags = 1;
56 else if (!strcmp(value, "false"))
57 options.followtags = 0;
58 else
59 return -1;
249b2004 60 return 0;
ef08ef9e 61 }
ae4efe19
SP
62 else if (!strcmp(name, "dry-run")) {
63 if (!strcmp(value, "true"))
64 options.dry_run = 1;
65 else if (!strcmp(value, "false"))
66 options.dry_run = 0;
67 else
68 return -1;
69 return 0;
70 }
ef08ef9e
SP
71 else {
72 return 1 /* unsupported */;
73 }
74}
75
97cc7bc4
SP
76struct discovery {
77 const char *service;
78 char *buf_alloc;
79 char *buf;
80 size_t len;
2a455202 81 struct ref *refs;
97cc7bc4
SP
82 unsigned proto_git : 1;
83};
84static struct discovery *last_discovery;
85
b8054bbe
JK
86static struct ref *parse_git_refs(struct discovery *heads, int for_push)
87{
88 struct ref *list = NULL;
89 get_remote_heads(-1, heads->buf, heads->len, &list,
90 for_push ? REF_NORMAL : 0, NULL);
91 return list;
92}
93
94static struct ref *parse_info_refs(struct discovery *heads)
95{
96 char *data, *start, *mid;
97 char *ref_name;
98 int i = 0;
99
100 struct ref *refs = NULL;
101 struct ref *ref = NULL;
102 struct ref *last_ref = NULL;
103
104 data = heads->buf;
105 start = NULL;
106 mid = data;
107 while (i < heads->len) {
108 if (!start) {
109 start = &data[i];
110 }
111 if (data[i] == '\t')
112 mid = &data[i];
113 if (data[i] == '\n') {
114 if (mid - start != 40)
115 die("%sinfo/refs not valid: is this a git repository?", url);
116 data[i] = 0;
117 ref_name = mid + 1;
118 ref = xmalloc(sizeof(struct ref) +
119 strlen(ref_name) + 1);
120 memset(ref, 0, sizeof(struct ref));
121 strcpy(ref->name, ref_name);
122 get_sha1_hex(start, ref->old_sha1);
123 if (!refs)
124 refs = ref;
125 if (last_ref)
126 last_ref->next = ref;
127 last_ref = ref;
128 start = NULL;
129 }
130 i++;
131 }
132
133 ref = alloc_ref("HEAD");
134 if (!http_fetch_ref(url, ref) &&
135 !resolve_remote_symref(ref, refs)) {
136 ref->next = refs;
137 refs = ref;
138 } else {
139 free(ref);
140 }
141
142 return refs;
143}
144
97cc7bc4
SP
145static void free_discovery(struct discovery *d)
146{
147 if (d) {
148 if (d == last_discovery)
149 last_discovery = NULL;
150 free(d->buf_alloc);
2a455202 151 free_refs(d->refs);
97cc7bc4
SP
152 free(d);
153 }
154}
155
426e70d4
JK
156static int show_http_message(struct strbuf *type, struct strbuf *msg)
157{
158 const char *p, *eol;
159
160 /*
161 * We only show text/plain parts, as other types are likely
162 * to be ugly to look at on the user's terminal.
163 *
164 * TODO should handle "; charset=XXX", and re-encode into
165 * logoutputencoding
166 */
167 if (strcasecmp(type->buf, "text/plain"))
168 return -1;
169
170 strbuf_trim(msg);
171 if (!msg->len)
172 return -1;
173
174 p = msg->buf;
175 do {
176 eol = strchrnul(p, '\n');
177 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
178 p = eol + 1;
179 } while(*eol);
180 return 0;
181}
182
2a455202 183static struct discovery* discover_refs(const char *service, int for_push)
a2d725b7 184{
4656bf47
SP
185 struct strbuf exp = STRBUF_INIT;
186 struct strbuf type = STRBUF_INIT;
a2d725b7 187 struct strbuf buffer = STRBUF_INIT;
97cc7bc4 188 struct discovery *last = last_discovery;
a2d725b7 189 char *refs_url;
243c329c 190 int http_ret, maybe_smart = 0;
1bbcc224 191 struct http_get_options options;
a2d725b7 192
97cc7bc4
SP
193 if (last && !strcmp(service, last->service))
194 return last;
195 free_discovery(last);
a2d725b7 196
d8fab072 197 strbuf_addf(&buffer, "%sinfo/refs", url);
02572c2e
JK
198 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
199 git_env_bool("GIT_SMART_HTTP", 1)) {
243c329c 200 maybe_smart = 1;
97cc7bc4
SP
201 if (!strchr(url, '?'))
202 strbuf_addch(&buffer, '?');
203 else
204 strbuf_addch(&buffer, '&');
205 strbuf_addf(&buffer, "service=%s", service);
206 }
207 refs_url = strbuf_detach(&buffer, NULL);
a2d725b7 208
1bbcc224
JK
209 memset(&options, 0, sizeof(options));
210 options.content_type = &type;
211 options.no_cache = 1;
212 options.keep_error = 1;
213
214 http_ret = http_get_strbuf(refs_url, &buffer, &options);
a2d725b7
DB
215 switch (http_ret) {
216 case HTTP_OK:
217 break;
218 case HTTP_MISSING_TARGET:
cfa0f404
JK
219 show_http_message(&type, &buffer);
220 die("repository '%s' not found", url);
42653c09 221 case HTTP_NOAUTH:
426e70d4 222 show_http_message(&type, &buffer);
d5ccbe4d 223 die("Authentication failed for '%s'", url);
a2d725b7 224 default:
426e70d4 225 show_http_message(&type, &buffer);
de89f0b2 226 die("unable to access '%s': %s", url, curl_errorstr);
a2d725b7
DB
227 }
228
97cc7bc4
SP
229 last= xcalloc(1, sizeof(*last_discovery));
230 last->service = service;
231 last->buf_alloc = strbuf_detach(&buffer, &last->len);
232 last->buf = last->buf_alloc;
233
4656bf47
SP
234 strbuf_addf(&exp, "application/x-%s-advertisement", service);
235 if (maybe_smart &&
236 (5 <= last->len && last->buf[4] == '#') &&
237 !strbuf_cmp(&exp, &type)) {
4981fe75
JK
238 char *line;
239
4656bf47
SP
240 /*
241 * smart HTTP response; validate that the service
97cc7bc4
SP
242 * pkt-line matches our request.
243 */
4981fe75 244 line = packet_read_line_buf(&last->buf, &last->len, NULL);
97cc7bc4 245
4656bf47 246 strbuf_reset(&exp);
97cc7bc4 247 strbuf_addf(&exp, "# service=%s", service);
4981fe75
JK
248 if (strcmp(line, exp.buf))
249 die("invalid server response; got '%s'", line);
97cc7bc4
SP
250 strbuf_release(&exp);
251
252 /* The header can include additional metadata lines, up
253 * until a packet flush marker. Ignore these now, but
254 * in the future we might start to scan them.
255 */
4981fe75
JK
256 while (packet_read_line_buf(&last->buf, &last->len, NULL))
257 ;
97cc7bc4
SP
258
259 last->proto_git = 1;
260 }
261
2a455202
JK
262 if (last->proto_git)
263 last->refs = parse_git_refs(last, for_push);
264 else
265 last->refs = parse_info_refs(last);
266
97cc7bc4 267 free(refs_url);
4656bf47
SP
268 strbuf_release(&exp);
269 strbuf_release(&type);
97cc7bc4
SP
270 strbuf_release(&buffer);
271 last_discovery = last;
272 return last;
273}
274
97cc7bc4
SP
275static struct ref *get_refs(int for_push)
276{
277 struct discovery *heads;
278
279 if (for_push)
2a455202 280 heads = discover_refs("git-receive-pack", for_push);
97cc7bc4 281 else
2a455202 282 heads = discover_refs("git-upload-pack", for_push);
97cc7bc4 283
2a455202 284 return heads->refs;
97cc7bc4
SP
285}
286
ae4efe19
SP
287static void output_refs(struct ref *refs)
288{
289 struct ref *posn;
290 for (posn = refs; posn; posn = posn->next) {
291 if (posn->symref)
292 printf("@%s %s\n", posn->symref, posn->name);
293 else
294 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
295 }
296 printf("\n");
297 fflush(stdout);
ae4efe19
SP
298}
299
de1a2fdd
SP
300struct rpc_state {
301 const char *service_name;
302 const char **argv;
8150749d 303 struct strbuf *stdin_preamble;
de1a2fdd
SP
304 char *service_url;
305 char *hdr_content_type;
306 char *hdr_accept;
307 char *buf;
308 size_t alloc;
309 size_t len;
310 size_t pos;
311 int in;
312 int out;
313 struct strbuf result;
b8538603 314 unsigned gzip_request : 1;
6c81a990 315 unsigned initial_buffer : 1;
de1a2fdd
SP
316};
317
318static size_t rpc_out(void *ptr, size_t eltsize,
319 size_t nmemb, void *buffer_)
320{
321 size_t max = eltsize * nmemb;
322 struct rpc_state *rpc = buffer_;
323 size_t avail = rpc->len - rpc->pos;
324
325 if (!avail) {
6c81a990 326 rpc->initial_buffer = 0;
4981fe75 327 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
328 if (!avail)
329 return 0;
330 rpc->pos = 0;
331 rpc->len = avail;
332 }
333
48310608 334 if (max < avail)
de1a2fdd
SP
335 avail = max;
336 memcpy(ptr, rpc->buf + rpc->pos, avail);
337 rpc->pos += avail;
338 return avail;
339}
340
6c81a990 341#ifndef NO_CURL_IOCTL
5092d3ec 342static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
6c81a990
MS
343{
344 struct rpc_state *rpc = clientp;
345
346 switch (cmd) {
347 case CURLIOCMD_NOP:
348 return CURLIOE_OK;
349
350 case CURLIOCMD_RESTARTREAD:
351 if (rpc->initial_buffer) {
352 rpc->pos = 0;
353 return CURLIOE_OK;
354 }
355 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
356 return CURLIOE_FAILRESTART;
357
358 default:
359 return CURLIOE_UNKNOWNCMD;
360 }
361}
362#endif
363
a04ff3ec 364static size_t rpc_in(char *ptr, size_t eltsize,
de1a2fdd
SP
365 size_t nmemb, void *buffer_)
366{
367 size_t size = eltsize * nmemb;
368 struct rpc_state *rpc = buffer_;
369 write_or_die(rpc->in, ptr, size);
370 return size;
371}
372
206b099d
SP
373static int run_slot(struct active_request_slot *slot)
374{
b81401c1 375 int err;
206b099d
SP
376 struct slot_results results;
377
378 slot->results = &results;
379 slot->curl_result = curl_easy_perform(slot->curl);
380 finish_active_slot(slot);
381
1960897e 382 err = handle_curl_result(&results);
b81401c1
JK
383 if (err != HTTP_OK && err != HTTP_REAUTH) {
384 error("RPC failed; result=%d, HTTP code = %ld",
385 results.curl_result, results.http_code);
206b099d
SP
386 }
387
388 return err;
389}
390
391static int probe_rpc(struct rpc_state *rpc)
392{
393 struct active_request_slot *slot;
394 struct curl_slist *headers = NULL;
395 struct strbuf buf = STRBUF_INIT;
396 int err;
397
398 slot = get_active_slot();
399
400 headers = curl_slist_append(headers, rpc->hdr_content_type);
401 headers = curl_slist_append(headers, rpc->hdr_accept);
402
403 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
404 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
405 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 406 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
206b099d
SP
407 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
408 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
409 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
410 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
411 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
412
413 err = run_slot(slot);
414
415 curl_slist_free_all(headers);
416 strbuf_release(&buf);
417 return err;
418}
419
de1a2fdd
SP
420static int post_rpc(struct rpc_state *rpc)
421{
422 struct active_request_slot *slot;
de1a2fdd 423 struct curl_slist *headers = NULL;
b8538603
SP
424 int use_gzip = rpc->gzip_request;
425 char *gzip_body = NULL;
37711549 426 size_t gzip_size = 0;
206b099d 427 int err, large_request = 0;
de1a2fdd
SP
428
429 /* Try to load the entire request, if we can fit it into the
430 * allocated buffer space we can use HTTP/1.0 and avoid the
431 * chunked encoding mess.
432 */
433 while (1) {
434 size_t left = rpc->alloc - rpc->len;
435 char *buf = rpc->buf + rpc->len;
436 int n;
437
438 if (left < LARGE_PACKET_MAX) {
439 large_request = 1;
b8538603 440 use_gzip = 0;
de1a2fdd
SP
441 break;
442 }
443
4981fe75 444 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
de1a2fdd
SP
445 if (!n)
446 break;
447 rpc->len += n;
448 }
449
206b099d 450 if (large_request) {
b81401c1
JK
451 do {
452 err = probe_rpc(rpc);
2501aff8
JK
453 if (err == HTTP_REAUTH)
454 credential_fill(&http_auth);
b81401c1
JK
455 } while (err == HTTP_REAUTH);
456 if (err != HTTP_OK)
457 return -1;
206b099d
SP
458 }
459
abf8df86
JK
460 headers = curl_slist_append(headers, rpc->hdr_content_type);
461 headers = curl_slist_append(headers, rpc->hdr_accept);
462 headers = curl_slist_append(headers, "Expect:");
463
464retry:
de1a2fdd 465 slot = get_active_slot();
de1a2fdd 466
de1a2fdd 467 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
d21f9794 468 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
de1a2fdd 469 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 470 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
de1a2fdd 471
de1a2fdd
SP
472 if (large_request) {
473 /* The request body is large and the size cannot be predicted.
474 * We must use chunked encoding to send it.
475 */
de1a2fdd 476 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
6c81a990 477 rpc->initial_buffer = 1;
de1a2fdd
SP
478 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
479 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
6c81a990
MS
480#ifndef NO_CURL_IOCTL
481 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
482 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
483#endif
de1a2fdd
SP
484 if (options.verbosity > 1) {
485 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
486 fflush(stderr);
487 }
488
2e736fd5
JK
489 } else if (gzip_body) {
490 /*
491 * If we are looping to retry authentication, then the previous
492 * run will have set up the headers and gzip buffer already,
493 * and we just need to send it.
494 */
495 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
496 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
497
b8538603
SP
498 } else if (use_gzip && 1024 < rpc->len) {
499 /* The client backend isn't giving us compressed data so
500 * we can try to deflate it ourselves, this may save on.
501 * the transfer time.
502 */
ef49a7a0 503 git_zstream stream;
b8538603
SP
504 int ret;
505
506 memset(&stream, 0, sizeof(stream));
55bb5c91 507 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
df126e10
JK
508 gzip_size = git_deflate_bound(&stream, rpc->len);
509 gzip_body = xmalloc(gzip_size);
b8538603
SP
510
511 stream.next_in = (unsigned char *)rpc->buf;
512 stream.avail_in = rpc->len;
513 stream.next_out = (unsigned char *)gzip_body;
df126e10 514 stream.avail_out = gzip_size;
b8538603 515
55bb5c91 516 ret = git_deflate(&stream, Z_FINISH);
b8538603
SP
517 if (ret != Z_STREAM_END)
518 die("cannot deflate request; zlib deflate error %d", ret);
519
55bb5c91 520 ret = git_deflate_end_gently(&stream);
b8538603
SP
521 if (ret != Z_OK)
522 die("cannot deflate request; zlib end error %d", ret);
523
df126e10 524 gzip_size = stream.total_out;
b8538603
SP
525
526 headers = curl_slist_append(headers, "Content-Encoding: gzip");
527 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
df126e10 528 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
b8538603
SP
529
530 if (options.verbosity > 1) {
531 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
532 rpc->service_name,
df126e10 533 (unsigned long)rpc->len, (unsigned long)gzip_size);
b8538603
SP
534 fflush(stderr);
535 }
de1a2fdd
SP
536 } else {
537 /* We know the complete request size in advance, use the
538 * more normal Content-Length approach.
539 */
540 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
541 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
542 if (options.verbosity > 1) {
543 fprintf(stderr, "POST %s (%lu bytes)\n",
544 rpc->service_name, (unsigned long)rpc->len);
545 fflush(stderr);
546 }
547 }
548
549 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
550 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
551 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
552
abf8df86 553 err = run_slot(slot);
2501aff8
JK
554 if (err == HTTP_REAUTH && !large_request) {
555 credential_fill(&http_auth);
abf8df86 556 goto retry;
2501aff8 557 }
b81401c1
JK
558 if (err != HTTP_OK)
559 err = -1;
de1a2fdd
SP
560
561 curl_slist_free_all(headers);
b8538603 562 free(gzip_body);
de1a2fdd
SP
563 return err;
564}
565
566static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
567{
568 const char *svc = rpc->service_name;
569 struct strbuf buf = STRBUF_INIT;
8150749d 570 struct strbuf *preamble = rpc->stdin_preamble;
de1a2fdd
SP
571 struct child_process client;
572 int err = 0;
573
de1a2fdd
SP
574 memset(&client, 0, sizeof(client));
575 client.in = -1;
576 client.out = -1;
577 client.git_cmd = 1;
578 client.argv = rpc->argv;
579 if (start_command(&client))
580 exit(1);
8150749d
IT
581 if (preamble)
582 write_or_die(client.in, preamble->buf, preamble->len);
de1a2fdd
SP
583 if (heads)
584 write_or_die(client.in, heads->buf, heads->len);
585
586 rpc->alloc = http_post_buffer;
587 rpc->buf = xmalloc(rpc->alloc);
588 rpc->in = client.in;
589 rpc->out = client.out;
590 strbuf_init(&rpc->result, 0);
591
d8fab072 592 strbuf_addf(&buf, "%s%s", url, svc);
de1a2fdd
SP
593 rpc->service_url = strbuf_detach(&buf, NULL);
594
595 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
596 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
597
8efa5f62 598 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
de1a2fdd
SP
599 rpc->hdr_accept = strbuf_detach(&buf, NULL);
600
601 while (!err) {
4981fe75 602 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
603 if (!n)
604 break;
605 rpc->pos = 0;
606 rpc->len = n;
607 err |= post_rpc(rpc);
608 }
de1a2fdd
SP
609
610 close(client.in);
de1a2fdd 611 client.in = -1;
6cdf0223
SP
612 if (!err) {
613 strbuf_read(&rpc->result, client.out, 0);
614 } else {
615 char buf[4096];
616 for (;;)
617 if (xread(client.out, buf, sizeof(buf)) <= 0)
618 break;
619 }
b4ee10f6
SP
620
621 close(client.out);
de1a2fdd
SP
622 client.out = -1;
623
624 err |= finish_command(&client);
625 free(rpc->service_url);
626 free(rpc->hdr_content_type);
627 free(rpc->hdr_accept);
628 free(rpc->buf);
629 strbuf_release(&buf);
630 return err;
631}
632
292ce46b
SP
633static int fetch_dumb(int nr_heads, struct ref **to_fetch)
634{
26e1e0b2 635 struct walker *walker;
292ce46b
SP
636 char **targets = xmalloc(nr_heads * sizeof(char*));
637 int ret, i;
638
249b2004
SP
639 if (options.depth)
640 die("dumb http transport does not support --depth");
292ce46b
SP
641 for (i = 0; i < nr_heads; i++)
642 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
643
26e1e0b2 644 walker = get_http_walker(url);
292ce46b
SP
645 walker->get_all = 1;
646 walker->get_tree = 1;
647 walker->get_history = 1;
ef08ef9e 648 walker->get_verbosely = options.verbosity >= 3;
292ce46b
SP
649 walker->get_recover = 0;
650 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
26e1e0b2 651 walker_free(walker);
292ce46b
SP
652
653 for (i = 0; i < nr_heads; i++)
654 free(targets[i]);
655 free(targets);
656
657 return ret ? error("Fetch failed.") : 0;
658}
659
249b2004
SP
660static int fetch_git(struct discovery *heads,
661 int nr_heads, struct ref **to_fetch)
662{
663 struct rpc_state rpc;
8150749d 664 struct strbuf preamble = STRBUF_INIT;
249b2004 665 char *depth_arg = NULL;
249b2004 666 int argc = 0, i, err;
8150749d 667 const char *argv[15];
249b2004 668
249b2004
SP
669 argv[argc++] = "fetch-pack";
670 argv[argc++] = "--stateless-rpc";
8150749d 671 argv[argc++] = "--stdin";
249b2004
SP
672 argv[argc++] = "--lock-pack";
673 if (options.followtags)
674 argv[argc++] = "--include-tag";
675 if (options.thin)
676 argv[argc++] = "--thin";
677 if (options.verbosity >= 3) {
678 argv[argc++] = "-v";
679 argv[argc++] = "-v";
680 }
681 if (!options.progress)
682 argv[argc++] = "--no-progress";
683 if (options.depth) {
684 struct strbuf buf = STRBUF_INIT;
685 strbuf_addf(&buf, "--depth=%lu", options.depth);
686 depth_arg = strbuf_detach(&buf, NULL);
687 argv[argc++] = depth_arg;
688 }
689 argv[argc++] = url;
8150749d
IT
690 argv[argc++] = NULL;
691
249b2004
SP
692 for (i = 0; i < nr_heads; i++) {
693 struct ref *ref = to_fetch[i];
694 if (!ref->name || !*ref->name)
695 die("cannot fetch by sha1 over smart http");
8150749d 696 packet_buf_write(&preamble, "%s\n", ref->name);
249b2004 697 }
8150749d 698 packet_buf_flush(&preamble);
249b2004
SP
699
700 memset(&rpc, 0, sizeof(rpc));
701 rpc.service_name = "git-upload-pack",
702 rpc.argv = argv;
8150749d 703 rpc.stdin_preamble = &preamble;
b8538603 704 rpc.gzip_request = 1;
249b2004
SP
705
706 err = rpc_service(&rpc, heads);
707 if (rpc.result.len)
cdf4fb8e 708 write_or_die(1, rpc.result.buf, rpc.result.len);
249b2004 709 strbuf_release(&rpc.result);
8150749d 710 strbuf_release(&preamble);
249b2004
SP
711 free(depth_arg);
712 return err;
713}
714
715static int fetch(int nr_heads, struct ref **to_fetch)
716{
2a455202 717 struct discovery *d = discover_refs("git-upload-pack", 0);
249b2004
SP
718 if (d->proto_git)
719 return fetch_git(d, nr_heads, to_fetch);
720 else
721 return fetch_dumb(nr_heads, to_fetch);
722}
723
292ce46b
SP
724static void parse_fetch(struct strbuf *buf)
725{
726 struct ref **to_fetch = NULL;
727 struct ref *list_head = NULL;
728 struct ref **list = &list_head;
729 int alloc_heads = 0, nr_heads = 0;
730
731 do {
732 if (!prefixcmp(buf->buf, "fetch ")) {
733 char *p = buf->buf + strlen("fetch ");
734 char *name;
735 struct ref *ref;
736 unsigned char old_sha1[20];
737
738 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
739 die("protocol error: expected sha/ref, got %s'", p);
740 if (p[40] == ' ')
741 name = p + 41;
742 else if (!p[40])
743 name = "";
744 else
745 die("protocol error: expected sha/ref, got %s'", p);
746
747 ref = alloc_ref(name);
748 hashcpy(ref->old_sha1, old_sha1);
749
750 *list = ref;
751 list = &ref->next;
752
753 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
754 to_fetch[nr_heads++] = ref;
755 }
756 else
757 die("http transport does not support %s", buf->buf);
758
759 strbuf_reset(buf);
760 if (strbuf_getline(buf, stdin, '\n') == EOF)
761 return;
762 if (!*buf->buf)
763 break;
764 } while (1);
765
249b2004 766 if (fetch(nr_heads, to_fetch))
292ce46b
SP
767 exit(128); /* error already reported */
768 free_refs(list_head);
769 free(to_fetch);
770
771 printf("\n");
772 fflush(stdout);
773 strbuf_reset(buf);
774}
775
ae4efe19
SP
776static int push_dav(int nr_spec, char **specs)
777{
778 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
779 int argc = 0, i;
780
781 argv[argc++] = "http-push";
782 argv[argc++] = "--helper-status";
783 if (options.dry_run)
784 argv[argc++] = "--dry-run";
785 if (options.verbosity > 1)
786 argv[argc++] = "--verbose";
787 argv[argc++] = url;
788 for (i = 0; i < nr_spec; i++)
789 argv[argc++] = specs[i];
790 argv[argc++] = NULL;
791
792 if (run_command_v_opt(argv, RUN_GIT_CMD))
793 die("git-%s failed", argv[0]);
794 free(argv);
795 return 0;
796}
797
de1a2fdd
SP
798static int push_git(struct discovery *heads, int nr_spec, char **specs)
799{
800 struct rpc_state rpc;
222b1212
JH
801 int i, err;
802 struct argv_array args;
803
804 argv_array_init(&args);
805 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
806 NULL);
de1a2fdd 807
de1a2fdd 808 if (options.thin)
222b1212 809 argv_array_push(&args, "--thin");
de1a2fdd 810 if (options.dry_run)
222b1212 811 argv_array_push(&args, "--dry-run");
c207e34f 812 if (options.verbosity == 0)
222b1212 813 argv_array_push(&args, "--quiet");
c207e34f 814 else if (options.verbosity > 1)
222b1212
JH
815 argv_array_push(&args, "--verbose");
816 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
817 argv_array_push(&args, url);
de1a2fdd 818 for (i = 0; i < nr_spec; i++)
222b1212 819 argv_array_push(&args, specs[i]);
de1a2fdd
SP
820
821 memset(&rpc, 0, sizeof(rpc));
822 rpc.service_name = "git-receive-pack",
222b1212 823 rpc.argv = args.argv;
de1a2fdd
SP
824
825 err = rpc_service(&rpc, heads);
826 if (rpc.result.len)
cdf4fb8e 827 write_or_die(1, rpc.result.buf, rpc.result.len);
de1a2fdd 828 strbuf_release(&rpc.result);
222b1212 829 argv_array_clear(&args);
de1a2fdd
SP
830 return err;
831}
832
833static int push(int nr_spec, char **specs)
834{
2a455202 835 struct discovery *heads = discover_refs("git-receive-pack", 1);
de1a2fdd
SP
836 int ret;
837
838 if (heads->proto_git)
839 ret = push_git(heads, nr_spec, specs);
840 else
841 ret = push_dav(nr_spec, specs);
842 free_discovery(heads);
843 return ret;
844}
845
ae4efe19
SP
846static void parse_push(struct strbuf *buf)
847{
848 char **specs = NULL;
5238cbf6 849 int alloc_spec = 0, nr_spec = 0, i, ret;
ae4efe19
SP
850
851 do {
852 if (!prefixcmp(buf->buf, "push ")) {
853 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
854 specs[nr_spec++] = xstrdup(buf->buf + 5);
855 }
856 else
857 die("http transport does not support %s", buf->buf);
858
859 strbuf_reset(buf);
860 if (strbuf_getline(buf, stdin, '\n') == EOF)
dc4cd767 861 goto free_specs;
ae4efe19
SP
862 if (!*buf->buf)
863 break;
864 } while (1);
865
5238cbf6 866 ret = push(nr_spec, specs);
ae4efe19
SP
867 printf("\n");
868 fflush(stdout);
dc4cd767 869
5238cbf6
SP
870 if (ret)
871 exit(128); /* error already reported */
872
dc4cd767
JM
873 free_specs:
874 for (i = 0; i < nr_spec; i++)
875 free(specs[i]);
876 free(specs);
ae4efe19
SP
877}
878
a2d725b7
DB
879int main(int argc, const char **argv)
880{
a2d725b7 881 struct strbuf buf = STRBUF_INIT;
a45d3d7e 882 int nongit;
a2d725b7 883
c6dfb399 884 git_extract_argv0_path(argv[0]);
a45d3d7e 885 setup_git_directory_gently(&nongit);
a2d725b7
DB
886 if (argc < 2) {
887 fprintf(stderr, "Remote needed\n");
888 return 1;
889 }
890
ef08ef9e
SP
891 options.verbosity = 1;
892 options.progress = !!isatty(2);
de1a2fdd 893 options.thin = 1;
ef08ef9e 894
a2d725b7
DB
895 remote = remote_get(argv[1]);
896
897 if (argc > 2) {
d8fab072 898 end_url_with_slash(&buf, argv[2]);
a2d725b7 899 } else {
d8fab072 900 end_url_with_slash(&buf, remote->url[0]);
a2d725b7
DB
901 }
902
d8fab072
TRC
903 url = strbuf_detach(&buf, NULL);
904
a4ddbc33 905 http_init(remote, url, 0);
888692b7 906
a2d725b7 907 do {
1843f0ce
SR
908 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
909 if (ferror(stdin))
910 fprintf(stderr, "Error reading command stream\n");
911 else
912 fprintf(stderr, "Unexpected end of command stream\n");
913 return 1;
914 }
915 if (buf.len == 0)
a2d725b7
DB
916 break;
917 if (!prefixcmp(buf.buf, "fetch ")) {
a45d3d7e
DB
918 if (nongit)
919 die("Fetch attempted without a local repo");
292ce46b
SP
920 parse_fetch(&buf);
921
ae4efe19 922 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
97cc7bc4
SP
923 int for_push = !!strstr(buf.buf + 4, "for-push");
924 output_refs(get_refs(for_push));
ae4efe19
SP
925
926 } else if (!prefixcmp(buf.buf, "push ")) {
927 parse_push(&buf);
928
ef08ef9e
SP
929 } else if (!prefixcmp(buf.buf, "option ")) {
930 char *name = buf.buf + strlen("option ");
931 char *value = strchr(name, ' ');
932 int result;
933
934 if (value)
935 *value++ = '\0';
936 else
937 value = "true";
938
939 result = set_option(name, value);
940 if (!result)
941 printf("ok\n");
942 else if (result < 0)
943 printf("error invalid value\n");
944 else
945 printf("unsupported\n");
a2d725b7 946 fflush(stdout);
ef08ef9e 947
a2d725b7
DB
948 } else if (!strcmp(buf.buf, "capabilities")) {
949 printf("fetch\n");
ef08ef9e 950 printf("option\n");
ae4efe19 951 printf("push\n");
a2d725b7
DB
952 printf("\n");
953 fflush(stdout);
954 } else {
1843f0ce 955 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
a2d725b7
DB
956 return 1;
957 }
958 strbuf_reset(&buf);
959 } while (1);
888692b7
TRC
960
961 http_cleanup();
962
a2d725b7
DB
963 return 0;
964}