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 void check_smart_http(struct discovery
*d
, const char *service
,
338 struct packet_reader reader
;
341 * If we don't see x-$service-advertisement, then it's not smart-http.
342 * But once we do, we commit to it and assume any other protocol
343 * violations are hard errors.
345 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
346 !skip_prefix(p
, service
, &p
) ||
347 strcmp(p
, "-advertisement"))
350 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
351 PACKET_READ_CHOMP_NEWLINE
|
352 PACKET_READ_DIE_ON_ERR_PACKET
);
353 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
354 die("invalid server response; expected service, got flush packet");
356 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
358 * The header can include additional metadata lines, up
359 * until a packet flush marker. Ignore these now, but
360 * in the future we might start to scan them.
363 packet_reader_read(&reader
);
364 if (reader
.pktlen
<= 0) {
370 * v0 smart http; callers expect us to soak up the
371 * service and header packets
373 d
->buf
= reader
.src_buffer
;
374 d
->len
= reader
.src_len
;
377 } else if (!strcmp(reader
.line
, "version 2")) {
379 * v2 smart http; do not consume version packet, which will
380 * be handled elsewhere.
385 die("invalid server response; got '%s'", reader
.line
);
389 static struct discovery
*discover_refs(const char *service
, int for_push
)
391 struct strbuf type
= STRBUF_INIT
;
392 struct strbuf charset
= STRBUF_INIT
;
393 struct strbuf buffer
= STRBUF_INIT
;
394 struct strbuf refs_url
= STRBUF_INIT
;
395 struct strbuf effective_url
= STRBUF_INIT
;
396 struct strbuf protocol_header
= STRBUF_INIT
;
397 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
398 struct discovery
*last
= last_discovery
;
399 int http_ret
, maybe_smart
= 0;
400 struct http_get_options http_options
;
401 enum protocol_version version
= get_protocol_version_config();
403 if (last
&& !strcmp(service
, last
->service
))
405 free_discovery(last
);
407 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
408 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
409 git_env_bool("GIT_SMART_HTTP", 1)) {
411 if (!strchr(url
.buf
, '?'))
412 strbuf_addch(&refs_url
, '?');
414 strbuf_addch(&refs_url
, '&');
415 strbuf_addf(&refs_url
, "service=%s", service
);
419 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
420 * to perform a push, then fallback to v0 since the client doesn't know
421 * how to push yet using v2.
423 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
424 version
= protocol_v0
;
426 /* Add the extra Git-Protocol header */
427 if (get_protocol_http_header(version
, &protocol_header
))
428 string_list_append(&extra_headers
, protocol_header
.buf
);
430 memset(&http_options
, 0, sizeof(http_options
));
431 http_options
.content_type
= &type
;
432 http_options
.charset
= &charset
;
433 http_options
.effective_url
= &effective_url
;
434 http_options
.base_url
= &url
;
435 http_options
.extra_headers
= &extra_headers
;
436 http_options
.initial_request
= 1;
437 http_options
.no_cache
= 1;
439 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
443 case HTTP_MISSING_TARGET
:
444 show_http_message(&type
, &charset
, &buffer
);
445 die("repository '%s' not found", url
.buf
);
447 show_http_message(&type
, &charset
, &buffer
);
448 die("Authentication failed for '%s'", url
.buf
);
450 show_http_message(&type
, &charset
, &buffer
);
451 die("unable to access '%s': %s", url
.buf
, curl_errorstr
);
454 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
))
455 warning(_("redirecting to %s"), url
.buf
);
457 last
= xcalloc(1, sizeof(*last_discovery
));
458 last
->service
= xstrdup(service
);
459 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
460 last
->buf
= last
->buf_alloc
;
463 check_smart_http(last
, service
, &type
);
466 last
->refs
= parse_git_refs(last
, for_push
);
468 last
->refs
= parse_info_refs(last
);
470 strbuf_release(&refs_url
);
471 strbuf_release(&type
);
472 strbuf_release(&charset
);
473 strbuf_release(&effective_url
);
474 strbuf_release(&buffer
);
475 strbuf_release(&protocol_header
);
476 string_list_clear(&extra_headers
, 0);
477 last_discovery
= last
;
481 static struct ref
*get_refs(int for_push
)
483 struct discovery
*heads
;
486 heads
= discover_refs("git-receive-pack", for_push
);
488 heads
= discover_refs("git-upload-pack", for_push
);
493 static void output_refs(struct ref
*refs
)
496 for (posn
= refs
; posn
; posn
= posn
->next
) {
498 printf("@%s %s\n", posn
->symref
, posn
->name
);
500 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
507 const char *service_name
;
509 struct strbuf
*stdin_preamble
;
511 char *hdr_content_type
;
513 char *protocol_header
;
521 struct strbuf result
;
522 unsigned gzip_request
: 1;
523 unsigned initial_buffer
: 1;
526 static size_t rpc_out(void *ptr
, size_t eltsize
,
527 size_t nmemb
, void *buffer_
)
529 size_t max
= eltsize
* nmemb
;
530 struct rpc_state
*rpc
= buffer_
;
531 size_t avail
= rpc
->len
- rpc
->pos
;
534 rpc
->initial_buffer
= 0;
535 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
544 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
549 #ifndef NO_CURL_IOCTL
550 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
552 struct rpc_state
*rpc
= clientp
;
558 case CURLIOCMD_RESTARTREAD
:
559 if (rpc
->initial_buffer
) {
563 error("unable to rewind rpc post data - try increasing http.postBuffer");
564 return CURLIOE_FAILRESTART
;
567 return CURLIOE_UNKNOWNCMD
;
573 struct rpc_state
*rpc
;
574 struct active_request_slot
*slot
;
578 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
581 static size_t rpc_in(char *ptr
, size_t eltsize
,
582 size_t nmemb
, void *buffer_
)
584 size_t size
= eltsize
* nmemb
;
585 struct rpc_in_data
*data
= buffer_
;
588 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
589 &response_code
) != CURLE_OK
)
591 if (response_code
>= 300)
594 data
->rpc
->any_written
= 1;
595 write_or_die(data
->rpc
->in
, ptr
, size
);
599 static int run_slot(struct active_request_slot
*slot
,
600 struct slot_results
*results
)
603 struct slot_results results_buf
;
606 results
= &results_buf
;
608 err
= run_one_slot(slot
, results
);
610 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
611 struct strbuf msg
= STRBUF_INIT
;
612 if (results
->http_code
&& results
->http_code
!= 200)
613 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
614 if (results
->curl_result
!= CURLE_OK
) {
616 strbuf_addch(&msg
, ' ');
617 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
618 if (curl_errorstr
[0]) {
619 strbuf_addch(&msg
, ' ');
620 strbuf_addstr(&msg
, curl_errorstr
);
623 error("RPC failed; %s", msg
.buf
);
624 strbuf_release(&msg
);
630 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
632 struct active_request_slot
*slot
;
633 struct curl_slist
*headers
= http_copy_default_headers();
634 struct strbuf buf
= STRBUF_INIT
;
637 slot
= get_active_slot();
639 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
640 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
642 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
643 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
644 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
645 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
646 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
647 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
648 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
649 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
650 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
652 err
= run_slot(slot
, results
);
654 curl_slist_free_all(headers
);
655 strbuf_release(&buf
);
659 static curl_off_t
xcurl_off_t(size_t len
)
661 uintmax_t size
= len
;
662 if (size
> maximum_signed_value_of_type(curl_off_t
))
663 die("cannot handle pushes this big");
664 return (curl_off_t
)size
;
667 static int post_rpc(struct rpc_state
*rpc
)
669 struct active_request_slot
*slot
;
670 struct curl_slist
*headers
= http_copy_default_headers();
671 int use_gzip
= rpc
->gzip_request
;
672 char *gzip_body
= NULL
;
673 size_t gzip_size
= 0;
674 int err
, large_request
= 0;
675 int needs_100_continue
= 0;
676 struct rpc_in_data rpc_in_data
;
678 /* Try to load the entire request, if we can fit it into the
679 * allocated buffer space we can use HTTP/1.0 and avoid the
680 * chunked encoding mess.
683 size_t left
= rpc
->alloc
- rpc
->len
;
684 char *buf
= rpc
->buf
+ rpc
->len
;
687 if (left
< LARGE_PACKET_MAX
) {
693 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
700 struct slot_results results
;
703 err
= probe_rpc(rpc
, &results
);
704 if (err
== HTTP_REAUTH
)
705 credential_fill(&http_auth
);
706 } while (err
== HTTP_REAUTH
);
710 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
711 needs_100_continue
= 1;
714 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
715 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
716 headers
= curl_slist_append(headers
, needs_100_continue
?
717 "Expect: 100-continue" : "Expect:");
719 /* Add the extra Git-Protocol header */
720 if (rpc
->protocol_header
)
721 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
724 slot
= get_active_slot();
726 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
727 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
728 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
729 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
732 /* The request body is large and the size cannot be predicted.
733 * We must use chunked encoding to send it.
735 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
736 rpc
->initial_buffer
= 1;
737 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
738 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
739 #ifndef NO_CURL_IOCTL
740 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
741 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
743 if (options
.verbosity
> 1) {
744 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
748 } else if (gzip_body
) {
750 * If we are looping to retry authentication, then the previous
751 * run will have set up the headers and gzip buffer already,
752 * and we just need to send it.
754 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
755 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
757 } else if (use_gzip
&& 1024 < rpc
->len
) {
758 /* The client backend isn't giving us compressed data so
759 * we can try to deflate it ourselves, this may save on
765 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
766 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
767 gzip_body
= xmalloc(gzip_size
);
769 stream
.next_in
= (unsigned char *)rpc
->buf
;
770 stream
.avail_in
= rpc
->len
;
771 stream
.next_out
= (unsigned char *)gzip_body
;
772 stream
.avail_out
= gzip_size
;
774 ret
= git_deflate(&stream
, Z_FINISH
);
775 if (ret
!= Z_STREAM_END
)
776 die("cannot deflate request; zlib deflate error %d", ret
);
778 ret
= git_deflate_end_gently(&stream
);
780 die("cannot deflate request; zlib end error %d", ret
);
782 gzip_size
= stream
.total_out
;
784 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
785 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
786 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
788 if (options
.verbosity
> 1) {
789 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
791 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
795 /* We know the complete request size in advance, use the
796 * more normal Content-Length approach.
798 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
799 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
800 if (options
.verbosity
> 1) {
801 fprintf(stderr
, "POST %s (%lu bytes)\n",
802 rpc
->service_name
, (unsigned long)rpc
->len
);
807 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
808 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
809 rpc_in_data
.rpc
= rpc
;
810 rpc_in_data
.slot
= slot
;
811 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
812 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
815 rpc
->any_written
= 0;
816 err
= run_slot(slot
, NULL
);
817 if (err
== HTTP_REAUTH
&& !large_request
) {
818 credential_fill(&http_auth
);
824 if (!rpc
->any_written
)
827 curl_slist_free_all(headers
);
832 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
834 const char *svc
= rpc
->service_name
;
835 struct strbuf buf
= STRBUF_INIT
;
836 struct strbuf
*preamble
= rpc
->stdin_preamble
;
837 struct child_process client
= CHILD_PROCESS_INIT
;
843 client
.argv
= rpc
->argv
;
844 if (start_command(&client
))
847 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
849 write_or_die(client
.in
, heads
->buf
, heads
->len
);
851 rpc
->alloc
= http_post_buffer
;
852 rpc
->buf
= xmalloc(rpc
->alloc
);
854 rpc
->out
= client
.out
;
855 strbuf_init(&rpc
->result
, 0);
857 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
858 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
860 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
861 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
863 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
864 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
866 if (get_protocol_http_header(heads
->version
, &buf
))
867 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
869 rpc
->protocol_header
= NULL
;
872 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
877 err
|= post_rpc(rpc
);
883 strbuf_read(&rpc
->result
, client
.out
, 0);
887 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
894 err
|= finish_command(&client
);
895 free(rpc
->service_url
);
896 free(rpc
->hdr_content_type
);
897 free(rpc
->hdr_accept
);
898 free(rpc
->protocol_header
);
900 strbuf_release(&buf
);
904 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
906 struct walker
*walker
;
910 ALLOC_ARRAY(targets
, nr_heads
);
911 if (options
.depth
|| options
.deepen_since
)
912 die("dumb http transport does not support shallow capabilities");
913 for (i
= 0; i
< nr_heads
; i
++)
914 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
916 walker
= get_http_walker(url
.buf
);
917 walker
->get_verbosely
= options
.verbosity
>= 3;
918 walker
->get_recover
= 0;
919 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
922 for (i
= 0; i
< nr_heads
; i
++)
926 return ret
? error("fetch failed.") : 0;
929 static int fetch_git(struct discovery
*heads
,
930 int nr_heads
, struct ref
**to_fetch
)
932 struct rpc_state rpc
;
933 struct strbuf preamble
= STRBUF_INIT
;
935 struct argv_array args
= ARGV_ARRAY_INIT
;
937 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
938 "--stdin", "--lock-pack", NULL
);
939 if (options
.followtags
)
940 argv_array_push(&args
, "--include-tag");
942 argv_array_push(&args
, "--thin");
943 if (options
.verbosity
>= 3)
944 argv_array_pushl(&args
, "-v", "-v", NULL
);
945 if (options
.check_self_contained_and_connected
)
946 argv_array_push(&args
, "--check-self-contained-and-connected");
948 argv_array_push(&args
, "--cloning");
949 if (options
.update_shallow
)
950 argv_array_push(&args
, "--update-shallow");
951 if (!options
.progress
)
952 argv_array_push(&args
, "--no-progress");
954 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
955 if (options
.deepen_since
)
956 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
957 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
958 argv_array_pushf(&args
, "--shallow-exclude=%s",
959 options
.deepen_not
.items
[i
].string
);
960 if (options
.deepen_relative
&& options
.depth
)
961 argv_array_push(&args
, "--deepen-relative");
962 if (options
.from_promisor
)
963 argv_array_push(&args
, "--from-promisor");
964 if (options
.no_dependents
)
965 argv_array_push(&args
, "--no-dependents");
967 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
968 argv_array_push(&args
, url
.buf
);
970 for (i
= 0; i
< nr_heads
; i
++) {
971 struct ref
*ref
= to_fetch
[i
];
973 die("cannot fetch by sha1 over smart http");
974 packet_buf_write(&preamble
, "%s %s\n",
975 oid_to_hex(&ref
->old_oid
), ref
->name
);
977 packet_buf_flush(&preamble
);
979 memset(&rpc
, 0, sizeof(rpc
));
980 rpc
.service_name
= "git-upload-pack",
981 rpc
.argv
= args
.argv
;
982 rpc
.stdin_preamble
= &preamble
;
983 rpc
.gzip_request
= 1;
985 err
= rpc_service(&rpc
, heads
);
987 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
988 strbuf_release(&rpc
.result
);
989 strbuf_release(&preamble
);
990 argv_array_clear(&args
);
994 static int fetch(int nr_heads
, struct ref
**to_fetch
)
996 struct discovery
*d
= discover_refs("git-upload-pack", 0);
998 return fetch_git(d
, nr_heads
, to_fetch
);
1000 return fetch_dumb(nr_heads
, to_fetch
);
1003 static void parse_fetch(struct strbuf
*buf
)
1005 struct ref
**to_fetch
= NULL
;
1006 struct ref
*list_head
= NULL
;
1007 struct ref
**list
= &list_head
;
1008 int alloc_heads
= 0, nr_heads
= 0;
1012 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1015 struct object_id old_oid
;
1017 if (get_oid_hex(p
, &old_oid
))
1018 die("protocol error: expected sha/ref, got %s'", p
);
1019 if (p
[GIT_SHA1_HEXSZ
] == ' ')
1020 name
= p
+ GIT_SHA1_HEXSZ
+ 1;
1021 else if (!p
[GIT_SHA1_HEXSZ
])
1024 die("protocol error: expected sha/ref, got %s'", p
);
1026 ref
= alloc_ref(name
);
1027 oidcpy(&ref
->old_oid
, &old_oid
);
1032 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1033 to_fetch
[nr_heads
++] = ref
;
1036 die("http transport does not support %s", buf
->buf
);
1039 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1045 if (fetch(nr_heads
, to_fetch
))
1046 exit(128); /* error already reported */
1047 free_refs(list_head
);
1055 static int push_dav(int nr_spec
, char **specs
)
1057 struct child_process child
= CHILD_PROCESS_INIT
;
1061 argv_array_push(&child
.args
, "http-push");
1062 argv_array_push(&child
.args
, "--helper-status");
1063 if (options
.dry_run
)
1064 argv_array_push(&child
.args
, "--dry-run");
1065 if (options
.verbosity
> 1)
1066 argv_array_push(&child
.args
, "--verbose");
1067 argv_array_push(&child
.args
, url
.buf
);
1068 for (i
= 0; i
< nr_spec
; i
++)
1069 argv_array_push(&child
.args
, specs
[i
]);
1071 if (run_command(&child
))
1072 die("git-http-push failed");
1076 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
1078 struct rpc_state rpc
;
1080 struct argv_array args
;
1081 struct string_list_item
*cas_option
;
1082 struct strbuf preamble
= STRBUF_INIT
;
1084 argv_array_init(&args
);
1085 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1089 argv_array_push(&args
, "--thin");
1090 if (options
.dry_run
)
1091 argv_array_push(&args
, "--dry-run");
1092 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1093 argv_array_push(&args
, "--signed=yes");
1094 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1095 argv_array_push(&args
, "--signed=if-asked");
1096 if (options
.verbosity
== 0)
1097 argv_array_push(&args
, "--quiet");
1098 else if (options
.verbosity
> 1)
1099 argv_array_push(&args
, "--verbose");
1100 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1101 argv_array_pushf(&args
, "--push-option=%s",
1102 options
.push_options
.items
[i
].string
);
1103 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
1104 for_each_string_list_item(cas_option
, &cas_options
)
1105 argv_array_push(&args
, cas_option
->string
);
1106 argv_array_push(&args
, url
.buf
);
1108 argv_array_push(&args
, "--stdin");
1109 for (i
= 0; i
< nr_spec
; i
++)
1110 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1111 packet_buf_flush(&preamble
);
1113 memset(&rpc
, 0, sizeof(rpc
));
1114 rpc
.service_name
= "git-receive-pack",
1115 rpc
.argv
= args
.argv
;
1116 rpc
.stdin_preamble
= &preamble
;
1118 err
= rpc_service(&rpc
, heads
);
1120 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
1121 strbuf_release(&rpc
.result
);
1122 strbuf_release(&preamble
);
1123 argv_array_clear(&args
);
1127 static int push(int nr_spec
, char **specs
)
1129 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1132 if (heads
->proto_git
)
1133 ret
= push_git(heads
, nr_spec
, specs
);
1135 ret
= push_dav(nr_spec
, specs
);
1136 free_discovery(heads
);
1140 static void parse_push(struct strbuf
*buf
)
1142 char **specs
= NULL
;
1143 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
1146 if (starts_with(buf
->buf
, "push ")) {
1147 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
1148 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
1151 die("http transport does not support %s", buf
->buf
);
1154 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1160 ret
= push(nr_spec
, specs
);
1165 exit(128); /* error already reported */
1168 for (i
= 0; i
< nr_spec
; i
++)
1174 * Used to represent the state of a connection to an HTTP server when
1175 * communicating using git's wire-protocol version 2.
1177 struct proxy_state
{
1180 struct curl_slist
*headers
;
1181 struct strbuf request_buffer
;
1184 struct packet_reader reader
;
1189 static void proxy_state_init(struct proxy_state
*p
, const char *service_name
,
1190 enum protocol_version version
)
1192 struct strbuf buf
= STRBUF_INIT
;
1194 memset(p
, 0, sizeof(*p
));
1195 p
->service_name
= xstrdup(service_name
);
1199 strbuf_init(&p
->request_buffer
, 0);
1201 strbuf_addf(&buf
, "%s%s", url
.buf
, p
->service_name
);
1202 p
->service_url
= strbuf_detach(&buf
, NULL
);
1204 p
->headers
= http_copy_default_headers();
1206 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", p
->service_name
);
1207 p
->headers
= curl_slist_append(p
->headers
, buf
.buf
);
1210 strbuf_addf(&buf
, "Accept: application/x-%s-result", p
->service_name
);
1211 p
->headers
= curl_slist_append(p
->headers
, buf
.buf
);
1214 p
->headers
= curl_slist_append(p
->headers
, "Transfer-Encoding: chunked");
1216 /* Add the Git-Protocol header */
1217 if (get_protocol_http_header(version
, &buf
))
1218 p
->headers
= curl_slist_append(p
->headers
, buf
.buf
);
1220 packet_reader_init(&p
->reader
, p
->in
, NULL
, 0,
1221 PACKET_READ_GENTLE_ON_EOF
|
1222 PACKET_READ_DIE_ON_ERR_PACKET
);
1224 strbuf_release(&buf
);
1227 static void proxy_state_clear(struct proxy_state
*p
)
1229 free(p
->service_name
);
1230 free(p
->service_url
);
1231 curl_slist_free_all(p
->headers
);
1232 strbuf_release(&p
->request_buffer
);
1236 * CURLOPT_READFUNCTION callback function.
1237 * Attempts to copy over a single packet-line at a time into the
1238 * curl provided buffer.
1240 static size_t proxy_in(char *buffer
, size_t eltsize
,
1241 size_t nmemb
, void *userdata
)
1244 struct proxy_state
*p
= userdata
;
1245 size_t avail
= p
->request_buffer
.len
- p
->pos
;
1249 BUG("curl read callback called with size = %"PRIuMAX
" != 1",
1250 (uintmax_t)eltsize
);
1254 if (p
->seen_flush
) {
1259 strbuf_reset(&p
->request_buffer
);
1260 switch (packet_reader_read(&p
->reader
)) {
1261 case PACKET_READ_EOF
:
1262 die("unexpected EOF when reading from parent process");
1263 case PACKET_READ_NORMAL
:
1264 packet_buf_write_len(&p
->request_buffer
, p
->reader
.line
,
1267 case PACKET_READ_DELIM
:
1268 packet_buf_delim(&p
->request_buffer
);
1270 case PACKET_READ_FLUSH
:
1271 packet_buf_flush(&p
->request_buffer
);
1276 avail
= p
->request_buffer
.len
;
1281 memcpy(buffer
, p
->request_buffer
.buf
+ p
->pos
, avail
);
1286 static size_t proxy_out(char *buffer
, size_t eltsize
,
1287 size_t nmemb
, void *userdata
)
1290 struct proxy_state
*p
= userdata
;
1293 BUG("curl read callback called with size = %"PRIuMAX
" != 1",
1294 (uintmax_t)eltsize
);
1297 write_or_die(p
->out
, buffer
, size
);
1301 /* Issues a request to the HTTP server configured in `p` */
1302 static int proxy_request(struct proxy_state
*p
)
1304 struct active_request_slot
*slot
;
1306 slot
= get_active_slot();
1308 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
1309 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1310 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
1311 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, p
->service_url
);
1312 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, p
->headers
);
1314 /* Setup function to read request from client */
1315 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, proxy_in
);
1316 curl_easy_setopt(slot
->curl
, CURLOPT_READDATA
, p
);
1318 /* Setup function to write server response to client */
1319 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, proxy_out
);
1320 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, p
);
1322 if (run_slot(slot
, NULL
) != HTTP_OK
)
1328 static int stateless_connect(const char *service_name
)
1330 struct discovery
*discover
;
1331 struct proxy_state p
;
1334 * Run the info/refs request and see if the server supports protocol
1335 * v2. If and only if the server supports v2 can we successfully
1336 * establish a stateless connection, otherwise we need to tell the
1337 * client to fallback to using other transport helper functions to
1338 * complete their request.
1340 discover
= discover_refs(service_name
, 0);
1341 if (discover
->version
!= protocol_v2
) {
1342 printf("fallback\n");
1346 /* Stateless Connection established */
1351 proxy_state_init(&p
, service_name
, discover
->version
);
1354 * Dump the capability listing that we got from the server earlier
1355 * during the info/refs request.
1357 write_or_die(p
.out
, discover
->buf
, discover
->len
);
1359 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1360 while (packet_reader_peek(&p
.reader
) != PACKET_READ_EOF
) {
1361 if (proxy_request(&p
)) {
1362 /* We would have an err here */
1367 proxy_state_clear(&p
);
1371 int cmd_main(int argc
, const char **argv
)
1373 struct strbuf buf
= STRBUF_INIT
;
1376 setup_git_directory_gently(&nongit
);
1378 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1382 options
.verbosity
= 1;
1383 options
.progress
= !!isatty(2);
1385 string_list_init(&options
.deepen_not
, 1);
1386 string_list_init(&options
.push_options
, 1);
1389 * Just report "remote-curl" here (folding all the various aliases
1390 * ("git-remote-http", "git-remote-https", and etc.) here since they
1391 * are all just copies of the same actual executable.
1393 trace2_cmd_name("remote-curl");
1395 remote
= remote_get(argv
[1]);
1398 end_url_with_slash(&url
, argv
[2]);
1400 end_url_with_slash(&url
, remote
->url
[0]);
1403 http_init(remote
, url
.buf
, 0);
1408 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1410 error("remote-curl: error reading command stream from git");
1415 if (starts_with(buf
.buf
, "fetch ")) {
1417 die("remote-curl: fetch attempted without a local repo");
1420 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1421 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1422 output_refs(get_refs(for_push
));
1424 } else if (starts_with(buf
.buf
, "push ")) {
1427 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1428 char *value
= strchr(arg
, ' ');
1436 result
= set_option(arg
, value
);
1439 else if (result
< 0)
1440 printf("error invalid value\n");
1442 printf("unsupported\n");
1445 } else if (!strcmp(buf
.buf
, "capabilities")) {
1446 printf("stateless-connect\n");
1450 printf("check-connectivity\n");
1453 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1454 if (!stateless_connect(arg
))
1457 error("remote-curl: unknown command '%s' from git", buf
.buf
);