9 #include "run-command.h"
11 #include "string-list.h"
13 #include "argv-array.h"
14 #include "credential.h"
15 #include "oid-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. */
46 static struct options options
;
47 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
49 static int set_option(const char *name
, const char *value
)
51 if (!strcmp(name
, "verbosity")) {
53 int v
= strtol(value
, &end
, 10);
54 if (value
== end
|| *end
)
56 options
.verbosity
= v
;
59 else if (!strcmp(name
, "progress")) {
60 if (!strcmp(value
, "true"))
62 else if (!strcmp(value
, "false"))
68 else if (!strcmp(name
, "depth")) {
70 unsigned long v
= strtoul(value
, &end
, 10);
71 if (value
== end
|| *end
)
76 else if (!strcmp(name
, "deepen-since")) {
77 options
.deepen_since
= xstrdup(value
);
80 else if (!strcmp(name
, "deepen-not")) {
81 string_list_append(&options
.deepen_not
, value
);
84 else if (!strcmp(name
, "deepen-relative")) {
85 if (!strcmp(value
, "true"))
86 options
.deepen_relative
= 1;
87 else if (!strcmp(value
, "false"))
88 options
.deepen_relative
= 0;
93 else if (!strcmp(name
, "followtags")) {
94 if (!strcmp(value
, "true"))
95 options
.followtags
= 1;
96 else if (!strcmp(value
, "false"))
97 options
.followtags
= 0;
102 else if (!strcmp(name
, "dry-run")) {
103 if (!strcmp(value
, "true"))
105 else if (!strcmp(value
, "false"))
111 else if (!strcmp(name
, "check-connectivity")) {
112 if (!strcmp(value
, "true"))
113 options
.check_self_contained_and_connected
= 1;
114 else if (!strcmp(value
, "false"))
115 options
.check_self_contained_and_connected
= 0;
120 else if (!strcmp(name
, "cas")) {
121 struct strbuf val
= STRBUF_INIT
;
122 strbuf_addf(&val
, "--" CAS_OPT_NAME
"=%s", value
);
123 string_list_append(&cas_options
, val
.buf
);
124 strbuf_release(&val
);
126 } else if (!strcmp(name
, "cloning")) {
127 if (!strcmp(value
, "true"))
129 else if (!strcmp(value
, "false"))
134 } else if (!strcmp(name
, "update-shallow")) {
135 if (!strcmp(value
, "true"))
136 options
.update_shallow
= 1;
137 else if (!strcmp(value
, "false"))
138 options
.update_shallow
= 0;
142 } else if (!strcmp(name
, "pushcert")) {
143 if (!strcmp(value
, "true"))
144 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
145 else if (!strcmp(value
, "false"))
146 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
147 else if (!strcmp(value
, "if-asked"))
148 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
152 } else if (!strcmp(name
, "atomic")) {
153 if (!strcmp(value
, "true"))
155 else if (!strcmp(value
, "false"))
160 } else if (!strcmp(name
, "push-option")) {
162 string_list_append(&options
.push_options
, value
);
164 struct strbuf unquoted
= STRBUF_INIT
;
165 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
166 die(_("invalid quoting in push-option value: '%s'"), value
);
167 string_list_append_nodup(&options
.push_options
,
168 strbuf_detach(&unquoted
, NULL
));
172 #if LIBCURL_VERSION_NUM >= 0x070a08
173 } else if (!strcmp(name
, "family")) {
174 if (!strcmp(value
, "ipv4"))
175 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
176 else if (!strcmp(value
, "ipv6"))
177 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
178 else if (!strcmp(value
, "all"))
179 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
183 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
184 } else if (!strcmp(name
, "from-promisor")) {
185 options
.from_promisor
= 1;
187 } else if (!strcmp(name
, "no-dependents")) {
188 options
.no_dependents
= 1;
190 } else if (!strcmp(name
, "filter")) {
191 options
.filter
= xstrdup(value
);
194 return 1 /* unsupported */;
204 struct oid_array shallow
;
205 enum protocol_version version
;
206 unsigned proto_git
: 1;
208 static struct discovery
*last_discovery
;
210 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
212 struct ref
*list
= NULL
;
213 struct packet_reader reader
;
215 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
216 PACKET_READ_CHOMP_NEWLINE
|
217 PACKET_READ_GENTLE_ON_EOF
|
218 PACKET_READ_DIE_ON_ERR_PACKET
);
220 heads
->version
= discover_version(&reader
);
221 switch (heads
->version
) {
224 * Do nothing. This isn't a list of refs but rather a
225 * capability advertisement. Client would have run
226 * 'stateless-connect' so we'll dump this capability listing
227 * and let them request the refs themselves.
232 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
233 NULL
, &heads
->shallow
);
235 case protocol_unknown_version
:
236 BUG("unknown protocol version");
242 static struct ref
*parse_info_refs(struct discovery
*heads
)
244 char *data
, *start
, *mid
;
248 struct ref
*refs
= NULL
;
249 struct ref
*ref
= NULL
;
250 struct ref
*last_ref
= NULL
;
255 while (i
< heads
->len
) {
261 if (data
[i
] == '\n') {
262 if (mid
- start
!= the_hash_algo
->hexsz
)
263 die(_("%sinfo/refs not valid: is this a git repository?"),
264 transport_anonymize_url(url
.buf
));
267 ref
= alloc_ref(ref_name
);
268 get_oid_hex(start
, &ref
->old_oid
);
272 last_ref
->next
= ref
;
279 ref
= alloc_ref("HEAD");
280 if (!http_fetch_ref(url
.buf
, ref
) &&
281 !resolve_remote_symref(ref
, refs
)) {
291 static void free_discovery(struct discovery
*d
)
294 if (d
== last_discovery
)
295 last_discovery
= NULL
;
296 free(d
->shallow
.oid
);
304 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
310 * We only show text/plain parts, as other types are likely
311 * to be ugly to look at on the user's terminal.
313 if (strcmp(type
->buf
, "text/plain"))
316 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
324 eol
= strchrnul(p
, '\n');
325 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
331 static int get_protocol_http_header(enum protocol_version version
,
332 struct strbuf
*header
)
335 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
344 static void check_smart_http(struct discovery
*d
, const char *service
,
348 struct packet_reader reader
;
351 * If we don't see x-$service-advertisement, then it's not smart-http.
352 * But once we do, we commit to it and assume any other protocol
353 * violations are hard errors.
355 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
356 !skip_prefix(p
, service
, &p
) ||
357 strcmp(p
, "-advertisement"))
360 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
361 PACKET_READ_CHOMP_NEWLINE
|
362 PACKET_READ_DIE_ON_ERR_PACKET
);
363 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
364 die(_("invalid server response; expected service, got flush packet"));
366 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
368 * The header can include additional metadata lines, up
369 * until a packet flush marker. Ignore these now, but
370 * in the future we might start to scan them.
373 packet_reader_read(&reader
);
374 if (reader
.pktlen
<= 0) {
380 * v0 smart http; callers expect us to soak up the
381 * service and header packets
383 d
->buf
= reader
.src_buffer
;
384 d
->len
= reader
.src_len
;
387 } else if (!strcmp(reader
.line
, "version 2")) {
389 * v2 smart http; do not consume version packet, which will
390 * be handled elsewhere.
395 die(_("invalid server response; got '%s'"), reader
.line
);
399 static struct discovery
*discover_refs(const char *service
, int for_push
)
401 struct strbuf type
= STRBUF_INIT
;
402 struct strbuf charset
= STRBUF_INIT
;
403 struct strbuf buffer
= STRBUF_INIT
;
404 struct strbuf refs_url
= STRBUF_INIT
;
405 struct strbuf effective_url
= STRBUF_INIT
;
406 struct strbuf protocol_header
= STRBUF_INIT
;
407 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
408 struct discovery
*last
= last_discovery
;
409 int http_ret
, maybe_smart
= 0;
410 struct http_get_options http_options
;
411 enum protocol_version version
= get_protocol_version_config();
413 if (last
&& !strcmp(service
, last
->service
))
415 free_discovery(last
);
417 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
418 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
419 git_env_bool("GIT_SMART_HTTP", 1)) {
421 if (!strchr(url
.buf
, '?'))
422 strbuf_addch(&refs_url
, '?');
424 strbuf_addch(&refs_url
, '&');
425 strbuf_addf(&refs_url
, "service=%s", service
);
429 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
430 * to perform a push, then fallback to v0 since the client doesn't know
431 * how to push yet using v2.
433 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
434 version
= protocol_v0
;
436 /* Add the extra Git-Protocol header */
437 if (get_protocol_http_header(version
, &protocol_header
))
438 string_list_append(&extra_headers
, protocol_header
.buf
);
440 memset(&http_options
, 0, sizeof(http_options
));
441 http_options
.content_type
= &type
;
442 http_options
.charset
= &charset
;
443 http_options
.effective_url
= &effective_url
;
444 http_options
.base_url
= &url
;
445 http_options
.extra_headers
= &extra_headers
;
446 http_options
.initial_request
= 1;
447 http_options
.no_cache
= 1;
449 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
453 case HTTP_MISSING_TARGET
:
454 show_http_message(&type
, &charset
, &buffer
);
455 die(_("repository '%s' not found"),
456 transport_anonymize_url(url
.buf
));
458 show_http_message(&type
, &charset
, &buffer
);
459 die(_("Authentication failed for '%s'"),
460 transport_anonymize_url(url
.buf
));
462 show_http_message(&type
, &charset
, &buffer
);
463 die(_("unable to access '%s': %s"),
464 transport_anonymize_url(url
.buf
), curl_errorstr
);
467 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
468 char *u
= transport_anonymize_url(url
.buf
);
469 warning(_("redirecting to %s"), u
);
473 last
= xcalloc(1, sizeof(*last_discovery
));
474 last
->service
= xstrdup(service
);
475 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
476 last
->buf
= last
->buf_alloc
;
479 check_smart_http(last
, service
, &type
);
482 last
->refs
= parse_git_refs(last
, for_push
);
484 last
->refs
= parse_info_refs(last
);
486 strbuf_release(&refs_url
);
487 strbuf_release(&type
);
488 strbuf_release(&charset
);
489 strbuf_release(&effective_url
);
490 strbuf_release(&buffer
);
491 strbuf_release(&protocol_header
);
492 string_list_clear(&extra_headers
, 0);
493 last_discovery
= last
;
497 static struct ref
*get_refs(int for_push
)
499 struct discovery
*heads
;
502 heads
= discover_refs("git-receive-pack", for_push
);
504 heads
= discover_refs("git-upload-pack", for_push
);
509 static void output_refs(struct ref
*refs
)
512 for (posn
= refs
; posn
; posn
= posn
->next
) {
514 printf("@%s %s\n", posn
->symref
, posn
->name
);
516 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
523 const char *service_name
;
525 char *hdr_content_type
;
527 char *protocol_header
;
535 unsigned gzip_request
: 1;
536 unsigned initial_buffer
: 1;
539 * Whenever a pkt-line is read into buf, append the 4 characters
540 * denoting its length before appending the payload.
542 unsigned write_line_lengths
: 1;
545 * Used by rpc_out; initialize to 0. This is true if a flush has been
546 * read, but the corresponding line length (if write_line_lengths is
547 * true) and EOF have not been sent to libcurl. Since each flush marks
548 * the end of a request, each flush must be completely sent before any
549 * further reading occurs.
551 unsigned flush_read_but_not_sent
: 1;
555 * Appends the result of reading from rpc->out to the string represented by
556 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
557 * enough space, 0 otherwise.
559 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
560 * hexadecimal string before appending the result described above.
562 * Writes the total number of bytes appended into appended.
564 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
566 enum packet_read_status
*status
) {
571 if (rpc
->write_line_lengths
) {
572 left
= rpc
->alloc
- rpc
->len
- 4;
573 buf
= rpc
->buf
+ rpc
->len
+ 4;
575 left
= rpc
->alloc
- rpc
->len
;
576 buf
= rpc
->buf
+ rpc
->len
;
579 if (left
< LARGE_PACKET_MAX
)
582 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
583 left
, &pktlen_raw
, options
);
584 if (*status
!= PACKET_READ_EOF
) {
585 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
586 rpc
->len
+= *appended
;
589 if (rpc
->write_line_lengths
) {
591 case PACKET_READ_EOF
:
592 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
593 die(_("shouldn't have EOF when not gentle on EOF"));
595 case PACKET_READ_NORMAL
:
596 set_packet_header(buf
- 4, *appended
);
598 case PACKET_READ_DELIM
:
599 memcpy(buf
- 4, "0001", 4);
601 case PACKET_READ_FLUSH
:
602 memcpy(buf
- 4, "0000", 4);
610 static size_t rpc_out(void *ptr
, size_t eltsize
,
611 size_t nmemb
, void *buffer_
)
613 size_t max
= eltsize
* nmemb
;
614 struct rpc_state
*rpc
= buffer_
;
615 size_t avail
= rpc
->len
- rpc
->pos
;
616 enum packet_read_status status
;
619 rpc
->initial_buffer
= 0;
622 if (!rpc
->flush_read_but_not_sent
) {
623 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
624 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
625 if (status
== PACKET_READ_FLUSH
)
626 rpc
->flush_read_but_not_sent
= 1;
629 * If flush_read_but_not_sent is true, we have already read one
630 * full request but have not fully sent it + EOF, which is why
631 * we need to refrain from reading.
634 if (rpc
->flush_read_but_not_sent
) {
637 * The line length either does not need to be sent at
638 * all or has already been completely sent. Now we can
639 * return 0, indicating EOF, meaning that the flush has
642 rpc
->flush_read_but_not_sent
= 0;
646 * If avail is non-zerp, the line length for the flush still
647 * hasn't been fully sent. Proceed with sending the line
654 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
659 #ifndef NO_CURL_IOCTL
660 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
662 struct rpc_state
*rpc
= clientp
;
668 case CURLIOCMD_RESTARTREAD
:
669 if (rpc
->initial_buffer
) {
673 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
674 return CURLIOE_FAILRESTART
;
677 return CURLIOE_UNKNOWNCMD
;
683 struct rpc_state
*rpc
;
684 struct active_request_slot
*slot
;
688 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
691 static size_t rpc_in(char *ptr
, size_t eltsize
,
692 size_t nmemb
, void *buffer_
)
694 size_t size
= eltsize
* nmemb
;
695 struct rpc_in_data
*data
= buffer_
;
698 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
699 &response_code
) != CURLE_OK
)
701 if (response_code
>= 300)
704 data
->rpc
->any_written
= 1;
705 write_or_die(data
->rpc
->in
, ptr
, size
);
709 static int run_slot(struct active_request_slot
*slot
,
710 struct slot_results
*results
)
713 struct slot_results results_buf
;
716 results
= &results_buf
;
718 err
= run_one_slot(slot
, results
);
720 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
721 struct strbuf msg
= STRBUF_INIT
;
722 if (results
->http_code
&& results
->http_code
!= 200)
723 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
724 if (results
->curl_result
!= CURLE_OK
) {
726 strbuf_addch(&msg
, ' ');
727 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
728 if (curl_errorstr
[0]) {
729 strbuf_addch(&msg
, ' ');
730 strbuf_addstr(&msg
, curl_errorstr
);
733 error(_("RPC failed; %s"), msg
.buf
);
734 strbuf_release(&msg
);
740 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
742 struct active_request_slot
*slot
;
743 struct curl_slist
*headers
= http_copy_default_headers();
744 struct strbuf buf
= STRBUF_INIT
;
747 slot
= get_active_slot();
749 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
750 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
752 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
753 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
754 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
755 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
756 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
757 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
758 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
759 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
760 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
762 err
= run_slot(slot
, results
);
764 curl_slist_free_all(headers
);
765 strbuf_release(&buf
);
769 static curl_off_t
xcurl_off_t(size_t len
)
771 uintmax_t size
= len
;
772 if (size
> maximum_signed_value_of_type(curl_off_t
))
773 die(_("cannot handle pushes this big"));
774 return (curl_off_t
)size
;
778 * If flush_received is true, do not attempt to read any more; just use what's
781 static int post_rpc(struct rpc_state
*rpc
, int flush_received
)
783 struct active_request_slot
*slot
;
784 struct curl_slist
*headers
= http_copy_default_headers();
785 int use_gzip
= rpc
->gzip_request
;
786 char *gzip_body
= NULL
;
787 size_t gzip_size
= 0;
788 int err
, large_request
= 0;
789 int needs_100_continue
= 0;
790 struct rpc_in_data rpc_in_data
;
792 /* Try to load the entire request, if we can fit it into the
793 * allocated buffer space we can use HTTP/1.0 and avoid the
794 * chunked encoding mess.
796 if (!flush_received
) {
799 enum packet_read_status status
;
801 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
806 if (status
== PACKET_READ_FLUSH
)
812 struct slot_results results
;
815 err
= probe_rpc(rpc
, &results
);
816 if (err
== HTTP_REAUTH
)
817 credential_fill(&http_auth
);
818 } while (err
== HTTP_REAUTH
);
822 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
823 needs_100_continue
= 1;
826 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
827 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
828 headers
= curl_slist_append(headers
, needs_100_continue
?
829 "Expect: 100-continue" : "Expect:");
831 /* Add the extra Git-Protocol header */
832 if (rpc
->protocol_header
)
833 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
836 slot
= get_active_slot();
838 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
839 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
840 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
841 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
844 /* The request body is large and the size cannot be predicted.
845 * We must use chunked encoding to send it.
847 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
848 rpc
->initial_buffer
= 1;
849 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
850 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
851 #ifndef NO_CURL_IOCTL
852 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
853 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
855 if (options
.verbosity
> 1) {
856 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
860 } else if (gzip_body
) {
862 * If we are looping to retry authentication, then the previous
863 * run will have set up the headers and gzip buffer already,
864 * and we just need to send it.
866 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
867 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
869 } else if (use_gzip
&& 1024 < rpc
->len
) {
870 /* The client backend isn't giving us compressed data so
871 * we can try to deflate it ourselves, this may save on
877 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
878 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
879 gzip_body
= xmalloc(gzip_size
);
881 stream
.next_in
= (unsigned char *)rpc
->buf
;
882 stream
.avail_in
= rpc
->len
;
883 stream
.next_out
= (unsigned char *)gzip_body
;
884 stream
.avail_out
= gzip_size
;
886 ret
= git_deflate(&stream
, Z_FINISH
);
887 if (ret
!= Z_STREAM_END
)
888 die(_("cannot deflate request; zlib deflate error %d"), ret
);
890 ret
= git_deflate_end_gently(&stream
);
892 die(_("cannot deflate request; zlib end error %d"), ret
);
894 gzip_size
= stream
.total_out
;
896 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
897 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
898 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
900 if (options
.verbosity
> 1) {
901 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
903 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
907 /* We know the complete request size in advance, use the
908 * more normal Content-Length approach.
910 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
911 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
912 if (options
.verbosity
> 1) {
913 fprintf(stderr
, "POST %s (%lu bytes)\n",
914 rpc
->service_name
, (unsigned long)rpc
->len
);
919 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
920 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
921 rpc_in_data
.rpc
= rpc
;
922 rpc_in_data
.slot
= slot
;
923 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
924 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
927 rpc
->any_written
= 0;
928 err
= run_slot(slot
, NULL
);
929 if (err
== HTTP_REAUTH
&& !large_request
) {
930 credential_fill(&http_auth
);
936 if (!rpc
->any_written
)
939 curl_slist_free_all(headers
);
944 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
945 const char **client_argv
, const struct strbuf
*preamble
,
946 struct strbuf
*rpc_result
)
948 const char *svc
= rpc
->service_name
;
949 struct strbuf buf
= STRBUF_INIT
;
950 struct child_process client
= CHILD_PROCESS_INIT
;
956 client
.argv
= client_argv
;
957 if (start_command(&client
))
959 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
961 write_or_die(client
.in
, heads
->buf
, heads
->len
);
963 rpc
->alloc
= http_post_buffer
;
964 rpc
->buf
= xmalloc(rpc
->alloc
);
966 rpc
->out
= client
.out
;
968 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
969 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
971 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
972 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
974 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
975 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
977 if (get_protocol_http_header(heads
->version
, &buf
))
978 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
980 rpc
->protocol_header
= NULL
;
983 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
988 err
|= post_rpc(rpc
, 0);
994 strbuf_read(rpc_result
, client
.out
, 0);
998 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1005 err
|= finish_command(&client
);
1006 free(rpc
->service_url
);
1007 free(rpc
->hdr_content_type
);
1008 free(rpc
->hdr_accept
);
1009 free(rpc
->protocol_header
);
1011 strbuf_release(&buf
);
1015 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1017 struct walker
*walker
;
1021 ALLOC_ARRAY(targets
, nr_heads
);
1022 if (options
.depth
|| options
.deepen_since
)
1023 die(_("dumb http transport does not support shallow capabilities"));
1024 for (i
= 0; i
< nr_heads
; i
++)
1025 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1027 walker
= get_http_walker(url
.buf
);
1028 walker
->get_verbosely
= options
.verbosity
>= 3;
1029 walker
->get_progress
= options
.progress
;
1030 walker
->get_recover
= 0;
1031 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1032 walker_free(walker
);
1034 for (i
= 0; i
< nr_heads
; i
++)
1038 return ret
? error(_("fetch failed.")) : 0;
1041 static int fetch_git(struct discovery
*heads
,
1042 int nr_heads
, struct ref
**to_fetch
)
1044 struct rpc_state rpc
;
1045 struct strbuf preamble
= STRBUF_INIT
;
1047 struct argv_array args
= ARGV_ARRAY_INIT
;
1048 struct strbuf rpc_result
= STRBUF_INIT
;
1050 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
1051 "--stdin", "--lock-pack", NULL
);
1052 if (options
.followtags
)
1053 argv_array_push(&args
, "--include-tag");
1055 argv_array_push(&args
, "--thin");
1056 if (options
.verbosity
>= 3)
1057 argv_array_pushl(&args
, "-v", "-v", NULL
);
1058 if (options
.check_self_contained_and_connected
)
1059 argv_array_push(&args
, "--check-self-contained-and-connected");
1060 if (options
.cloning
)
1061 argv_array_push(&args
, "--cloning");
1062 if (options
.update_shallow
)
1063 argv_array_push(&args
, "--update-shallow");
1064 if (!options
.progress
)
1065 argv_array_push(&args
, "--no-progress");
1067 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
1068 if (options
.deepen_since
)
1069 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1070 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1071 argv_array_pushf(&args
, "--shallow-exclude=%s",
1072 options
.deepen_not
.items
[i
].string
);
1073 if (options
.deepen_relative
&& options
.depth
)
1074 argv_array_push(&args
, "--deepen-relative");
1075 if (options
.from_promisor
)
1076 argv_array_push(&args
, "--from-promisor");
1077 if (options
.no_dependents
)
1078 argv_array_push(&args
, "--no-dependents");
1080 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
1081 argv_array_push(&args
, url
.buf
);
1083 for (i
= 0; i
< nr_heads
; i
++) {
1084 struct ref
*ref
= to_fetch
[i
];
1086 die(_("cannot fetch by sha1 over smart http"));
1087 packet_buf_write(&preamble
, "%s %s\n",
1088 oid_to_hex(&ref
->old_oid
), ref
->name
);
1090 packet_buf_flush(&preamble
);
1092 memset(&rpc
, 0, sizeof(rpc
));
1093 rpc
.service_name
= "git-upload-pack",
1094 rpc
.gzip_request
= 1;
1096 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1098 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1099 strbuf_release(&rpc_result
);
1100 strbuf_release(&preamble
);
1101 argv_array_clear(&args
);
1105 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1107 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1109 return fetch_git(d
, nr_heads
, to_fetch
);
1111 return fetch_dumb(nr_heads
, to_fetch
);
1114 static void parse_fetch(struct strbuf
*buf
)
1116 struct ref
**to_fetch
= NULL
;
1117 struct ref
*list_head
= NULL
;
1118 struct ref
**list
= &list_head
;
1119 int alloc_heads
= 0, nr_heads
= 0;
1123 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1126 struct object_id old_oid
;
1129 if (parse_oid_hex(p
, &old_oid
, &q
))
1130 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1136 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1138 ref
= alloc_ref(name
);
1139 oidcpy(&ref
->old_oid
, &old_oid
);
1144 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1145 to_fetch
[nr_heads
++] = ref
;
1148 die(_("http transport does not support %s"), buf
->buf
);
1151 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1157 if (fetch(nr_heads
, to_fetch
))
1158 exit(128); /* error already reported */
1159 free_refs(list_head
);
1167 static int push_dav(int nr_spec
, const char **specs
)
1169 struct child_process child
= CHILD_PROCESS_INIT
;
1173 argv_array_push(&child
.args
, "http-push");
1174 argv_array_push(&child
.args
, "--helper-status");
1175 if (options
.dry_run
)
1176 argv_array_push(&child
.args
, "--dry-run");
1177 if (options
.verbosity
> 1)
1178 argv_array_push(&child
.args
, "--verbose");
1179 argv_array_push(&child
.args
, url
.buf
);
1180 for (i
= 0; i
< nr_spec
; i
++)
1181 argv_array_push(&child
.args
, specs
[i
]);
1183 if (run_command(&child
))
1184 die(_("git-http-push failed"));
1188 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1190 struct rpc_state rpc
;
1192 struct argv_array args
;
1193 struct string_list_item
*cas_option
;
1194 struct strbuf preamble
= STRBUF_INIT
;
1195 struct strbuf rpc_result
= STRBUF_INIT
;
1197 argv_array_init(&args
);
1198 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1202 argv_array_push(&args
, "--thin");
1203 if (options
.dry_run
)
1204 argv_array_push(&args
, "--dry-run");
1205 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1206 argv_array_push(&args
, "--signed=yes");
1207 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1208 argv_array_push(&args
, "--signed=if-asked");
1210 argv_array_push(&args
, "--atomic");
1211 if (options
.verbosity
== 0)
1212 argv_array_push(&args
, "--quiet");
1213 else if (options
.verbosity
> 1)
1214 argv_array_push(&args
, "--verbose");
1215 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1216 argv_array_pushf(&args
, "--push-option=%s",
1217 options
.push_options
.items
[i
].string
);
1218 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
1219 for_each_string_list_item(cas_option
, &cas_options
)
1220 argv_array_push(&args
, cas_option
->string
);
1221 argv_array_push(&args
, url
.buf
);
1223 argv_array_push(&args
, "--stdin");
1224 for (i
= 0; i
< nr_spec
; i
++)
1225 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1226 packet_buf_flush(&preamble
);
1228 memset(&rpc
, 0, sizeof(rpc
));
1229 rpc
.service_name
= "git-receive-pack",
1231 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1233 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1234 strbuf_release(&rpc_result
);
1235 strbuf_release(&preamble
);
1236 argv_array_clear(&args
);
1240 static int push(int nr_spec
, const char **specs
)
1242 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1245 if (heads
->proto_git
)
1246 ret
= push_git(heads
, nr_spec
, specs
);
1248 ret
= push_dav(nr_spec
, specs
);
1249 free_discovery(heads
);
1253 static void parse_push(struct strbuf
*buf
)
1255 struct argv_array specs
= ARGV_ARRAY_INIT
;
1260 if (skip_prefix(buf
->buf
, "push ", &arg
))
1261 argv_array_push(&specs
, arg
);
1263 die(_("http transport does not support %s"), buf
->buf
);
1266 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1272 ret
= push(specs
.argc
, specs
.argv
);
1277 exit(128); /* error already reported */
1280 argv_array_clear(&specs
);
1283 static int stateless_connect(const char *service_name
)
1285 struct discovery
*discover
;
1286 struct rpc_state rpc
;
1287 struct strbuf buf
= STRBUF_INIT
;
1290 * Run the info/refs request and see if the server supports protocol
1291 * v2. If and only if the server supports v2 can we successfully
1292 * establish a stateless connection, otherwise we need to tell the
1293 * client to fallback to using other transport helper functions to
1294 * complete their request.
1296 discover
= discover_refs(service_name
, 0);
1297 if (discover
->version
!= protocol_v2
) {
1298 printf("fallback\n");
1302 /* Stateless Connection established */
1307 rpc
.service_name
= service_name
;
1308 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1309 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1310 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1311 if (get_protocol_http_header(discover
->version
, &buf
)) {
1312 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1314 rpc
.protocol_header
= NULL
;
1315 strbuf_release(&buf
);
1317 rpc
.buf
= xmalloc(http_post_buffer
);
1318 rpc
.alloc
= http_post_buffer
;
1323 rpc
.any_written
= 0;
1324 rpc
.gzip_request
= 1;
1325 rpc
.initial_buffer
= 0;
1326 rpc
.write_line_lengths
= 1;
1327 rpc
.flush_read_but_not_sent
= 0;
1330 * Dump the capability listing that we got from the server earlier
1331 * during the info/refs request.
1333 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1335 /* Until we see EOF keep sending POSTs */
1338 enum packet_read_status status
;
1340 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1342 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1343 if (status
== PACKET_READ_EOF
)
1345 if (post_rpc(&rpc
, status
== PACKET_READ_FLUSH
))
1346 /* We would have an err here */
1348 /* Reset the buffer for next request */
1352 free(rpc
.service_url
);
1353 free(rpc
.hdr_content_type
);
1354 free(rpc
.hdr_accept
);
1355 free(rpc
.protocol_header
);
1357 strbuf_release(&buf
);
1362 int cmd_main(int argc
, const char **argv
)
1364 struct strbuf buf
= STRBUF_INIT
;
1367 setup_git_directory_gently(&nongit
);
1369 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1373 options
.verbosity
= 1;
1374 options
.progress
= !!isatty(2);
1376 string_list_init(&options
.deepen_not
, 1);
1377 string_list_init(&options
.push_options
, 1);
1380 * Just report "remote-curl" here (folding all the various aliases
1381 * ("git-remote-http", "git-remote-https", and etc.) here since they
1382 * are all just copies of the same actual executable.
1384 trace2_cmd_name("remote-curl");
1386 remote
= remote_get(argv
[1]);
1389 end_url_with_slash(&url
, argv
[2]);
1391 end_url_with_slash(&url
, remote
->url
[0]);
1394 http_init(remote
, url
.buf
, 0);
1399 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1401 error(_("remote-curl: error reading command stream from git"));
1406 if (starts_with(buf
.buf
, "fetch ")) {
1408 die(_("remote-curl: fetch attempted without a local repo"));
1411 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1412 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1413 output_refs(get_refs(for_push
));
1415 } else if (starts_with(buf
.buf
, "push ")) {
1418 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1419 char *value
= strchr(arg
, ' ');
1427 result
= set_option(arg
, value
);
1430 else if (result
< 0)
1431 printf("error invalid value\n");
1433 printf("unsupported\n");
1436 } else if (!strcmp(buf
.buf
, "capabilities")) {
1437 printf("stateless-connect\n");
1441 printf("check-connectivity\n");
1444 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1445 if (!stateless_connect(arg
))
1448 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);