9 #include "run-command.h"
11 #include "string-list.h"
13 #include "argv-array.h"
14 #include "credential.h"
15 #include "sha1-array.h"
16 #include "send-pack.h"
20 static struct remote
*remote
;
21 /* always ends with a trailing slash */
22 static struct strbuf url
= STRBUF_INIT
;
28 struct string_list deepen_not
;
29 struct string_list push_options
;
31 unsigned progress
: 1,
32 check_self_contained_and_connected
: 1,
38 /* One of the SEND_PACK_PUSH_CERT_* constants. */
44 static struct options options
;
45 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
47 static int set_option(const char *name
, const char *value
)
49 if (!strcmp(name
, "verbosity")) {
51 int v
= strtol(value
, &end
, 10);
52 if (value
== end
|| *end
)
54 options
.verbosity
= v
;
57 else if (!strcmp(name
, "progress")) {
58 if (!strcmp(value
, "true"))
60 else if (!strcmp(value
, "false"))
66 else if (!strcmp(name
, "depth")) {
68 unsigned long v
= strtoul(value
, &end
, 10);
69 if (value
== end
|| *end
)
74 else if (!strcmp(name
, "deepen-since")) {
75 options
.deepen_since
= xstrdup(value
);
78 else if (!strcmp(name
, "deepen-not")) {
79 string_list_append(&options
.deepen_not
, value
);
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;
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;
100 else if (!strcmp(name
, "dry-run")) {
101 if (!strcmp(value
, "true"))
103 else if (!strcmp(value
, "false"))
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;
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
);
124 } else if (!strcmp(name
, "cloning")) {
125 if (!strcmp(value
, "true"))
127 else if (!strcmp(value
, "false"))
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;
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
;
150 } else if (!strcmp(name
, "push-option")) {
152 string_list_append(&options
.push_options
, value
);
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
));
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
;
173 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
174 } else if (!strcmp(name
, "from-promisor")) {
175 options
.from_promisor
= 1;
177 } else if (!strcmp(name
, "no-dependents")) {
178 options
.no_dependents
= 1;
180 } else if (!strcmp(name
, "filter")) {
181 options
.filter
= xstrdup(value
);
184 return 1 /* unsupported */;
194 struct oid_array shallow
;
195 enum protocol_version version
;
196 unsigned proto_git
: 1;
198 static struct discovery
*last_discovery
;
200 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
202 struct ref
*list
= NULL
;
203 struct packet_reader reader
;
205 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
206 PACKET_READ_CHOMP_NEWLINE
|
207 PACKET_READ_GENTLE_ON_EOF
|
208 PACKET_READ_DIE_ON_ERR_PACKET
);
210 heads
->version
= discover_version(&reader
);
211 switch (heads
->version
) {
214 * Do nothing. This isn't a list of refs but rather a
215 * capability advertisement. Client would have run
216 * 'stateless-connect' so we'll dump this capability listing
217 * and let them request the refs themselves.
222 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
223 NULL
, &heads
->shallow
);
225 case protocol_unknown_version
:
226 BUG("unknown protocol version");
232 static struct ref
*parse_info_refs(struct discovery
*heads
)
234 char *data
, *start
, *mid
;
238 struct ref
*refs
= NULL
;
239 struct ref
*ref
= NULL
;
240 struct ref
*last_ref
= NULL
;
245 while (i
< heads
->len
) {
251 if (data
[i
] == '\n') {
252 if (mid
- start
!= 40)
253 die("%sinfo/refs not valid: is this a git repository?",
257 ref
= alloc_ref(ref_name
);
258 get_oid_hex(start
, &ref
->old_oid
);
262 last_ref
->next
= ref
;
269 ref
= alloc_ref("HEAD");
270 if (!http_fetch_ref(url
.buf
, ref
) &&
271 !resolve_remote_symref(ref
, refs
)) {
281 static void free_discovery(struct discovery
*d
)
284 if (d
== last_discovery
)
285 last_discovery
= NULL
;
286 free(d
->shallow
.oid
);
294 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
300 * We only show text/plain parts, as other types are likely
301 * to be ugly to look at on the user's terminal.
303 if (strcmp(type
->buf
, "text/plain"))
306 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
314 eol
= strchrnul(p
, '\n');
315 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
321 static int get_protocol_http_header(enum protocol_version version
,
322 struct strbuf
*header
)
325 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
334 static struct discovery
*discover_refs(const char *service
, int for_push
)
336 struct strbuf exp
= STRBUF_INIT
;
337 struct strbuf type
= STRBUF_INIT
;
338 struct strbuf charset
= STRBUF_INIT
;
339 struct strbuf buffer
= STRBUF_INIT
;
340 struct strbuf refs_url
= STRBUF_INIT
;
341 struct strbuf effective_url
= STRBUF_INIT
;
342 struct strbuf protocol_header
= STRBUF_INIT
;
343 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
344 struct discovery
*last
= last_discovery
;
345 int http_ret
, maybe_smart
= 0;
346 struct http_get_options http_options
;
347 enum protocol_version version
= get_protocol_version_config();
349 if (last
&& !strcmp(service
, last
->service
))
351 free_discovery(last
);
353 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
354 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
355 git_env_bool("GIT_SMART_HTTP", 1)) {
357 if (!strchr(url
.buf
, '?'))
358 strbuf_addch(&refs_url
, '?');
360 strbuf_addch(&refs_url
, '&');
361 strbuf_addf(&refs_url
, "service=%s", service
);
365 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
366 * to perform a push, then fallback to v0 since the client doesn't know
367 * how to push yet using v2.
369 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
370 version
= protocol_v0
;
372 /* Add the extra Git-Protocol header */
373 if (get_protocol_http_header(version
, &protocol_header
))
374 string_list_append(&extra_headers
, protocol_header
.buf
);
376 memset(&http_options
, 0, sizeof(http_options
));
377 http_options
.content_type
= &type
;
378 http_options
.charset
= &charset
;
379 http_options
.effective_url
= &effective_url
;
380 http_options
.base_url
= &url
;
381 http_options
.extra_headers
= &extra_headers
;
382 http_options
.initial_request
= 1;
383 http_options
.no_cache
= 1;
385 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
389 case HTTP_MISSING_TARGET
:
390 show_http_message(&type
, &charset
, &buffer
);
391 die("repository '%s' not found", url
.buf
);
393 show_http_message(&type
, &charset
, &buffer
);
394 die("Authentication failed for '%s'", url
.buf
);
396 show_http_message(&type
, &charset
, &buffer
);
397 die("unable to access '%s': %s", url
.buf
, curl_errorstr
);
400 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
))
401 warning(_("redirecting to %s"), url
.buf
);
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
;
408 strbuf_addf(&exp
, "application/x-%s-advertisement", service
);
410 (5 <= last
->len
&& last
->buf
[4] == '#') &&
411 !strbuf_cmp(&exp
, &type
)) {
412 struct packet_reader reader
;
413 packet_reader_init(&reader
, -1, last
->buf
, last
->len
,
414 PACKET_READ_CHOMP_NEWLINE
|
415 PACKET_READ_DIE_ON_ERR_PACKET
);
418 * smart HTTP response; validate that the service
419 * pkt-line matches our request.
421 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
422 die("invalid server response; expected service, got flush packet");
425 strbuf_addf(&exp
, "# service=%s", service
);
426 if (strcmp(reader
.line
, exp
.buf
))
427 die("invalid server response; got '%s'", reader
.line
);
428 strbuf_release(&exp
);
430 /* The header can include additional metadata lines, up
431 * until a packet flush marker. Ignore these now, but
432 * in the future we might start to scan them.
435 packet_reader_read(&reader
);
436 if (reader
.pktlen
<= 0) {
441 last
->buf
= reader
.src_buffer
;
442 last
->len
= reader
.src_len
;
445 } else if (maybe_smart
&&
446 last
->len
> 5 && starts_with(last
->buf
+ 4, "version 2")) {
451 last
->refs
= parse_git_refs(last
, for_push
);
453 last
->refs
= parse_info_refs(last
);
455 strbuf_release(&refs_url
);
456 strbuf_release(&exp
);
457 strbuf_release(&type
);
458 strbuf_release(&charset
);
459 strbuf_release(&effective_url
);
460 strbuf_release(&buffer
);
461 strbuf_release(&protocol_header
);
462 string_list_clear(&extra_headers
, 0);
463 last_discovery
= last
;
467 static struct ref
*get_refs(int for_push
)
469 struct discovery
*heads
;
472 heads
= discover_refs("git-receive-pack", for_push
);
474 heads
= discover_refs("git-upload-pack", for_push
);
479 static void output_refs(struct ref
*refs
)
482 for (posn
= refs
; posn
; posn
= posn
->next
) {
484 printf("@%s %s\n", posn
->symref
, posn
->name
);
486 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
493 const char *service_name
;
495 struct strbuf
*stdin_preamble
;
497 char *hdr_content_type
;
499 char *protocol_header
;
507 struct strbuf result
;
508 unsigned gzip_request
: 1;
509 unsigned initial_buffer
: 1;
512 static size_t rpc_out(void *ptr
, size_t eltsize
,
513 size_t nmemb
, void *buffer_
)
515 size_t max
= eltsize
* nmemb
;
516 struct rpc_state
*rpc
= buffer_
;
517 size_t avail
= rpc
->len
- rpc
->pos
;
520 rpc
->initial_buffer
= 0;
521 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
530 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
535 #ifndef NO_CURL_IOCTL
536 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
538 struct rpc_state
*rpc
= clientp
;
544 case CURLIOCMD_RESTARTREAD
:
545 if (rpc
->initial_buffer
) {
549 error("unable to rewind rpc post data - try increasing http.postBuffer");
550 return CURLIOE_FAILRESTART
;
553 return CURLIOE_UNKNOWNCMD
;
559 struct rpc_state
*rpc
;
560 struct active_request_slot
*slot
;
564 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
567 static size_t rpc_in(char *ptr
, size_t eltsize
,
568 size_t nmemb
, void *buffer_
)
570 size_t size
= eltsize
* nmemb
;
571 struct rpc_in_data
*data
= buffer_
;
574 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
575 &response_code
) != CURLE_OK
)
577 if (response_code
>= 300)
580 data
->rpc
->any_written
= 1;
581 write_or_die(data
->rpc
->in
, ptr
, size
);
585 static int run_slot(struct active_request_slot
*slot
,
586 struct slot_results
*results
)
589 struct slot_results results_buf
;
592 results
= &results_buf
;
594 err
= run_one_slot(slot
, results
);
596 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
597 struct strbuf msg
= STRBUF_INIT
;
598 if (results
->http_code
&& results
->http_code
!= 200)
599 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
600 if (results
->curl_result
!= CURLE_OK
) {
602 strbuf_addch(&msg
, ' ');
603 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
604 if (curl_errorstr
[0]) {
605 strbuf_addch(&msg
, ' ');
606 strbuf_addstr(&msg
, curl_errorstr
);
609 error("RPC failed; %s", msg
.buf
);
610 strbuf_release(&msg
);
616 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
618 struct active_request_slot
*slot
;
619 struct curl_slist
*headers
= http_copy_default_headers();
620 struct strbuf buf
= STRBUF_INIT
;
623 slot
= get_active_slot();
625 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
626 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
628 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
629 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
630 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
631 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
632 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
633 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
634 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
635 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
636 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
638 err
= run_slot(slot
, results
);
640 curl_slist_free_all(headers
);
641 strbuf_release(&buf
);
645 static curl_off_t
xcurl_off_t(size_t len
)
647 uintmax_t size
= len
;
648 if (size
> maximum_signed_value_of_type(curl_off_t
))
649 die("cannot handle pushes this big");
650 return (curl_off_t
)size
;
653 static int post_rpc(struct rpc_state
*rpc
)
655 struct active_request_slot
*slot
;
656 struct curl_slist
*headers
= http_copy_default_headers();
657 int use_gzip
= rpc
->gzip_request
;
658 char *gzip_body
= NULL
;
659 size_t gzip_size
= 0;
660 int err
, large_request
= 0;
661 int needs_100_continue
= 0;
662 struct rpc_in_data rpc_in_data
;
664 /* Try to load the entire request, if we can fit it into the
665 * allocated buffer space we can use HTTP/1.0 and avoid the
666 * chunked encoding mess.
669 size_t left
= rpc
->alloc
- rpc
->len
;
670 char *buf
= rpc
->buf
+ rpc
->len
;
673 if (left
< LARGE_PACKET_MAX
) {
679 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
686 struct slot_results results
;
689 err
= probe_rpc(rpc
, &results
);
690 if (err
== HTTP_REAUTH
)
691 credential_fill(&http_auth
);
692 } while (err
== HTTP_REAUTH
);
696 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
697 needs_100_continue
= 1;
700 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
701 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
702 headers
= curl_slist_append(headers
, needs_100_continue
?
703 "Expect: 100-continue" : "Expect:");
705 /* Add the extra Git-Protocol header */
706 if (rpc
->protocol_header
)
707 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
710 slot
= get_active_slot();
712 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
713 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
714 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
715 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
718 /* The request body is large and the size cannot be predicted.
719 * We must use chunked encoding to send it.
721 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
722 rpc
->initial_buffer
= 1;
723 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
724 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
725 #ifndef NO_CURL_IOCTL
726 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
727 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
729 if (options
.verbosity
> 1) {
730 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
734 } else if (gzip_body
) {
736 * If we are looping to retry authentication, then the previous
737 * run will have set up the headers and gzip buffer already,
738 * and we just need to send it.
740 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
741 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
743 } else if (use_gzip
&& 1024 < rpc
->len
) {
744 /* The client backend isn't giving us compressed data so
745 * we can try to deflate it ourselves, this may save on
751 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
752 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
753 gzip_body
= xmalloc(gzip_size
);
755 stream
.next_in
= (unsigned char *)rpc
->buf
;
756 stream
.avail_in
= rpc
->len
;
757 stream
.next_out
= (unsigned char *)gzip_body
;
758 stream
.avail_out
= gzip_size
;
760 ret
= git_deflate(&stream
, Z_FINISH
);
761 if (ret
!= Z_STREAM_END
)
762 die("cannot deflate request; zlib deflate error %d", ret
);
764 ret
= git_deflate_end_gently(&stream
);
766 die("cannot deflate request; zlib end error %d", ret
);
768 gzip_size
= stream
.total_out
;
770 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
771 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
772 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
774 if (options
.verbosity
> 1) {
775 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
777 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
781 /* We know the complete request size in advance, use the
782 * more normal Content-Length approach.
784 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
785 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
786 if (options
.verbosity
> 1) {
787 fprintf(stderr
, "POST %s (%lu bytes)\n",
788 rpc
->service_name
, (unsigned long)rpc
->len
);
793 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
794 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
795 rpc_in_data
.rpc
= rpc
;
796 rpc_in_data
.slot
= slot
;
797 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
798 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
801 rpc
->any_written
= 0;
802 err
= run_slot(slot
, NULL
);
803 if (err
== HTTP_REAUTH
&& !large_request
) {
804 credential_fill(&http_auth
);
810 if (!rpc
->any_written
)
813 curl_slist_free_all(headers
);
818 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
820 const char *svc
= rpc
->service_name
;
821 struct strbuf buf
= STRBUF_INIT
;
822 struct strbuf
*preamble
= rpc
->stdin_preamble
;
823 struct child_process client
= CHILD_PROCESS_INIT
;
829 client
.argv
= rpc
->argv
;
830 if (start_command(&client
))
833 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
835 write_or_die(client
.in
, heads
->buf
, heads
->len
);
837 rpc
->alloc
= http_post_buffer
;
838 rpc
->buf
= xmalloc(rpc
->alloc
);
840 rpc
->out
= client
.out
;
841 strbuf_init(&rpc
->result
, 0);
843 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
844 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
846 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
847 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
849 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
850 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
852 if (get_protocol_http_header(heads
->version
, &buf
))
853 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
855 rpc
->protocol_header
= NULL
;
858 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
863 err
|= post_rpc(rpc
);
869 strbuf_read(&rpc
->result
, client
.out
, 0);
873 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
880 err
|= finish_command(&client
);
881 free(rpc
->service_url
);
882 free(rpc
->hdr_content_type
);
883 free(rpc
->hdr_accept
);
884 free(rpc
->protocol_header
);
886 strbuf_release(&buf
);
890 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
892 struct walker
*walker
;
896 ALLOC_ARRAY(targets
, nr_heads
);
897 if (options
.depth
|| options
.deepen_since
)
898 die("dumb http transport does not support shallow capabilities");
899 for (i
= 0; i
< nr_heads
; i
++)
900 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
902 walker
= get_http_walker(url
.buf
);
903 walker
->get_verbosely
= options
.verbosity
>= 3;
904 walker
->get_recover
= 0;
905 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
908 for (i
= 0; i
< nr_heads
; i
++)
912 return ret
? error("fetch failed.") : 0;
915 static int fetch_git(struct discovery
*heads
,
916 int nr_heads
, struct ref
**to_fetch
)
918 struct rpc_state rpc
;
919 struct strbuf preamble
= STRBUF_INIT
;
921 struct argv_array args
= ARGV_ARRAY_INIT
;
923 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
924 "--stdin", "--lock-pack", NULL
);
925 if (options
.followtags
)
926 argv_array_push(&args
, "--include-tag");
928 argv_array_push(&args
, "--thin");
929 if (options
.verbosity
>= 3)
930 argv_array_pushl(&args
, "-v", "-v", NULL
);
931 if (options
.check_self_contained_and_connected
)
932 argv_array_push(&args
, "--check-self-contained-and-connected");
934 argv_array_push(&args
, "--cloning");
935 if (options
.update_shallow
)
936 argv_array_push(&args
, "--update-shallow");
937 if (!options
.progress
)
938 argv_array_push(&args
, "--no-progress");
940 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
941 if (options
.deepen_since
)
942 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
943 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
944 argv_array_pushf(&args
, "--shallow-exclude=%s",
945 options
.deepen_not
.items
[i
].string
);
946 if (options
.deepen_relative
&& options
.depth
)
947 argv_array_push(&args
, "--deepen-relative");
948 if (options
.from_promisor
)
949 argv_array_push(&args
, "--from-promisor");
950 if (options
.no_dependents
)
951 argv_array_push(&args
, "--no-dependents");
953 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
954 argv_array_push(&args
, url
.buf
);
956 for (i
= 0; i
< nr_heads
; i
++) {
957 struct ref
*ref
= to_fetch
[i
];
959 die("cannot fetch by sha1 over smart http");
960 packet_buf_write(&preamble
, "%s %s\n",
961 oid_to_hex(&ref
->old_oid
), ref
->name
);
963 packet_buf_flush(&preamble
);
965 memset(&rpc
, 0, sizeof(rpc
));
966 rpc
.service_name
= "git-upload-pack",
967 rpc
.argv
= args
.argv
;
968 rpc
.stdin_preamble
= &preamble
;
969 rpc
.gzip_request
= 1;
971 err
= rpc_service(&rpc
, heads
);
973 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
974 strbuf_release(&rpc
.result
);
975 strbuf_release(&preamble
);
976 argv_array_clear(&args
);
980 static int fetch(int nr_heads
, struct ref
**to_fetch
)
982 struct discovery
*d
= discover_refs("git-upload-pack", 0);
984 return fetch_git(d
, nr_heads
, to_fetch
);
986 return fetch_dumb(nr_heads
, to_fetch
);
989 static void parse_fetch(struct strbuf
*buf
)
991 struct ref
**to_fetch
= NULL
;
992 struct ref
*list_head
= NULL
;
993 struct ref
**list
= &list_head
;
994 int alloc_heads
= 0, nr_heads
= 0;
998 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1001 struct object_id old_oid
;
1003 if (get_oid_hex(p
, &old_oid
))
1004 die("protocol error: expected sha/ref, got %s'", p
);
1005 if (p
[GIT_SHA1_HEXSZ
] == ' ')
1006 name
= p
+ GIT_SHA1_HEXSZ
+ 1;
1007 else if (!p
[GIT_SHA1_HEXSZ
])
1010 die("protocol error: expected sha/ref, got %s'", p
);
1012 ref
= alloc_ref(name
);
1013 oidcpy(&ref
->old_oid
, &old_oid
);
1018 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1019 to_fetch
[nr_heads
++] = ref
;
1022 die("http transport does not support %s", buf
->buf
);
1025 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1031 if (fetch(nr_heads
, to_fetch
))
1032 exit(128); /* error already reported */
1033 free_refs(list_head
);
1041 static int push_dav(int nr_spec
, char **specs
)
1043 struct child_process child
= CHILD_PROCESS_INIT
;
1047 argv_array_push(&child
.args
, "http-push");
1048 argv_array_push(&child
.args
, "--helper-status");
1049 if (options
.dry_run
)
1050 argv_array_push(&child
.args
, "--dry-run");
1051 if (options
.verbosity
> 1)
1052 argv_array_push(&child
.args
, "--verbose");
1053 argv_array_push(&child
.args
, url
.buf
);
1054 for (i
= 0; i
< nr_spec
; i
++)
1055 argv_array_push(&child
.args
, specs
[i
]);
1057 if (run_command(&child
))
1058 die("git-http-push failed");
1062 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
1064 struct rpc_state rpc
;
1066 struct argv_array args
;
1067 struct string_list_item
*cas_option
;
1068 struct strbuf preamble
= STRBUF_INIT
;
1070 argv_array_init(&args
);
1071 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1075 argv_array_push(&args
, "--thin");
1076 if (options
.dry_run
)
1077 argv_array_push(&args
, "--dry-run");
1078 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1079 argv_array_push(&args
, "--signed=yes");
1080 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1081 argv_array_push(&args
, "--signed=if-asked");
1082 if (options
.verbosity
== 0)
1083 argv_array_push(&args
, "--quiet");
1084 else if (options
.verbosity
> 1)
1085 argv_array_push(&args
, "--verbose");
1086 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1087 argv_array_pushf(&args
, "--push-option=%s",
1088 options
.push_options
.items
[i
].string
);
1089 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
1090 for_each_string_list_item(cas_option
, &cas_options
)
1091 argv_array_push(&args
, cas_option
->string
);
1092 argv_array_push(&args
, url
.buf
);
1094 argv_array_push(&args
, "--stdin");
1095 for (i
= 0; i
< nr_spec
; i
++)
1096 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1097 packet_buf_flush(&preamble
);
1099 memset(&rpc
, 0, sizeof(rpc
));
1100 rpc
.service_name
= "git-receive-pack",
1101 rpc
.argv
= args
.argv
;
1102 rpc
.stdin_preamble
= &preamble
;
1104 err
= rpc_service(&rpc
, heads
);
1106 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
1107 strbuf_release(&rpc
.result
);
1108 strbuf_release(&preamble
);
1109 argv_array_clear(&args
);
1113 static int push(int nr_spec
, char **specs
)
1115 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1118 if (heads
->proto_git
)
1119 ret
= push_git(heads
, nr_spec
, specs
);
1121 ret
= push_dav(nr_spec
, specs
);
1122 free_discovery(heads
);
1126 static void parse_push(struct strbuf
*buf
)
1128 char **specs
= NULL
;
1129 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
1132 if (starts_with(buf
->buf
, "push ")) {
1133 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
1134 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
1137 die("http transport does not support %s", buf
->buf
);
1140 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1146 ret
= push(nr_spec
, specs
);
1151 exit(128); /* error already reported */
1154 for (i
= 0; i
< nr_spec
; i
++)
1160 * Used to represent the state of a connection to an HTTP server when
1161 * communicating using git's wire-protocol version 2.
1163 struct proxy_state
{
1166 struct curl_slist
*headers
;
1167 struct strbuf request_buffer
;
1170 struct packet_reader reader
;
1175 static void proxy_state_init(struct proxy_state
*p
, const char *service_name
,
1176 enum protocol_version version
)
1178 struct strbuf buf
= STRBUF_INIT
;
1180 memset(p
, 0, sizeof(*p
));
1181 p
->service_name
= xstrdup(service_name
);
1185 strbuf_init(&p
->request_buffer
, 0);
1187 strbuf_addf(&buf
, "%s%s", url
.buf
, p
->service_name
);
1188 p
->service_url
= strbuf_detach(&buf
, NULL
);
1190 p
->headers
= http_copy_default_headers();
1192 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", p
->service_name
);
1193 p
->headers
= curl_slist_append(p
->headers
, buf
.buf
);
1196 strbuf_addf(&buf
, "Accept: application/x-%s-result", p
->service_name
);
1197 p
->headers
= curl_slist_append(p
->headers
, buf
.buf
);
1200 p
->headers
= curl_slist_append(p
->headers
, "Transfer-Encoding: chunked");
1202 /* Add the Git-Protocol header */
1203 if (get_protocol_http_header(version
, &buf
))
1204 p
->headers
= curl_slist_append(p
->headers
, buf
.buf
);
1206 packet_reader_init(&p
->reader
, p
->in
, NULL
, 0,
1207 PACKET_READ_GENTLE_ON_EOF
|
1208 PACKET_READ_DIE_ON_ERR_PACKET
);
1210 strbuf_release(&buf
);
1213 static void proxy_state_clear(struct proxy_state
*p
)
1215 free(p
->service_name
);
1216 free(p
->service_url
);
1217 curl_slist_free_all(p
->headers
);
1218 strbuf_release(&p
->request_buffer
);
1222 * CURLOPT_READFUNCTION callback function.
1223 * Attempts to copy over a single packet-line at a time into the
1224 * curl provided buffer.
1226 static size_t proxy_in(char *buffer
, size_t eltsize
,
1227 size_t nmemb
, void *userdata
)
1230 struct proxy_state
*p
= userdata
;
1231 size_t avail
= p
->request_buffer
.len
- p
->pos
;
1235 BUG("curl read callback called with size = %"PRIuMAX
" != 1",
1236 (uintmax_t)eltsize
);
1240 if (p
->seen_flush
) {
1245 strbuf_reset(&p
->request_buffer
);
1246 switch (packet_reader_read(&p
->reader
)) {
1247 case PACKET_READ_EOF
:
1248 die("unexpected EOF when reading from parent process");
1249 case PACKET_READ_NORMAL
:
1250 packet_buf_write_len(&p
->request_buffer
, p
->reader
.line
,
1253 case PACKET_READ_DELIM
:
1254 packet_buf_delim(&p
->request_buffer
);
1256 case PACKET_READ_FLUSH
:
1257 packet_buf_flush(&p
->request_buffer
);
1262 avail
= p
->request_buffer
.len
;
1267 memcpy(buffer
, p
->request_buffer
.buf
+ p
->pos
, avail
);
1272 static size_t proxy_out(char *buffer
, size_t eltsize
,
1273 size_t nmemb
, void *userdata
)
1276 struct proxy_state
*p
= userdata
;
1279 BUG("curl read callback called with size = %"PRIuMAX
" != 1",
1280 (uintmax_t)eltsize
);
1283 write_or_die(p
->out
, buffer
, size
);
1287 /* Issues a request to the HTTP server configured in `p` */
1288 static int proxy_request(struct proxy_state
*p
)
1290 struct active_request_slot
*slot
;
1292 slot
= get_active_slot();
1294 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
1295 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1296 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
1297 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, p
->service_url
);
1298 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, p
->headers
);
1300 /* Setup function to read request from client */
1301 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, proxy_in
);
1302 curl_easy_setopt(slot
->curl
, CURLOPT_READDATA
, p
);
1304 /* Setup function to write server response to client */
1305 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, proxy_out
);
1306 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, p
);
1308 if (run_slot(slot
, NULL
) != HTTP_OK
)
1314 static int stateless_connect(const char *service_name
)
1316 struct discovery
*discover
;
1317 struct proxy_state p
;
1320 * Run the info/refs request and see if the server supports protocol
1321 * v2. If and only if the server supports v2 can we successfully
1322 * establish a stateless connection, otherwise we need to tell the
1323 * client to fallback to using other transport helper functions to
1324 * complete their request.
1326 discover
= discover_refs(service_name
, 0);
1327 if (discover
->version
!= protocol_v2
) {
1328 printf("fallback\n");
1332 /* Stateless Connection established */
1337 proxy_state_init(&p
, service_name
, discover
->version
);
1340 * Dump the capability listing that we got from the server earlier
1341 * during the info/refs request.
1343 write_or_die(p
.out
, discover
->buf
, discover
->len
);
1345 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1346 while (packet_reader_peek(&p
.reader
) != PACKET_READ_EOF
) {
1347 if (proxy_request(&p
)) {
1348 /* We would have an err here */
1353 proxy_state_clear(&p
);
1357 int cmd_main(int argc
, const char **argv
)
1359 struct strbuf buf
= STRBUF_INIT
;
1362 setup_git_directory_gently(&nongit
);
1364 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1368 options
.verbosity
= 1;
1369 options
.progress
= !!isatty(2);
1371 string_list_init(&options
.deepen_not
, 1);
1372 string_list_init(&options
.push_options
, 1);
1374 remote
= remote_get(argv
[1]);
1377 end_url_with_slash(&url
, argv
[2]);
1379 end_url_with_slash(&url
, remote
->url
[0]);
1382 http_init(remote
, url
.buf
, 0);
1387 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1389 error("remote-curl: error reading command stream from git");
1394 if (starts_with(buf
.buf
, "fetch ")) {
1396 die("remote-curl: fetch attempted without a local repo");
1399 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1400 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1401 output_refs(get_refs(for_push
));
1403 } else if (starts_with(buf
.buf
, "push ")) {
1406 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1407 char *value
= strchr(arg
, ' ');
1415 result
= set_option(arg
, value
);
1418 else if (result
< 0)
1419 printf("error invalid value\n");
1421 printf("unsupported\n");
1424 } else if (!strcmp(buf
.buf
, "capabilities")) {
1425 printf("stateless-connect\n");
1429 printf("check-connectivity\n");
1432 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1433 if (!stateless_connect(arg
))
1436 error("remote-curl: unknown command '%s' from git", buf
.buf
);