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