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