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