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 const struct git_hash_algo
*hash_algo
;
48 static struct options options
;
49 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
51 static int set_option(const char *name
, const char *value
)
53 if (!strcmp(name
, "verbosity")) {
55 int v
= strtol(value
, &end
, 10);
56 if (value
== end
|| *end
)
58 options
.verbosity
= v
;
61 else if (!strcmp(name
, "progress")) {
62 if (!strcmp(value
, "true"))
64 else if (!strcmp(value
, "false"))
70 else if (!strcmp(name
, "depth")) {
72 unsigned long v
= strtoul(value
, &end
, 10);
73 if (value
== end
|| *end
)
78 else if (!strcmp(name
, "deepen-since")) {
79 options
.deepen_since
= xstrdup(value
);
82 else if (!strcmp(name
, "deepen-not")) {
83 string_list_append(&options
.deepen_not
, value
);
86 else if (!strcmp(name
, "deepen-relative")) {
87 if (!strcmp(value
, "true"))
88 options
.deepen_relative
= 1;
89 else if (!strcmp(value
, "false"))
90 options
.deepen_relative
= 0;
95 else if (!strcmp(name
, "followtags")) {
96 if (!strcmp(value
, "true"))
97 options
.followtags
= 1;
98 else if (!strcmp(value
, "false"))
99 options
.followtags
= 0;
104 else if (!strcmp(name
, "dry-run")) {
105 if (!strcmp(value
, "true"))
107 else if (!strcmp(value
, "false"))
113 else if (!strcmp(name
, "check-connectivity")) {
114 if (!strcmp(value
, "true"))
115 options
.check_self_contained_and_connected
= 1;
116 else if (!strcmp(value
, "false"))
117 options
.check_self_contained_and_connected
= 0;
122 else if (!strcmp(name
, "cas")) {
123 struct strbuf val
= STRBUF_INIT
;
124 strbuf_addf(&val
, "--" CAS_OPT_NAME
"=%s", value
);
125 string_list_append(&cas_options
, val
.buf
);
126 strbuf_release(&val
);
128 } else if (!strcmp(name
, "cloning")) {
129 if (!strcmp(value
, "true"))
131 else if (!strcmp(value
, "false"))
136 } else if (!strcmp(name
, "update-shallow")) {
137 if (!strcmp(value
, "true"))
138 options
.update_shallow
= 1;
139 else if (!strcmp(value
, "false"))
140 options
.update_shallow
= 0;
144 } else if (!strcmp(name
, "pushcert")) {
145 if (!strcmp(value
, "true"))
146 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
147 else if (!strcmp(value
, "false"))
148 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
149 else if (!strcmp(value
, "if-asked"))
150 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
154 } else if (!strcmp(name
, "atomic")) {
155 if (!strcmp(value
, "true"))
157 else if (!strcmp(value
, "false"))
162 } else if (!strcmp(name
, "push-option")) {
164 string_list_append(&options
.push_options
, value
);
166 struct strbuf unquoted
= STRBUF_INIT
;
167 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
168 die(_("invalid quoting in push-option value: '%s'"), value
);
169 string_list_append_nodup(&options
.push_options
,
170 strbuf_detach(&unquoted
, NULL
));
174 #if LIBCURL_VERSION_NUM >= 0x070a08
175 } else if (!strcmp(name
, "family")) {
176 if (!strcmp(value
, "ipv4"))
177 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
178 else if (!strcmp(value
, "ipv6"))
179 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
180 else if (!strcmp(value
, "all"))
181 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
185 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
186 } else if (!strcmp(name
, "from-promisor")) {
187 options
.from_promisor
= 1;
189 } else if (!strcmp(name
, "no-dependents")) {
190 options
.no_dependents
= 1;
192 } else if (!strcmp(name
, "filter")) {
193 options
.filter
= xstrdup(value
);
195 } else if (!strcmp(name
, "object-format")) {
197 options
.object_format
= 1;
198 if (strcmp(value
, "true")) {
199 algo
= hash_algo_by_name(value
);
200 if (algo
== GIT_HASH_UNKNOWN
)
201 die("unknown object format '%s'", value
);
202 options
.hash_algo
= &hash_algos
[algo
];
206 return 1 /* unsupported */;
216 struct oid_array shallow
;
217 enum protocol_version version
;
218 unsigned proto_git
: 1;
220 static struct discovery
*last_discovery
;
222 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
224 struct ref
*list
= NULL
;
225 struct packet_reader reader
;
227 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
228 PACKET_READ_CHOMP_NEWLINE
|
229 PACKET_READ_GENTLE_ON_EOF
|
230 PACKET_READ_DIE_ON_ERR_PACKET
);
232 heads
->version
= discover_version(&reader
);
233 switch (heads
->version
) {
236 * Do nothing. This isn't a list of refs but rather a
237 * capability advertisement. Client would have run
238 * 'stateless-connect' so we'll dump this capability listing
239 * and let them request the refs themselves.
244 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
245 NULL
, &heads
->shallow
);
246 options
.hash_algo
= reader
.hash_algo
;
248 case protocol_unknown_version
:
249 BUG("unknown protocol version");
255 static struct ref
*parse_info_refs(struct discovery
*heads
)
257 char *data
, *start
, *mid
;
261 struct ref
*refs
= NULL
;
262 struct ref
*ref
= NULL
;
263 struct ref
*last_ref
= NULL
;
268 while (i
< heads
->len
) {
274 if (data
[i
] == '\n') {
275 if (mid
- start
!= the_hash_algo
->hexsz
)
276 die(_("%sinfo/refs not valid: is this a git repository?"),
277 transport_anonymize_url(url
.buf
));
280 ref
= alloc_ref(ref_name
);
281 get_oid_hex(start
, &ref
->old_oid
);
285 last_ref
->next
= ref
;
292 ref
= alloc_ref("HEAD");
293 if (!http_fetch_ref(url
.buf
, ref
) &&
294 !resolve_remote_symref(ref
, refs
)) {
304 static void free_discovery(struct discovery
*d
)
307 if (d
== last_discovery
)
308 last_discovery
= NULL
;
309 free(d
->shallow
.oid
);
317 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
323 * We only show text/plain parts, as other types are likely
324 * to be ugly to look at on the user's terminal.
326 if (strcmp(type
->buf
, "text/plain"))
329 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
337 eol
= strchrnul(p
, '\n');
338 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
344 static int get_protocol_http_header(enum protocol_version version
,
345 struct strbuf
*header
)
348 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
357 static void check_smart_http(struct discovery
*d
, const char *service
,
361 struct packet_reader reader
;
364 * If we don't see x-$service-advertisement, then it's not smart-http.
365 * But once we do, we commit to it and assume any other protocol
366 * violations are hard errors.
368 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
369 !skip_prefix(p
, service
, &p
) ||
370 strcmp(p
, "-advertisement"))
373 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
374 PACKET_READ_CHOMP_NEWLINE
|
375 PACKET_READ_DIE_ON_ERR_PACKET
);
376 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
377 die(_("invalid server response; expected service, got flush packet"));
379 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
381 * The header can include additional metadata lines, up
382 * until a packet flush marker. Ignore these now, but
383 * in the future we might start to scan them.
386 packet_reader_read(&reader
);
387 if (reader
.pktlen
<= 0) {
393 * v0 smart http; callers expect us to soak up the
394 * service and header packets
396 d
->buf
= reader
.src_buffer
;
397 d
->len
= reader
.src_len
;
400 } else if (!strcmp(reader
.line
, "version 2")) {
402 * v2 smart http; do not consume version packet, which will
403 * be handled elsewhere.
408 die(_("invalid server response; got '%s'"), reader
.line
);
412 static struct discovery
*discover_refs(const char *service
, int for_push
)
414 struct strbuf type
= STRBUF_INIT
;
415 struct strbuf charset
= STRBUF_INIT
;
416 struct strbuf buffer
= STRBUF_INIT
;
417 struct strbuf refs_url
= STRBUF_INIT
;
418 struct strbuf effective_url
= STRBUF_INIT
;
419 struct strbuf protocol_header
= STRBUF_INIT
;
420 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
421 struct discovery
*last
= last_discovery
;
422 int http_ret
, maybe_smart
= 0;
423 struct http_get_options http_options
;
424 enum protocol_version version
= get_protocol_version_config();
426 if (last
&& !strcmp(service
, last
->service
))
428 free_discovery(last
);
430 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
431 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
432 git_env_bool("GIT_SMART_HTTP", 1)) {
434 if (!strchr(url
.buf
, '?'))
435 strbuf_addch(&refs_url
, '?');
437 strbuf_addch(&refs_url
, '&');
438 strbuf_addf(&refs_url
, "service=%s", service
);
442 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
443 * to perform a push, then fallback to v0 since the client doesn't know
444 * how to push yet using v2.
446 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
447 version
= protocol_v0
;
449 /* Add the extra Git-Protocol header */
450 if (get_protocol_http_header(version
, &protocol_header
))
451 string_list_append(&extra_headers
, protocol_header
.buf
);
453 memset(&http_options
, 0, sizeof(http_options
));
454 http_options
.content_type
= &type
;
455 http_options
.charset
= &charset
;
456 http_options
.effective_url
= &effective_url
;
457 http_options
.base_url
= &url
;
458 http_options
.extra_headers
= &extra_headers
;
459 http_options
.initial_request
= 1;
460 http_options
.no_cache
= 1;
462 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
466 case HTTP_MISSING_TARGET
:
467 show_http_message(&type
, &charset
, &buffer
);
468 die(_("repository '%s' not found"),
469 transport_anonymize_url(url
.buf
));
471 show_http_message(&type
, &charset
, &buffer
);
472 die(_("Authentication failed for '%s'"),
473 transport_anonymize_url(url
.buf
));
475 show_http_message(&type
, &charset
, &buffer
);
476 die(_("unable to access '%s': %s"),
477 transport_anonymize_url(url
.buf
), curl_errorstr
);
480 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
481 char *u
= transport_anonymize_url(url
.buf
);
482 warning(_("redirecting to %s"), u
);
486 last
= xcalloc(1, sizeof(*last_discovery
));
487 last
->service
= xstrdup(service
);
488 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
489 last
->buf
= last
->buf_alloc
;
492 check_smart_http(last
, service
, &type
);
495 last
->refs
= parse_git_refs(last
, for_push
);
497 last
->refs
= parse_info_refs(last
);
499 strbuf_release(&refs_url
);
500 strbuf_release(&type
);
501 strbuf_release(&charset
);
502 strbuf_release(&effective_url
);
503 strbuf_release(&buffer
);
504 strbuf_release(&protocol_header
);
505 string_list_clear(&extra_headers
, 0);
506 last_discovery
= last
;
510 static struct ref
*get_refs(int for_push
)
512 struct discovery
*heads
;
515 heads
= discover_refs("git-receive-pack", for_push
);
517 heads
= discover_refs("git-upload-pack", for_push
);
522 static void output_refs(struct ref
*refs
)
525 if (options
.object_format
&& options
.hash_algo
) {
526 printf(":object-format %s\n", options
.hash_algo
->name
);
528 for (posn
= refs
; posn
; posn
= posn
->next
) {
530 printf("@%s %s\n", posn
->symref
, posn
->name
);
532 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
539 const char *service_name
;
541 char *hdr_content_type
;
543 char *protocol_header
;
551 unsigned gzip_request
: 1;
552 unsigned initial_buffer
: 1;
555 * Whenever a pkt-line is read into buf, append the 4 characters
556 * denoting its length before appending the payload.
558 unsigned write_line_lengths
: 1;
561 * Used by rpc_out; initialize to 0. This is true if a flush has been
562 * read, but the corresponding line length (if write_line_lengths is
563 * true) and EOF have not been sent to libcurl. Since each flush marks
564 * the end of a request, each flush must be completely sent before any
565 * further reading occurs.
567 unsigned flush_read_but_not_sent
: 1;
571 * Appends the result of reading from rpc->out to the string represented by
572 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
573 * enough space, 0 otherwise.
575 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
576 * hexadecimal string before appending the result described above.
578 * Writes the total number of bytes appended into appended.
580 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
582 enum packet_read_status
*status
) {
587 if (rpc
->write_line_lengths
) {
588 left
= rpc
->alloc
- rpc
->len
- 4;
589 buf
= rpc
->buf
+ rpc
->len
+ 4;
591 left
= rpc
->alloc
- rpc
->len
;
592 buf
= rpc
->buf
+ rpc
->len
;
595 if (left
< LARGE_PACKET_MAX
)
598 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
599 left
, &pktlen_raw
, options
);
600 if (*status
!= PACKET_READ_EOF
) {
601 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
602 rpc
->len
+= *appended
;
605 if (rpc
->write_line_lengths
) {
607 case PACKET_READ_EOF
:
608 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
609 die(_("shouldn't have EOF when not gentle on EOF"));
611 case PACKET_READ_NORMAL
:
612 set_packet_header(buf
- 4, *appended
);
614 case PACKET_READ_DELIM
:
615 memcpy(buf
- 4, "0001", 4);
617 case PACKET_READ_FLUSH
:
618 memcpy(buf
- 4, "0000", 4);
626 static size_t rpc_out(void *ptr
, size_t eltsize
,
627 size_t nmemb
, void *buffer_
)
629 size_t max
= eltsize
* nmemb
;
630 struct rpc_state
*rpc
= buffer_
;
631 size_t avail
= rpc
->len
- rpc
->pos
;
632 enum packet_read_status status
;
635 rpc
->initial_buffer
= 0;
638 if (!rpc
->flush_read_but_not_sent
) {
639 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
640 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
641 if (status
== PACKET_READ_FLUSH
)
642 rpc
->flush_read_but_not_sent
= 1;
645 * If flush_read_but_not_sent is true, we have already read one
646 * full request but have not fully sent it + EOF, which is why
647 * we need to refrain from reading.
650 if (rpc
->flush_read_but_not_sent
) {
653 * The line length either does not need to be sent at
654 * all or has already been completely sent. Now we can
655 * return 0, indicating EOF, meaning that the flush has
658 rpc
->flush_read_but_not_sent
= 0;
662 * If avail is non-zerp, the line length for the flush still
663 * hasn't been fully sent. Proceed with sending the line
670 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
675 #ifndef NO_CURL_IOCTL
676 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
678 struct rpc_state
*rpc
= clientp
;
684 case CURLIOCMD_RESTARTREAD
:
685 if (rpc
->initial_buffer
) {
689 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
690 return CURLIOE_FAILRESTART
;
693 return CURLIOE_UNKNOWNCMD
;
699 struct rpc_state
*rpc
;
700 struct active_request_slot
*slot
;
704 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
707 static size_t rpc_in(char *ptr
, size_t eltsize
,
708 size_t nmemb
, void *buffer_
)
710 size_t size
= eltsize
* nmemb
;
711 struct rpc_in_data
*data
= buffer_
;
714 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
715 &response_code
) != CURLE_OK
)
717 if (response_code
>= 300)
720 data
->rpc
->any_written
= 1;
721 write_or_die(data
->rpc
->in
, ptr
, size
);
725 static int run_slot(struct active_request_slot
*slot
,
726 struct slot_results
*results
)
729 struct slot_results results_buf
;
732 results
= &results_buf
;
734 err
= run_one_slot(slot
, results
);
736 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
737 struct strbuf msg
= STRBUF_INIT
;
738 if (results
->http_code
&& results
->http_code
!= 200)
739 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
740 if (results
->curl_result
!= CURLE_OK
) {
742 strbuf_addch(&msg
, ' ');
743 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
744 if (curl_errorstr
[0]) {
745 strbuf_addch(&msg
, ' ');
746 strbuf_addstr(&msg
, curl_errorstr
);
749 error(_("RPC failed; %s"), msg
.buf
);
750 strbuf_release(&msg
);
756 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
758 struct active_request_slot
*slot
;
759 struct curl_slist
*headers
= http_copy_default_headers();
760 struct strbuf buf
= STRBUF_INIT
;
763 slot
= get_active_slot();
765 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
766 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
768 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
769 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
770 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
771 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
772 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
773 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
774 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
775 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
776 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
778 err
= run_slot(slot
, results
);
780 curl_slist_free_all(headers
);
781 strbuf_release(&buf
);
785 static curl_off_t
xcurl_off_t(size_t len
)
787 uintmax_t size
= len
;
788 if (size
> maximum_signed_value_of_type(curl_off_t
))
789 die(_("cannot handle pushes this big"));
790 return (curl_off_t
)size
;
794 * If flush_received is true, do not attempt to read any more; just use what's
797 static int post_rpc(struct rpc_state
*rpc
, int flush_received
)
799 struct active_request_slot
*slot
;
800 struct curl_slist
*headers
= http_copy_default_headers();
801 int use_gzip
= rpc
->gzip_request
;
802 char *gzip_body
= NULL
;
803 size_t gzip_size
= 0;
804 int err
, large_request
= 0;
805 int needs_100_continue
= 0;
806 struct rpc_in_data rpc_in_data
;
808 /* Try to load the entire request, if we can fit it into the
809 * allocated buffer space we can use HTTP/1.0 and avoid the
810 * chunked encoding mess.
812 if (!flush_received
) {
815 enum packet_read_status status
;
817 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
822 if (status
== PACKET_READ_FLUSH
)
828 struct slot_results results
;
831 err
= probe_rpc(rpc
, &results
);
832 if (err
== HTTP_REAUTH
)
833 credential_fill(&http_auth
);
834 } while (err
== HTTP_REAUTH
);
838 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
839 needs_100_continue
= 1;
842 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
843 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
844 headers
= curl_slist_append(headers
, needs_100_continue
?
845 "Expect: 100-continue" : "Expect:");
847 /* Add the extra Git-Protocol header */
848 if (rpc
->protocol_header
)
849 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
852 slot
= get_active_slot();
854 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
855 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
856 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
857 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
860 /* The request body is large and the size cannot be predicted.
861 * We must use chunked encoding to send it.
863 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
864 rpc
->initial_buffer
= 1;
865 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
866 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
867 #ifndef NO_CURL_IOCTL
868 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
869 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
871 if (options
.verbosity
> 1) {
872 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
876 } else if (gzip_body
) {
878 * If we are looping to retry authentication, then the previous
879 * run will have set up the headers and gzip buffer already,
880 * and we just need to send it.
882 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
883 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
885 } else if (use_gzip
&& 1024 < rpc
->len
) {
886 /* The client backend isn't giving us compressed data so
887 * we can try to deflate it ourselves, this may save on
893 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
894 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
895 gzip_body
= xmalloc(gzip_size
);
897 stream
.next_in
= (unsigned char *)rpc
->buf
;
898 stream
.avail_in
= rpc
->len
;
899 stream
.next_out
= (unsigned char *)gzip_body
;
900 stream
.avail_out
= gzip_size
;
902 ret
= git_deflate(&stream
, Z_FINISH
);
903 if (ret
!= Z_STREAM_END
)
904 die(_("cannot deflate request; zlib deflate error %d"), ret
);
906 ret
= git_deflate_end_gently(&stream
);
908 die(_("cannot deflate request; zlib end error %d"), ret
);
910 gzip_size
= stream
.total_out
;
912 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
913 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
914 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
916 if (options
.verbosity
> 1) {
917 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
919 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
923 /* We know the complete request size in advance, use the
924 * more normal Content-Length approach.
926 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
927 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
928 if (options
.verbosity
> 1) {
929 fprintf(stderr
, "POST %s (%lu bytes)\n",
930 rpc
->service_name
, (unsigned long)rpc
->len
);
935 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
936 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
937 rpc_in_data
.rpc
= rpc
;
938 rpc_in_data
.slot
= slot
;
939 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
940 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
943 rpc
->any_written
= 0;
944 err
= run_slot(slot
, NULL
);
945 if (err
== HTTP_REAUTH
&& !large_request
) {
946 credential_fill(&http_auth
);
952 if (!rpc
->any_written
)
955 curl_slist_free_all(headers
);
960 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
961 const char **client_argv
, const struct strbuf
*preamble
,
962 struct strbuf
*rpc_result
)
964 const char *svc
= rpc
->service_name
;
965 struct strbuf buf
= STRBUF_INIT
;
966 struct child_process client
= CHILD_PROCESS_INIT
;
972 client
.argv
= client_argv
;
973 if (start_command(&client
))
975 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
977 write_or_die(client
.in
, heads
->buf
, heads
->len
);
979 rpc
->alloc
= http_post_buffer
;
980 rpc
->buf
= xmalloc(rpc
->alloc
);
982 rpc
->out
= client
.out
;
984 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
985 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
987 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
988 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
990 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
991 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
993 if (get_protocol_http_header(heads
->version
, &buf
))
994 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
996 rpc
->protocol_header
= NULL
;
999 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
1004 err
|= post_rpc(rpc
, 0);
1010 strbuf_read(rpc_result
, client
.out
, 0);
1014 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1021 err
|= finish_command(&client
);
1022 free(rpc
->service_url
);
1023 free(rpc
->hdr_content_type
);
1024 free(rpc
->hdr_accept
);
1025 free(rpc
->protocol_header
);
1027 strbuf_release(&buf
);
1031 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1033 struct walker
*walker
;
1037 ALLOC_ARRAY(targets
, nr_heads
);
1038 if (options
.depth
|| options
.deepen_since
)
1039 die(_("dumb http transport does not support shallow capabilities"));
1040 for (i
= 0; i
< nr_heads
; i
++)
1041 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1043 walker
= get_http_walker(url
.buf
);
1044 walker
->get_verbosely
= options
.verbosity
>= 3;
1045 walker
->get_progress
= options
.progress
;
1046 walker
->get_recover
= 0;
1047 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1048 walker_free(walker
);
1050 for (i
= 0; i
< nr_heads
; i
++)
1054 return ret
? error(_("fetch failed.")) : 0;
1057 static int fetch_git(struct discovery
*heads
,
1058 int nr_heads
, struct ref
**to_fetch
)
1060 struct rpc_state rpc
;
1061 struct strbuf preamble
= STRBUF_INIT
;
1063 struct argv_array args
= ARGV_ARRAY_INIT
;
1064 struct strbuf rpc_result
= STRBUF_INIT
;
1066 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
1067 "--stdin", "--lock-pack", NULL
);
1068 if (options
.followtags
)
1069 argv_array_push(&args
, "--include-tag");
1071 argv_array_push(&args
, "--thin");
1072 if (options
.verbosity
>= 3)
1073 argv_array_pushl(&args
, "-v", "-v", NULL
);
1074 if (options
.check_self_contained_and_connected
)
1075 argv_array_push(&args
, "--check-self-contained-and-connected");
1076 if (options
.cloning
)
1077 argv_array_push(&args
, "--cloning");
1078 if (options
.update_shallow
)
1079 argv_array_push(&args
, "--update-shallow");
1080 if (!options
.progress
)
1081 argv_array_push(&args
, "--no-progress");
1083 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
1084 if (options
.deepen_since
)
1085 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1086 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1087 argv_array_pushf(&args
, "--shallow-exclude=%s",
1088 options
.deepen_not
.items
[i
].string
);
1089 if (options
.deepen_relative
&& options
.depth
)
1090 argv_array_push(&args
, "--deepen-relative");
1091 if (options
.from_promisor
)
1092 argv_array_push(&args
, "--from-promisor");
1093 if (options
.no_dependents
)
1094 argv_array_push(&args
, "--no-dependents");
1096 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
1097 argv_array_push(&args
, url
.buf
);
1099 for (i
= 0; i
< nr_heads
; i
++) {
1100 struct ref
*ref
= to_fetch
[i
];
1102 die(_("cannot fetch by sha1 over smart http"));
1103 packet_buf_write(&preamble
, "%s %s\n",
1104 oid_to_hex(&ref
->old_oid
), ref
->name
);
1106 packet_buf_flush(&preamble
);
1108 memset(&rpc
, 0, sizeof(rpc
));
1109 rpc
.service_name
= "git-upload-pack",
1110 rpc
.gzip_request
= 1;
1112 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1114 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1115 strbuf_release(&rpc_result
);
1116 strbuf_release(&preamble
);
1117 argv_array_clear(&args
);
1121 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1123 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1125 return fetch_git(d
, nr_heads
, to_fetch
);
1127 return fetch_dumb(nr_heads
, to_fetch
);
1130 static void parse_fetch(struct strbuf
*buf
)
1132 struct ref
**to_fetch
= NULL
;
1133 struct ref
*list_head
= NULL
;
1134 struct ref
**list
= &list_head
;
1135 int alloc_heads
= 0, nr_heads
= 0;
1139 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1142 struct object_id old_oid
;
1145 if (parse_oid_hex(p
, &old_oid
, &q
))
1146 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1152 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1154 ref
= alloc_ref(name
);
1155 oidcpy(&ref
->old_oid
, &old_oid
);
1160 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1161 to_fetch
[nr_heads
++] = ref
;
1164 die(_("http transport does not support %s"), buf
->buf
);
1167 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1173 if (fetch(nr_heads
, to_fetch
))
1174 exit(128); /* error already reported */
1175 free_refs(list_head
);
1183 static int push_dav(int nr_spec
, const char **specs
)
1185 struct child_process child
= CHILD_PROCESS_INIT
;
1189 argv_array_push(&child
.args
, "http-push");
1190 argv_array_push(&child
.args
, "--helper-status");
1191 if (options
.dry_run
)
1192 argv_array_push(&child
.args
, "--dry-run");
1193 if (options
.verbosity
> 1)
1194 argv_array_push(&child
.args
, "--verbose");
1195 argv_array_push(&child
.args
, url
.buf
);
1196 for (i
= 0; i
< nr_spec
; i
++)
1197 argv_array_push(&child
.args
, specs
[i
]);
1199 if (run_command(&child
))
1200 die(_("git-http-push failed"));
1204 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1206 struct rpc_state rpc
;
1208 struct argv_array args
;
1209 struct string_list_item
*cas_option
;
1210 struct strbuf preamble
= STRBUF_INIT
;
1211 struct strbuf rpc_result
= STRBUF_INIT
;
1213 argv_array_init(&args
);
1214 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1218 argv_array_push(&args
, "--thin");
1219 if (options
.dry_run
)
1220 argv_array_push(&args
, "--dry-run");
1221 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1222 argv_array_push(&args
, "--signed=yes");
1223 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1224 argv_array_push(&args
, "--signed=if-asked");
1226 argv_array_push(&args
, "--atomic");
1227 if (options
.verbosity
== 0)
1228 argv_array_push(&args
, "--quiet");
1229 else if (options
.verbosity
> 1)
1230 argv_array_push(&args
, "--verbose");
1231 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1232 argv_array_pushf(&args
, "--push-option=%s",
1233 options
.push_options
.items
[i
].string
);
1234 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
1235 for_each_string_list_item(cas_option
, &cas_options
)
1236 argv_array_push(&args
, cas_option
->string
);
1237 argv_array_push(&args
, url
.buf
);
1239 argv_array_push(&args
, "--stdin");
1240 for (i
= 0; i
< nr_spec
; i
++)
1241 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1242 packet_buf_flush(&preamble
);
1244 memset(&rpc
, 0, sizeof(rpc
));
1245 rpc
.service_name
= "git-receive-pack",
1247 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1249 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1250 strbuf_release(&rpc_result
);
1251 strbuf_release(&preamble
);
1252 argv_array_clear(&args
);
1256 static int push(int nr_spec
, const char **specs
)
1258 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1261 if (heads
->proto_git
)
1262 ret
= push_git(heads
, nr_spec
, specs
);
1264 ret
= push_dav(nr_spec
, specs
);
1265 free_discovery(heads
);
1269 static void parse_push(struct strbuf
*buf
)
1271 struct argv_array specs
= ARGV_ARRAY_INIT
;
1276 if (skip_prefix(buf
->buf
, "push ", &arg
))
1277 argv_array_push(&specs
, arg
);
1279 die(_("http transport does not support %s"), buf
->buf
);
1282 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1288 ret
= push(specs
.argc
, specs
.argv
);
1293 exit(128); /* error already reported */
1296 argv_array_clear(&specs
);
1299 static int stateless_connect(const char *service_name
)
1301 struct discovery
*discover
;
1302 struct rpc_state rpc
;
1303 struct strbuf buf
= STRBUF_INIT
;
1306 * Run the info/refs request and see if the server supports protocol
1307 * v2. If and only if the server supports v2 can we successfully
1308 * establish a stateless connection, otherwise we need to tell the
1309 * client to fallback to using other transport helper functions to
1310 * complete their request.
1312 discover
= discover_refs(service_name
, 0);
1313 if (discover
->version
!= protocol_v2
) {
1314 printf("fallback\n");
1318 /* Stateless Connection established */
1323 rpc
.service_name
= service_name
;
1324 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1325 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1326 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1327 if (get_protocol_http_header(discover
->version
, &buf
)) {
1328 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1330 rpc
.protocol_header
= NULL
;
1331 strbuf_release(&buf
);
1333 rpc
.buf
= xmalloc(http_post_buffer
);
1334 rpc
.alloc
= http_post_buffer
;
1339 rpc
.any_written
= 0;
1340 rpc
.gzip_request
= 1;
1341 rpc
.initial_buffer
= 0;
1342 rpc
.write_line_lengths
= 1;
1343 rpc
.flush_read_but_not_sent
= 0;
1346 * Dump the capability listing that we got from the server earlier
1347 * during the info/refs request.
1349 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1351 /* Until we see EOF keep sending POSTs */
1354 enum packet_read_status status
;
1356 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1358 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1359 if (status
== PACKET_READ_EOF
)
1361 if (post_rpc(&rpc
, status
== PACKET_READ_FLUSH
))
1362 /* We would have an err here */
1364 /* Reset the buffer for next request */
1368 free(rpc
.service_url
);
1369 free(rpc
.hdr_content_type
);
1370 free(rpc
.hdr_accept
);
1371 free(rpc
.protocol_header
);
1373 strbuf_release(&buf
);
1378 int cmd_main(int argc
, const char **argv
)
1380 struct strbuf buf
= STRBUF_INIT
;
1383 setup_git_directory_gently(&nongit
);
1385 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1389 options
.verbosity
= 1;
1390 options
.progress
= !!isatty(2);
1392 string_list_init(&options
.deepen_not
, 1);
1393 string_list_init(&options
.push_options
, 1);
1396 * Just report "remote-curl" here (folding all the various aliases
1397 * ("git-remote-http", "git-remote-https", and etc.) here since they
1398 * are all just copies of the same actual executable.
1400 trace2_cmd_name("remote-curl");
1402 remote
= remote_get(argv
[1]);
1405 end_url_with_slash(&url
, argv
[2]);
1407 end_url_with_slash(&url
, remote
->url
[0]);
1410 http_init(remote
, url
.buf
, 0);
1415 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1417 error(_("remote-curl: error reading command stream from git"));
1422 if (starts_with(buf
.buf
, "fetch ")) {
1424 die(_("remote-curl: fetch attempted without a local repo"));
1427 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1428 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1429 output_refs(get_refs(for_push
));
1431 } else if (starts_with(buf
.buf
, "push ")) {
1434 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1435 char *value
= strchr(arg
, ' ');
1443 result
= set_option(arg
, value
);
1446 else if (result
< 0)
1447 printf("error invalid value\n");
1449 printf("unsupported\n");
1452 } else if (!strcmp(buf
.buf
, "capabilities")) {
1453 printf("stateless-connect\n");
1457 printf("check-connectivity\n");
1458 printf("object-format\n");
1461 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1462 if (!stateless_connect(arg
))
1465 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);