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