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"
19 #include "transport.h"
21 static struct remote
*remote
;
22 /* always ends with a trailing slash */
23 static struct strbuf url
= STRBUF_INIT
;
29 struct string_list deepen_not
;
30 struct string_list push_options
;
32 unsigned progress
: 1,
33 check_self_contained_and_connected
: 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
45 static struct options options
;
46 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
48 static int set_option(const char *name
, const char *value
)
50 if (!strcmp(name
, "verbosity")) {
52 int v
= strtol(value
, &end
, 10);
53 if (value
== end
|| *end
)
55 options
.verbosity
= v
;
58 else if (!strcmp(name
, "progress")) {
59 if (!strcmp(value
, "true"))
61 else if (!strcmp(value
, "false"))
67 else if (!strcmp(name
, "depth")) {
69 unsigned long v
= strtoul(value
, &end
, 10);
70 if (value
== end
|| *end
)
75 else if (!strcmp(name
, "deepen-since")) {
76 options
.deepen_since
= xstrdup(value
);
79 else if (!strcmp(name
, "deepen-not")) {
80 string_list_append(&options
.deepen_not
, value
);
83 else if (!strcmp(name
, "deepen-relative")) {
84 if (!strcmp(value
, "true"))
85 options
.deepen_relative
= 1;
86 else if (!strcmp(value
, "false"))
87 options
.deepen_relative
= 0;
92 else if (!strcmp(name
, "followtags")) {
93 if (!strcmp(value
, "true"))
94 options
.followtags
= 1;
95 else if (!strcmp(value
, "false"))
96 options
.followtags
= 0;
101 else if (!strcmp(name
, "dry-run")) {
102 if (!strcmp(value
, "true"))
104 else if (!strcmp(value
, "false"))
110 else if (!strcmp(name
, "check-connectivity")) {
111 if (!strcmp(value
, "true"))
112 options
.check_self_contained_and_connected
= 1;
113 else if (!strcmp(value
, "false"))
114 options
.check_self_contained_and_connected
= 0;
119 else if (!strcmp(name
, "cas")) {
120 struct strbuf val
= STRBUF_INIT
;
121 strbuf_addf(&val
, "--" CAS_OPT_NAME
"=%s", value
);
122 string_list_append(&cas_options
, val
.buf
);
123 strbuf_release(&val
);
125 } else if (!strcmp(name
, "cloning")) {
126 if (!strcmp(value
, "true"))
128 else if (!strcmp(value
, "false"))
133 } else if (!strcmp(name
, "update-shallow")) {
134 if (!strcmp(value
, "true"))
135 options
.update_shallow
= 1;
136 else if (!strcmp(value
, "false"))
137 options
.update_shallow
= 0;
141 } else if (!strcmp(name
, "pushcert")) {
142 if (!strcmp(value
, "true"))
143 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
144 else if (!strcmp(value
, "false"))
145 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
146 else if (!strcmp(value
, "if-asked"))
147 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
151 } else if (!strcmp(name
, "push-option")) {
153 string_list_append(&options
.push_options
, value
);
155 struct strbuf unquoted
= STRBUF_INIT
;
156 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
157 die(_("invalid quoting in push-option value: '%s'"), value
);
158 string_list_append_nodup(&options
.push_options
,
159 strbuf_detach(&unquoted
, NULL
));
163 #if LIBCURL_VERSION_NUM >= 0x070a08
164 } else if (!strcmp(name
, "family")) {
165 if (!strcmp(value
, "ipv4"))
166 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
167 else if (!strcmp(value
, "ipv6"))
168 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
169 else if (!strcmp(value
, "all"))
170 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
174 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
175 } else if (!strcmp(name
, "from-promisor")) {
176 options
.from_promisor
= 1;
178 } else if (!strcmp(name
, "no-dependents")) {
179 options
.no_dependents
= 1;
181 } else if (!strcmp(name
, "filter")) {
182 options
.filter
= xstrdup(value
);
185 return 1 /* unsupported */;
195 struct oid_array shallow
;
196 enum protocol_version version
;
197 unsigned proto_git
: 1;
199 static struct discovery
*last_discovery
;
201 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
203 struct ref
*list
= NULL
;
204 struct packet_reader reader
;
206 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
207 PACKET_READ_CHOMP_NEWLINE
|
208 PACKET_READ_GENTLE_ON_EOF
|
209 PACKET_READ_DIE_ON_ERR_PACKET
);
211 heads
->version
= discover_version(&reader
);
212 switch (heads
->version
) {
215 * Do nothing. This isn't a list of refs but rather a
216 * capability advertisement. Client would have run
217 * 'stateless-connect' so we'll dump this capability listing
218 * and let them request the refs themselves.
223 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
224 NULL
, &heads
->shallow
);
226 case protocol_unknown_version
:
227 BUG("unknown protocol version");
233 static struct ref
*parse_info_refs(struct discovery
*heads
)
235 char *data
, *start
, *mid
;
239 struct ref
*refs
= NULL
;
240 struct ref
*ref
= NULL
;
241 struct ref
*last_ref
= NULL
;
246 while (i
< heads
->len
) {
252 if (data
[i
] == '\n') {
253 if (mid
- start
!= the_hash_algo
->hexsz
)
254 die(_("%sinfo/refs not valid: is this a git repository?"),
255 transport_anonymize_url(url
.buf
));
258 ref
= alloc_ref(ref_name
);
259 get_oid_hex(start
, &ref
->old_oid
);
263 last_ref
->next
= ref
;
270 ref
= alloc_ref("HEAD");
271 if (!http_fetch_ref(url
.buf
, ref
) &&
272 !resolve_remote_symref(ref
, refs
)) {
282 static void free_discovery(struct discovery
*d
)
285 if (d
== last_discovery
)
286 last_discovery
= NULL
;
287 free(d
->shallow
.oid
);
295 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
301 * We only show text/plain parts, as other types are likely
302 * to be ugly to look at on the user's terminal.
304 if (strcmp(type
->buf
, "text/plain"))
307 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
315 eol
= strchrnul(p
, '\n');
316 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
322 static int get_protocol_http_header(enum protocol_version version
,
323 struct strbuf
*header
)
326 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
335 static void check_smart_http(struct discovery
*d
, const char *service
,
339 struct packet_reader reader
;
342 * If we don't see x-$service-advertisement, then it's not smart-http.
343 * But once we do, we commit to it and assume any other protocol
344 * violations are hard errors.
346 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
347 !skip_prefix(p
, service
, &p
) ||
348 strcmp(p
, "-advertisement"))
351 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
352 PACKET_READ_CHOMP_NEWLINE
|
353 PACKET_READ_DIE_ON_ERR_PACKET
);
354 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
355 die(_("invalid server response; expected service, got flush packet"));
357 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
359 * The header can include additional metadata lines, up
360 * until a packet flush marker. Ignore these now, but
361 * in the future we might start to scan them.
364 packet_reader_read(&reader
);
365 if (reader
.pktlen
<= 0) {
371 * v0 smart http; callers expect us to soak up the
372 * service and header packets
374 d
->buf
= reader
.src_buffer
;
375 d
->len
= reader
.src_len
;
378 } else if (!strcmp(reader
.line
, "version 2")) {
380 * v2 smart http; do not consume version packet, which will
381 * be handled elsewhere.
386 die(_("invalid server response; got '%s'"), reader
.line
);
390 static struct discovery
*discover_refs(const char *service
, int for_push
)
392 struct strbuf type
= STRBUF_INIT
;
393 struct strbuf charset
= STRBUF_INIT
;
394 struct strbuf buffer
= STRBUF_INIT
;
395 struct strbuf refs_url
= STRBUF_INIT
;
396 struct strbuf effective_url
= STRBUF_INIT
;
397 struct strbuf protocol_header
= STRBUF_INIT
;
398 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
399 struct discovery
*last
= last_discovery
;
400 int http_ret
, maybe_smart
= 0;
401 struct http_get_options http_options
;
402 enum protocol_version version
= get_protocol_version_config();
404 if (last
&& !strcmp(service
, last
->service
))
406 free_discovery(last
);
408 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
409 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
410 git_env_bool("GIT_SMART_HTTP", 1)) {
412 if (!strchr(url
.buf
, '?'))
413 strbuf_addch(&refs_url
, '?');
415 strbuf_addch(&refs_url
, '&');
416 strbuf_addf(&refs_url
, "service=%s", service
);
420 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
421 * to perform a push, then fallback to v0 since the client doesn't know
422 * how to push yet using v2.
424 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
425 version
= protocol_v0
;
427 /* Add the extra Git-Protocol header */
428 if (get_protocol_http_header(version
, &protocol_header
))
429 string_list_append(&extra_headers
, protocol_header
.buf
);
431 memset(&http_options
, 0, sizeof(http_options
));
432 http_options
.content_type
= &type
;
433 http_options
.charset
= &charset
;
434 http_options
.effective_url
= &effective_url
;
435 http_options
.base_url
= &url
;
436 http_options
.extra_headers
= &extra_headers
;
437 http_options
.initial_request
= 1;
438 http_options
.no_cache
= 1;
440 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
444 case HTTP_MISSING_TARGET
:
445 show_http_message(&type
, &charset
, &buffer
);
446 die(_("repository '%s' not found"),
447 transport_anonymize_url(url
.buf
));
449 show_http_message(&type
, &charset
, &buffer
);
450 die(_("Authentication failed for '%s'"),
451 transport_anonymize_url(url
.buf
));
453 show_http_message(&type
, &charset
, &buffer
);
454 die(_("unable to access '%s': %s"),
455 transport_anonymize_url(url
.buf
), curl_errorstr
);
458 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
459 char *u
= transport_anonymize_url(url
.buf
);
460 warning(_("redirecting to %s"), u
);
464 last
= xcalloc(1, sizeof(*last_discovery
));
465 last
->service
= xstrdup(service
);
466 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
467 last
->buf
= last
->buf_alloc
;
470 check_smart_http(last
, service
, &type
);
473 last
->refs
= parse_git_refs(last
, for_push
);
475 last
->refs
= parse_info_refs(last
);
477 strbuf_release(&refs_url
);
478 strbuf_release(&type
);
479 strbuf_release(&charset
);
480 strbuf_release(&effective_url
);
481 strbuf_release(&buffer
);
482 strbuf_release(&protocol_header
);
483 string_list_clear(&extra_headers
, 0);
484 last_discovery
= last
;
488 static struct ref
*get_refs(int for_push
)
490 struct discovery
*heads
;
493 heads
= discover_refs("git-receive-pack", for_push
);
495 heads
= discover_refs("git-upload-pack", for_push
);
500 static void output_refs(struct ref
*refs
)
503 for (posn
= refs
; posn
; posn
= posn
->next
) {
505 printf("@%s %s\n", posn
->symref
, posn
->name
);
507 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
514 const char *service_name
;
516 char *hdr_content_type
;
518 char *protocol_header
;
526 unsigned gzip_request
: 1;
527 unsigned initial_buffer
: 1;
530 * Whenever a pkt-line is read into buf, append the 4 characters
531 * denoting its length before appending the payload.
533 unsigned write_line_lengths
: 1;
536 * Used by rpc_out; initialize to 0. This is true if a flush has been
537 * read, but the corresponding line length (if write_line_lengths is
538 * true) and EOF have not been sent to libcurl. Since each flush marks
539 * the end of a request, each flush must be completely sent before any
540 * further reading occurs.
542 unsigned flush_read_but_not_sent
: 1;
546 * Appends the result of reading from rpc->out to the string represented by
547 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
548 * enough space, 0 otherwise.
550 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
551 * hexadecimal string before appending the result described above.
553 * Writes the total number of bytes appended into appended.
555 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
557 enum packet_read_status
*status
) {
562 if (rpc
->write_line_lengths
) {
563 left
= rpc
->alloc
- rpc
->len
- 4;
564 buf
= rpc
->buf
+ rpc
->len
+ 4;
566 left
= rpc
->alloc
- rpc
->len
;
567 buf
= rpc
->buf
+ rpc
->len
;
570 if (left
< LARGE_PACKET_MAX
)
573 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
574 left
, &pktlen_raw
, options
);
575 if (*status
!= PACKET_READ_EOF
) {
576 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
577 rpc
->len
+= *appended
;
580 if (rpc
->write_line_lengths
) {
582 case PACKET_READ_EOF
:
583 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
584 die(_("shouldn't have EOF when not gentle on EOF"));
586 case PACKET_READ_NORMAL
:
587 set_packet_header(buf
- 4, *appended
);
589 case PACKET_READ_DELIM
:
590 memcpy(buf
- 4, "0001", 4);
592 case PACKET_READ_FLUSH
:
593 memcpy(buf
- 4, "0000", 4);
601 static size_t rpc_out(void *ptr
, size_t eltsize
,
602 size_t nmemb
, void *buffer_
)
604 size_t max
= eltsize
* nmemb
;
605 struct rpc_state
*rpc
= buffer_
;
606 size_t avail
= rpc
->len
- rpc
->pos
;
607 enum packet_read_status status
;
610 rpc
->initial_buffer
= 0;
613 if (!rpc
->flush_read_but_not_sent
) {
614 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
615 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
616 if (status
== PACKET_READ_FLUSH
)
617 rpc
->flush_read_but_not_sent
= 1;
620 * If flush_read_but_not_sent is true, we have already read one
621 * full request but have not fully sent it + EOF, which is why
622 * we need to refrain from reading.
625 if (rpc
->flush_read_but_not_sent
) {
628 * The line length either does not need to be sent at
629 * all or has already been completely sent. Now we can
630 * return 0, indicating EOF, meaning that the flush has
633 rpc
->flush_read_but_not_sent
= 0;
637 * If avail is non-zerp, the line length for the flush still
638 * hasn't been fully sent. Proceed with sending the line
645 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
650 #ifndef NO_CURL_IOCTL
651 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
653 struct rpc_state
*rpc
= clientp
;
659 case CURLIOCMD_RESTARTREAD
:
660 if (rpc
->initial_buffer
) {
664 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
665 return CURLIOE_FAILRESTART
;
668 return CURLIOE_UNKNOWNCMD
;
674 struct rpc_state
*rpc
;
675 struct active_request_slot
*slot
;
679 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
682 static size_t rpc_in(char *ptr
, size_t eltsize
,
683 size_t nmemb
, void *buffer_
)
685 size_t size
= eltsize
* nmemb
;
686 struct rpc_in_data
*data
= buffer_
;
689 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
690 &response_code
) != CURLE_OK
)
692 if (response_code
>= 300)
695 data
->rpc
->any_written
= 1;
696 write_or_die(data
->rpc
->in
, ptr
, size
);
700 static int run_slot(struct active_request_slot
*slot
,
701 struct slot_results
*results
)
704 struct slot_results results_buf
;
707 results
= &results_buf
;
709 err
= run_one_slot(slot
, results
);
711 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
712 struct strbuf msg
= STRBUF_INIT
;
713 if (results
->http_code
&& results
->http_code
!= 200)
714 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
715 if (results
->curl_result
!= CURLE_OK
) {
717 strbuf_addch(&msg
, ' ');
718 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
719 if (curl_errorstr
[0]) {
720 strbuf_addch(&msg
, ' ');
721 strbuf_addstr(&msg
, curl_errorstr
);
724 error(_("RPC failed; %s"), msg
.buf
);
725 strbuf_release(&msg
);
731 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
733 struct active_request_slot
*slot
;
734 struct curl_slist
*headers
= http_copy_default_headers();
735 struct strbuf buf
= STRBUF_INIT
;
738 slot
= get_active_slot();
740 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
741 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
743 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
744 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
745 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
746 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
747 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
748 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
749 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
750 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
751 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
753 err
= run_slot(slot
, results
);
755 curl_slist_free_all(headers
);
756 strbuf_release(&buf
);
760 static curl_off_t
xcurl_off_t(size_t len
)
762 uintmax_t size
= len
;
763 if (size
> maximum_signed_value_of_type(curl_off_t
))
764 die(_("cannot handle pushes this big"));
765 return (curl_off_t
)size
;
769 * If flush_received is true, do not attempt to read any more; just use what's
772 static int post_rpc(struct rpc_state
*rpc
, int flush_received
)
774 struct active_request_slot
*slot
;
775 struct curl_slist
*headers
= http_copy_default_headers();
776 int use_gzip
= rpc
->gzip_request
;
777 char *gzip_body
= NULL
;
778 size_t gzip_size
= 0;
779 int err
, large_request
= 0;
780 int needs_100_continue
= 0;
781 struct rpc_in_data rpc_in_data
;
783 /* Try to load the entire request, if we can fit it into the
784 * allocated buffer space we can use HTTP/1.0 and avoid the
785 * chunked encoding mess.
787 if (!flush_received
) {
790 enum packet_read_status status
;
792 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
797 if (status
== PACKET_READ_FLUSH
)
803 struct slot_results results
;
806 err
= probe_rpc(rpc
, &results
);
807 if (err
== HTTP_REAUTH
)
808 credential_fill(&http_auth
);
809 } while (err
== HTTP_REAUTH
);
813 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
814 needs_100_continue
= 1;
817 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
818 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
819 headers
= curl_slist_append(headers
, needs_100_continue
?
820 "Expect: 100-continue" : "Expect:");
822 /* Add the extra Git-Protocol header */
823 if (rpc
->protocol_header
)
824 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
827 slot
= get_active_slot();
829 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
830 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
831 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
832 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
835 /* The request body is large and the size cannot be predicted.
836 * We must use chunked encoding to send it.
838 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
839 rpc
->initial_buffer
= 1;
840 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
841 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
842 #ifndef NO_CURL_IOCTL
843 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
844 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
846 if (options
.verbosity
> 1) {
847 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
851 } else if (gzip_body
) {
853 * If we are looping to retry authentication, then the previous
854 * run will have set up the headers and gzip buffer already,
855 * and we just need to send it.
857 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
858 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
860 } else if (use_gzip
&& 1024 < rpc
->len
) {
861 /* The client backend isn't giving us compressed data so
862 * we can try to deflate it ourselves, this may save on
868 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
869 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
870 gzip_body
= xmalloc(gzip_size
);
872 stream
.next_in
= (unsigned char *)rpc
->buf
;
873 stream
.avail_in
= rpc
->len
;
874 stream
.next_out
= (unsigned char *)gzip_body
;
875 stream
.avail_out
= gzip_size
;
877 ret
= git_deflate(&stream
, Z_FINISH
);
878 if (ret
!= Z_STREAM_END
)
879 die(_("cannot deflate request; zlib deflate error %d"), ret
);
881 ret
= git_deflate_end_gently(&stream
);
883 die(_("cannot deflate request; zlib end error %d"), ret
);
885 gzip_size
= stream
.total_out
;
887 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
888 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
889 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
891 if (options
.verbosity
> 1) {
892 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
894 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
898 /* We know the complete request size in advance, use the
899 * more normal Content-Length approach.
901 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
902 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
903 if (options
.verbosity
> 1) {
904 fprintf(stderr
, "POST %s (%lu bytes)\n",
905 rpc
->service_name
, (unsigned long)rpc
->len
);
910 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
911 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
912 rpc_in_data
.rpc
= rpc
;
913 rpc_in_data
.slot
= slot
;
914 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
915 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
918 rpc
->any_written
= 0;
919 err
= run_slot(slot
, NULL
);
920 if (err
== HTTP_REAUTH
&& !large_request
) {
921 credential_fill(&http_auth
);
927 if (!rpc
->any_written
)
930 curl_slist_free_all(headers
);
935 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
936 const char **client_argv
, const struct strbuf
*preamble
,
937 struct strbuf
*rpc_result
)
939 const char *svc
= rpc
->service_name
;
940 struct strbuf buf
= STRBUF_INIT
;
941 struct child_process client
= CHILD_PROCESS_INIT
;
947 client
.argv
= client_argv
;
948 if (start_command(&client
))
950 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
952 write_or_die(client
.in
, heads
->buf
, heads
->len
);
954 rpc
->alloc
= http_post_buffer
;
955 rpc
->buf
= xmalloc(rpc
->alloc
);
957 rpc
->out
= client
.out
;
959 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
960 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
962 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
963 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
965 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
966 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
968 if (get_protocol_http_header(heads
->version
, &buf
))
969 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
971 rpc
->protocol_header
= NULL
;
974 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
979 err
|= post_rpc(rpc
, 0);
985 strbuf_read(rpc_result
, client
.out
, 0);
989 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
996 err
|= finish_command(&client
);
997 free(rpc
->service_url
);
998 free(rpc
->hdr_content_type
);
999 free(rpc
->hdr_accept
);
1000 free(rpc
->protocol_header
);
1002 strbuf_release(&buf
);
1006 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1008 struct walker
*walker
;
1012 ALLOC_ARRAY(targets
, nr_heads
);
1013 if (options
.depth
|| options
.deepen_since
)
1014 die(_("dumb http transport does not support shallow capabilities"));
1015 for (i
= 0; i
< nr_heads
; i
++)
1016 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1018 walker
= get_http_walker(url
.buf
);
1019 walker
->get_verbosely
= options
.verbosity
>= 3;
1020 walker
->get_recover
= 0;
1021 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1022 walker_free(walker
);
1024 for (i
= 0; i
< nr_heads
; i
++)
1028 return ret
? error(_("fetch failed.")) : 0;
1031 static int fetch_git(struct discovery
*heads
,
1032 int nr_heads
, struct ref
**to_fetch
)
1034 struct rpc_state rpc
;
1035 struct strbuf preamble
= STRBUF_INIT
;
1037 struct argv_array args
= ARGV_ARRAY_INIT
;
1038 struct strbuf rpc_result
= STRBUF_INIT
;
1040 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
1041 "--stdin", "--lock-pack", NULL
);
1042 if (options
.followtags
)
1043 argv_array_push(&args
, "--include-tag");
1045 argv_array_push(&args
, "--thin");
1046 if (options
.verbosity
>= 3)
1047 argv_array_pushl(&args
, "-v", "-v", NULL
);
1048 if (options
.check_self_contained_and_connected
)
1049 argv_array_push(&args
, "--check-self-contained-and-connected");
1050 if (options
.cloning
)
1051 argv_array_push(&args
, "--cloning");
1052 if (options
.update_shallow
)
1053 argv_array_push(&args
, "--update-shallow");
1054 if (!options
.progress
)
1055 argv_array_push(&args
, "--no-progress");
1057 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
1058 if (options
.deepen_since
)
1059 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1060 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1061 argv_array_pushf(&args
, "--shallow-exclude=%s",
1062 options
.deepen_not
.items
[i
].string
);
1063 if (options
.deepen_relative
&& options
.depth
)
1064 argv_array_push(&args
, "--deepen-relative");
1065 if (options
.from_promisor
)
1066 argv_array_push(&args
, "--from-promisor");
1067 if (options
.no_dependents
)
1068 argv_array_push(&args
, "--no-dependents");
1070 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
1071 argv_array_push(&args
, url
.buf
);
1073 for (i
= 0; i
< nr_heads
; i
++) {
1074 struct ref
*ref
= to_fetch
[i
];
1076 die(_("cannot fetch by sha1 over smart http"));
1077 packet_buf_write(&preamble
, "%s %s\n",
1078 oid_to_hex(&ref
->old_oid
), ref
->name
);
1080 packet_buf_flush(&preamble
);
1082 memset(&rpc
, 0, sizeof(rpc
));
1083 rpc
.service_name
= "git-upload-pack",
1084 rpc
.gzip_request
= 1;
1086 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1088 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1089 strbuf_release(&rpc_result
);
1090 strbuf_release(&preamble
);
1091 argv_array_clear(&args
);
1095 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1097 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1099 return fetch_git(d
, nr_heads
, to_fetch
);
1101 return fetch_dumb(nr_heads
, to_fetch
);
1104 static void parse_fetch(struct strbuf
*buf
)
1106 struct ref
**to_fetch
= NULL
;
1107 struct ref
*list_head
= NULL
;
1108 struct ref
**list
= &list_head
;
1109 int alloc_heads
= 0, nr_heads
= 0;
1113 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1116 struct object_id old_oid
;
1119 if (parse_oid_hex(p
, &old_oid
, &q
))
1120 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1126 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1128 ref
= alloc_ref(name
);
1129 oidcpy(&ref
->old_oid
, &old_oid
);
1134 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1135 to_fetch
[nr_heads
++] = ref
;
1138 die(_("http transport does not support %s"), buf
->buf
);
1141 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1147 if (fetch(nr_heads
, to_fetch
))
1148 exit(128); /* error already reported */
1149 free_refs(list_head
);
1157 static int push_dav(int nr_spec
, char **specs
)
1159 struct child_process child
= CHILD_PROCESS_INIT
;
1163 argv_array_push(&child
.args
, "http-push");
1164 argv_array_push(&child
.args
, "--helper-status");
1165 if (options
.dry_run
)
1166 argv_array_push(&child
.args
, "--dry-run");
1167 if (options
.verbosity
> 1)
1168 argv_array_push(&child
.args
, "--verbose");
1169 argv_array_push(&child
.args
, url
.buf
);
1170 for (i
= 0; i
< nr_spec
; i
++)
1171 argv_array_push(&child
.args
, specs
[i
]);
1173 if (run_command(&child
))
1174 die(_("git-http-push failed"));
1178 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
1180 struct rpc_state rpc
;
1182 struct argv_array args
;
1183 struct string_list_item
*cas_option
;
1184 struct strbuf preamble
= STRBUF_INIT
;
1185 struct strbuf rpc_result
= STRBUF_INIT
;
1187 argv_array_init(&args
);
1188 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1192 argv_array_push(&args
, "--thin");
1193 if (options
.dry_run
)
1194 argv_array_push(&args
, "--dry-run");
1195 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1196 argv_array_push(&args
, "--signed=yes");
1197 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1198 argv_array_push(&args
, "--signed=if-asked");
1199 if (options
.verbosity
== 0)
1200 argv_array_push(&args
, "--quiet");
1201 else if (options
.verbosity
> 1)
1202 argv_array_push(&args
, "--verbose");
1203 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1204 argv_array_pushf(&args
, "--push-option=%s",
1205 options
.push_options
.items
[i
].string
);
1206 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
1207 for_each_string_list_item(cas_option
, &cas_options
)
1208 argv_array_push(&args
, cas_option
->string
);
1209 argv_array_push(&args
, url
.buf
);
1211 argv_array_push(&args
, "--stdin");
1212 for (i
= 0; i
< nr_spec
; i
++)
1213 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1214 packet_buf_flush(&preamble
);
1216 memset(&rpc
, 0, sizeof(rpc
));
1217 rpc
.service_name
= "git-receive-pack",
1219 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1221 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1222 strbuf_release(&rpc_result
);
1223 strbuf_release(&preamble
);
1224 argv_array_clear(&args
);
1228 static int push(int nr_spec
, char **specs
)
1230 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1233 if (heads
->proto_git
)
1234 ret
= push_git(heads
, nr_spec
, specs
);
1236 ret
= push_dav(nr_spec
, specs
);
1237 free_discovery(heads
);
1241 static void parse_push(struct strbuf
*buf
)
1243 char **specs
= NULL
;
1244 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
1247 if (starts_with(buf
->buf
, "push ")) {
1248 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
1249 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
1252 die(_("http transport does not support %s"), buf
->buf
);
1255 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1261 ret
= push(nr_spec
, specs
);
1266 exit(128); /* error already reported */
1269 for (i
= 0; i
< nr_spec
; i
++)
1274 static int stateless_connect(const char *service_name
)
1276 struct discovery
*discover
;
1277 struct rpc_state rpc
;
1278 struct strbuf buf
= STRBUF_INIT
;
1281 * Run the info/refs request and see if the server supports protocol
1282 * v2. If and only if the server supports v2 can we successfully
1283 * establish a stateless connection, otherwise we need to tell the
1284 * client to fallback to using other transport helper functions to
1285 * complete their request.
1287 discover
= discover_refs(service_name
, 0);
1288 if (discover
->version
!= protocol_v2
) {
1289 printf("fallback\n");
1293 /* Stateless Connection established */
1298 rpc
.service_name
= service_name
;
1299 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1300 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1301 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1302 if (get_protocol_http_header(discover
->version
, &buf
)) {
1303 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1305 rpc
.protocol_header
= NULL
;
1306 strbuf_release(&buf
);
1308 rpc
.buf
= xmalloc(http_post_buffer
);
1309 rpc
.alloc
= http_post_buffer
;
1314 rpc
.any_written
= 0;
1315 rpc
.gzip_request
= 1;
1316 rpc
.initial_buffer
= 0;
1317 rpc
.write_line_lengths
= 1;
1318 rpc
.flush_read_but_not_sent
= 0;
1321 * Dump the capability listing that we got from the server earlier
1322 * during the info/refs request.
1324 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1326 /* Until we see EOF keep sending POSTs */
1329 enum packet_read_status status
;
1331 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1333 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1334 if (status
== PACKET_READ_EOF
)
1336 if (post_rpc(&rpc
, status
== PACKET_READ_FLUSH
))
1337 /* We would have an err here */
1339 /* Reset the buffer for next request */
1343 free(rpc
.service_url
);
1344 free(rpc
.hdr_content_type
);
1345 free(rpc
.hdr_accept
);
1346 free(rpc
.protocol_header
);
1348 strbuf_release(&buf
);
1353 int cmd_main(int argc
, const char **argv
)
1355 struct strbuf buf
= STRBUF_INIT
;
1358 setup_git_directory_gently(&nongit
);
1360 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1364 options
.verbosity
= 1;
1365 options
.progress
= !!isatty(2);
1367 string_list_init(&options
.deepen_not
, 1);
1368 string_list_init(&options
.push_options
, 1);
1371 * Just report "remote-curl" here (folding all the various aliases
1372 * ("git-remote-http", "git-remote-https", and etc.) here since they
1373 * are all just copies of the same actual executable.
1375 trace2_cmd_name("remote-curl");
1377 remote
= remote_get(argv
[1]);
1380 end_url_with_slash(&url
, argv
[2]);
1382 end_url_with_slash(&url
, remote
->url
[0]);
1385 http_init(remote
, url
.buf
, 0);
1390 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1392 error(_("remote-curl: error reading command stream from git"));
1397 if (starts_with(buf
.buf
, "fetch ")) {
1399 die(_("remote-curl: fetch attempted without a local repo"));
1402 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1403 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1404 output_refs(get_refs(for_push
));
1406 } else if (starts_with(buf
.buf
, "push ")) {
1409 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1410 char *value
= strchr(arg
, ' ');
1418 result
= set_option(arg
, value
);
1421 else if (result
< 0)
1422 printf("error invalid value\n");
1424 printf("unsupported\n");
1427 } else if (!strcmp(buf
.buf
, "capabilities")) {
1428 printf("stateless-connect\n");
1432 printf("check-connectivity\n");
1435 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1436 if (!stateless_connect(arg
))
1439 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);