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