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