8 #include "run-command.h"
10 #include "string-list.h"
12 #include "argv-array.h"
13 #include "credential.h"
14 #include "sha1-array.h"
15 #include "send-pack.h"
17 static struct remote
*remote
;
18 /* always ends with a trailing slash */
19 static struct strbuf url
= STRBUF_INIT
;
25 struct string_list deepen_not
;
26 struct string_list push_options
;
27 unsigned progress
: 1,
28 check_self_contained_and_connected
: 1,
34 /* One of the SEND_PACK_PUSH_CERT_* constants. */
40 static struct options options
;
41 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
43 static int set_option(const char *name
, const char *value
)
45 if (!strcmp(name
, "verbosity")) {
47 int v
= strtol(value
, &end
, 10);
48 if (value
== end
|| *end
)
50 options
.verbosity
= v
;
53 else if (!strcmp(name
, "progress")) {
54 if (!strcmp(value
, "true"))
56 else if (!strcmp(value
, "false"))
62 else if (!strcmp(name
, "depth")) {
64 unsigned long v
= strtoul(value
, &end
, 10);
65 if (value
== end
|| *end
)
70 else if (!strcmp(name
, "deepen-since")) {
71 options
.deepen_since
= xstrdup(value
);
74 else if (!strcmp(name
, "deepen-not")) {
75 string_list_append(&options
.deepen_not
, value
);
78 else if (!strcmp(name
, "deepen-relative")) {
79 if (!strcmp(value
, "true"))
80 options
.deepen_relative
= 1;
81 else if (!strcmp(value
, "false"))
82 options
.deepen_relative
= 0;
87 else if (!strcmp(name
, "followtags")) {
88 if (!strcmp(value
, "true"))
89 options
.followtags
= 1;
90 else if (!strcmp(value
, "false"))
91 options
.followtags
= 0;
96 else if (!strcmp(name
, "dry-run")) {
97 if (!strcmp(value
, "true"))
99 else if (!strcmp(value
, "false"))
105 else if (!strcmp(name
, "check-connectivity")) {
106 if (!strcmp(value
, "true"))
107 options
.check_self_contained_and_connected
= 1;
108 else if (!strcmp(value
, "false"))
109 options
.check_self_contained_and_connected
= 0;
114 else if (!strcmp(name
, "cas")) {
115 struct strbuf val
= STRBUF_INIT
;
116 strbuf_addf(&val
, "--" CAS_OPT_NAME
"=%s", value
);
117 string_list_append(&cas_options
, val
.buf
);
118 strbuf_release(&val
);
120 } else if (!strcmp(name
, "cloning")) {
121 if (!strcmp(value
, "true"))
123 else if (!strcmp(value
, "false"))
128 } else if (!strcmp(name
, "update-shallow")) {
129 if (!strcmp(value
, "true"))
130 options
.update_shallow
= 1;
131 else if (!strcmp(value
, "false"))
132 options
.update_shallow
= 0;
136 } else if (!strcmp(name
, "pushcert")) {
137 if (!strcmp(value
, "true"))
138 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
139 else if (!strcmp(value
, "false"))
140 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
141 else if (!strcmp(value
, "if-asked"))
142 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
146 } else if (!strcmp(name
, "push-option")) {
147 string_list_append(&options
.push_options
, value
);
150 #if LIBCURL_VERSION_NUM >= 0x070a08
151 } else if (!strcmp(name
, "family")) {
152 if (!strcmp(value
, "ipv4"))
153 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
154 else if (!strcmp(value
, "ipv6"))
155 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
156 else if (!strcmp(value
, "all"))
157 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
161 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
162 } else if (!strcmp(name
, "from-promisor")) {
163 options
.from_promisor
= 1;
165 } else if (!strcmp(name
, "no-dependents")) {
166 options
.no_dependents
= 1;
169 return 1 /* unsupported */;
179 struct oid_array shallow
;
180 unsigned proto_git
: 1;
182 static struct discovery
*last_discovery
;
184 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
186 struct ref
*list
= NULL
;
187 get_remote_heads(-1, heads
->buf
, heads
->len
, &list
,
188 for_push
? REF_NORMAL
: 0, NULL
, &heads
->shallow
);
192 static struct ref
*parse_info_refs(struct discovery
*heads
)
194 char *data
, *start
, *mid
;
198 struct ref
*refs
= NULL
;
199 struct ref
*ref
= NULL
;
200 struct ref
*last_ref
= NULL
;
205 while (i
< heads
->len
) {
211 if (data
[i
] == '\n') {
212 if (mid
- start
!= 40)
213 die("%sinfo/refs not valid: is this a git repository?",
217 ref
= alloc_ref(ref_name
);
218 get_oid_hex(start
, &ref
->old_oid
);
222 last_ref
->next
= ref
;
229 ref
= alloc_ref("HEAD");
230 if (!http_fetch_ref(url
.buf
, ref
) &&
231 !resolve_remote_symref(ref
, refs
)) {
241 static void free_discovery(struct discovery
*d
)
244 if (d
== last_discovery
)
245 last_discovery
= NULL
;
246 free(d
->shallow
.oid
);
253 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
259 * We only show text/plain parts, as other types are likely
260 * to be ugly to look at on the user's terminal.
262 if (strcmp(type
->buf
, "text/plain"))
265 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
273 eol
= strchrnul(p
, '\n');
274 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
280 static struct discovery
*discover_refs(const char *service
, int for_push
)
282 struct strbuf exp
= STRBUF_INIT
;
283 struct strbuf type
= STRBUF_INIT
;
284 struct strbuf charset
= STRBUF_INIT
;
285 struct strbuf buffer
= STRBUF_INIT
;
286 struct strbuf refs_url
= STRBUF_INIT
;
287 struct strbuf effective_url
= STRBUF_INIT
;
288 struct discovery
*last
= last_discovery
;
289 int http_ret
, maybe_smart
= 0;
290 struct http_get_options http_options
;
292 if (last
&& !strcmp(service
, last
->service
))
294 free_discovery(last
);
296 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
297 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
298 git_env_bool("GIT_SMART_HTTP", 1)) {
300 if (!strchr(url
.buf
, '?'))
301 strbuf_addch(&refs_url
, '?');
303 strbuf_addch(&refs_url
, '&');
304 strbuf_addf(&refs_url
, "service=%s", service
);
307 memset(&http_options
, 0, sizeof(http_options
));
308 http_options
.content_type
= &type
;
309 http_options
.charset
= &charset
;
310 http_options
.effective_url
= &effective_url
;
311 http_options
.base_url
= &url
;
312 http_options
.initial_request
= 1;
313 http_options
.no_cache
= 1;
314 http_options
.keep_error
= 1;
316 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
320 case HTTP_MISSING_TARGET
:
321 show_http_message(&type
, &charset
, &buffer
);
322 die("repository '%s' not found", url
.buf
);
324 show_http_message(&type
, &charset
, &buffer
);
325 die("Authentication failed for '%s'", url
.buf
);
327 show_http_message(&type
, &charset
, &buffer
);
328 die("unable to access '%s': %s", url
.buf
, curl_errorstr
);
331 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
))
332 warning(_("redirecting to %s"), url
.buf
);
334 last
= xcalloc(1, sizeof(*last_discovery
));
335 last
->service
= service
;
336 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
337 last
->buf
= last
->buf_alloc
;
339 strbuf_addf(&exp
, "application/x-%s-advertisement", service
);
341 (5 <= last
->len
&& last
->buf
[4] == '#') &&
342 !strbuf_cmp(&exp
, &type
)) {
346 * smart HTTP response; validate that the service
347 * pkt-line matches our request.
349 line
= packet_read_line_buf(&last
->buf
, &last
->len
, NULL
);
352 strbuf_addf(&exp
, "# service=%s", service
);
353 if (strcmp(line
, exp
.buf
))
354 die("invalid server response; got '%s'", line
);
355 strbuf_release(&exp
);
357 /* The header can include additional metadata lines, up
358 * until a packet flush marker. Ignore these now, but
359 * in the future we might start to scan them.
361 while (packet_read_line_buf(&last
->buf
, &last
->len
, NULL
))
368 last
->refs
= parse_git_refs(last
, for_push
);
370 last
->refs
= parse_info_refs(last
);
372 strbuf_release(&refs_url
);
373 strbuf_release(&exp
);
374 strbuf_release(&type
);
375 strbuf_release(&charset
);
376 strbuf_release(&effective_url
);
377 strbuf_release(&buffer
);
378 last_discovery
= last
;
382 static struct ref
*get_refs(int for_push
)
384 struct discovery
*heads
;
387 heads
= discover_refs("git-receive-pack", for_push
);
389 heads
= discover_refs("git-upload-pack", for_push
);
394 static void output_refs(struct ref
*refs
)
397 for (posn
= refs
; posn
; posn
= posn
->next
) {
399 printf("@%s %s\n", posn
->symref
, posn
->name
);
401 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
408 const char *service_name
;
410 struct strbuf
*stdin_preamble
;
412 char *hdr_content_type
;
421 struct strbuf result
;
422 unsigned gzip_request
: 1;
423 unsigned initial_buffer
: 1;
426 static size_t rpc_out(void *ptr
, size_t eltsize
,
427 size_t nmemb
, void *buffer_
)
429 size_t max
= eltsize
* nmemb
;
430 struct rpc_state
*rpc
= buffer_
;
431 size_t avail
= rpc
->len
- rpc
->pos
;
434 rpc
->initial_buffer
= 0;
435 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
444 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
449 #ifndef NO_CURL_IOCTL
450 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
452 struct rpc_state
*rpc
= clientp
;
458 case CURLIOCMD_RESTARTREAD
:
459 if (rpc
->initial_buffer
) {
463 error("unable to rewind rpc post data - try increasing http.postBuffer");
464 return CURLIOE_FAILRESTART
;
467 return CURLIOE_UNKNOWNCMD
;
472 static size_t rpc_in(char *ptr
, size_t eltsize
,
473 size_t nmemb
, void *buffer_
)
475 size_t size
= eltsize
* nmemb
;
476 struct rpc_state
*rpc
= buffer_
;
478 rpc
->any_written
= 1;
479 write_or_die(rpc
->in
, ptr
, size
);
483 static int run_slot(struct active_request_slot
*slot
,
484 struct slot_results
*results
)
487 struct slot_results results_buf
;
490 results
= &results_buf
;
492 err
= run_one_slot(slot
, results
);
494 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
495 struct strbuf msg
= STRBUF_INIT
;
496 if (results
->http_code
&& results
->http_code
!= 200)
497 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
498 if (results
->curl_result
!= CURLE_OK
) {
500 strbuf_addch(&msg
, ' ');
501 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
502 if (curl_errorstr
[0]) {
503 strbuf_addch(&msg
, ' ');
504 strbuf_addstr(&msg
, curl_errorstr
);
507 error("RPC failed; %s", msg
.buf
);
508 strbuf_release(&msg
);
514 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
516 struct active_request_slot
*slot
;
517 struct curl_slist
*headers
= http_copy_default_headers();
518 struct strbuf buf
= STRBUF_INIT
;
521 slot
= get_active_slot();
523 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
524 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
526 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
527 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
528 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
529 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
530 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
531 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
532 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
533 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
534 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
536 err
= run_slot(slot
, results
);
538 curl_slist_free_all(headers
);
539 strbuf_release(&buf
);
543 static curl_off_t
xcurl_off_t(ssize_t len
) {
544 if (len
> maximum_signed_value_of_type(curl_off_t
))
545 die("cannot handle pushes this big");
546 return (curl_off_t
) len
;
549 static int post_rpc(struct rpc_state
*rpc
)
551 struct active_request_slot
*slot
;
552 struct curl_slist
*headers
= http_copy_default_headers();
553 int use_gzip
= rpc
->gzip_request
;
554 char *gzip_body
= NULL
;
555 size_t gzip_size
= 0;
556 int err
, large_request
= 0;
557 int needs_100_continue
= 0;
559 /* Try to load the entire request, if we can fit it into the
560 * allocated buffer space we can use HTTP/1.0 and avoid the
561 * chunked encoding mess.
564 size_t left
= rpc
->alloc
- rpc
->len
;
565 char *buf
= rpc
->buf
+ rpc
->len
;
568 if (left
< LARGE_PACKET_MAX
) {
574 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
581 struct slot_results results
;
584 err
= probe_rpc(rpc
, &results
);
585 if (err
== HTTP_REAUTH
)
586 credential_fill(&http_auth
);
587 } while (err
== HTTP_REAUTH
);
591 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
592 needs_100_continue
= 1;
595 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
596 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
597 headers
= curl_slist_append(headers
, needs_100_continue
?
598 "Expect: 100-continue" : "Expect:");
601 slot
= get_active_slot();
603 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
604 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
605 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
606 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
609 /* The request body is large and the size cannot be predicted.
610 * We must use chunked encoding to send it.
612 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
613 rpc
->initial_buffer
= 1;
614 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
615 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
616 #ifndef NO_CURL_IOCTL
617 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
618 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
620 if (options
.verbosity
> 1) {
621 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
625 } else if (gzip_body
) {
627 * If we are looping to retry authentication, then the previous
628 * run will have set up the headers and gzip buffer already,
629 * and we just need to send it.
631 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
632 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
634 } else if (use_gzip
&& 1024 < rpc
->len
) {
635 /* The client backend isn't giving us compressed data so
636 * we can try to deflate it ourselves, this may save on.
642 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
643 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
644 gzip_body
= xmalloc(gzip_size
);
646 stream
.next_in
= (unsigned char *)rpc
->buf
;
647 stream
.avail_in
= rpc
->len
;
648 stream
.next_out
= (unsigned char *)gzip_body
;
649 stream
.avail_out
= gzip_size
;
651 ret
= git_deflate(&stream
, Z_FINISH
);
652 if (ret
!= Z_STREAM_END
)
653 die("cannot deflate request; zlib deflate error %d", ret
);
655 ret
= git_deflate_end_gently(&stream
);
657 die("cannot deflate request; zlib end error %d", ret
);
659 gzip_size
= stream
.total_out
;
661 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
662 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
663 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
665 if (options
.verbosity
> 1) {
666 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
668 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
672 /* We know the complete request size in advance, use the
673 * more normal Content-Length approach.
675 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
676 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
677 if (options
.verbosity
> 1) {
678 fprintf(stderr
, "POST %s (%lu bytes)\n",
679 rpc
->service_name
, (unsigned long)rpc
->len
);
684 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
685 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
686 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
689 rpc
->any_written
= 0;
690 err
= run_slot(slot
, NULL
);
691 if (err
== HTTP_REAUTH
&& !large_request
) {
692 credential_fill(&http_auth
);
698 if (!rpc
->any_written
)
701 curl_slist_free_all(headers
);
706 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
708 const char *svc
= rpc
->service_name
;
709 struct strbuf buf
= STRBUF_INIT
;
710 struct strbuf
*preamble
= rpc
->stdin_preamble
;
711 struct child_process client
= CHILD_PROCESS_INIT
;
717 client
.argv
= rpc
->argv
;
718 if (start_command(&client
))
721 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
723 write_or_die(client
.in
, heads
->buf
, heads
->len
);
725 rpc
->alloc
= http_post_buffer
;
726 rpc
->buf
= xmalloc(rpc
->alloc
);
728 rpc
->out
= client
.out
;
729 strbuf_init(&rpc
->result
, 0);
731 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
732 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
734 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
735 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
737 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
738 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
741 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
746 err
|= post_rpc(rpc
);
752 strbuf_read(&rpc
->result
, client
.out
, 0);
756 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
763 err
|= finish_command(&client
);
764 free(rpc
->service_url
);
765 free(rpc
->hdr_content_type
);
766 free(rpc
->hdr_accept
);
768 strbuf_release(&buf
);
772 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
774 struct walker
*walker
;
778 ALLOC_ARRAY(targets
, nr_heads
);
779 if (options
.depth
|| options
.deepen_since
)
780 die("dumb http transport does not support shallow capabilities");
781 for (i
= 0; i
< nr_heads
; i
++)
782 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
784 walker
= get_http_walker(url
.buf
);
786 walker
->get_tree
= 1;
787 walker
->get_history
= 1;
788 walker
->get_verbosely
= options
.verbosity
>= 3;
789 walker
->get_recover
= 0;
790 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
793 for (i
= 0; i
< nr_heads
; i
++)
797 return ret
? error("fetch failed.") : 0;
800 static int fetch_git(struct discovery
*heads
,
801 int nr_heads
, struct ref
**to_fetch
)
803 struct rpc_state rpc
;
804 struct strbuf preamble
= STRBUF_INIT
;
806 struct argv_array args
= ARGV_ARRAY_INIT
;
808 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
809 "--stdin", "--lock-pack", NULL
);
810 if (options
.followtags
)
811 argv_array_push(&args
, "--include-tag");
813 argv_array_push(&args
, "--thin");
814 if (options
.verbosity
>= 3)
815 argv_array_pushl(&args
, "-v", "-v", NULL
);
816 if (options
.check_self_contained_and_connected
)
817 argv_array_push(&args
, "--check-self-contained-and-connected");
819 argv_array_push(&args
, "--cloning");
820 if (options
.update_shallow
)
821 argv_array_push(&args
, "--update-shallow");
822 if (!options
.progress
)
823 argv_array_push(&args
, "--no-progress");
825 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
826 if (options
.deepen_since
)
827 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
828 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
829 argv_array_pushf(&args
, "--shallow-exclude=%s",
830 options
.deepen_not
.items
[i
].string
);
831 if (options
.deepen_relative
&& options
.depth
)
832 argv_array_push(&args
, "--deepen-relative");
833 if (options
.from_promisor
)
834 argv_array_push(&args
, "--from-promisor");
835 if (options
.no_dependents
)
836 argv_array_push(&args
, "--no-dependents");
837 argv_array_push(&args
, url
.buf
);
839 for (i
= 0; i
< nr_heads
; i
++) {
840 struct ref
*ref
= to_fetch
[i
];
842 die("cannot fetch by sha1 over smart http");
843 packet_buf_write(&preamble
, "%s %s\n",
844 oid_to_hex(&ref
->old_oid
), ref
->name
);
846 packet_buf_flush(&preamble
);
848 memset(&rpc
, 0, sizeof(rpc
));
849 rpc
.service_name
= "git-upload-pack",
850 rpc
.argv
= args
.argv
;
851 rpc
.stdin_preamble
= &preamble
;
852 rpc
.gzip_request
= 1;
854 err
= rpc_service(&rpc
, heads
);
856 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
857 strbuf_release(&rpc
.result
);
858 strbuf_release(&preamble
);
859 argv_array_clear(&args
);
863 static int fetch(int nr_heads
, struct ref
**to_fetch
)
865 struct discovery
*d
= discover_refs("git-upload-pack", 0);
867 return fetch_git(d
, nr_heads
, to_fetch
);
869 return fetch_dumb(nr_heads
, to_fetch
);
872 static void parse_fetch(struct strbuf
*buf
)
874 struct ref
**to_fetch
= NULL
;
875 struct ref
*list_head
= NULL
;
876 struct ref
**list
= &list_head
;
877 int alloc_heads
= 0, nr_heads
= 0;
881 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
884 struct object_id old_oid
;
886 if (get_oid_hex(p
, &old_oid
))
887 die("protocol error: expected sha/ref, got %s'", p
);
888 if (p
[GIT_SHA1_HEXSZ
] == ' ')
889 name
= p
+ GIT_SHA1_HEXSZ
+ 1;
890 else if (!p
[GIT_SHA1_HEXSZ
])
893 die("protocol error: expected sha/ref, got %s'", p
);
895 ref
= alloc_ref(name
);
896 oidcpy(&ref
->old_oid
, &old_oid
);
901 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
902 to_fetch
[nr_heads
++] = ref
;
905 die("http transport does not support %s", buf
->buf
);
908 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
914 if (fetch(nr_heads
, to_fetch
))
915 exit(128); /* error already reported */
916 free_refs(list_head
);
924 static int push_dav(int nr_spec
, char **specs
)
926 struct child_process child
= CHILD_PROCESS_INIT
;
930 argv_array_push(&child
.args
, "http-push");
931 argv_array_push(&child
.args
, "--helper-status");
933 argv_array_push(&child
.args
, "--dry-run");
934 if (options
.verbosity
> 1)
935 argv_array_push(&child
.args
, "--verbose");
936 argv_array_push(&child
.args
, url
.buf
);
937 for (i
= 0; i
< nr_spec
; i
++)
938 argv_array_push(&child
.args
, specs
[i
]);
940 if (run_command(&child
))
941 die("git-http-push failed");
945 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
947 struct rpc_state rpc
;
949 struct argv_array args
;
950 struct string_list_item
*cas_option
;
951 struct strbuf preamble
= STRBUF_INIT
;
953 argv_array_init(&args
);
954 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
958 argv_array_push(&args
, "--thin");
960 argv_array_push(&args
, "--dry-run");
961 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
962 argv_array_push(&args
, "--signed=yes");
963 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
964 argv_array_push(&args
, "--signed=if-asked");
965 if (options
.verbosity
== 0)
966 argv_array_push(&args
, "--quiet");
967 else if (options
.verbosity
> 1)
968 argv_array_push(&args
, "--verbose");
969 for (i
= 0; i
< options
.push_options
.nr
; i
++)
970 argv_array_pushf(&args
, "--push-option=%s",
971 options
.push_options
.items
[i
].string
);
972 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
973 for_each_string_list_item(cas_option
, &cas_options
)
974 argv_array_push(&args
, cas_option
->string
);
975 argv_array_push(&args
, url
.buf
);
977 argv_array_push(&args
, "--stdin");
978 for (i
= 0; i
< nr_spec
; i
++)
979 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
980 packet_buf_flush(&preamble
);
982 memset(&rpc
, 0, sizeof(rpc
));
983 rpc
.service_name
= "git-receive-pack",
984 rpc
.argv
= args
.argv
;
985 rpc
.stdin_preamble
= &preamble
;
987 err
= rpc_service(&rpc
, heads
);
989 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
990 strbuf_release(&rpc
.result
);
991 strbuf_release(&preamble
);
992 argv_array_clear(&args
);
996 static int push(int nr_spec
, char **specs
)
998 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1001 if (heads
->proto_git
)
1002 ret
= push_git(heads
, nr_spec
, specs
);
1004 ret
= push_dav(nr_spec
, specs
);
1005 free_discovery(heads
);
1009 static void parse_push(struct strbuf
*buf
)
1011 char **specs
= NULL
;
1012 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
1015 if (starts_with(buf
->buf
, "push ")) {
1016 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
1017 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
1020 die("http transport does not support %s", buf
->buf
);
1023 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1029 ret
= push(nr_spec
, specs
);
1034 exit(128); /* error already reported */
1037 for (i
= 0; i
< nr_spec
; i
++)
1042 int cmd_main(int argc
, const char **argv
)
1044 struct strbuf buf
= STRBUF_INIT
;
1047 setup_git_directory_gently(&nongit
);
1049 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1053 options
.verbosity
= 1;
1054 options
.progress
= !!isatty(2);
1056 string_list_init(&options
.deepen_not
, 1);
1057 string_list_init(&options
.push_options
, 1);
1059 remote
= remote_get(argv
[1]);
1062 end_url_with_slash(&url
, argv
[2]);
1064 end_url_with_slash(&url
, remote
->url
[0]);
1067 http_init(remote
, url
.buf
, 0);
1072 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1074 error("remote-curl: error reading command stream from git");
1079 if (starts_with(buf
.buf
, "fetch ")) {
1081 die("remote-curl: fetch attempted without a local repo");
1084 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1085 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1086 output_refs(get_refs(for_push
));
1088 } else if (starts_with(buf
.buf
, "push ")) {
1091 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1092 char *value
= strchr(arg
, ' ');
1100 result
= set_option(arg
, value
);
1103 else if (result
< 0)
1104 printf("error invalid value\n");
1106 printf("unsupported\n");
1109 } else if (!strcmp(buf
.buf
, "capabilities")) {
1113 printf("check-connectivity\n");
1117 error("remote-curl: unknown command '%s' from git", buf
.buf
);