git p4 test: do not pollute /tmp
[git/raj.git] / remote-curl.c
blob91b07a41456fe5cdb2157a648353d982b88f7e9a
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 "string-list.h"
10 #include "sideband.h"
11 #include "argv-array.h"
12 #include "credential.h"
14 static struct remote *remote;
15 /* always ends with a trailing slash */
16 static struct strbuf url = STRBUF_INIT;
18 struct options {
19 int verbosity;
20 unsigned long depth;
21 unsigned progress : 1,
22 check_self_contained_and_connected : 1,
23 followtags : 1,
24 dry_run : 1,
25 thin : 1;
27 static struct options options;
28 static struct string_list cas_options = STRING_LIST_INIT_DUP;
30 static int set_option(const char *name, const char *value)
32 if (!strcmp(name, "verbosity")) {
33 char *end;
34 int v = strtol(value, &end, 10);
35 if (value == end || *end)
36 return -1;
37 options.verbosity = v;
38 return 0;
40 else if (!strcmp(name, "progress")) {
41 if (!strcmp(value, "true"))
42 options.progress = 1;
43 else if (!strcmp(value, "false"))
44 options.progress = 0;
45 else
46 return -1;
47 return 0;
49 else if (!strcmp(name, "depth")) {
50 char *end;
51 unsigned long v = strtoul(value, &end, 10);
52 if (value == end || *end)
53 return -1;
54 options.depth = v;
55 return 0;
57 else if (!strcmp(name, "followtags")) {
58 if (!strcmp(value, "true"))
59 options.followtags = 1;
60 else if (!strcmp(value, "false"))
61 options.followtags = 0;
62 else
63 return -1;
64 return 0;
66 else if (!strcmp(name, "dry-run")) {
67 if (!strcmp(value, "true"))
68 options.dry_run = 1;
69 else if (!strcmp(value, "false"))
70 options.dry_run = 0;
71 else
72 return -1;
73 return 0;
75 else if (!strcmp(name, "check-connectivity")) {
76 if (!strcmp(value, "true"))
77 options.check_self_contained_and_connected = 1;
78 else if (!strcmp(value, "false"))
79 options.check_self_contained_and_connected = 0;
80 else
81 return -1;
82 return 0;
84 else if (!strcmp(name, "cas")) {
85 struct strbuf val = STRBUF_INIT;
86 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
87 string_list_append(&cas_options, val.buf);
88 strbuf_release(&val);
89 return 0;
91 else {
92 return 1 /* unsupported */;
96 struct discovery {
97 const char *service;
98 char *buf_alloc;
99 char *buf;
100 size_t len;
101 struct ref *refs;
102 unsigned proto_git : 1;
104 static struct discovery *last_discovery;
106 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
108 struct ref *list = NULL;
109 get_remote_heads(-1, heads->buf, heads->len, &list,
110 for_push ? REF_NORMAL : 0, NULL);
111 return list;
114 static struct ref *parse_info_refs(struct discovery *heads)
116 char *data, *start, *mid;
117 char *ref_name;
118 int i = 0;
120 struct ref *refs = NULL;
121 struct ref *ref = NULL;
122 struct ref *last_ref = NULL;
124 data = heads->buf;
125 start = NULL;
126 mid = data;
127 while (i < heads->len) {
128 if (!start) {
129 start = &data[i];
131 if (data[i] == '\t')
132 mid = &data[i];
133 if (data[i] == '\n') {
134 if (mid - start != 40)
135 die("%sinfo/refs not valid: is this a git repository?",
136 url.buf);
137 data[i] = 0;
138 ref_name = mid + 1;
139 ref = xmalloc(sizeof(struct ref) +
140 strlen(ref_name) + 1);
141 memset(ref, 0, sizeof(struct ref));
142 strcpy(ref->name, ref_name);
143 get_sha1_hex(start, ref->old_sha1);
144 if (!refs)
145 refs = ref;
146 if (last_ref)
147 last_ref->next = ref;
148 last_ref = ref;
149 start = NULL;
151 i++;
154 ref = alloc_ref("HEAD");
155 if (!http_fetch_ref(url.buf, ref) &&
156 !resolve_remote_symref(ref, refs)) {
157 ref->next = refs;
158 refs = ref;
159 } else {
160 free(ref);
163 return refs;
166 static void free_discovery(struct discovery *d)
168 if (d) {
169 if (d == last_discovery)
170 last_discovery = NULL;
171 free(d->buf_alloc);
172 free_refs(d->refs);
173 free(d);
177 static int show_http_message(struct strbuf *type, struct strbuf *msg)
179 const char *p, *eol;
182 * We only show text/plain parts, as other types are likely
183 * to be ugly to look at on the user's terminal.
185 * TODO should handle "; charset=XXX", and re-encode into
186 * logoutputencoding
188 if (strcasecmp(type->buf, "text/plain"))
189 return -1;
191 strbuf_trim(msg);
192 if (!msg->len)
193 return -1;
195 p = msg->buf;
196 do {
197 eol = strchrnul(p, '\n');
198 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
199 p = eol + 1;
200 } while(*eol);
201 return 0;
204 static struct discovery* discover_refs(const char *service, int for_push)
206 struct strbuf exp = STRBUF_INIT;
207 struct strbuf type = STRBUF_INIT;
208 struct strbuf buffer = STRBUF_INIT;
209 struct strbuf refs_url = STRBUF_INIT;
210 struct strbuf effective_url = STRBUF_INIT;
211 struct discovery *last = last_discovery;
212 int http_ret, maybe_smart = 0;
213 struct http_get_options options;
215 if (last && !strcmp(service, last->service))
216 return last;
217 free_discovery(last);
219 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
220 if ((!prefixcmp(url.buf, "http://") || !prefixcmp(url.buf, "https://")) &&
221 git_env_bool("GIT_SMART_HTTP", 1)) {
222 maybe_smart = 1;
223 if (!strchr(url.buf, '?'))
224 strbuf_addch(&refs_url, '?');
225 else
226 strbuf_addch(&refs_url, '&');
227 strbuf_addf(&refs_url, "service=%s", service);
230 memset(&options, 0, sizeof(options));
231 options.content_type = &type;
232 options.effective_url = &effective_url;
233 options.base_url = &url;
234 options.no_cache = 1;
235 options.keep_error = 1;
237 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
238 switch (http_ret) {
239 case HTTP_OK:
240 break;
241 case HTTP_MISSING_TARGET:
242 show_http_message(&type, &buffer);
243 die("repository '%s' not found", url.buf);
244 case HTTP_NOAUTH:
245 show_http_message(&type, &buffer);
246 die("Authentication failed for '%s'", url.buf);
247 default:
248 show_http_message(&type, &buffer);
249 die("unable to access '%s': %s", url.buf, curl_errorstr);
252 last= xcalloc(1, sizeof(*last_discovery));
253 last->service = service;
254 last->buf_alloc = strbuf_detach(&buffer, &last->len);
255 last->buf = last->buf_alloc;
257 strbuf_addf(&exp, "application/x-%s-advertisement", service);
258 if (maybe_smart &&
259 (5 <= last->len && last->buf[4] == '#') &&
260 !strbuf_cmp(&exp, &type)) {
261 char *line;
264 * smart HTTP response; validate that the service
265 * pkt-line matches our request.
267 line = packet_read_line_buf(&last->buf, &last->len, NULL);
269 strbuf_reset(&exp);
270 strbuf_addf(&exp, "# service=%s", service);
271 if (strcmp(line, exp.buf))
272 die("invalid server response; got '%s'", line);
273 strbuf_release(&exp);
275 /* The header can include additional metadata lines, up
276 * until a packet flush marker. Ignore these now, but
277 * in the future we might start to scan them.
279 while (packet_read_line_buf(&last->buf, &last->len, NULL))
282 last->proto_git = 1;
285 if (last->proto_git)
286 last->refs = parse_git_refs(last, for_push);
287 else
288 last->refs = parse_info_refs(last);
290 strbuf_release(&refs_url);
291 strbuf_release(&exp);
292 strbuf_release(&type);
293 strbuf_release(&effective_url);
294 strbuf_release(&buffer);
295 last_discovery = last;
296 return last;
299 static struct ref *get_refs(int for_push)
301 struct discovery *heads;
303 if (for_push)
304 heads = discover_refs("git-receive-pack", for_push);
305 else
306 heads = discover_refs("git-upload-pack", for_push);
308 return heads->refs;
311 static void output_refs(struct ref *refs)
313 struct ref *posn;
314 for (posn = refs; posn; posn = posn->next) {
315 if (posn->symref)
316 printf("@%s %s\n", posn->symref, posn->name);
317 else
318 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
320 printf("\n");
321 fflush(stdout);
324 struct rpc_state {
325 const char *service_name;
326 const char **argv;
327 struct strbuf *stdin_preamble;
328 char *service_url;
329 char *hdr_content_type;
330 char *hdr_accept;
331 char *buf;
332 size_t alloc;
333 size_t len;
334 size_t pos;
335 int in;
336 int out;
337 struct strbuf result;
338 unsigned gzip_request : 1;
339 unsigned initial_buffer : 1;
342 static size_t rpc_out(void *ptr, size_t eltsize,
343 size_t nmemb, void *buffer_)
345 size_t max = eltsize * nmemb;
346 struct rpc_state *rpc = buffer_;
347 size_t avail = rpc->len - rpc->pos;
349 if (!avail) {
350 rpc->initial_buffer = 0;
351 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
352 if (!avail)
353 return 0;
354 rpc->pos = 0;
355 rpc->len = avail;
358 if (max < avail)
359 avail = max;
360 memcpy(ptr, rpc->buf + rpc->pos, avail);
361 rpc->pos += avail;
362 return avail;
365 #ifndef NO_CURL_IOCTL
366 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
368 struct rpc_state *rpc = clientp;
370 switch (cmd) {
371 case CURLIOCMD_NOP:
372 return CURLIOE_OK;
374 case CURLIOCMD_RESTARTREAD:
375 if (rpc->initial_buffer) {
376 rpc->pos = 0;
377 return CURLIOE_OK;
379 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
380 return CURLIOE_FAILRESTART;
382 default:
383 return CURLIOE_UNKNOWNCMD;
386 #endif
388 static size_t rpc_in(char *ptr, size_t eltsize,
389 size_t nmemb, void *buffer_)
391 size_t size = eltsize * nmemb;
392 struct rpc_state *rpc = buffer_;
393 write_or_die(rpc->in, ptr, size);
394 return size;
397 static int run_slot(struct active_request_slot *slot,
398 struct slot_results *results)
400 int err;
401 struct slot_results results_buf;
403 if (!results)
404 results = &results_buf;
406 slot->results = results;
407 slot->curl_result = curl_easy_perform(slot->curl);
408 finish_active_slot(slot);
410 err = handle_curl_result(results);
411 if (err != HTTP_OK && err != HTTP_REAUTH) {
412 error("RPC failed; result=%d, HTTP code = %ld",
413 results->curl_result, results->http_code);
416 return err;
419 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
421 struct active_request_slot *slot;
422 struct curl_slist *headers = NULL;
423 struct strbuf buf = STRBUF_INIT;
424 int err;
426 slot = get_active_slot();
428 headers = curl_slist_append(headers, rpc->hdr_content_type);
429 headers = curl_slist_append(headers, rpc->hdr_accept);
431 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
432 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
433 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
434 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
435 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
436 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
437 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
438 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
439 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
441 err = run_slot(slot, results);
443 curl_slist_free_all(headers);
444 strbuf_release(&buf);
445 return err;
448 static int post_rpc(struct rpc_state *rpc)
450 struct active_request_slot *slot;
451 struct curl_slist *headers = NULL;
452 int use_gzip = rpc->gzip_request;
453 char *gzip_body = NULL;
454 size_t gzip_size = 0;
455 int err, large_request = 0;
456 int needs_100_continue = 0;
458 /* Try to load the entire request, if we can fit it into the
459 * allocated buffer space we can use HTTP/1.0 and avoid the
460 * chunked encoding mess.
462 while (1) {
463 size_t left = rpc->alloc - rpc->len;
464 char *buf = rpc->buf + rpc->len;
465 int n;
467 if (left < LARGE_PACKET_MAX) {
468 large_request = 1;
469 use_gzip = 0;
470 break;
473 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
474 if (!n)
475 break;
476 rpc->len += n;
479 if (large_request) {
480 struct slot_results results;
482 do {
483 err = probe_rpc(rpc, &results);
484 if (err == HTTP_REAUTH)
485 credential_fill(&http_auth);
486 } while (err == HTTP_REAUTH);
487 if (err != HTTP_OK)
488 return -1;
490 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
491 needs_100_continue = 1;
494 headers = curl_slist_append(headers, rpc->hdr_content_type);
495 headers = curl_slist_append(headers, rpc->hdr_accept);
496 headers = curl_slist_append(headers, needs_100_continue ?
497 "Expect: 100-continue" : "Expect:");
499 retry:
500 slot = get_active_slot();
502 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
503 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
504 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
505 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
507 if (large_request) {
508 /* The request body is large and the size cannot be predicted.
509 * We must use chunked encoding to send it.
511 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
512 rpc->initial_buffer = 1;
513 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
514 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
515 #ifndef NO_CURL_IOCTL
516 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
517 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
518 #endif
519 if (options.verbosity > 1) {
520 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
521 fflush(stderr);
524 } else if (gzip_body) {
526 * If we are looping to retry authentication, then the previous
527 * run will have set up the headers and gzip buffer already,
528 * and we just need to send it.
530 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
531 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
533 } else if (use_gzip && 1024 < rpc->len) {
534 /* The client backend isn't giving us compressed data so
535 * we can try to deflate it ourselves, this may save on.
536 * the transfer time.
538 git_zstream stream;
539 int ret;
541 memset(&stream, 0, sizeof(stream));
542 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
543 gzip_size = git_deflate_bound(&stream, rpc->len);
544 gzip_body = xmalloc(gzip_size);
546 stream.next_in = (unsigned char *)rpc->buf;
547 stream.avail_in = rpc->len;
548 stream.next_out = (unsigned char *)gzip_body;
549 stream.avail_out = gzip_size;
551 ret = git_deflate(&stream, Z_FINISH);
552 if (ret != Z_STREAM_END)
553 die("cannot deflate request; zlib deflate error %d", ret);
555 ret = git_deflate_end_gently(&stream);
556 if (ret != Z_OK)
557 die("cannot deflate request; zlib end error %d", ret);
559 gzip_size = stream.total_out;
561 headers = curl_slist_append(headers, "Content-Encoding: gzip");
562 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
563 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
565 if (options.verbosity > 1) {
566 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
567 rpc->service_name,
568 (unsigned long)rpc->len, (unsigned long)gzip_size);
569 fflush(stderr);
571 } else {
572 /* We know the complete request size in advance, use the
573 * more normal Content-Length approach.
575 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
576 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
577 if (options.verbosity > 1) {
578 fprintf(stderr, "POST %s (%lu bytes)\n",
579 rpc->service_name, (unsigned long)rpc->len);
580 fflush(stderr);
584 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
585 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
586 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
588 err = run_slot(slot, NULL);
589 if (err == HTTP_REAUTH && !large_request) {
590 credential_fill(&http_auth);
591 goto retry;
593 if (err != HTTP_OK)
594 err = -1;
596 curl_slist_free_all(headers);
597 free(gzip_body);
598 return err;
601 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
603 const char *svc = rpc->service_name;
604 struct strbuf buf = STRBUF_INIT;
605 struct strbuf *preamble = rpc->stdin_preamble;
606 struct child_process client;
607 int err = 0;
609 memset(&client, 0, sizeof(client));
610 client.in = -1;
611 client.out = -1;
612 client.git_cmd = 1;
613 client.argv = rpc->argv;
614 if (start_command(&client))
615 exit(1);
616 if (preamble)
617 write_or_die(client.in, preamble->buf, preamble->len);
618 if (heads)
619 write_or_die(client.in, heads->buf, heads->len);
621 rpc->alloc = http_post_buffer;
622 rpc->buf = xmalloc(rpc->alloc);
623 rpc->in = client.in;
624 rpc->out = client.out;
625 strbuf_init(&rpc->result, 0);
627 strbuf_addf(&buf, "%s%s", url.buf, svc);
628 rpc->service_url = strbuf_detach(&buf, NULL);
630 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
631 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
633 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
634 rpc->hdr_accept = strbuf_detach(&buf, NULL);
636 while (!err) {
637 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
638 if (!n)
639 break;
640 rpc->pos = 0;
641 rpc->len = n;
642 err |= post_rpc(rpc);
645 close(client.in);
646 client.in = -1;
647 if (!err) {
648 strbuf_read(&rpc->result, client.out, 0);
649 } else {
650 char buf[4096];
651 for (;;)
652 if (xread(client.out, buf, sizeof(buf)) <= 0)
653 break;
656 close(client.out);
657 client.out = -1;
659 err |= finish_command(&client);
660 free(rpc->service_url);
661 free(rpc->hdr_content_type);
662 free(rpc->hdr_accept);
663 free(rpc->buf);
664 strbuf_release(&buf);
665 return err;
668 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
670 struct walker *walker;
671 char **targets = xmalloc(nr_heads * sizeof(char*));
672 int ret, i;
674 if (options.depth)
675 die("dumb http transport does not support --depth");
676 for (i = 0; i < nr_heads; i++)
677 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
679 walker = get_http_walker(url.buf);
680 walker->get_all = 1;
681 walker->get_tree = 1;
682 walker->get_history = 1;
683 walker->get_verbosely = options.verbosity >= 3;
684 walker->get_recover = 0;
685 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
686 walker_free(walker);
688 for (i = 0; i < nr_heads; i++)
689 free(targets[i]);
690 free(targets);
692 return ret ? error("Fetch failed.") : 0;
695 static int fetch_git(struct discovery *heads,
696 int nr_heads, struct ref **to_fetch)
698 struct rpc_state rpc;
699 struct strbuf preamble = STRBUF_INIT;
700 char *depth_arg = NULL;
701 int argc = 0, i, err;
702 const char *argv[16];
704 argv[argc++] = "fetch-pack";
705 argv[argc++] = "--stateless-rpc";
706 argv[argc++] = "--stdin";
707 argv[argc++] = "--lock-pack";
708 if (options.followtags)
709 argv[argc++] = "--include-tag";
710 if (options.thin)
711 argv[argc++] = "--thin";
712 if (options.verbosity >= 3) {
713 argv[argc++] = "-v";
714 argv[argc++] = "-v";
716 if (options.check_self_contained_and_connected)
717 argv[argc++] = "--check-self-contained-and-connected";
718 if (!options.progress)
719 argv[argc++] = "--no-progress";
720 if (options.depth) {
721 struct strbuf buf = STRBUF_INIT;
722 strbuf_addf(&buf, "--depth=%lu", options.depth);
723 depth_arg = strbuf_detach(&buf, NULL);
724 argv[argc++] = depth_arg;
726 argv[argc++] = url.buf;
727 argv[argc++] = NULL;
729 for (i = 0; i < nr_heads; i++) {
730 struct ref *ref = to_fetch[i];
731 if (!ref->name || !*ref->name)
732 die("cannot fetch by sha1 over smart http");
733 packet_buf_write(&preamble, "%s\n", ref->name);
735 packet_buf_flush(&preamble);
737 memset(&rpc, 0, sizeof(rpc));
738 rpc.service_name = "git-upload-pack",
739 rpc.argv = argv;
740 rpc.stdin_preamble = &preamble;
741 rpc.gzip_request = 1;
743 err = rpc_service(&rpc, heads);
744 if (rpc.result.len)
745 write_or_die(1, rpc.result.buf, rpc.result.len);
746 strbuf_release(&rpc.result);
747 strbuf_release(&preamble);
748 free(depth_arg);
749 return err;
752 static int fetch(int nr_heads, struct ref **to_fetch)
754 struct discovery *d = discover_refs("git-upload-pack", 0);
755 if (d->proto_git)
756 return fetch_git(d, nr_heads, to_fetch);
757 else
758 return fetch_dumb(nr_heads, to_fetch);
761 static void parse_fetch(struct strbuf *buf)
763 struct ref **to_fetch = NULL;
764 struct ref *list_head = NULL;
765 struct ref **list = &list_head;
766 int alloc_heads = 0, nr_heads = 0;
768 do {
769 if (!prefixcmp(buf->buf, "fetch ")) {
770 char *p = buf->buf + strlen("fetch ");
771 char *name;
772 struct ref *ref;
773 unsigned char old_sha1[20];
775 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
776 die("protocol error: expected sha/ref, got %s'", p);
777 if (p[40] == ' ')
778 name = p + 41;
779 else if (!p[40])
780 name = "";
781 else
782 die("protocol error: expected sha/ref, got %s'", p);
784 ref = alloc_ref(name);
785 hashcpy(ref->old_sha1, old_sha1);
787 *list = ref;
788 list = &ref->next;
790 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
791 to_fetch[nr_heads++] = ref;
793 else
794 die("http transport does not support %s", buf->buf);
796 strbuf_reset(buf);
797 if (strbuf_getline(buf, stdin, '\n') == EOF)
798 return;
799 if (!*buf->buf)
800 break;
801 } while (1);
803 if (fetch(nr_heads, to_fetch))
804 exit(128); /* error already reported */
805 free_refs(list_head);
806 free(to_fetch);
808 printf("\n");
809 fflush(stdout);
810 strbuf_reset(buf);
813 static int push_dav(int nr_spec, char **specs)
815 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
816 int argc = 0, i;
818 argv[argc++] = "http-push";
819 argv[argc++] = "--helper-status";
820 if (options.dry_run)
821 argv[argc++] = "--dry-run";
822 if (options.verbosity > 1)
823 argv[argc++] = "--verbose";
824 argv[argc++] = url.buf;
825 for (i = 0; i < nr_spec; i++)
826 argv[argc++] = specs[i];
827 argv[argc++] = NULL;
829 if (run_command_v_opt(argv, RUN_GIT_CMD))
830 die("git-%s failed", argv[0]);
831 free(argv);
832 return 0;
835 static int push_git(struct discovery *heads, int nr_spec, char **specs)
837 struct rpc_state rpc;
838 int i, err;
839 struct argv_array args;
840 struct string_list_item *cas_option;
842 argv_array_init(&args);
843 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
844 NULL);
846 if (options.thin)
847 argv_array_push(&args, "--thin");
848 if (options.dry_run)
849 argv_array_push(&args, "--dry-run");
850 if (options.verbosity == 0)
851 argv_array_push(&args, "--quiet");
852 else if (options.verbosity > 1)
853 argv_array_push(&args, "--verbose");
854 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
855 for_each_string_list_item(cas_option, &cas_options)
856 argv_array_push(&args, cas_option->string);
857 argv_array_push(&args, url.buf);
858 for (i = 0; i < nr_spec; i++)
859 argv_array_push(&args, specs[i]);
861 memset(&rpc, 0, sizeof(rpc));
862 rpc.service_name = "git-receive-pack",
863 rpc.argv = args.argv;
865 err = rpc_service(&rpc, heads);
866 if (rpc.result.len)
867 write_or_die(1, rpc.result.buf, rpc.result.len);
868 strbuf_release(&rpc.result);
869 argv_array_clear(&args);
870 return err;
873 static int push(int nr_spec, char **specs)
875 struct discovery *heads = discover_refs("git-receive-pack", 1);
876 int ret;
878 if (heads->proto_git)
879 ret = push_git(heads, nr_spec, specs);
880 else
881 ret = push_dav(nr_spec, specs);
882 free_discovery(heads);
883 return ret;
886 static void parse_push(struct strbuf *buf)
888 char **specs = NULL;
889 int alloc_spec = 0, nr_spec = 0, i, ret;
891 do {
892 if (!prefixcmp(buf->buf, "push ")) {
893 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
894 specs[nr_spec++] = xstrdup(buf->buf + 5);
896 else
897 die("http transport does not support %s", buf->buf);
899 strbuf_reset(buf);
900 if (strbuf_getline(buf, stdin, '\n') == EOF)
901 goto free_specs;
902 if (!*buf->buf)
903 break;
904 } while (1);
906 ret = push(nr_spec, specs);
907 printf("\n");
908 fflush(stdout);
910 if (ret)
911 exit(128); /* error already reported */
913 free_specs:
914 for (i = 0; i < nr_spec; i++)
915 free(specs[i]);
916 free(specs);
919 int main(int argc, const char **argv)
921 struct strbuf buf = STRBUF_INIT;
922 int nongit;
924 git_extract_argv0_path(argv[0]);
925 setup_git_directory_gently(&nongit);
926 if (argc < 2) {
927 fprintf(stderr, "Remote needed\n");
928 return 1;
931 options.verbosity = 1;
932 options.progress = !!isatty(2);
933 options.thin = 1;
935 remote = remote_get(argv[1]);
937 if (argc > 2) {
938 end_url_with_slash(&url, argv[2]);
939 } else {
940 end_url_with_slash(&url, remote->url[0]);
943 http_init(remote, url.buf, 0);
945 do {
946 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
947 if (ferror(stdin))
948 fprintf(stderr, "Error reading command stream\n");
949 else
950 fprintf(stderr, "Unexpected end of command stream\n");
951 return 1;
953 if (buf.len == 0)
954 break;
955 if (!prefixcmp(buf.buf, "fetch ")) {
956 if (nongit)
957 die("Fetch attempted without a local repo");
958 parse_fetch(&buf);
960 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
961 int for_push = !!strstr(buf.buf + 4, "for-push");
962 output_refs(get_refs(for_push));
964 } else if (!prefixcmp(buf.buf, "push ")) {
965 parse_push(&buf);
967 } else if (!prefixcmp(buf.buf, "option ")) {
968 char *name = buf.buf + strlen("option ");
969 char *value = strchr(name, ' ');
970 int result;
972 if (value)
973 *value++ = '\0';
974 else
975 value = "true";
977 result = set_option(name, value);
978 if (!result)
979 printf("ok\n");
980 else if (result < 0)
981 printf("error invalid value\n");
982 else
983 printf("unsupported\n");
984 fflush(stdout);
986 } else if (!strcmp(buf.buf, "capabilities")) {
987 printf("fetch\n");
988 printf("option\n");
989 printf("push\n");
990 printf("check-connectivity\n");
991 printf("\n");
992 fflush(stdout);
993 } else {
994 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
995 return 1;
997 strbuf_reset(&buf);
998 } while (1);
1000 http_cleanup();
1002 return 0;