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