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 const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
257 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
260 return the_hash_algo
;
262 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
263 if (algo
== GIT_HASH_UNKNOWN
)
265 return &hash_algos
[algo
];
268 static struct ref
*parse_info_refs(struct discovery
*heads
)
270 char *data
, *start
, *mid
;
274 struct ref
*refs
= NULL
;
275 struct ref
*ref
= NULL
;
276 struct ref
*last_ref
= NULL
;
278 options
.hash_algo
= detect_hash_algo(heads
);
279 if (!options
.hash_algo
)
280 die("%sinfo/refs not valid: could not determine hash algorithm; "
281 "is this a git repository?",
282 transport_anonymize_url(url
.buf
));
287 while (i
< heads
->len
) {
293 if (data
[i
] == '\n') {
294 if (mid
- start
!= options
.hash_algo
->hexsz
)
295 die(_("%sinfo/refs not valid: is this a git repository?"),
296 transport_anonymize_url(url
.buf
));
299 ref
= alloc_ref(ref_name
);
300 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
304 last_ref
->next
= ref
;
311 ref
= alloc_ref("HEAD");
312 if (!http_fetch_ref(url
.buf
, ref
) &&
313 !resolve_remote_symref(ref
, refs
)) {
323 static void free_discovery(struct discovery
*d
)
326 if (d
== last_discovery
)
327 last_discovery
= NULL
;
328 free(d
->shallow
.oid
);
336 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
342 * We only show text/plain parts, as other types are likely
343 * to be ugly to look at on the user's terminal.
345 if (strcmp(type
->buf
, "text/plain"))
348 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
356 eol
= strchrnul(p
, '\n');
357 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
363 static int get_protocol_http_header(enum protocol_version version
,
364 struct strbuf
*header
)
367 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
376 static void check_smart_http(struct discovery
*d
, const char *service
,
380 struct packet_reader reader
;
383 * If we don't see x-$service-advertisement, then it's not smart-http.
384 * But once we do, we commit to it and assume any other protocol
385 * violations are hard errors.
387 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
388 !skip_prefix(p
, service
, &p
) ||
389 strcmp(p
, "-advertisement"))
392 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
393 PACKET_READ_CHOMP_NEWLINE
|
394 PACKET_READ_DIE_ON_ERR_PACKET
);
395 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
396 die(_("invalid server response; expected service, got flush packet"));
398 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
400 * The header can include additional metadata lines, up
401 * until a packet flush marker. Ignore these now, but
402 * in the future we might start to scan them.
405 packet_reader_read(&reader
);
406 if (reader
.pktlen
<= 0) {
412 * v0 smart http; callers expect us to soak up the
413 * service and header packets
415 d
->buf
= reader
.src_buffer
;
416 d
->len
= reader
.src_len
;
419 } else if (!strcmp(reader
.line
, "version 2")) {
421 * v2 smart http; do not consume version packet, which will
422 * be handled elsewhere.
427 die(_("invalid server response; got '%s'"), reader
.line
);
431 static struct discovery
*discover_refs(const char *service
, int for_push
)
433 struct strbuf type
= STRBUF_INIT
;
434 struct strbuf charset
= STRBUF_INIT
;
435 struct strbuf buffer
= STRBUF_INIT
;
436 struct strbuf refs_url
= STRBUF_INIT
;
437 struct strbuf effective_url
= STRBUF_INIT
;
438 struct strbuf protocol_header
= STRBUF_INIT
;
439 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
440 struct discovery
*last
= last_discovery
;
441 int http_ret
, maybe_smart
= 0;
442 struct http_get_options http_options
;
443 enum protocol_version version
= get_protocol_version_config();
445 if (last
&& !strcmp(service
, last
->service
))
447 free_discovery(last
);
449 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
450 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
451 git_env_bool("GIT_SMART_HTTP", 1)) {
453 if (!strchr(url
.buf
, '?'))
454 strbuf_addch(&refs_url
, '?');
456 strbuf_addch(&refs_url
, '&');
457 strbuf_addf(&refs_url
, "service=%s", service
);
461 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
462 * to perform a push, then fallback to v0 since the client doesn't know
463 * how to push yet using v2.
465 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
466 version
= protocol_v0
;
468 /* Add the extra Git-Protocol header */
469 if (get_protocol_http_header(version
, &protocol_header
))
470 string_list_append(&extra_headers
, protocol_header
.buf
);
472 memset(&http_options
, 0, sizeof(http_options
));
473 http_options
.content_type
= &type
;
474 http_options
.charset
= &charset
;
475 http_options
.effective_url
= &effective_url
;
476 http_options
.base_url
= &url
;
477 http_options
.extra_headers
= &extra_headers
;
478 http_options
.initial_request
= 1;
479 http_options
.no_cache
= 1;
481 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
485 case HTTP_MISSING_TARGET
:
486 show_http_message(&type
, &charset
, &buffer
);
487 die(_("repository '%s' not found"),
488 transport_anonymize_url(url
.buf
));
490 show_http_message(&type
, &charset
, &buffer
);
491 die(_("Authentication failed for '%s'"),
492 transport_anonymize_url(url
.buf
));
494 show_http_message(&type
, &charset
, &buffer
);
495 die(_("unable to access '%s': %s"),
496 transport_anonymize_url(url
.buf
), curl_errorstr
);
499 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
500 char *u
= transport_anonymize_url(url
.buf
);
501 warning(_("redirecting to %s"), u
);
505 last
= xcalloc(1, sizeof(*last_discovery
));
506 last
->service
= xstrdup(service
);
507 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
508 last
->buf
= last
->buf_alloc
;
511 check_smart_http(last
, service
, &type
);
514 last
->refs
= parse_git_refs(last
, for_push
);
516 last
->refs
= parse_info_refs(last
);
518 strbuf_release(&refs_url
);
519 strbuf_release(&type
);
520 strbuf_release(&charset
);
521 strbuf_release(&effective_url
);
522 strbuf_release(&buffer
);
523 strbuf_release(&protocol_header
);
524 string_list_clear(&extra_headers
, 0);
525 last_discovery
= last
;
529 static struct ref
*get_refs(int for_push
)
531 struct discovery
*heads
;
534 heads
= discover_refs("git-receive-pack", for_push
);
536 heads
= discover_refs("git-upload-pack", for_push
);
541 static void output_refs(struct ref
*refs
)
544 if (options
.object_format
&& options
.hash_algo
) {
545 printf(":object-format %s\n", options
.hash_algo
->name
);
547 for (posn
= refs
; posn
; posn
= posn
->next
) {
549 printf("@%s %s\n", posn
->symref
, posn
->name
);
551 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
560 const char *service_name
;
562 char *hdr_content_type
;
564 char *protocol_header
;
572 unsigned gzip_request
: 1;
573 unsigned initial_buffer
: 1;
576 * Whenever a pkt-line is read into buf, append the 4 characters
577 * denoting its length before appending the payload.
579 unsigned write_line_lengths
: 1;
582 * Used by rpc_out; initialize to 0. This is true if a flush has been
583 * read, but the corresponding line length (if write_line_lengths is
584 * true) and EOF have not been sent to libcurl. Since each flush marks
585 * the end of a request, each flush must be completely sent before any
586 * further reading occurs.
588 unsigned flush_read_but_not_sent
: 1;
592 * Appends the result of reading from rpc->out to the string represented by
593 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
594 * enough space, 0 otherwise.
596 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
597 * hexadecimal string before appending the result described above.
599 * Writes the total number of bytes appended into appended.
601 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
603 enum packet_read_status
*status
) {
608 if (rpc
->write_line_lengths
) {
609 left
= rpc
->alloc
- rpc
->len
- 4;
610 buf
= rpc
->buf
+ rpc
->len
+ 4;
612 left
= rpc
->alloc
- rpc
->len
;
613 buf
= rpc
->buf
+ rpc
->len
;
616 if (left
< LARGE_PACKET_MAX
)
619 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
620 left
, &pktlen_raw
, options
);
621 if (*status
!= PACKET_READ_EOF
) {
622 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
623 rpc
->len
+= *appended
;
626 if (rpc
->write_line_lengths
) {
628 case PACKET_READ_EOF
:
629 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
630 die(_("shouldn't have EOF when not gentle on EOF"));
632 case PACKET_READ_NORMAL
:
633 set_packet_header(buf
- 4, *appended
);
635 case PACKET_READ_DELIM
:
636 memcpy(buf
- 4, "0001", 4);
638 case PACKET_READ_FLUSH
:
639 memcpy(buf
- 4, "0000", 4);
647 static size_t rpc_out(void *ptr
, size_t eltsize
,
648 size_t nmemb
, void *buffer_
)
650 size_t max
= eltsize
* nmemb
;
651 struct rpc_state
*rpc
= buffer_
;
652 size_t avail
= rpc
->len
- rpc
->pos
;
653 enum packet_read_status status
;
656 rpc
->initial_buffer
= 0;
659 if (!rpc
->flush_read_but_not_sent
) {
660 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
661 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
662 if (status
== PACKET_READ_FLUSH
)
663 rpc
->flush_read_but_not_sent
= 1;
666 * If flush_read_but_not_sent is true, we have already read one
667 * full request but have not fully sent it + EOF, which is why
668 * we need to refrain from reading.
671 if (rpc
->flush_read_but_not_sent
) {
674 * The line length either does not need to be sent at
675 * all or has already been completely sent. Now we can
676 * return 0, indicating EOF, meaning that the flush has
679 rpc
->flush_read_but_not_sent
= 0;
683 * If avail is non-zerp, the line length for the flush still
684 * hasn't been fully sent. Proceed with sending the line
691 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
696 #ifndef NO_CURL_IOCTL
697 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
699 struct rpc_state
*rpc
= clientp
;
705 case CURLIOCMD_RESTARTREAD
:
706 if (rpc
->initial_buffer
) {
710 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
711 return CURLIOE_FAILRESTART
;
714 return CURLIOE_UNKNOWNCMD
;
720 struct rpc_state
*rpc
;
721 struct active_request_slot
*slot
;
725 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
728 static size_t rpc_in(char *ptr
, size_t eltsize
,
729 size_t nmemb
, void *buffer_
)
731 size_t size
= eltsize
* nmemb
;
732 struct rpc_in_data
*data
= buffer_
;
735 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
736 &response_code
) != CURLE_OK
)
738 if (response_code
>= 300)
741 data
->rpc
->any_written
= 1;
742 write_or_die(data
->rpc
->in
, ptr
, size
);
746 static int run_slot(struct active_request_slot
*slot
,
747 struct slot_results
*results
)
750 struct slot_results results_buf
;
753 results
= &results_buf
;
755 err
= run_one_slot(slot
, results
);
757 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
758 struct strbuf msg
= STRBUF_INIT
;
759 if (results
->http_code
&& results
->http_code
!= 200)
760 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
761 if (results
->curl_result
!= CURLE_OK
) {
763 strbuf_addch(&msg
, ' ');
764 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
765 if (curl_errorstr
[0]) {
766 strbuf_addch(&msg
, ' ');
767 strbuf_addstr(&msg
, curl_errorstr
);
770 error(_("RPC failed; %s"), msg
.buf
);
771 strbuf_release(&msg
);
777 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
779 struct active_request_slot
*slot
;
780 struct curl_slist
*headers
= http_copy_default_headers();
781 struct strbuf buf
= STRBUF_INIT
;
784 slot
= get_active_slot();
786 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
787 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
789 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
790 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
791 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
792 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
793 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
794 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
795 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
796 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
797 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
799 err
= run_slot(slot
, results
);
801 curl_slist_free_all(headers
);
802 strbuf_release(&buf
);
806 static curl_off_t
xcurl_off_t(size_t len
)
808 uintmax_t size
= len
;
809 if (size
> maximum_signed_value_of_type(curl_off_t
))
810 die(_("cannot handle pushes this big"));
811 return (curl_off_t
)size
;
815 * If flush_received is true, do not attempt to read any more; just use what's
818 static int post_rpc(struct rpc_state
*rpc
, int flush_received
)
820 struct active_request_slot
*slot
;
821 struct curl_slist
*headers
= http_copy_default_headers();
822 int use_gzip
= rpc
->gzip_request
;
823 char *gzip_body
= NULL
;
824 size_t gzip_size
= 0;
825 int err
, large_request
= 0;
826 int needs_100_continue
= 0;
827 struct rpc_in_data rpc_in_data
;
829 /* Try to load the entire request, if we can fit it into the
830 * allocated buffer space we can use HTTP/1.0 and avoid the
831 * chunked encoding mess.
833 if (!flush_received
) {
836 enum packet_read_status status
;
838 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
843 if (status
== PACKET_READ_FLUSH
)
849 struct slot_results results
;
852 err
= probe_rpc(rpc
, &results
);
853 if (err
== HTTP_REAUTH
)
854 credential_fill(&http_auth
);
855 } while (err
== HTTP_REAUTH
);
859 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
860 needs_100_continue
= 1;
863 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
864 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
865 headers
= curl_slist_append(headers
, needs_100_continue
?
866 "Expect: 100-continue" : "Expect:");
868 /* Add the extra Git-Protocol header */
869 if (rpc
->protocol_header
)
870 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
873 slot
= get_active_slot();
875 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
876 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
877 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
878 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
881 /* The request body is large and the size cannot be predicted.
882 * We must use chunked encoding to send it.
884 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
885 rpc
->initial_buffer
= 1;
886 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
887 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
888 #ifndef NO_CURL_IOCTL
889 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
890 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
892 if (options
.verbosity
> 1) {
893 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
897 } else if (gzip_body
) {
899 * If we are looping to retry authentication, then the previous
900 * run will have set up the headers and gzip buffer already,
901 * and we just need to send it.
903 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
904 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
906 } else if (use_gzip
&& 1024 < rpc
->len
) {
907 /* The client backend isn't giving us compressed data so
908 * we can try to deflate it ourselves, this may save on
914 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
915 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
916 gzip_body
= xmalloc(gzip_size
);
918 stream
.next_in
= (unsigned char *)rpc
->buf
;
919 stream
.avail_in
= rpc
->len
;
920 stream
.next_out
= (unsigned char *)gzip_body
;
921 stream
.avail_out
= gzip_size
;
923 ret
= git_deflate(&stream
, Z_FINISH
);
924 if (ret
!= Z_STREAM_END
)
925 die(_("cannot deflate request; zlib deflate error %d"), ret
);
927 ret
= git_deflate_end_gently(&stream
);
929 die(_("cannot deflate request; zlib end error %d"), ret
);
931 gzip_size
= stream
.total_out
;
933 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
934 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
935 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
937 if (options
.verbosity
> 1) {
938 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
940 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
944 /* We know the complete request size in advance, use the
945 * more normal Content-Length approach.
947 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
948 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
949 if (options
.verbosity
> 1) {
950 fprintf(stderr
, "POST %s (%lu bytes)\n",
951 rpc
->service_name
, (unsigned long)rpc
->len
);
956 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
957 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
958 rpc_in_data
.rpc
= rpc
;
959 rpc_in_data
.slot
= slot
;
960 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
961 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
964 rpc
->any_written
= 0;
965 err
= run_slot(slot
, NULL
);
966 if (err
== HTTP_REAUTH
&& !large_request
) {
967 credential_fill(&http_auth
);
973 if (!rpc
->any_written
)
976 curl_slist_free_all(headers
);
981 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
982 const char **client_argv
, const struct strbuf
*preamble
,
983 struct strbuf
*rpc_result
)
985 const char *svc
= rpc
->service_name
;
986 struct strbuf buf
= STRBUF_INIT
;
987 struct child_process client
= CHILD_PROCESS_INIT
;
993 client
.argv
= client_argv
;
994 if (start_command(&client
))
996 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
998 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1000 rpc
->alloc
= http_post_buffer
;
1001 rpc
->buf
= xmalloc(rpc
->alloc
);
1002 rpc
->in
= client
.in
;
1003 rpc
->out
= client
.out
;
1005 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1006 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1008 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1009 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1011 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1012 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1014 if (get_protocol_http_header(heads
->version
, &buf
))
1015 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1017 rpc
->protocol_header
= NULL
;
1020 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
1025 err
|= post_rpc(rpc
, 0);
1031 strbuf_read(rpc_result
, client
.out
, 0);
1035 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1042 err
|= finish_command(&client
);
1043 free(rpc
->service_url
);
1044 free(rpc
->hdr_content_type
);
1045 free(rpc
->hdr_accept
);
1046 free(rpc
->protocol_header
);
1048 strbuf_release(&buf
);
1052 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1054 struct walker
*walker
;
1058 ALLOC_ARRAY(targets
, nr_heads
);
1059 if (options
.depth
|| options
.deepen_since
)
1060 die(_("dumb http transport does not support shallow capabilities"));
1061 for (i
= 0; i
< nr_heads
; i
++)
1062 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1064 walker
= get_http_walker(url
.buf
);
1065 walker
->get_verbosely
= options
.verbosity
>= 3;
1066 walker
->get_progress
= options
.progress
;
1067 walker
->get_recover
= 0;
1068 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1069 walker_free(walker
);
1071 for (i
= 0; i
< nr_heads
; i
++)
1075 return ret
? error(_("fetch failed.")) : 0;
1078 static int fetch_git(struct discovery
*heads
,
1079 int nr_heads
, struct ref
**to_fetch
)
1081 struct rpc_state rpc
;
1082 struct strbuf preamble
= STRBUF_INIT
;
1084 struct argv_array args
= ARGV_ARRAY_INIT
;
1085 struct strbuf rpc_result
= STRBUF_INIT
;
1087 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
1088 "--stdin", "--lock-pack", NULL
);
1089 if (options
.followtags
)
1090 argv_array_push(&args
, "--include-tag");
1092 argv_array_push(&args
, "--thin");
1093 if (options
.verbosity
>= 3)
1094 argv_array_pushl(&args
, "-v", "-v", NULL
);
1095 if (options
.check_self_contained_and_connected
)
1096 argv_array_push(&args
, "--check-self-contained-and-connected");
1097 if (options
.cloning
)
1098 argv_array_push(&args
, "--cloning");
1099 if (options
.update_shallow
)
1100 argv_array_push(&args
, "--update-shallow");
1101 if (!options
.progress
)
1102 argv_array_push(&args
, "--no-progress");
1104 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
1105 if (options
.deepen_since
)
1106 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1107 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1108 argv_array_pushf(&args
, "--shallow-exclude=%s",
1109 options
.deepen_not
.items
[i
].string
);
1110 if (options
.deepen_relative
&& options
.depth
)
1111 argv_array_push(&args
, "--deepen-relative");
1112 if (options
.from_promisor
)
1113 argv_array_push(&args
, "--from-promisor");
1114 if (options
.no_dependents
)
1115 argv_array_push(&args
, "--no-dependents");
1117 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
1118 argv_array_push(&args
, url
.buf
);
1120 for (i
= 0; i
< nr_heads
; i
++) {
1121 struct ref
*ref
= to_fetch
[i
];
1123 die(_("cannot fetch by sha1 over smart http"));
1124 packet_buf_write(&preamble
, "%s %s\n",
1125 oid_to_hex(&ref
->old_oid
), ref
->name
);
1127 packet_buf_flush(&preamble
);
1129 memset(&rpc
, 0, sizeof(rpc
));
1130 rpc
.service_name
= "git-upload-pack",
1131 rpc
.gzip_request
= 1;
1133 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1135 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1136 strbuf_release(&rpc_result
);
1137 strbuf_release(&preamble
);
1138 argv_array_clear(&args
);
1142 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1144 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1146 return fetch_git(d
, nr_heads
, to_fetch
);
1148 return fetch_dumb(nr_heads
, to_fetch
);
1151 static void parse_fetch(struct strbuf
*buf
)
1153 struct ref
**to_fetch
= NULL
;
1154 struct ref
*list_head
= NULL
;
1155 struct ref
**list
= &list_head
;
1156 int alloc_heads
= 0, nr_heads
= 0;
1160 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1163 struct object_id old_oid
;
1166 if (parse_oid_hex(p
, &old_oid
, &q
))
1167 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1173 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1175 ref
= alloc_ref(name
);
1176 oidcpy(&ref
->old_oid
, &old_oid
);
1181 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1182 to_fetch
[nr_heads
++] = ref
;
1185 die(_("http transport does not support %s"), buf
->buf
);
1188 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1194 if (fetch(nr_heads
, to_fetch
))
1195 exit(128); /* error already reported */
1196 free_refs(list_head
);
1204 static int push_dav(int nr_spec
, const char **specs
)
1206 struct child_process child
= CHILD_PROCESS_INIT
;
1210 argv_array_push(&child
.args
, "http-push");
1211 argv_array_push(&child
.args
, "--helper-status");
1212 if (options
.dry_run
)
1213 argv_array_push(&child
.args
, "--dry-run");
1214 if (options
.verbosity
> 1)
1215 argv_array_push(&child
.args
, "--verbose");
1216 argv_array_push(&child
.args
, url
.buf
);
1217 for (i
= 0; i
< nr_spec
; i
++)
1218 argv_array_push(&child
.args
, specs
[i
]);
1220 if (run_command(&child
))
1221 die(_("git-http-push failed"));
1225 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1227 struct rpc_state rpc
;
1229 struct argv_array args
;
1230 struct string_list_item
*cas_option
;
1231 struct strbuf preamble
= STRBUF_INIT
;
1232 struct strbuf rpc_result
= STRBUF_INIT
;
1234 argv_array_init(&args
);
1235 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1239 argv_array_push(&args
, "--thin");
1240 if (options
.dry_run
)
1241 argv_array_push(&args
, "--dry-run");
1242 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1243 argv_array_push(&args
, "--signed=yes");
1244 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1245 argv_array_push(&args
, "--signed=if-asked");
1247 argv_array_push(&args
, "--atomic");
1248 if (options
.verbosity
== 0)
1249 argv_array_push(&args
, "--quiet");
1250 else if (options
.verbosity
> 1)
1251 argv_array_push(&args
, "--verbose");
1252 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1253 argv_array_pushf(&args
, "--push-option=%s",
1254 options
.push_options
.items
[i
].string
);
1255 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
1256 for_each_string_list_item(cas_option
, &cas_options
)
1257 argv_array_push(&args
, cas_option
->string
);
1258 argv_array_push(&args
, url
.buf
);
1260 argv_array_push(&args
, "--stdin");
1261 for (i
= 0; i
< nr_spec
; i
++)
1262 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1263 packet_buf_flush(&preamble
);
1265 memset(&rpc
, 0, sizeof(rpc
));
1266 rpc
.service_name
= "git-receive-pack",
1268 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1270 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1271 strbuf_release(&rpc_result
);
1272 strbuf_release(&preamble
);
1273 argv_array_clear(&args
);
1277 static int push(int nr_spec
, const char **specs
)
1279 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1282 if (heads
->proto_git
)
1283 ret
= push_git(heads
, nr_spec
, specs
);
1285 ret
= push_dav(nr_spec
, specs
);
1286 free_discovery(heads
);
1290 static void parse_push(struct strbuf
*buf
)
1292 struct argv_array specs
= ARGV_ARRAY_INIT
;
1297 if (skip_prefix(buf
->buf
, "push ", &arg
))
1298 argv_array_push(&specs
, arg
);
1300 die(_("http transport does not support %s"), buf
->buf
);
1303 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1309 ret
= push(specs
.argc
, specs
.argv
);
1314 exit(128); /* error already reported */
1317 argv_array_clear(&specs
);
1320 static int stateless_connect(const char *service_name
)
1322 struct discovery
*discover
;
1323 struct rpc_state rpc
;
1324 struct strbuf buf
= STRBUF_INIT
;
1327 * Run the info/refs request and see if the server supports protocol
1328 * v2. If and only if the server supports v2 can we successfully
1329 * establish a stateless connection, otherwise we need to tell the
1330 * client to fallback to using other transport helper functions to
1331 * complete their request.
1333 discover
= discover_refs(service_name
, 0);
1334 if (discover
->version
!= protocol_v2
) {
1335 printf("fallback\n");
1339 /* Stateless Connection established */
1344 rpc
.service_name
= service_name
;
1345 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1346 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1347 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1348 if (get_protocol_http_header(discover
->version
, &buf
)) {
1349 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1351 rpc
.protocol_header
= NULL
;
1352 strbuf_release(&buf
);
1354 rpc
.buf
= xmalloc(http_post_buffer
);
1355 rpc
.alloc
= http_post_buffer
;
1360 rpc
.any_written
= 0;
1361 rpc
.gzip_request
= 1;
1362 rpc
.initial_buffer
= 0;
1363 rpc
.write_line_lengths
= 1;
1364 rpc
.flush_read_but_not_sent
= 0;
1367 * Dump the capability listing that we got from the server earlier
1368 * during the info/refs request.
1370 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1372 /* Until we see EOF keep sending POSTs */
1375 enum packet_read_status status
;
1377 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1379 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1380 if (status
== PACKET_READ_EOF
)
1382 if (post_rpc(&rpc
, status
== PACKET_READ_FLUSH
))
1383 /* We would have an err here */
1385 /* Reset the buffer for next request */
1389 free(rpc
.service_url
);
1390 free(rpc
.hdr_content_type
);
1391 free(rpc
.hdr_accept
);
1392 free(rpc
.protocol_header
);
1394 strbuf_release(&buf
);
1399 int cmd_main(int argc
, const char **argv
)
1401 struct strbuf buf
= STRBUF_INIT
;
1404 setup_git_directory_gently(&nongit
);
1406 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1410 options
.verbosity
= 1;
1411 options
.progress
= !!isatty(2);
1413 string_list_init(&options
.deepen_not
, 1);
1414 string_list_init(&options
.push_options
, 1);
1417 * Just report "remote-curl" here (folding all the various aliases
1418 * ("git-remote-http", "git-remote-https", and etc.) here since they
1419 * are all just copies of the same actual executable.
1421 trace2_cmd_name("remote-curl");
1423 remote
= remote_get(argv
[1]);
1426 end_url_with_slash(&url
, argv
[2]);
1428 end_url_with_slash(&url
, remote
->url
[0]);
1431 http_init(remote
, url
.buf
, 0);
1436 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1438 error(_("remote-curl: error reading command stream from git"));
1443 if (starts_with(buf
.buf
, "fetch ")) {
1445 die(_("remote-curl: fetch attempted without a local repo"));
1448 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1449 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1450 output_refs(get_refs(for_push
));
1452 } else if (starts_with(buf
.buf
, "push ")) {
1455 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1456 char *value
= strchr(arg
, ' ');
1464 result
= set_option(arg
, value
);
1467 else if (result
< 0)
1468 printf("error invalid value\n");
1470 printf("unsupported\n");
1473 } else if (!strcmp(buf
.buf
, "capabilities")) {
1474 printf("stateless-connect\n");
1478 printf("check-connectivity\n");
1479 printf("object-format\n");
1482 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1483 if (!stateless_connect(arg
))
1486 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);