grep: prepare for new header field filter
[git/jrn.git] / remote-curl.c
blob3ec474fc631eb3b433b3cadbd74aed4b83cba724
1 #include "cache.h"
2 #include "remote.h"
3 #include "strbuf.h"
4 #include "walker.h"
5 #include "http.h"
6 #include "exec_cmd.h"
7 #include "run-command.h"
8 #include "pkt-line.h"
9 #include "sideband.h"
11 static struct remote *remote;
12 static const char *url; /* always ends with a trailing slash */
14 struct options {
15 int verbosity;
16 unsigned long depth;
17 unsigned progress : 1,
18 followtags : 1,
19 dry_run : 1,
20 thin : 1;
22 static struct options options;
24 static int set_option(const char *name, const char *value)
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;
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;
41 return 0;
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;
49 return 0;
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;
58 return 0;
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;
69 else {
70 return 1 /* unsupported */;
74 struct discovery {
75 const char *service;
76 char *buf_alloc;
77 char *buf;
78 size_t len;
79 unsigned proto_git : 1;
81 static struct discovery *last_discovery;
83 static void free_discovery(struct discovery *d)
85 if (d) {
86 if (d == last_discovery)
87 last_discovery = NULL;
88 free(d->buf_alloc);
89 free(d);
93 static struct discovery* discover_refs(const char *service)
95 struct strbuf buffer = STRBUF_INIT;
96 struct discovery *last = last_discovery;
97 char *refs_url;
98 int http_ret, is_http = 0, proto_git_candidate = 1;
100 if (last && !strcmp(service, last->service))
101 return last;
102 free_discovery(last);
104 strbuf_addf(&buffer, "%sinfo/refs", url);
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);
113 refs_url = strbuf_detach(&buffer, NULL);
115 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
117 /* try again with "plain" url (no ? or & appended) */
118 if (http_ret != HTTP_OK && http_ret != HTTP_NOAUTH) {
119 free(refs_url);
120 strbuf_reset(&buffer);
122 proto_git_candidate = 0;
123 strbuf_addf(&buffer, "%sinfo/refs", url);
124 refs_url = strbuf_detach(&buffer, NULL);
126 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
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);
135 case HTTP_NOAUTH:
136 die("Authentication failed");
137 default:
138 http_error(refs_url, http_ret);
139 die("HTTP request failed");
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;
147 if (is_http && proto_git_candidate
148 && 5 <= last->len && last->buf[4] == '#') {
149 /* smart HTTP response; validate that the service
150 * pkt-line matches our request.
152 struct strbuf exp = STRBUF_INIT;
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);
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);
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.
168 strbuf_reset(&buffer);
169 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
170 strbuf_reset(&buffer);
172 last->proto_git = 1;
175 free(refs_url);
176 strbuf_release(&buffer);
177 last_discovery = last;
178 return last;
181 static int write_discovery(int in, int out, void *data)
183 struct discovery *heads = data;
184 int err = 0;
185 if (write_in_full(out, heads->buf, heads->len) != heads->len)
186 err = 1;
187 close(out);
188 return err;
191 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
193 struct ref *list = NULL;
194 struct async async;
196 memset(&async, 0, sizeof(async));
197 async.proc = write_discovery;
198 async.data = heads;
199 async.out = -1;
201 if (start_async(&async))
202 die("cannot start thread to parse advertised refs");
203 get_remote_heads(async.out, &list,
204 for_push ? REF_NORMAL : 0, NULL);
205 close(async.out);
206 if (finish_async(&async))
207 die("ref parsing thread failed");
208 return list;
211 static struct ref *parse_info_refs(struct discovery *heads)
213 char *data, *start, *mid;
214 char *ref_name;
215 int i = 0;
217 struct ref *refs = NULL;
218 struct ref *ref = NULL;
219 struct ref *last_ref = NULL;
221 data = heads->buf;
222 start = NULL;
223 mid = data;
224 while (i < heads->len) {
225 if (!start) {
226 start = &data[i];
228 if (data[i] == '\t')
229 mid = &data[i];
230 if (data[i] == '\n') {
231 if (mid - start != 40)
232 die("%sinfo/refs not valid: is this a git repository?", url);
233 data[i] = 0;
234 ref_name = mid + 1;
235 ref = xmalloc(sizeof(struct ref) +
236 strlen(ref_name) + 1);
237 memset(ref, 0, sizeof(struct ref));
238 strcpy(ref->name, ref_name);
239 get_sha1_hex(start, ref->old_sha1);
240 if (!refs)
241 refs = ref;
242 if (last_ref)
243 last_ref->next = ref;
244 last_ref = ref;
245 start = NULL;
247 i++;
250 ref = alloc_ref("HEAD");
251 if (!http_fetch_ref(url, ref) &&
252 !resolve_remote_symref(ref, refs)) {
253 ref->next = refs;
254 refs = ref;
255 } else {
256 free(ref);
259 return refs;
262 static struct ref *get_refs(int for_push)
264 struct discovery *heads;
266 if (for_push)
267 heads = discover_refs("git-receive-pack");
268 else
269 heads = discover_refs("git-upload-pack");
271 if (heads->proto_git)
272 return parse_git_refs(heads, for_push);
273 return parse_info_refs(heads);
276 static void output_refs(struct ref *refs)
278 struct ref *posn;
279 for (posn = refs; posn; posn = posn->next) {
280 if (posn->symref)
281 printf("@%s %s\n", posn->symref, posn->name);
282 else
283 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
285 printf("\n");
286 fflush(stdout);
287 free_refs(refs);
290 struct rpc_state {
291 const char *service_name;
292 const char **argv;
293 struct strbuf *stdin_preamble;
294 char *service_url;
295 char *hdr_content_type;
296 char *hdr_accept;
297 char *buf;
298 size_t alloc;
299 size_t len;
300 size_t pos;
301 int in;
302 int out;
303 struct strbuf result;
304 unsigned gzip_request : 1;
305 unsigned initial_buffer : 1;
308 static size_t rpc_out(void *ptr, size_t eltsize,
309 size_t nmemb, void *buffer_)
311 size_t max = eltsize * nmemb;
312 struct rpc_state *rpc = buffer_;
313 size_t avail = rpc->len - rpc->pos;
315 if (!avail) {
316 rpc->initial_buffer = 0;
317 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
318 if (!avail)
319 return 0;
320 rpc->pos = 0;
321 rpc->len = avail;
324 if (max < avail)
325 avail = max;
326 memcpy(ptr, rpc->buf + rpc->pos, avail);
327 rpc->pos += avail;
328 return avail;
331 #ifndef NO_CURL_IOCTL
332 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
334 struct rpc_state *rpc = clientp;
336 switch (cmd) {
337 case CURLIOCMD_NOP:
338 return CURLIOE_OK;
340 case CURLIOCMD_RESTARTREAD:
341 if (rpc->initial_buffer) {
342 rpc->pos = 0;
343 return CURLIOE_OK;
345 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
346 return CURLIOE_FAILRESTART;
348 default:
349 return CURLIOE_UNKNOWNCMD;
352 #endif
354 static size_t rpc_in(char *ptr, size_t eltsize,
355 size_t nmemb, void *buffer_)
357 size_t size = eltsize * nmemb;
358 struct rpc_state *rpc = buffer_;
359 write_or_die(rpc->in, ptr, size);
360 return size;
363 static int run_slot(struct active_request_slot *slot)
365 int err;
366 struct slot_results results;
368 slot->results = &results;
369 slot->curl_result = curl_easy_perform(slot->curl);
370 finish_active_slot(slot);
372 err = handle_curl_result(slot);
373 if (err != HTTP_OK && err != HTTP_REAUTH) {
374 error("RPC failed; result=%d, HTTP code = %ld",
375 results.curl_result, results.http_code);
378 return err;
381 static int probe_rpc(struct rpc_state *rpc)
383 struct active_request_slot *slot;
384 struct curl_slist *headers = NULL;
385 struct strbuf buf = STRBUF_INIT;
386 int err;
388 slot = get_active_slot();
390 headers = curl_slist_append(headers, rpc->hdr_content_type);
391 headers = curl_slist_append(headers, rpc->hdr_accept);
393 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
394 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
395 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
396 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
397 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
398 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
399 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
400 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
401 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
403 err = run_slot(slot);
405 curl_slist_free_all(headers);
406 strbuf_release(&buf);
407 return err;
410 static int post_rpc(struct rpc_state *rpc)
412 struct active_request_slot *slot;
413 struct curl_slist *headers = NULL;
414 int use_gzip = rpc->gzip_request;
415 char *gzip_body = NULL;
416 int err, large_request = 0;
418 /* Try to load the entire request, if we can fit it into the
419 * allocated buffer space we can use HTTP/1.0 and avoid the
420 * chunked encoding mess.
422 while (1) {
423 size_t left = rpc->alloc - rpc->len;
424 char *buf = rpc->buf + rpc->len;
425 int n;
427 if (left < LARGE_PACKET_MAX) {
428 large_request = 1;
429 use_gzip = 0;
430 break;
433 n = packet_read_line(rpc->out, buf, left);
434 if (!n)
435 break;
436 rpc->len += n;
439 if (large_request) {
440 do {
441 err = probe_rpc(rpc);
442 } while (err == HTTP_REAUTH);
443 if (err != HTTP_OK)
444 return -1;
447 slot = get_active_slot();
449 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
450 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
451 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
452 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
454 headers = curl_slist_append(headers, rpc->hdr_content_type);
455 headers = curl_slist_append(headers, rpc->hdr_accept);
456 headers = curl_slist_append(headers, "Expect:");
458 if (large_request) {
459 /* The request body is large and the size cannot be predicted.
460 * We must use chunked encoding to send it.
462 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
463 rpc->initial_buffer = 1;
464 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
465 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
466 #ifndef NO_CURL_IOCTL
467 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
468 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
469 #endif
470 if (options.verbosity > 1) {
471 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
472 fflush(stderr);
475 } else if (use_gzip && 1024 < rpc->len) {
476 /* The client backend isn't giving us compressed data so
477 * we can try to deflate it ourselves, this may save on.
478 * the transfer time.
480 size_t size;
481 git_zstream stream;
482 int ret;
484 memset(&stream, 0, sizeof(stream));
485 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
486 size = git_deflate_bound(&stream, rpc->len);
487 gzip_body = xmalloc(size);
489 stream.next_in = (unsigned char *)rpc->buf;
490 stream.avail_in = rpc->len;
491 stream.next_out = (unsigned char *)gzip_body;
492 stream.avail_out = size;
494 ret = git_deflate(&stream, Z_FINISH);
495 if (ret != Z_STREAM_END)
496 die("cannot deflate request; zlib deflate error %d", ret);
498 ret = git_deflate_end_gently(&stream);
499 if (ret != Z_OK)
500 die("cannot deflate request; zlib end error %d", ret);
502 size = stream.total_out;
504 headers = curl_slist_append(headers, "Content-Encoding: gzip");
505 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
506 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
508 if (options.verbosity > 1) {
509 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
510 rpc->service_name,
511 (unsigned long)rpc->len, (unsigned long)size);
512 fflush(stderr);
514 } else {
515 /* We know the complete request size in advance, use the
516 * more normal Content-Length approach.
518 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
519 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
520 if (options.verbosity > 1) {
521 fprintf(stderr, "POST %s (%lu bytes)\n",
522 rpc->service_name, (unsigned long)rpc->len);
523 fflush(stderr);
527 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
528 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
529 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
531 do {
532 err = run_slot(slot);
533 } while (err == HTTP_REAUTH && !large_request && !use_gzip);
534 if (err != HTTP_OK)
535 err = -1;
537 curl_slist_free_all(headers);
538 free(gzip_body);
539 return err;
542 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
544 const char *svc = rpc->service_name;
545 struct strbuf buf = STRBUF_INIT;
546 struct strbuf *preamble = rpc->stdin_preamble;
547 struct child_process client;
548 int err = 0;
550 memset(&client, 0, sizeof(client));
551 client.in = -1;
552 client.out = -1;
553 client.git_cmd = 1;
554 client.argv = rpc->argv;
555 if (start_command(&client))
556 exit(1);
557 if (preamble)
558 write_or_die(client.in, preamble->buf, preamble->len);
559 if (heads)
560 write_or_die(client.in, heads->buf, heads->len);
562 rpc->alloc = http_post_buffer;
563 rpc->buf = xmalloc(rpc->alloc);
564 rpc->in = client.in;
565 rpc->out = client.out;
566 strbuf_init(&rpc->result, 0);
568 strbuf_addf(&buf, "%s%s", url, svc);
569 rpc->service_url = strbuf_detach(&buf, NULL);
571 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
572 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
574 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
575 rpc->hdr_accept = strbuf_detach(&buf, NULL);
577 while (!err) {
578 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
579 if (!n)
580 break;
581 rpc->pos = 0;
582 rpc->len = n;
583 err |= post_rpc(rpc);
586 close(client.in);
587 client.in = -1;
588 if (!err) {
589 strbuf_read(&rpc->result, client.out, 0);
590 } else {
591 char buf[4096];
592 for (;;)
593 if (xread(client.out, buf, sizeof(buf)) <= 0)
594 break;
597 close(client.out);
598 client.out = -1;
600 err |= finish_command(&client);
601 free(rpc->service_url);
602 free(rpc->hdr_content_type);
603 free(rpc->hdr_accept);
604 free(rpc->buf);
605 strbuf_release(&buf);
606 return err;
609 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
611 struct walker *walker;
612 char **targets = xmalloc(nr_heads * sizeof(char*));
613 int ret, i;
615 if (options.depth)
616 die("dumb http transport does not support --depth");
617 for (i = 0; i < nr_heads; i++)
618 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
620 walker = get_http_walker(url);
621 walker->get_all = 1;
622 walker->get_tree = 1;
623 walker->get_history = 1;
624 walker->get_verbosely = options.verbosity >= 3;
625 walker->get_recover = 0;
626 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
627 walker_free(walker);
629 for (i = 0; i < nr_heads; i++)
630 free(targets[i]);
631 free(targets);
633 return ret ? error("Fetch failed.") : 0;
636 static int fetch_git(struct discovery *heads,
637 int nr_heads, struct ref **to_fetch)
639 struct rpc_state rpc;
640 struct strbuf preamble = STRBUF_INIT;
641 char *depth_arg = NULL;
642 int argc = 0, i, err;
643 const char *argv[15];
645 argv[argc++] = "fetch-pack";
646 argv[argc++] = "--stateless-rpc";
647 argv[argc++] = "--stdin";
648 argv[argc++] = "--lock-pack";
649 if (options.followtags)
650 argv[argc++] = "--include-tag";
651 if (options.thin)
652 argv[argc++] = "--thin";
653 if (options.verbosity >= 3) {
654 argv[argc++] = "-v";
655 argv[argc++] = "-v";
657 if (!options.progress)
658 argv[argc++] = "--no-progress";
659 if (options.depth) {
660 struct strbuf buf = STRBUF_INIT;
661 strbuf_addf(&buf, "--depth=%lu", options.depth);
662 depth_arg = strbuf_detach(&buf, NULL);
663 argv[argc++] = depth_arg;
665 argv[argc++] = url;
666 argv[argc++] = NULL;
668 for (i = 0; i < nr_heads; i++) {
669 struct ref *ref = to_fetch[i];
670 if (!ref->name || !*ref->name)
671 die("cannot fetch by sha1 over smart http");
672 packet_buf_write(&preamble, "%s\n", ref->name);
674 packet_buf_flush(&preamble);
676 memset(&rpc, 0, sizeof(rpc));
677 rpc.service_name = "git-upload-pack",
678 rpc.argv = argv;
679 rpc.stdin_preamble = &preamble;
680 rpc.gzip_request = 1;
682 err = rpc_service(&rpc, heads);
683 if (rpc.result.len)
684 safe_write(1, rpc.result.buf, rpc.result.len);
685 strbuf_release(&rpc.result);
686 strbuf_release(&preamble);
687 free(depth_arg);
688 return err;
691 static int fetch(int nr_heads, struct ref **to_fetch)
693 struct discovery *d = discover_refs("git-upload-pack");
694 if (d->proto_git)
695 return fetch_git(d, nr_heads, to_fetch);
696 else
697 return fetch_dumb(nr_heads, to_fetch);
700 static void parse_fetch(struct strbuf *buf)
702 struct ref **to_fetch = NULL;
703 struct ref *list_head = NULL;
704 struct ref **list = &list_head;
705 int alloc_heads = 0, nr_heads = 0;
707 do {
708 if (!prefixcmp(buf->buf, "fetch ")) {
709 char *p = buf->buf + strlen("fetch ");
710 char *name;
711 struct ref *ref;
712 unsigned char old_sha1[20];
714 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
715 die("protocol error: expected sha/ref, got %s'", p);
716 if (p[40] == ' ')
717 name = p + 41;
718 else if (!p[40])
719 name = "";
720 else
721 die("protocol error: expected sha/ref, got %s'", p);
723 ref = alloc_ref(name);
724 hashcpy(ref->old_sha1, old_sha1);
726 *list = ref;
727 list = &ref->next;
729 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
730 to_fetch[nr_heads++] = ref;
732 else
733 die("http transport does not support %s", buf->buf);
735 strbuf_reset(buf);
736 if (strbuf_getline(buf, stdin, '\n') == EOF)
737 return;
738 if (!*buf->buf)
739 break;
740 } while (1);
742 if (fetch(nr_heads, to_fetch))
743 exit(128); /* error already reported */
744 free_refs(list_head);
745 free(to_fetch);
747 printf("\n");
748 fflush(stdout);
749 strbuf_reset(buf);
752 static int push_dav(int nr_spec, char **specs)
754 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
755 int argc = 0, i;
757 argv[argc++] = "http-push";
758 argv[argc++] = "--helper-status";
759 if (options.dry_run)
760 argv[argc++] = "--dry-run";
761 if (options.verbosity > 1)
762 argv[argc++] = "--verbose";
763 argv[argc++] = url;
764 for (i = 0; i < nr_spec; i++)
765 argv[argc++] = specs[i];
766 argv[argc++] = NULL;
768 if (run_command_v_opt(argv, RUN_GIT_CMD))
769 die("git-%s failed", argv[0]);
770 free(argv);
771 return 0;
774 static int push_git(struct discovery *heads, int nr_spec, char **specs)
776 struct rpc_state rpc;
777 const char **argv;
778 int argc = 0, i, err;
780 argv = xmalloc((10 + nr_spec) * sizeof(char*));
781 argv[argc++] = "send-pack";
782 argv[argc++] = "--stateless-rpc";
783 argv[argc++] = "--helper-status";
784 if (options.thin)
785 argv[argc++] = "--thin";
786 if (options.dry_run)
787 argv[argc++] = "--dry-run";
788 if (options.verbosity == 0)
789 argv[argc++] = "--quiet";
790 else if (options.verbosity > 1)
791 argv[argc++] = "--verbose";
792 argv[argc++] = options.progress ? "--progress" : "--no-progress";
793 argv[argc++] = url;
794 for (i = 0; i < nr_spec; i++)
795 argv[argc++] = specs[i];
796 argv[argc++] = NULL;
798 memset(&rpc, 0, sizeof(rpc));
799 rpc.service_name = "git-receive-pack",
800 rpc.argv = argv;
802 err = rpc_service(&rpc, heads);
803 if (rpc.result.len)
804 safe_write(1, rpc.result.buf, rpc.result.len);
805 strbuf_release(&rpc.result);
806 free(argv);
807 return err;
810 static int push(int nr_spec, char **specs)
812 struct discovery *heads = discover_refs("git-receive-pack");
813 int ret;
815 if (heads->proto_git)
816 ret = push_git(heads, nr_spec, specs);
817 else
818 ret = push_dav(nr_spec, specs);
819 free_discovery(heads);
820 return ret;
823 static void parse_push(struct strbuf *buf)
825 char **specs = NULL;
826 int alloc_spec = 0, nr_spec = 0, i, ret;
828 do {
829 if (!prefixcmp(buf->buf, "push ")) {
830 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
831 specs[nr_spec++] = xstrdup(buf->buf + 5);
833 else
834 die("http transport does not support %s", buf->buf);
836 strbuf_reset(buf);
837 if (strbuf_getline(buf, stdin, '\n') == EOF)
838 goto free_specs;
839 if (!*buf->buf)
840 break;
841 } while (1);
843 ret = push(nr_spec, specs);
844 printf("\n");
845 fflush(stdout);
847 if (ret)
848 exit(128); /* error already reported */
850 free_specs:
851 for (i = 0; i < nr_spec; i++)
852 free(specs[i]);
853 free(specs);
856 int main(int argc, const char **argv)
858 struct strbuf buf = STRBUF_INIT;
859 int nongit;
861 git_extract_argv0_path(argv[0]);
862 setup_git_directory_gently(&nongit);
863 if (argc < 2) {
864 fprintf(stderr, "Remote needed\n");
865 return 1;
868 options.verbosity = 1;
869 options.progress = !!isatty(2);
870 options.thin = 1;
872 remote = remote_get(argv[1]);
874 if (argc > 2) {
875 end_url_with_slash(&buf, argv[2]);
876 } else {
877 end_url_with_slash(&buf, remote->url[0]);
880 url = strbuf_detach(&buf, NULL);
882 http_init(remote, url, 0);
884 do {
885 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
886 if (ferror(stdin))
887 fprintf(stderr, "Error reading command stream\n");
888 else
889 fprintf(stderr, "Unexpected end of command stream\n");
890 return 1;
892 if (buf.len == 0)
893 break;
894 if (!prefixcmp(buf.buf, "fetch ")) {
895 if (nongit)
896 die("Fetch attempted without a local repo");
897 parse_fetch(&buf);
899 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
900 int for_push = !!strstr(buf.buf + 4, "for-push");
901 output_refs(get_refs(for_push));
903 } else if (!prefixcmp(buf.buf, "push ")) {
904 parse_push(&buf);
906 } else if (!prefixcmp(buf.buf, "option ")) {
907 char *name = buf.buf + strlen("option ");
908 char *value = strchr(name, ' ');
909 int result;
911 if (value)
912 *value++ = '\0';
913 else
914 value = "true";
916 result = set_option(name, value);
917 if (!result)
918 printf("ok\n");
919 else if (result < 0)
920 printf("error invalid value\n");
921 else
922 printf("unsupported\n");
923 fflush(stdout);
925 } else if (!strcmp(buf.buf, "capabilities")) {
926 printf("fetch\n");
927 printf("option\n");
928 printf("push\n");
929 printf("\n");
930 fflush(stdout);
931 } else {
932 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
933 return 1;
935 strbuf_reset(&buf);
936 } while (1);
938 http_cleanup();
940 return 0;