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