7 #include "run-command.h"
11 static struct remote
*remote
;
12 static const char *url
; /* always ends with a trailing slash */
17 unsigned progress
: 1,
22 static struct options options
;
24 static int set_option(const char *name
, const char *value
)
26 if (!strcmp(name
, "verbosity")) {
28 int v
= strtol(value
, &end
, 10);
29 if (value
== end
|| *end
)
31 options
.verbosity
= v
;
34 else if (!strcmp(name
, "progress")) {
35 if (!strcmp(value
, "true"))
37 else if (!strcmp(value
, "false"))
43 else if (!strcmp(name
, "depth")) {
45 unsigned long v
= strtoul(value
, &end
, 10);
46 if (value
== end
|| *end
)
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;
60 else if (!strcmp(name
, "dry-run")) {
61 if (!strcmp(value
, "true"))
63 else if (!strcmp(value
, "false"))
70 return 1 /* unsupported */;
79 unsigned proto_git
: 1;
81 static struct discovery
*last_discovery
;
83 static void free_discovery(struct discovery
*d
)
86 if (d
== last_discovery
)
87 last_discovery
= NULL
;
93 static struct discovery
* discover_refs(const char *service
)
95 struct strbuf exp
= STRBUF_INIT
;
96 struct strbuf type
= STRBUF_INIT
;
97 struct strbuf buffer
= STRBUF_INIT
;
98 struct discovery
*last
= last_discovery
;
100 int http_ret
, maybe_smart
= 0;
102 if (last
&& !strcmp(service
, last
->service
))
104 free_discovery(last
);
106 strbuf_addf(&buffer
, "%sinfo/refs", url
);
107 if ((!prefixcmp(url
, "http://") || !prefixcmp(url
, "https://")) &&
108 git_env_bool("GIT_SMART_HTTP", 1)) {
110 if (!strchr(url
, '?'))
111 strbuf_addch(&buffer
, '?');
113 strbuf_addch(&buffer
, '&');
114 strbuf_addf(&buffer
, "service=%s", service
);
116 refs_url
= strbuf_detach(&buffer
, NULL
);
118 http_ret
= http_get_strbuf(refs_url
, &type
, &buffer
, HTTP_NO_CACHE
);
122 case HTTP_MISSING_TARGET
:
123 die("%s not found: did you run git update-server-info on the"
124 " server?", refs_url
);
126 die("Authentication failed");
128 http_error(refs_url
, http_ret
);
129 die("HTTP request failed");
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
;
137 strbuf_addf(&exp
, "application/x-%s-advertisement", service
);
139 (5 <= last
->len
&& last
->buf
[4] == '#') &&
140 !strbuf_cmp(&exp
, &type
)) {
144 * smart HTTP response; validate that the service
145 * pkt-line matches our request.
147 line
= packet_read_line_buf(&last
->buf
, &last
->len
, NULL
);
150 strbuf_addf(&exp
, "# service=%s", service
);
151 if (strcmp(line
, exp
.buf
))
152 die("invalid server response; got '%s'", line
);
153 strbuf_release(&exp
);
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.
159 while (packet_read_line_buf(&last
->buf
, &last
->len
, NULL
))
166 strbuf_release(&exp
);
167 strbuf_release(&type
);
168 strbuf_release(&buffer
);
169 last_discovery
= last
;
173 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
175 struct ref
*list
= NULL
;
176 get_remote_heads(-1, heads
->buf
, heads
->len
, &list
,
177 for_push
? REF_NORMAL
: 0, NULL
);
181 static struct ref
*parse_info_refs(struct discovery
*heads
)
183 char *data
, *start
, *mid
;
187 struct ref
*refs
= NULL
;
188 struct ref
*ref
= NULL
;
189 struct ref
*last_ref
= NULL
;
194 while (i
< heads
->len
) {
200 if (data
[i
] == '\n') {
201 if (mid
- start
!= 40)
202 die("%sinfo/refs not valid: is this a git repository?", url
);
205 ref
= xmalloc(sizeof(struct ref
) +
206 strlen(ref_name
) + 1);
207 memset(ref
, 0, sizeof(struct ref
));
208 strcpy(ref
->name
, ref_name
);
209 get_sha1_hex(start
, ref
->old_sha1
);
213 last_ref
->next
= ref
;
220 ref
= alloc_ref("HEAD");
221 if (!http_fetch_ref(url
, ref
) &&
222 !resolve_remote_symref(ref
, refs
)) {
232 static struct ref
*get_refs(int for_push
)
234 struct discovery
*heads
;
237 heads
= discover_refs("git-receive-pack");
239 heads
= discover_refs("git-upload-pack");
241 if (heads
->proto_git
)
242 return parse_git_refs(heads
, for_push
);
243 return parse_info_refs(heads
);
246 static void output_refs(struct ref
*refs
)
249 for (posn
= refs
; posn
; posn
= posn
->next
) {
251 printf("@%s %s\n", posn
->symref
, posn
->name
);
253 printf("%s %s\n", sha1_to_hex(posn
->old_sha1
), posn
->name
);
261 const char *service_name
;
263 struct strbuf
*stdin_preamble
;
265 char *hdr_content_type
;
273 struct strbuf result
;
274 unsigned gzip_request
: 1;
275 unsigned initial_buffer
: 1;
278 static size_t rpc_out(void *ptr
, size_t eltsize
,
279 size_t nmemb
, void *buffer_
)
281 size_t max
= eltsize
* nmemb
;
282 struct rpc_state
*rpc
= buffer_
;
283 size_t avail
= rpc
->len
- rpc
->pos
;
286 rpc
->initial_buffer
= 0;
287 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
296 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
301 #ifndef NO_CURL_IOCTL
302 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
304 struct rpc_state
*rpc
= clientp
;
310 case CURLIOCMD_RESTARTREAD
:
311 if (rpc
->initial_buffer
) {
315 fprintf(stderr
, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
316 return CURLIOE_FAILRESTART
;
319 return CURLIOE_UNKNOWNCMD
;
324 static size_t rpc_in(char *ptr
, size_t eltsize
,
325 size_t nmemb
, void *buffer_
)
327 size_t size
= eltsize
* nmemb
;
328 struct rpc_state
*rpc
= buffer_
;
329 write_or_die(rpc
->in
, ptr
, size
);
333 static int run_slot(struct active_request_slot
*slot
)
336 struct slot_results results
;
338 slot
->results
= &results
;
339 slot
->curl_result
= curl_easy_perform(slot
->curl
);
340 finish_active_slot(slot
);
342 err
= handle_curl_result(&results
);
343 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
344 error("RPC failed; result=%d, HTTP code = %ld",
345 results
.curl_result
, results
.http_code
);
351 static int probe_rpc(struct rpc_state
*rpc
)
353 struct active_request_slot
*slot
;
354 struct curl_slist
*headers
= NULL
;
355 struct strbuf buf
= STRBUF_INIT
;
358 slot
= get_active_slot();
360 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
361 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
363 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
364 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
365 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
366 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
367 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
368 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
369 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
370 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
371 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
373 err
= run_slot(slot
);
375 curl_slist_free_all(headers
);
376 strbuf_release(&buf
);
380 static int post_rpc(struct rpc_state
*rpc
)
382 struct active_request_slot
*slot
;
383 struct curl_slist
*headers
= NULL
;
384 int use_gzip
= rpc
->gzip_request
;
385 char *gzip_body
= NULL
;
386 size_t gzip_size
= 0;
387 int err
, large_request
= 0;
389 /* Try to load the entire request, if we can fit it into the
390 * allocated buffer space we can use HTTP/1.0 and avoid the
391 * chunked encoding mess.
394 size_t left
= rpc
->alloc
- rpc
->len
;
395 char *buf
= rpc
->buf
+ rpc
->len
;
398 if (left
< LARGE_PACKET_MAX
) {
404 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
412 err
= probe_rpc(rpc
);
413 } while (err
== HTTP_REAUTH
);
418 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
419 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
420 headers
= curl_slist_append(headers
, "Expect:");
423 slot
= get_active_slot();
425 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
426 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
427 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
428 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
431 /* The request body is large and the size cannot be predicted.
432 * We must use chunked encoding to send it.
434 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
435 rpc
->initial_buffer
= 1;
436 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
437 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
438 #ifndef NO_CURL_IOCTL
439 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
440 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
442 if (options
.verbosity
> 1) {
443 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
447 } else if (gzip_body
) {
449 * If we are looping to retry authentication, then the previous
450 * run will have set up the headers and gzip buffer already,
451 * and we just need to send it.
453 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
454 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, gzip_size
);
456 } else if (use_gzip
&& 1024 < rpc
->len
) {
457 /* The client backend isn't giving us compressed data so
458 * we can try to deflate it ourselves, this may save on.
464 memset(&stream
, 0, sizeof(stream
));
465 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
466 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
467 gzip_body
= xmalloc(gzip_size
);
469 stream
.next_in
= (unsigned char *)rpc
->buf
;
470 stream
.avail_in
= rpc
->len
;
471 stream
.next_out
= (unsigned char *)gzip_body
;
472 stream
.avail_out
= gzip_size
;
474 ret
= git_deflate(&stream
, Z_FINISH
);
475 if (ret
!= Z_STREAM_END
)
476 die("cannot deflate request; zlib deflate error %d", ret
);
478 ret
= git_deflate_end_gently(&stream
);
480 die("cannot deflate request; zlib end error %d", ret
);
482 gzip_size
= stream
.total_out
;
484 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
485 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
486 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, gzip_size
);
488 if (options
.verbosity
> 1) {
489 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
491 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
495 /* We know the complete request size in advance, use the
496 * more normal Content-Length approach.
498 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
499 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, rpc
->len
);
500 if (options
.verbosity
> 1) {
501 fprintf(stderr
, "POST %s (%lu bytes)\n",
502 rpc
->service_name
, (unsigned long)rpc
->len
);
507 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
508 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
509 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
511 err
= run_slot(slot
);
512 if (err
== HTTP_REAUTH
&& !large_request
)
517 curl_slist_free_all(headers
);
522 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
524 const char *svc
= rpc
->service_name
;
525 struct strbuf buf
= STRBUF_INIT
;
526 struct strbuf
*preamble
= rpc
->stdin_preamble
;
527 struct child_process client
;
530 memset(&client
, 0, sizeof(client
));
534 client
.argv
= rpc
->argv
;
535 if (start_command(&client
))
538 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
540 write_or_die(client
.in
, heads
->buf
, heads
->len
);
542 rpc
->alloc
= http_post_buffer
;
543 rpc
->buf
= xmalloc(rpc
->alloc
);
545 rpc
->out
= client
.out
;
546 strbuf_init(&rpc
->result
, 0);
548 strbuf_addf(&buf
, "%s%s", url
, svc
);
549 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
551 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
552 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
554 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
555 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
558 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
563 err
|= post_rpc(rpc
);
569 strbuf_read(&rpc
->result
, client
.out
, 0);
573 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
580 err
|= finish_command(&client
);
581 free(rpc
->service_url
);
582 free(rpc
->hdr_content_type
);
583 free(rpc
->hdr_accept
);
585 strbuf_release(&buf
);
589 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
591 struct walker
*walker
;
592 char **targets
= xmalloc(nr_heads
* sizeof(char*));
596 die("dumb http transport does not support --depth");
597 for (i
= 0; i
< nr_heads
; i
++)
598 targets
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
600 walker
= get_http_walker(url
);
602 walker
->get_tree
= 1;
603 walker
->get_history
= 1;
604 walker
->get_verbosely
= options
.verbosity
>= 3;
605 walker
->get_recover
= 0;
606 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
609 for (i
= 0; i
< nr_heads
; i
++)
613 return ret
? error("Fetch failed.") : 0;
616 static int fetch_git(struct discovery
*heads
,
617 int nr_heads
, struct ref
**to_fetch
)
619 struct rpc_state rpc
;
620 struct strbuf preamble
= STRBUF_INIT
;
621 char *depth_arg
= NULL
;
622 int argc
= 0, i
, err
;
623 const char *argv
[15];
625 argv
[argc
++] = "fetch-pack";
626 argv
[argc
++] = "--stateless-rpc";
627 argv
[argc
++] = "--stdin";
628 argv
[argc
++] = "--lock-pack";
629 if (options
.followtags
)
630 argv
[argc
++] = "--include-tag";
632 argv
[argc
++] = "--thin";
633 if (options
.verbosity
>= 3) {
637 if (!options
.progress
)
638 argv
[argc
++] = "--no-progress";
640 struct strbuf buf
= STRBUF_INIT
;
641 strbuf_addf(&buf
, "--depth=%lu", options
.depth
);
642 depth_arg
= strbuf_detach(&buf
, NULL
);
643 argv
[argc
++] = depth_arg
;
648 for (i
= 0; i
< nr_heads
; i
++) {
649 struct ref
*ref
= to_fetch
[i
];
650 if (!ref
->name
|| !*ref
->name
)
651 die("cannot fetch by sha1 over smart http");
652 packet_buf_write(&preamble
, "%s\n", ref
->name
);
654 packet_buf_flush(&preamble
);
656 memset(&rpc
, 0, sizeof(rpc
));
657 rpc
.service_name
= "git-upload-pack",
659 rpc
.stdin_preamble
= &preamble
;
660 rpc
.gzip_request
= 1;
662 err
= rpc_service(&rpc
, heads
);
664 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
665 strbuf_release(&rpc
.result
);
666 strbuf_release(&preamble
);
671 static int fetch(int nr_heads
, struct ref
**to_fetch
)
673 struct discovery
*d
= discover_refs("git-upload-pack");
675 return fetch_git(d
, nr_heads
, to_fetch
);
677 return fetch_dumb(nr_heads
, to_fetch
);
680 static void parse_fetch(struct strbuf
*buf
)
682 struct ref
**to_fetch
= NULL
;
683 struct ref
*list_head
= NULL
;
684 struct ref
**list
= &list_head
;
685 int alloc_heads
= 0, nr_heads
= 0;
688 if (!prefixcmp(buf
->buf
, "fetch ")) {
689 char *p
= buf
->buf
+ strlen("fetch ");
692 unsigned char old_sha1
[20];
694 if (strlen(p
) < 40 || get_sha1_hex(p
, old_sha1
))
695 die("protocol error: expected sha/ref, got %s'", p
);
701 die("protocol error: expected sha/ref, got %s'", p
);
703 ref
= alloc_ref(name
);
704 hashcpy(ref
->old_sha1
, old_sha1
);
709 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
710 to_fetch
[nr_heads
++] = ref
;
713 die("http transport does not support %s", buf
->buf
);
716 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
722 if (fetch(nr_heads
, to_fetch
))
723 exit(128); /* error already reported */
724 free_refs(list_head
);
732 static int push_dav(int nr_spec
, char **specs
)
734 const char **argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
737 argv
[argc
++] = "http-push";
738 argv
[argc
++] = "--helper-status";
740 argv
[argc
++] = "--dry-run";
741 if (options
.verbosity
> 1)
742 argv
[argc
++] = "--verbose";
744 for (i
= 0; i
< nr_spec
; i
++)
745 argv
[argc
++] = specs
[i
];
748 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
749 die("git-%s failed", argv
[0]);
754 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
756 struct rpc_state rpc
;
758 int argc
= 0, i
, err
;
760 argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
761 argv
[argc
++] = "send-pack";
762 argv
[argc
++] = "--stateless-rpc";
763 argv
[argc
++] = "--helper-status";
765 argv
[argc
++] = "--thin";
767 argv
[argc
++] = "--dry-run";
768 if (options
.verbosity
== 0)
769 argv
[argc
++] = "--quiet";
770 else if (options
.verbosity
> 1)
771 argv
[argc
++] = "--verbose";
772 argv
[argc
++] = options
.progress
? "--progress" : "--no-progress";
774 for (i
= 0; i
< nr_spec
; i
++)
775 argv
[argc
++] = specs
[i
];
778 memset(&rpc
, 0, sizeof(rpc
));
779 rpc
.service_name
= "git-receive-pack",
782 err
= rpc_service(&rpc
, heads
);
784 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
785 strbuf_release(&rpc
.result
);
790 static int push(int nr_spec
, char **specs
)
792 struct discovery
*heads
= discover_refs("git-receive-pack");
795 if (heads
->proto_git
)
796 ret
= push_git(heads
, nr_spec
, specs
);
798 ret
= push_dav(nr_spec
, specs
);
799 free_discovery(heads
);
803 static void parse_push(struct strbuf
*buf
)
806 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
809 if (!prefixcmp(buf
->buf
, "push ")) {
810 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
811 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
814 die("http transport does not support %s", buf
->buf
);
817 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
823 ret
= push(nr_spec
, specs
);
828 exit(128); /* error already reported */
831 for (i
= 0; i
< nr_spec
; i
++)
836 int main(int argc
, const char **argv
)
838 struct strbuf buf
= STRBUF_INIT
;
841 git_extract_argv0_path(argv
[0]);
842 setup_git_directory_gently(&nongit
);
844 fprintf(stderr
, "Remote needed\n");
848 options
.verbosity
= 1;
849 options
.progress
= !!isatty(2);
852 remote
= remote_get(argv
[1]);
855 end_url_with_slash(&buf
, argv
[2]);
857 end_url_with_slash(&buf
, remote
->url
[0]);
860 url
= strbuf_detach(&buf
, NULL
);
862 http_init(remote
, url
, 0);
865 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
) {
867 fprintf(stderr
, "Error reading command stream\n");
869 fprintf(stderr
, "Unexpected end of command stream\n");
874 if (!prefixcmp(buf
.buf
, "fetch ")) {
876 die("Fetch attempted without a local repo");
879 } else if (!strcmp(buf
.buf
, "list") || !prefixcmp(buf
.buf
, "list ")) {
880 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
881 output_refs(get_refs(for_push
));
883 } else if (!prefixcmp(buf
.buf
, "push ")) {
886 } else if (!prefixcmp(buf
.buf
, "option ")) {
887 char *name
= buf
.buf
+ strlen("option ");
888 char *value
= strchr(name
, ' ');
896 result
= set_option(name
, value
);
900 printf("error invalid value\n");
902 printf("unsupported\n");
905 } else if (!strcmp(buf
.buf
, "capabilities")) {
912 fprintf(stderr
, "Unknown command '%s'\n", buf
.buf
);