Merge branch 'ma/fast-export-skip-merge-fix'
[git] / remote-curl.c
1 #include "cache.h"
2 #include "config.h"
3 #include "remote.h"
4 #include "connect.h"
5 #include "strbuf.h"
6 #include "walker.h"
7 #include "http.h"
8 #include "exec-cmd.h"
9 #include "run-command.h"
10 #include "pkt-line.h"
11 #include "string-list.h"
12 #include "sideband.h"
13 #include "argv-array.h"
14 #include "credential.h"
15 #include "sha1-array.h"
16 #include "send-pack.h"
17 #include "protocol.h"
18 #include "quote.h"
19
20 static struct remote *remote;
21 /* always ends with a trailing slash */
22 static struct strbuf url = STRBUF_INIT;
23
24 struct options {
25         int verbosity;
26         unsigned long depth;
27         char *deepen_since;
28         struct string_list deepen_not;
29         struct string_list push_options;
30         char *filter;
31         unsigned progress : 1,
32                 check_self_contained_and_connected : 1,
33                 cloning : 1,
34                 update_shallow : 1,
35                 followtags : 1,
36                 dry_run : 1,
37                 thin : 1,
38                 /* One of the SEND_PACK_PUSH_CERT_* constants. */
39                 push_cert : 2,
40                 deepen_relative : 1,
41                 from_promisor : 1,
42                 no_dependents : 1;
43 };
44 static struct options options;
45 static struct string_list cas_options = STRING_LIST_INIT_DUP;
46
47 static int set_option(const char *name, const char *value)
48 {
49         if (!strcmp(name, "verbosity")) {
50                 char *end;
51                 int v = strtol(value, &end, 10);
52                 if (value == end || *end)
53                         return -1;
54                 options.verbosity = v;
55                 return 0;
56         }
57         else if (!strcmp(name, "progress")) {
58                 if (!strcmp(value, "true"))
59                         options.progress = 1;
60                 else if (!strcmp(value, "false"))
61                         options.progress = 0;
62                 else
63                         return -1;
64                 return 0;
65         }
66         else if (!strcmp(name, "depth")) {
67                 char *end;
68                 unsigned long v = strtoul(value, &end, 10);
69                 if (value == end || *end)
70                         return -1;
71                 options.depth = v;
72                 return 0;
73         }
74         else if (!strcmp(name, "deepen-since")) {
75                 options.deepen_since = xstrdup(value);
76                 return 0;
77         }
78         else if (!strcmp(name, "deepen-not")) {
79                 string_list_append(&options.deepen_not, value);
80                 return 0;
81         }
82         else if (!strcmp(name, "deepen-relative")) {
83                 if (!strcmp(value, "true"))
84                         options.deepen_relative = 1;
85                 else if (!strcmp(value, "false"))
86                         options.deepen_relative = 0;
87                 else
88                         return -1;
89                 return 0;
90         }
91         else if (!strcmp(name, "followtags")) {
92                 if (!strcmp(value, "true"))
93                         options.followtags = 1;
94                 else if (!strcmp(value, "false"))
95                         options.followtags = 0;
96                 else
97                         return -1;
98                 return 0;
99         }
100         else if (!strcmp(name, "dry-run")) {
101                 if (!strcmp(value, "true"))
102                         options.dry_run = 1;
103                 else if (!strcmp(value, "false"))
104                         options.dry_run = 0;
105                 else
106                         return -1;
107                 return 0;
108         }
109         else if (!strcmp(name, "check-connectivity")) {
110                 if (!strcmp(value, "true"))
111                         options.check_self_contained_and_connected = 1;
112                 else if (!strcmp(value, "false"))
113                         options.check_self_contained_and_connected = 0;
114                 else
115                         return -1;
116                 return 0;
117         }
118         else if (!strcmp(name, "cas")) {
119                 struct strbuf val = STRBUF_INIT;
120                 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
121                 string_list_append(&cas_options, val.buf);
122                 strbuf_release(&val);
123                 return 0;
124         } else if (!strcmp(name, "cloning")) {
125                 if (!strcmp(value, "true"))
126                         options.cloning = 1;
127                 else if (!strcmp(value, "false"))
128                         options.cloning = 0;
129                 else
130                         return -1;
131                 return 0;
132         } else if (!strcmp(name, "update-shallow")) {
133                 if (!strcmp(value, "true"))
134                         options.update_shallow = 1;
135                 else if (!strcmp(value, "false"))
136                         options.update_shallow = 0;
137                 else
138                         return -1;
139                 return 0;
140         } else if (!strcmp(name, "pushcert")) {
141                 if (!strcmp(value, "true"))
142                         options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
143                 else if (!strcmp(value, "false"))
144                         options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
145                 else if (!strcmp(value, "if-asked"))
146                         options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
147                 else
148                         return -1;
149                 return 0;
150         } else if (!strcmp(name, "push-option")) {
151                 if (*value != '"')
152                         string_list_append(&options.push_options, value);
153                 else {
154                         struct strbuf unquoted = STRBUF_INIT;
155                         if (unquote_c_style(&unquoted, value, NULL) < 0)
156                                 die("invalid quoting in push-option value");
157                         string_list_append_nodup(&options.push_options,
158                                                  strbuf_detach(&unquoted, NULL));
159                 }
160                 return 0;
161
162 #if LIBCURL_VERSION_NUM >= 0x070a08
163         } else if (!strcmp(name, "family")) {
164                 if (!strcmp(value, "ipv4"))
165                         git_curl_ipresolve = CURL_IPRESOLVE_V4;
166                 else if (!strcmp(value, "ipv6"))
167                         git_curl_ipresolve = CURL_IPRESOLVE_V6;
168                 else if (!strcmp(value, "all"))
169                         git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
170                 else
171                         return -1;
172                 return 0;
173 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
174         } else if (!strcmp(name, "from-promisor")) {
175                 options.from_promisor = 1;
176                 return 0;
177         } else if (!strcmp(name, "no-dependents")) {
178                 options.no_dependents = 1;
179                 return 0;
180         } else if (!strcmp(name, "filter")) {
181                 options.filter = xstrdup(value);;
182                 return 0;
183         } else {
184                 return 1 /* unsupported */;
185         }
186 }
187
188 struct discovery {
189         char *service;
190         char *buf_alloc;
191         char *buf;
192         size_t len;
193         struct ref *refs;
194         struct oid_array shallow;
195         enum protocol_version version;
196         unsigned proto_git : 1;
197 };
198 static struct discovery *last_discovery;
199
200 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
201 {
202         struct ref *list = NULL;
203         struct packet_reader reader;
204
205         packet_reader_init(&reader, -1, heads->buf, heads->len,
206                            PACKET_READ_CHOMP_NEWLINE |
207                            PACKET_READ_GENTLE_ON_EOF);
208
209         heads->version = discover_version(&reader);
210         switch (heads->version) {
211         case protocol_v2:
212                 /*
213                  * Do nothing.  This isn't a list of refs but rather a
214                  * capability advertisement.  Client would have run
215                  * 'stateless-connect' so we'll dump this capability listing
216                  * and let them request the refs themselves.
217                  */
218                 break;
219         case protocol_v1:
220         case protocol_v0:
221                 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
222                                  NULL, &heads->shallow);
223                 break;
224         case protocol_unknown_version:
225                 BUG("unknown protocol version");
226         }
227
228         return list;
229 }
230
231 static struct ref *parse_info_refs(struct discovery *heads)
232 {
233         char *data, *start, *mid;
234         char *ref_name;
235         int i = 0;
236
237         struct ref *refs = NULL;
238         struct ref *ref = NULL;
239         struct ref *last_ref = NULL;
240
241         data = heads->buf;
242         start = NULL;
243         mid = data;
244         while (i < heads->len) {
245                 if (!start) {
246                         start = &data[i];
247                 }
248                 if (data[i] == '\t')
249                         mid = &data[i];
250                 if (data[i] == '\n') {
251                         if (mid - start != 40)
252                                 die("%sinfo/refs not valid: is this a git repository?",
253                                     url.buf);
254                         data[i] = 0;
255                         ref_name = mid + 1;
256                         ref = alloc_ref(ref_name);
257                         get_oid_hex(start, &ref->old_oid);
258                         if (!refs)
259                                 refs = ref;
260                         if (last_ref)
261                                 last_ref->next = ref;
262                         last_ref = ref;
263                         start = NULL;
264                 }
265                 i++;
266         }
267
268         ref = alloc_ref("HEAD");
269         if (!http_fetch_ref(url.buf, ref) &&
270             !resolve_remote_symref(ref, refs)) {
271                 ref->next = refs;
272                 refs = ref;
273         } else {
274                 free(ref);
275         }
276
277         return refs;
278 }
279
280 static void free_discovery(struct discovery *d)
281 {
282         if (d) {
283                 if (d == last_discovery)
284                         last_discovery = NULL;
285                 free(d->shallow.oid);
286                 free(d->buf_alloc);
287                 free_refs(d->refs);
288                 free(d->service);
289                 free(d);
290         }
291 }
292
293 static int show_http_message(struct strbuf *type, struct strbuf *charset,
294                              struct strbuf *msg)
295 {
296         const char *p, *eol;
297
298         /*
299          * We only show text/plain parts, as other types are likely
300          * to be ugly to look at on the user's terminal.
301          */
302         if (strcmp(type->buf, "text/plain"))
303                 return -1;
304         if (charset->len)
305                 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
306
307         strbuf_trim(msg);
308         if (!msg->len)
309                 return -1;
310
311         p = msg->buf;
312         do {
313                 eol = strchrnul(p, '\n');
314                 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
315                 p = eol + 1;
316         } while(*eol);
317         return 0;
318 }
319
320 static int get_protocol_http_header(enum protocol_version version,
321                                     struct strbuf *header)
322 {
323         if (version > 0) {
324                 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
325                             version);
326
327                 return 1;
328         }
329
330         return 0;
331 }
332
333 static struct discovery *discover_refs(const char *service, int for_push)
334 {
335         struct strbuf exp = STRBUF_INIT;
336         struct strbuf type = STRBUF_INIT;
337         struct strbuf charset = STRBUF_INIT;
338         struct strbuf buffer = STRBUF_INIT;
339         struct strbuf refs_url = STRBUF_INIT;
340         struct strbuf effective_url = STRBUF_INIT;
341         struct strbuf protocol_header = STRBUF_INIT;
342         struct string_list extra_headers = STRING_LIST_INIT_DUP;
343         struct discovery *last = last_discovery;
344         int http_ret, maybe_smart = 0;
345         struct http_get_options http_options;
346         enum protocol_version version = get_protocol_version_config();
347
348         if (last && !strcmp(service, last->service))
349                 return last;
350         free_discovery(last);
351
352         strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
353         if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
354              git_env_bool("GIT_SMART_HTTP", 1)) {
355                 maybe_smart = 1;
356                 if (!strchr(url.buf, '?'))
357                         strbuf_addch(&refs_url, '?');
358                 else
359                         strbuf_addch(&refs_url, '&');
360                 strbuf_addf(&refs_url, "service=%s", service);
361         }
362
363         /*
364          * NEEDSWORK: If we are trying to use protocol v2 and we are planning
365          * to perform a push, then fallback to v0 since the client doesn't know
366          * how to push yet using v2.
367          */
368         if (version == protocol_v2 && !strcmp("git-receive-pack", service))
369                 version = protocol_v0;
370
371         /* Add the extra Git-Protocol header */
372         if (get_protocol_http_header(version, &protocol_header))
373                 string_list_append(&extra_headers, protocol_header.buf);
374
375         memset(&http_options, 0, sizeof(http_options));
376         http_options.content_type = &type;
377         http_options.charset = &charset;
378         http_options.effective_url = &effective_url;
379         http_options.base_url = &url;
380         http_options.extra_headers = &extra_headers;
381         http_options.initial_request = 1;
382         http_options.no_cache = 1;
383         http_options.keep_error = 1;
384
385         http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
386         switch (http_ret) {
387         case HTTP_OK:
388                 break;
389         case HTTP_MISSING_TARGET:
390                 show_http_message(&type, &charset, &buffer);
391                 die("repository '%s' not found", url.buf);
392         case HTTP_NOAUTH:
393                 show_http_message(&type, &charset, &buffer);
394                 die("Authentication failed for '%s'", url.buf);
395         default:
396                 show_http_message(&type, &charset, &buffer);
397                 die("unable to access '%s': %s", url.buf, curl_errorstr);
398         }
399
400         if (options.verbosity && !starts_with(refs_url.buf, url.buf))
401                 warning(_("redirecting to %s"), url.buf);
402
403         last= xcalloc(1, sizeof(*last_discovery));
404         last->service = xstrdup(service);
405         last->buf_alloc = strbuf_detach(&buffer, &last->len);
406         last->buf = last->buf_alloc;
407
408         strbuf_addf(&exp, "application/x-%s-advertisement", service);
409         if (maybe_smart &&
410             (5 <= last->len && last->buf[4] == '#') &&
411             !strbuf_cmp(&exp, &type)) {
412                 char *line;
413
414                 /*
415                  * smart HTTP response; validate that the service
416                  * pkt-line matches our request.
417                  */
418                 line = packet_read_line_buf(&last->buf, &last->len, NULL);
419                 if (!line)
420                         die("invalid server response; expected service, got flush packet");
421
422                 strbuf_reset(&exp);
423                 strbuf_addf(&exp, "# service=%s", service);
424                 if (strcmp(line, exp.buf))
425                         die("invalid server response; got '%s'", line);
426                 strbuf_release(&exp);
427
428                 /* The header can include additional metadata lines, up
429                  * until a packet flush marker.  Ignore these now, but
430                  * in the future we might start to scan them.
431                  */
432                 while (packet_read_line_buf(&last->buf, &last->len, NULL))
433                         ;
434
435                 last->proto_git = 1;
436         } else if (maybe_smart &&
437                    last->len > 5 && starts_with(last->buf + 4, "version 2")) {
438                 last->proto_git = 1;
439         }
440
441         if (last->proto_git)
442                 last->refs = parse_git_refs(last, for_push);
443         else
444                 last->refs = parse_info_refs(last);
445
446         strbuf_release(&refs_url);
447         strbuf_release(&exp);
448         strbuf_release(&type);
449         strbuf_release(&charset);
450         strbuf_release(&effective_url);
451         strbuf_release(&buffer);
452         strbuf_release(&protocol_header);
453         string_list_clear(&extra_headers, 0);
454         last_discovery = last;
455         return last;
456 }
457
458 static struct ref *get_refs(int for_push)
459 {
460         struct discovery *heads;
461
462         if (for_push)
463                 heads = discover_refs("git-receive-pack", for_push);
464         else
465                 heads = discover_refs("git-upload-pack", for_push);
466
467         return heads->refs;
468 }
469
470 static void output_refs(struct ref *refs)
471 {
472         struct ref *posn;
473         for (posn = refs; posn; posn = posn->next) {
474                 if (posn->symref)
475                         printf("@%s %s\n", posn->symref, posn->name);
476                 else
477                         printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
478         }
479         printf("\n");
480         fflush(stdout);
481 }
482
483 struct rpc_state {
484         const char *service_name;
485         const char **argv;
486         struct strbuf *stdin_preamble;
487         char *service_url;
488         char *hdr_content_type;
489         char *hdr_accept;
490         char *protocol_header;
491         char *buf;
492         size_t alloc;
493         size_t len;
494         size_t pos;
495         int in;
496         int out;
497         int any_written;
498         struct strbuf result;
499         unsigned gzip_request : 1;
500         unsigned initial_buffer : 1;
501 };
502
503 static size_t rpc_out(void *ptr, size_t eltsize,
504                 size_t nmemb, void *buffer_)
505 {
506         size_t max = eltsize * nmemb;
507         struct rpc_state *rpc = buffer_;
508         size_t avail = rpc->len - rpc->pos;
509
510         if (!avail) {
511                 rpc->initial_buffer = 0;
512                 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
513                 if (!avail)
514                         return 0;
515                 rpc->pos = 0;
516                 rpc->len = avail;
517         }
518
519         if (max < avail)
520                 avail = max;
521         memcpy(ptr, rpc->buf + rpc->pos, avail);
522         rpc->pos += avail;
523         return avail;
524 }
525
526 #ifndef NO_CURL_IOCTL
527 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
528 {
529         struct rpc_state *rpc = clientp;
530
531         switch (cmd) {
532         case CURLIOCMD_NOP:
533                 return CURLIOE_OK;
534
535         case CURLIOCMD_RESTARTREAD:
536                 if (rpc->initial_buffer) {
537                         rpc->pos = 0;
538                         return CURLIOE_OK;
539                 }
540                 error("unable to rewind rpc post data - try increasing http.postBuffer");
541                 return CURLIOE_FAILRESTART;
542
543         default:
544                 return CURLIOE_UNKNOWNCMD;
545         }
546 }
547 #endif
548
549 static size_t rpc_in(char *ptr, size_t eltsize,
550                 size_t nmemb, void *buffer_)
551 {
552         size_t size = eltsize * nmemb;
553         struct rpc_state *rpc = buffer_;
554         if (size)
555                 rpc->any_written = 1;
556         write_or_die(rpc->in, ptr, size);
557         return size;
558 }
559
560 static int run_slot(struct active_request_slot *slot,
561                     struct slot_results *results)
562 {
563         int err;
564         struct slot_results results_buf;
565
566         if (!results)
567                 results = &results_buf;
568
569         err = run_one_slot(slot, results);
570
571         if (err != HTTP_OK && err != HTTP_REAUTH) {
572                 struct strbuf msg = STRBUF_INIT;
573                 if (results->http_code && results->http_code != 200)
574                         strbuf_addf(&msg, "HTTP %ld", results->http_code);
575                 if (results->curl_result != CURLE_OK) {
576                         if (msg.len)
577                                 strbuf_addch(&msg, ' ');
578                         strbuf_addf(&msg, "curl %d", results->curl_result);
579                         if (curl_errorstr[0]) {
580                                 strbuf_addch(&msg, ' ');
581                                 strbuf_addstr(&msg, curl_errorstr);
582                         }
583                 }
584                 error("RPC failed; %s", msg.buf);
585                 strbuf_release(&msg);
586         }
587
588         return err;
589 }
590
591 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
592 {
593         struct active_request_slot *slot;
594         struct curl_slist *headers = http_copy_default_headers();
595         struct strbuf buf = STRBUF_INIT;
596         int err;
597
598         slot = get_active_slot();
599
600         headers = curl_slist_append(headers, rpc->hdr_content_type);
601         headers = curl_slist_append(headers, rpc->hdr_accept);
602
603         curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
604         curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
605         curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
606         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
607         curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
608         curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
609         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
610         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
611         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
612
613         err = run_slot(slot, results);
614
615         curl_slist_free_all(headers);
616         strbuf_release(&buf);
617         return err;
618 }
619
620 static curl_off_t xcurl_off_t(ssize_t len) {
621         if (len > maximum_signed_value_of_type(curl_off_t))
622                 die("cannot handle pushes this big");
623         return (curl_off_t) len;
624 }
625
626 static int post_rpc(struct rpc_state *rpc)
627 {
628         struct active_request_slot *slot;
629         struct curl_slist *headers = http_copy_default_headers();
630         int use_gzip = rpc->gzip_request;
631         char *gzip_body = NULL;
632         size_t gzip_size = 0;
633         int err, large_request = 0;
634         int needs_100_continue = 0;
635
636         /* Try to load the entire request, if we can fit it into the
637          * allocated buffer space we can use HTTP/1.0 and avoid the
638          * chunked encoding mess.
639          */
640         while (1) {
641                 size_t left = rpc->alloc - rpc->len;
642                 char *buf = rpc->buf + rpc->len;
643                 int n;
644
645                 if (left < LARGE_PACKET_MAX) {
646                         large_request = 1;
647                         use_gzip = 0;
648                         break;
649                 }
650
651                 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
652                 if (!n)
653                         break;
654                 rpc->len += n;
655         }
656
657         if (large_request) {
658                 struct slot_results results;
659
660                 do {
661                         err = probe_rpc(rpc, &results);
662                         if (err == HTTP_REAUTH)
663                                 credential_fill(&http_auth);
664                 } while (err == HTTP_REAUTH);
665                 if (err != HTTP_OK)
666                         return -1;
667
668                 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
669                         needs_100_continue = 1;
670         }
671
672         headers = curl_slist_append(headers, rpc->hdr_content_type);
673         headers = curl_slist_append(headers, rpc->hdr_accept);
674         headers = curl_slist_append(headers, needs_100_continue ?
675                 "Expect: 100-continue" : "Expect:");
676
677         /* Add the extra Git-Protocol header */
678         if (rpc->protocol_header)
679                 headers = curl_slist_append(headers, rpc->protocol_header);
680
681 retry:
682         slot = get_active_slot();
683
684         curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
685         curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
686         curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
687         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
688
689         if (large_request) {
690                 /* The request body is large and the size cannot be predicted.
691                  * We must use chunked encoding to send it.
692                  */
693                 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
694                 rpc->initial_buffer = 1;
695                 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
696                 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
697 #ifndef NO_CURL_IOCTL
698                 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
699                 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
700 #endif
701                 if (options.verbosity > 1) {
702                         fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
703                         fflush(stderr);
704                 }
705
706         } else if (gzip_body) {
707                 /*
708                  * If we are looping to retry authentication, then the previous
709                  * run will have set up the headers and gzip buffer already,
710                  * and we just need to send it.
711                  */
712                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
713                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
714
715         } else if (use_gzip && 1024 < rpc->len) {
716                 /* The client backend isn't giving us compressed data so
717                  * we can try to deflate it ourselves, this may save on.
718                  * the transfer time.
719                  */
720                 git_zstream stream;
721                 int ret;
722
723                 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
724                 gzip_size = git_deflate_bound(&stream, rpc->len);
725                 gzip_body = xmalloc(gzip_size);
726
727                 stream.next_in = (unsigned char *)rpc->buf;
728                 stream.avail_in = rpc->len;
729                 stream.next_out = (unsigned char *)gzip_body;
730                 stream.avail_out = gzip_size;
731
732                 ret = git_deflate(&stream, Z_FINISH);
733                 if (ret != Z_STREAM_END)
734                         die("cannot deflate request; zlib deflate error %d", ret);
735
736                 ret = git_deflate_end_gently(&stream);
737                 if (ret != Z_OK)
738                         die("cannot deflate request; zlib end error %d", ret);
739
740                 gzip_size = stream.total_out;
741
742                 headers = curl_slist_append(headers, "Content-Encoding: gzip");
743                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
744                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
745
746                 if (options.verbosity > 1) {
747                         fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
748                                 rpc->service_name,
749                                 (unsigned long)rpc->len, (unsigned long)gzip_size);
750                         fflush(stderr);
751                 }
752         } else {
753                 /* We know the complete request size in advance, use the
754                  * more normal Content-Length approach.
755                  */
756                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
757                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
758                 if (options.verbosity > 1) {
759                         fprintf(stderr, "POST %s (%lu bytes)\n",
760                                 rpc->service_name, (unsigned long)rpc->len);
761                         fflush(stderr);
762                 }
763         }
764
765         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
766         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
767         curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
768
769
770         rpc->any_written = 0;
771         err = run_slot(slot, NULL);
772         if (err == HTTP_REAUTH && !large_request) {
773                 credential_fill(&http_auth);
774                 goto retry;
775         }
776         if (err != HTTP_OK)
777                 err = -1;
778
779         if (!rpc->any_written)
780                 err = -1;
781
782         curl_slist_free_all(headers);
783         free(gzip_body);
784         return err;
785 }
786
787 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
788 {
789         const char *svc = rpc->service_name;
790         struct strbuf buf = STRBUF_INIT;
791         struct strbuf *preamble = rpc->stdin_preamble;
792         struct child_process client = CHILD_PROCESS_INIT;
793         int err = 0;
794
795         client.in = -1;
796         client.out = -1;
797         client.git_cmd = 1;
798         client.argv = rpc->argv;
799         if (start_command(&client))
800                 exit(1);
801         if (preamble)
802                 write_or_die(client.in, preamble->buf, preamble->len);
803         if (heads)
804                 write_or_die(client.in, heads->buf, heads->len);
805
806         rpc->alloc = http_post_buffer;
807         rpc->buf = xmalloc(rpc->alloc);
808         rpc->in = client.in;
809         rpc->out = client.out;
810         strbuf_init(&rpc->result, 0);
811
812         strbuf_addf(&buf, "%s%s", url.buf, svc);
813         rpc->service_url = strbuf_detach(&buf, NULL);
814
815         strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
816         rpc->hdr_content_type = strbuf_detach(&buf, NULL);
817
818         strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
819         rpc->hdr_accept = strbuf_detach(&buf, NULL);
820
821         if (get_protocol_http_header(heads->version, &buf))
822                 rpc->protocol_header = strbuf_detach(&buf, NULL);
823         else
824                 rpc->protocol_header = NULL;
825
826         while (!err) {
827                 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
828                 if (!n)
829                         break;
830                 rpc->pos = 0;
831                 rpc->len = n;
832                 err |= post_rpc(rpc);
833         }
834
835         close(client.in);
836         client.in = -1;
837         if (!err) {
838                 strbuf_read(&rpc->result, client.out, 0);
839         } else {
840                 char buf[4096];
841                 for (;;)
842                         if (xread(client.out, buf, sizeof(buf)) <= 0)
843                                 break;
844         }
845
846         close(client.out);
847         client.out = -1;
848
849         err |= finish_command(&client);
850         free(rpc->service_url);
851         free(rpc->hdr_content_type);
852         free(rpc->hdr_accept);
853         free(rpc->protocol_header);
854         free(rpc->buf);
855         strbuf_release(&buf);
856         return err;
857 }
858
859 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
860 {
861         struct walker *walker;
862         char **targets;
863         int ret, i;
864
865         ALLOC_ARRAY(targets, nr_heads);
866         if (options.depth || options.deepen_since)
867                 die("dumb http transport does not support shallow capabilities");
868         for (i = 0; i < nr_heads; i++)
869                 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
870
871         walker = get_http_walker(url.buf);
872         walker->get_all = 1;
873         walker->get_tree = 1;
874         walker->get_history = 1;
875         walker->get_verbosely = options.verbosity >= 3;
876         walker->get_recover = 0;
877         ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
878         walker_free(walker);
879
880         for (i = 0; i < nr_heads; i++)
881                 free(targets[i]);
882         free(targets);
883
884         return ret ? error("fetch failed.") : 0;
885 }
886
887 static int fetch_git(struct discovery *heads,
888         int nr_heads, struct ref **to_fetch)
889 {
890         struct rpc_state rpc;
891         struct strbuf preamble = STRBUF_INIT;
892         int i, err;
893         struct argv_array args = ARGV_ARRAY_INIT;
894
895         argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
896                          "--stdin", "--lock-pack", NULL);
897         if (options.followtags)
898                 argv_array_push(&args, "--include-tag");
899         if (options.thin)
900                 argv_array_push(&args, "--thin");
901         if (options.verbosity >= 3)
902                 argv_array_pushl(&args, "-v", "-v", NULL);
903         if (options.check_self_contained_and_connected)
904                 argv_array_push(&args, "--check-self-contained-and-connected");
905         if (options.cloning)
906                 argv_array_push(&args, "--cloning");
907         if (options.update_shallow)
908                 argv_array_push(&args, "--update-shallow");
909         if (!options.progress)
910                 argv_array_push(&args, "--no-progress");
911         if (options.depth)
912                 argv_array_pushf(&args, "--depth=%lu", options.depth);
913         if (options.deepen_since)
914                 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
915         for (i = 0; i < options.deepen_not.nr; i++)
916                 argv_array_pushf(&args, "--shallow-exclude=%s",
917                                  options.deepen_not.items[i].string);
918         if (options.deepen_relative && options.depth)
919                 argv_array_push(&args, "--deepen-relative");
920         if (options.from_promisor)
921                 argv_array_push(&args, "--from-promisor");
922         if (options.no_dependents)
923                 argv_array_push(&args, "--no-dependents");
924         if (options.filter)
925                 argv_array_pushf(&args, "--filter=%s", options.filter);
926         argv_array_push(&args, url.buf);
927
928         for (i = 0; i < nr_heads; i++) {
929                 struct ref *ref = to_fetch[i];
930                 if (!*ref->name)
931                         die("cannot fetch by sha1 over smart http");
932                 packet_buf_write(&preamble, "%s %s\n",
933                                  oid_to_hex(&ref->old_oid), ref->name);
934         }
935         packet_buf_flush(&preamble);
936
937         memset(&rpc, 0, sizeof(rpc));
938         rpc.service_name = "git-upload-pack",
939         rpc.argv = args.argv;
940         rpc.stdin_preamble = &preamble;
941         rpc.gzip_request = 1;
942
943         err = rpc_service(&rpc, heads);
944         if (rpc.result.len)
945                 write_or_die(1, rpc.result.buf, rpc.result.len);
946         strbuf_release(&rpc.result);
947         strbuf_release(&preamble);
948         argv_array_clear(&args);
949         return err;
950 }
951
952 static int fetch(int nr_heads, struct ref **to_fetch)
953 {
954         struct discovery *d = discover_refs("git-upload-pack", 0);
955         if (d->proto_git)
956                 return fetch_git(d, nr_heads, to_fetch);
957         else
958                 return fetch_dumb(nr_heads, to_fetch);
959 }
960
961 static void parse_fetch(struct strbuf *buf)
962 {
963         struct ref **to_fetch = NULL;
964         struct ref *list_head = NULL;
965         struct ref **list = &list_head;
966         int alloc_heads = 0, nr_heads = 0;
967
968         do {
969                 const char *p;
970                 if (skip_prefix(buf->buf, "fetch ", &p)) {
971                         const char *name;
972                         struct ref *ref;
973                         struct object_id old_oid;
974
975                         if (get_oid_hex(p, &old_oid))
976                                 die("protocol error: expected sha/ref, got %s'", p);
977                         if (p[GIT_SHA1_HEXSZ] == ' ')
978                                 name = p + GIT_SHA1_HEXSZ + 1;
979                         else if (!p[GIT_SHA1_HEXSZ])
980                                 name = "";
981                         else
982                                 die("protocol error: expected sha/ref, got %s'", p);
983
984                         ref = alloc_ref(name);
985                         oidcpy(&ref->old_oid, &old_oid);
986
987                         *list = ref;
988                         list = &ref->next;
989
990                         ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
991                         to_fetch[nr_heads++] = ref;
992                 }
993                 else
994                         die("http transport does not support %s", buf->buf);
995
996                 strbuf_reset(buf);
997                 if (strbuf_getline_lf(buf, stdin) == EOF)
998                         return;
999                 if (!*buf->buf)
1000                         break;
1001         } while (1);
1002
1003         if (fetch(nr_heads, to_fetch))
1004                 exit(128); /* error already reported */
1005         free_refs(list_head);
1006         free(to_fetch);
1007
1008         printf("\n");
1009         fflush(stdout);
1010         strbuf_reset(buf);
1011 }
1012
1013 static int push_dav(int nr_spec, char **specs)
1014 {
1015         struct child_process child = CHILD_PROCESS_INIT;
1016         size_t i;
1017
1018         child.git_cmd = 1;
1019         argv_array_push(&child.args, "http-push");
1020         argv_array_push(&child.args, "--helper-status");
1021         if (options.dry_run)
1022                 argv_array_push(&child.args, "--dry-run");
1023         if (options.verbosity > 1)
1024                 argv_array_push(&child.args, "--verbose");
1025         argv_array_push(&child.args, url.buf);
1026         for (i = 0; i < nr_spec; i++)
1027                 argv_array_push(&child.args, specs[i]);
1028
1029         if (run_command(&child))
1030                 die("git-http-push failed");
1031         return 0;
1032 }
1033
1034 static int push_git(struct discovery *heads, int nr_spec, char **specs)
1035 {
1036         struct rpc_state rpc;
1037         int i, err;
1038         struct argv_array args;
1039         struct string_list_item *cas_option;
1040         struct strbuf preamble = STRBUF_INIT;
1041
1042         argv_array_init(&args);
1043         argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1044                          NULL);
1045
1046         if (options.thin)
1047                 argv_array_push(&args, "--thin");
1048         if (options.dry_run)
1049                 argv_array_push(&args, "--dry-run");
1050         if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1051                 argv_array_push(&args, "--signed=yes");
1052         else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1053                 argv_array_push(&args, "--signed=if-asked");
1054         if (options.verbosity == 0)
1055                 argv_array_push(&args, "--quiet");
1056         else if (options.verbosity > 1)
1057                 argv_array_push(&args, "--verbose");
1058         for (i = 0; i < options.push_options.nr; i++)
1059                 argv_array_pushf(&args, "--push-option=%s",
1060                                  options.push_options.items[i].string);
1061         argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1062         for_each_string_list_item(cas_option, &cas_options)
1063                 argv_array_push(&args, cas_option->string);
1064         argv_array_push(&args, url.buf);
1065
1066         argv_array_push(&args, "--stdin");
1067         for (i = 0; i < nr_spec; i++)
1068                 packet_buf_write(&preamble, "%s\n", specs[i]);
1069         packet_buf_flush(&preamble);
1070
1071         memset(&rpc, 0, sizeof(rpc));
1072         rpc.service_name = "git-receive-pack",
1073         rpc.argv = args.argv;
1074         rpc.stdin_preamble = &preamble;
1075
1076         err = rpc_service(&rpc, heads);
1077         if (rpc.result.len)
1078                 write_or_die(1, rpc.result.buf, rpc.result.len);
1079         strbuf_release(&rpc.result);
1080         strbuf_release(&preamble);
1081         argv_array_clear(&args);
1082         return err;
1083 }
1084
1085 static int push(int nr_spec, char **specs)
1086 {
1087         struct discovery *heads = discover_refs("git-receive-pack", 1);
1088         int ret;
1089
1090         if (heads->proto_git)
1091                 ret = push_git(heads, nr_spec, specs);
1092         else
1093                 ret = push_dav(nr_spec, specs);
1094         free_discovery(heads);
1095         return ret;
1096 }
1097
1098 static void parse_push(struct strbuf *buf)
1099 {
1100         char **specs = NULL;
1101         int alloc_spec = 0, nr_spec = 0, i, ret;
1102
1103         do {
1104                 if (starts_with(buf->buf, "push ")) {
1105                         ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1106                         specs[nr_spec++] = xstrdup(buf->buf + 5);
1107                 }
1108                 else
1109                         die("http transport does not support %s", buf->buf);
1110
1111                 strbuf_reset(buf);
1112                 if (strbuf_getline_lf(buf, stdin) == EOF)
1113                         goto free_specs;
1114                 if (!*buf->buf)
1115                         break;
1116         } while (1);
1117
1118         ret = push(nr_spec, specs);
1119         printf("\n");
1120         fflush(stdout);
1121
1122         if (ret)
1123                 exit(128); /* error already reported */
1124
1125  free_specs:
1126         for (i = 0; i < nr_spec; i++)
1127                 free(specs[i]);
1128         free(specs);
1129 }
1130
1131 /*
1132  * Used to represent the state of a connection to an HTTP server when
1133  * communicating using git's wire-protocol version 2.
1134  */
1135 struct proxy_state {
1136         char *service_name;
1137         char *service_url;
1138         struct curl_slist *headers;
1139         struct strbuf request_buffer;
1140         int in;
1141         int out;
1142         struct packet_reader reader;
1143         size_t pos;
1144         int seen_flush;
1145 };
1146
1147 static void proxy_state_init(struct proxy_state *p, const char *service_name,
1148                              enum protocol_version version)
1149 {
1150         struct strbuf buf = STRBUF_INIT;
1151
1152         memset(p, 0, sizeof(*p));
1153         p->service_name = xstrdup(service_name);
1154
1155         p->in = 0;
1156         p->out = 1;
1157         strbuf_init(&p->request_buffer, 0);
1158
1159         strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1160         p->service_url = strbuf_detach(&buf, NULL);
1161
1162         p->headers = http_copy_default_headers();
1163
1164         strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1165         p->headers = curl_slist_append(p->headers, buf.buf);
1166         strbuf_reset(&buf);
1167
1168         strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1169         p->headers = curl_slist_append(p->headers, buf.buf);
1170         strbuf_reset(&buf);
1171
1172         p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1173
1174         /* Add the Git-Protocol header */
1175         if (get_protocol_http_header(version, &buf))
1176                 p->headers = curl_slist_append(p->headers, buf.buf);
1177
1178         packet_reader_init(&p->reader, p->in, NULL, 0,
1179                            PACKET_READ_GENTLE_ON_EOF);
1180
1181         strbuf_release(&buf);
1182 }
1183
1184 static void proxy_state_clear(struct proxy_state *p)
1185 {
1186         free(p->service_name);
1187         free(p->service_url);
1188         curl_slist_free_all(p->headers);
1189         strbuf_release(&p->request_buffer);
1190 }
1191
1192 /*
1193  * CURLOPT_READFUNCTION callback function.
1194  * Attempts to copy over a single packet-line at a time into the
1195  * curl provided buffer.
1196  */
1197 static size_t proxy_in(char *buffer, size_t eltsize,
1198                        size_t nmemb, void *userdata)
1199 {
1200         size_t max;
1201         struct proxy_state *p = userdata;
1202         size_t avail = p->request_buffer.len - p->pos;
1203
1204
1205         if (eltsize != 1)
1206                 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1207                     (uintmax_t)eltsize);
1208         max = nmemb;
1209
1210         if (!avail) {
1211                 if (p->seen_flush) {
1212                         p->seen_flush = 0;
1213                         return 0;
1214                 }
1215
1216                 strbuf_reset(&p->request_buffer);
1217                 switch (packet_reader_read(&p->reader)) {
1218                 case PACKET_READ_EOF:
1219                         die("unexpected EOF when reading from parent process");
1220                 case PACKET_READ_NORMAL:
1221                         packet_buf_write_len(&p->request_buffer, p->reader.line,
1222                                              p->reader.pktlen);
1223                         break;
1224                 case PACKET_READ_DELIM:
1225                         packet_buf_delim(&p->request_buffer);
1226                         break;
1227                 case PACKET_READ_FLUSH:
1228                         packet_buf_flush(&p->request_buffer);
1229                         p->seen_flush = 1;
1230                         break;
1231                 }
1232                 p->pos = 0;
1233                 avail = p->request_buffer.len;
1234         }
1235
1236         if (max < avail)
1237                 avail = max;
1238         memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1239         p->pos += avail;
1240         return avail;
1241 }
1242
1243 static size_t proxy_out(char *buffer, size_t eltsize,
1244                         size_t nmemb, void *userdata)
1245 {
1246         size_t size;
1247         struct proxy_state *p = userdata;
1248
1249         if (eltsize != 1)
1250                 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1251                     (uintmax_t)eltsize);
1252         size = nmemb;
1253
1254         write_or_die(p->out, buffer, size);
1255         return size;
1256 }
1257
1258 /* Issues a request to the HTTP server configured in `p` */
1259 static int proxy_request(struct proxy_state *p)
1260 {
1261         struct active_request_slot *slot;
1262
1263         slot = get_active_slot();
1264
1265         curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1266         curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1267         curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1268         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1269
1270         /* Setup function to read request from client */
1271         curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1272         curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1273
1274         /* Setup function to write server response to client */
1275         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1276         curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1277
1278         if (run_slot(slot, NULL) != HTTP_OK)
1279                 return -1;
1280
1281         return 0;
1282 }
1283
1284 static int stateless_connect(const char *service_name)
1285 {
1286         struct discovery *discover;
1287         struct proxy_state p;
1288
1289         /*
1290          * Run the info/refs request and see if the server supports protocol
1291          * v2.  If and only if the server supports v2 can we successfully
1292          * establish a stateless connection, otherwise we need to tell the
1293          * client to fallback to using other transport helper functions to
1294          * complete their request.
1295          */
1296         discover = discover_refs(service_name, 0);
1297         if (discover->version != protocol_v2) {
1298                 printf("fallback\n");
1299                 fflush(stdout);
1300                 return -1;
1301         } else {
1302                 /* Stateless Connection established */
1303                 printf("\n");
1304                 fflush(stdout);
1305         }
1306
1307         proxy_state_init(&p, service_name, discover->version);
1308
1309         /*
1310          * Dump the capability listing that we got from the server earlier
1311          * during the info/refs request.
1312          */
1313         write_or_die(p.out, discover->buf, discover->len);
1314
1315         /* Peek the next packet line.  Until we see EOF keep sending POSTs */
1316         while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1317                 if (proxy_request(&p)) {
1318                         /* We would have an err here */
1319                         break;
1320                 }
1321         }
1322
1323         proxy_state_clear(&p);
1324         return 0;
1325 }
1326
1327 int cmd_main(int argc, const char **argv)
1328 {
1329         struct strbuf buf = STRBUF_INIT;
1330         int nongit;
1331
1332         setup_git_directory_gently(&nongit);
1333         if (argc < 2) {
1334                 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1335                 return 1;
1336         }
1337
1338         options.verbosity = 1;
1339         options.progress = !!isatty(2);
1340         options.thin = 1;
1341         string_list_init(&options.deepen_not, 1);
1342         string_list_init(&options.push_options, 1);
1343
1344         remote = remote_get(argv[1]);
1345
1346         if (argc > 2) {
1347                 end_url_with_slash(&url, argv[2]);
1348         } else {
1349                 end_url_with_slash(&url, remote->url[0]);
1350         }
1351
1352         http_init(remote, url.buf, 0);
1353
1354         do {
1355                 const char *arg;
1356
1357                 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1358                         if (ferror(stdin))
1359                                 error("remote-curl: error reading command stream from git");
1360                         return 1;
1361                 }
1362                 if (buf.len == 0)
1363                         break;
1364                 if (starts_with(buf.buf, "fetch ")) {
1365                         if (nongit)
1366                                 die("remote-curl: fetch attempted without a local repo");
1367                         parse_fetch(&buf);
1368
1369                 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1370                         int for_push = !!strstr(buf.buf + 4, "for-push");
1371                         output_refs(get_refs(for_push));
1372
1373                 } else if (starts_with(buf.buf, "push ")) {
1374                         parse_push(&buf);
1375
1376                 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1377                         char *value = strchr(arg, ' ');
1378                         int result;
1379
1380                         if (value)
1381                                 *value++ = '\0';
1382                         else
1383                                 value = "true";
1384
1385                         result = set_option(arg, value);
1386                         if (!result)
1387                                 printf("ok\n");
1388                         else if (result < 0)
1389                                 printf("error invalid value\n");
1390                         else
1391                                 printf("unsupported\n");
1392                         fflush(stdout);
1393
1394                 } else if (!strcmp(buf.buf, "capabilities")) {
1395                         printf("stateless-connect\n");
1396                         printf("fetch\n");
1397                         printf("option\n");
1398                         printf("push\n");
1399                         printf("check-connectivity\n");
1400                         printf("\n");
1401                         fflush(stdout);
1402                 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1403                         if (!stateless_connect(arg))
1404                                 break;
1405                 } else {
1406                         error("remote-curl: unknown command '%s' from git", buf.buf);
1407                         return 1;
1408                 }
1409                 strbuf_reset(&buf);
1410         } while (1);
1411
1412         http_cleanup();
1413
1414         return 0;
1415 }