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