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