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. */
38 static struct options options
;
39 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
41 static int set_option(const char *name
, const char *value
)
43 if (!strcmp(name
, "verbosity")) {
45 int v
= strtol(value
, &end
, 10);
46 if (value
== end
|| *end
)
48 options
.verbosity
= v
;
51 else if (!strcmp(name
, "progress")) {
52 if (!strcmp(value
, "true"))
54 else if (!strcmp(value
, "false"))
60 else if (!strcmp(name
, "depth")) {
62 unsigned long v
= strtoul(value
, &end
, 10);
63 if (value
== end
|| *end
)
68 else if (!strcmp(name
, "deepen-since")) {
69 options
.deepen_since
= xstrdup(value
);
72 else if (!strcmp(name
, "deepen-not")) {
73 string_list_append(&options
.deepen_not
, value
);
76 else if (!strcmp(name
, "deepen-relative")) {
77 if (!strcmp(value
, "true"))
78 options
.deepen_relative
= 1;
79 else if (!strcmp(value
, "false"))
80 options
.deepen_relative
= 0;
85 else if (!strcmp(name
, "followtags")) {
86 if (!strcmp(value
, "true"))
87 options
.followtags
= 1;
88 else if (!strcmp(value
, "false"))
89 options
.followtags
= 0;
94 else if (!strcmp(name
, "dry-run")) {
95 if (!strcmp(value
, "true"))
97 else if (!strcmp(value
, "false"))
103 else if (!strcmp(name
, "check-connectivity")) {
104 if (!strcmp(value
, "true"))
105 options
.check_self_contained_and_connected
= 1;
106 else if (!strcmp(value
, "false"))
107 options
.check_self_contained_and_connected
= 0;
112 else if (!strcmp(name
, "cas")) {
113 struct strbuf val
= STRBUF_INIT
;
114 strbuf_addf(&val
, "--" CAS_OPT_NAME
"=%s", value
);
115 string_list_append(&cas_options
, val
.buf
);
116 strbuf_release(&val
);
118 } else if (!strcmp(name
, "cloning")) {
119 if (!strcmp(value
, "true"))
121 else if (!strcmp(value
, "false"))
126 } else if (!strcmp(name
, "update-shallow")) {
127 if (!strcmp(value
, "true"))
128 options
.update_shallow
= 1;
129 else if (!strcmp(value
, "false"))
130 options
.update_shallow
= 0;
134 } else if (!strcmp(name
, "pushcert")) {
135 if (!strcmp(value
, "true"))
136 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
137 else if (!strcmp(value
, "false"))
138 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
139 else if (!strcmp(value
, "if-asked"))
140 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
144 } else if (!strcmp(name
, "push-option")) {
145 string_list_append(&options
.push_options
, value
);
148 #if LIBCURL_VERSION_NUM >= 0x070a08
149 } else if (!strcmp(name
, "family")) {
150 if (!strcmp(value
, "ipv4"))
151 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
152 else if (!strcmp(value
, "ipv6"))
153 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
154 else if (!strcmp(value
, "all"))
155 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
159 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
161 return 1 /* unsupported */;
171 struct oid_array shallow
;
172 unsigned proto_git
: 1;
174 static struct discovery
*last_discovery
;
176 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
178 struct ref
*list
= NULL
;
179 get_remote_heads(-1, heads
->buf
, heads
->len
, &list
,
180 for_push
? REF_NORMAL
: 0, NULL
, &heads
->shallow
);
184 static struct ref
*parse_info_refs(struct discovery
*heads
)
186 char *data
, *start
, *mid
;
190 struct ref
*refs
= NULL
;
191 struct ref
*ref
= NULL
;
192 struct ref
*last_ref
= NULL
;
197 while (i
< heads
->len
) {
203 if (data
[i
] == '\n') {
204 if (mid
- start
!= 40)
205 die("%sinfo/refs not valid: is this a git repository?",
209 ref
= alloc_ref(ref_name
);
210 get_oid_hex(start
, &ref
->old_oid
);
214 last_ref
->next
= ref
;
221 ref
= alloc_ref("HEAD");
222 if (!http_fetch_ref(url
.buf
, ref
) &&
223 !resolve_remote_symref(ref
, refs
)) {
233 static void free_discovery(struct discovery
*d
)
236 if (d
== last_discovery
)
237 last_discovery
= NULL
;
238 free(d
->shallow
.oid
);
245 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
251 * We only show text/plain parts, as other types are likely
252 * to be ugly to look at on the user's terminal.
254 if (strcmp(type
->buf
, "text/plain"))
257 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
265 eol
= strchrnul(p
, '\n');
266 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
272 static struct discovery
*discover_refs(const char *service
, int for_push
)
274 struct strbuf exp
= STRBUF_INIT
;
275 struct strbuf type
= STRBUF_INIT
;
276 struct strbuf charset
= STRBUF_INIT
;
277 struct strbuf buffer
= STRBUF_INIT
;
278 struct strbuf refs_url
= STRBUF_INIT
;
279 struct strbuf effective_url
= STRBUF_INIT
;
280 struct discovery
*last
= last_discovery
;
281 int http_ret
, maybe_smart
= 0;
282 struct http_get_options http_options
;
284 if (last
&& !strcmp(service
, last
->service
))
286 free_discovery(last
);
288 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
289 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
290 git_env_bool("GIT_SMART_HTTP", 1)) {
292 if (!strchr(url
.buf
, '?'))
293 strbuf_addch(&refs_url
, '?');
295 strbuf_addch(&refs_url
, '&');
296 strbuf_addf(&refs_url
, "service=%s", service
);
299 memset(&http_options
, 0, sizeof(http_options
));
300 http_options
.content_type
= &type
;
301 http_options
.charset
= &charset
;
302 http_options
.effective_url
= &effective_url
;
303 http_options
.base_url
= &url
;
304 http_options
.initial_request
= 1;
305 http_options
.no_cache
= 1;
306 http_options
.keep_error
= 1;
308 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
312 case HTTP_MISSING_TARGET
:
313 show_http_message(&type
, &charset
, &buffer
);
314 die("repository '%s' not found", url
.buf
);
316 show_http_message(&type
, &charset
, &buffer
);
317 die("Authentication failed for '%s'", url
.buf
);
319 show_http_message(&type
, &charset
, &buffer
);
320 die("unable to access '%s': %s", url
.buf
, curl_errorstr
);
323 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
))
324 warning(_("redirecting to %s"), url
.buf
);
326 last
= xcalloc(1, sizeof(*last_discovery
));
327 last
->service
= service
;
328 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
329 last
->buf
= last
->buf_alloc
;
331 strbuf_addf(&exp
, "application/x-%s-advertisement", service
);
333 (5 <= last
->len
&& last
->buf
[4] == '#') &&
334 !strbuf_cmp(&exp
, &type
)) {
338 * smart HTTP response; validate that the service
339 * pkt-line matches our request.
341 line
= packet_read_line_buf(&last
->buf
, &last
->len
, NULL
);
343 die("invalid server response; expected service, got flush packet");
346 strbuf_addf(&exp
, "# service=%s", service
);
347 if (strcmp(line
, exp
.buf
))
348 die("invalid server response; got '%s'", line
);
349 strbuf_release(&exp
);
351 /* The header can include additional metadata lines, up
352 * until a packet flush marker. Ignore these now, but
353 * in the future we might start to scan them.
355 while (packet_read_line_buf(&last
->buf
, &last
->len
, NULL
))
362 last
->refs
= parse_git_refs(last
, for_push
);
364 last
->refs
= parse_info_refs(last
);
366 strbuf_release(&refs_url
);
367 strbuf_release(&exp
);
368 strbuf_release(&type
);
369 strbuf_release(&charset
);
370 strbuf_release(&effective_url
);
371 strbuf_release(&buffer
);
372 last_discovery
= last
;
376 static struct ref
*get_refs(int for_push
)
378 struct discovery
*heads
;
381 heads
= discover_refs("git-receive-pack", for_push
);
383 heads
= discover_refs("git-upload-pack", for_push
);
388 static void output_refs(struct ref
*refs
)
391 for (posn
= refs
; posn
; posn
= posn
->next
) {
393 printf("@%s %s\n", posn
->symref
, posn
->name
);
395 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
402 const char *service_name
;
404 struct strbuf
*stdin_preamble
;
406 char *hdr_content_type
;
415 struct strbuf result
;
416 unsigned gzip_request
: 1;
417 unsigned initial_buffer
: 1;
420 static size_t rpc_out(void *ptr
, size_t eltsize
,
421 size_t nmemb
, void *buffer_
)
423 size_t max
= eltsize
* nmemb
;
424 struct rpc_state
*rpc
= buffer_
;
425 size_t avail
= rpc
->len
- rpc
->pos
;
428 rpc
->initial_buffer
= 0;
429 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
438 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
443 #ifndef NO_CURL_IOCTL
444 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
446 struct rpc_state
*rpc
= clientp
;
452 case CURLIOCMD_RESTARTREAD
:
453 if (rpc
->initial_buffer
) {
457 error("unable to rewind rpc post data - try increasing http.postBuffer");
458 return CURLIOE_FAILRESTART
;
461 return CURLIOE_UNKNOWNCMD
;
466 static size_t rpc_in(char *ptr
, size_t eltsize
,
467 size_t nmemb
, void *buffer_
)
469 size_t size
= eltsize
* nmemb
;
470 struct rpc_state
*rpc
= buffer_
;
472 rpc
->any_written
= 1;
473 write_or_die(rpc
->in
, ptr
, size
);
477 static int run_slot(struct active_request_slot
*slot
,
478 struct slot_results
*results
)
481 struct slot_results results_buf
;
484 results
= &results_buf
;
486 err
= run_one_slot(slot
, results
);
488 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
489 struct strbuf msg
= STRBUF_INIT
;
490 if (results
->http_code
&& results
->http_code
!= 200)
491 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
492 if (results
->curl_result
!= CURLE_OK
) {
494 strbuf_addch(&msg
, ' ');
495 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
496 if (curl_errorstr
[0]) {
497 strbuf_addch(&msg
, ' ');
498 strbuf_addstr(&msg
, curl_errorstr
);
501 error("RPC failed; %s", msg
.buf
);
502 strbuf_release(&msg
);
508 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
510 struct active_request_slot
*slot
;
511 struct curl_slist
*headers
= http_copy_default_headers();
512 struct strbuf buf
= STRBUF_INIT
;
515 slot
= get_active_slot();
517 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
518 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
520 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
521 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
522 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
523 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
524 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
525 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
526 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
527 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
528 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
530 err
= run_slot(slot
, results
);
532 curl_slist_free_all(headers
);
533 strbuf_release(&buf
);
537 static curl_off_t
xcurl_off_t(ssize_t len
) {
538 if (len
> maximum_signed_value_of_type(curl_off_t
))
539 die("cannot handle pushes this big");
540 return (curl_off_t
) len
;
543 static int post_rpc(struct rpc_state
*rpc
)
545 struct active_request_slot
*slot
;
546 struct curl_slist
*headers
= http_copy_default_headers();
547 int use_gzip
= rpc
->gzip_request
;
548 char *gzip_body
= NULL
;
549 size_t gzip_size
= 0;
550 int err
, large_request
= 0;
551 int needs_100_continue
= 0;
553 /* Try to load the entire request, if we can fit it into the
554 * allocated buffer space we can use HTTP/1.0 and avoid the
555 * chunked encoding mess.
558 size_t left
= rpc
->alloc
- rpc
->len
;
559 char *buf
= rpc
->buf
+ rpc
->len
;
562 if (left
< LARGE_PACKET_MAX
) {
568 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
575 struct slot_results results
;
578 err
= probe_rpc(rpc
, &results
);
579 if (err
== HTTP_REAUTH
)
580 credential_fill(&http_auth
);
581 } while (err
== HTTP_REAUTH
);
585 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
586 needs_100_continue
= 1;
589 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
590 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
591 headers
= curl_slist_append(headers
, needs_100_continue
?
592 "Expect: 100-continue" : "Expect:");
595 slot
= get_active_slot();
597 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
598 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
599 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
600 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
603 /* The request body is large and the size cannot be predicted.
604 * We must use chunked encoding to send it.
606 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
607 rpc
->initial_buffer
= 1;
608 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
609 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
610 #ifndef NO_CURL_IOCTL
611 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
612 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
614 if (options
.verbosity
> 1) {
615 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
619 } else if (gzip_body
) {
621 * If we are looping to retry authentication, then the previous
622 * run will have set up the headers and gzip buffer already,
623 * and we just need to send it.
625 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
626 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
628 } else if (use_gzip
&& 1024 < rpc
->len
) {
629 /* The client backend isn't giving us compressed data so
630 * we can try to deflate it ourselves, this may save on.
636 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
637 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
638 gzip_body
= xmalloc(gzip_size
);
640 stream
.next_in
= (unsigned char *)rpc
->buf
;
641 stream
.avail_in
= rpc
->len
;
642 stream
.next_out
= (unsigned char *)gzip_body
;
643 stream
.avail_out
= gzip_size
;
645 ret
= git_deflate(&stream
, Z_FINISH
);
646 if (ret
!= Z_STREAM_END
)
647 die("cannot deflate request; zlib deflate error %d", ret
);
649 ret
= git_deflate_end_gently(&stream
);
651 die("cannot deflate request; zlib end error %d", ret
);
653 gzip_size
= stream
.total_out
;
655 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
656 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
657 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
659 if (options
.verbosity
> 1) {
660 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
662 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
666 /* We know the complete request size in advance, use the
667 * more normal Content-Length approach.
669 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
670 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
671 if (options
.verbosity
> 1) {
672 fprintf(stderr
, "POST %s (%lu bytes)\n",
673 rpc
->service_name
, (unsigned long)rpc
->len
);
678 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
679 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
680 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
683 rpc
->any_written
= 0;
684 err
= run_slot(slot
, NULL
);
685 if (err
== HTTP_REAUTH
&& !large_request
) {
686 credential_fill(&http_auth
);
692 if (!rpc
->any_written
)
695 curl_slist_free_all(headers
);
700 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
702 const char *svc
= rpc
->service_name
;
703 struct strbuf buf
= STRBUF_INIT
;
704 struct strbuf
*preamble
= rpc
->stdin_preamble
;
705 struct child_process client
= CHILD_PROCESS_INIT
;
711 client
.argv
= rpc
->argv
;
712 if (start_command(&client
))
715 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
717 write_or_die(client
.in
, heads
->buf
, heads
->len
);
719 rpc
->alloc
= http_post_buffer
;
720 rpc
->buf
= xmalloc(rpc
->alloc
);
722 rpc
->out
= client
.out
;
723 strbuf_init(&rpc
->result
, 0);
725 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
726 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
728 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
729 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
731 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
732 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
735 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
740 err
|= post_rpc(rpc
);
746 strbuf_read(&rpc
->result
, client
.out
, 0);
750 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
757 err
|= finish_command(&client
);
758 free(rpc
->service_url
);
759 free(rpc
->hdr_content_type
);
760 free(rpc
->hdr_accept
);
762 strbuf_release(&buf
);
766 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
768 struct walker
*walker
;
772 ALLOC_ARRAY(targets
, nr_heads
);
773 if (options
.depth
|| options
.deepen_since
)
774 die("dumb http transport does not support shallow capabilities");
775 for (i
= 0; i
< nr_heads
; i
++)
776 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
778 walker
= get_http_walker(url
.buf
);
780 walker
->get_tree
= 1;
781 walker
->get_history
= 1;
782 walker
->get_verbosely
= options
.verbosity
>= 3;
783 walker
->get_recover
= 0;
784 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
787 for (i
= 0; i
< nr_heads
; i
++)
791 return ret
? error("fetch failed.") : 0;
794 static int fetch_git(struct discovery
*heads
,
795 int nr_heads
, struct ref
**to_fetch
)
797 struct rpc_state rpc
;
798 struct strbuf preamble
= STRBUF_INIT
;
800 struct argv_array args
= ARGV_ARRAY_INIT
;
802 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
803 "--stdin", "--lock-pack", NULL
);
804 if (options
.followtags
)
805 argv_array_push(&args
, "--include-tag");
807 argv_array_push(&args
, "--thin");
808 if (options
.verbosity
>= 3)
809 argv_array_pushl(&args
, "-v", "-v", NULL
);
810 if (options
.check_self_contained_and_connected
)
811 argv_array_push(&args
, "--check-self-contained-and-connected");
813 argv_array_push(&args
, "--cloning");
814 if (options
.update_shallow
)
815 argv_array_push(&args
, "--update-shallow");
816 if (!options
.progress
)
817 argv_array_push(&args
, "--no-progress");
819 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
820 if (options
.deepen_since
)
821 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
822 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
823 argv_array_pushf(&args
, "--shallow-exclude=%s",
824 options
.deepen_not
.items
[i
].string
);
825 if (options
.deepen_relative
&& options
.depth
)
826 argv_array_push(&args
, "--deepen-relative");
827 argv_array_push(&args
, url
.buf
);
829 for (i
= 0; i
< nr_heads
; i
++) {
830 struct ref
*ref
= to_fetch
[i
];
832 die("cannot fetch by sha1 over smart http");
833 packet_buf_write(&preamble
, "%s %s\n",
834 oid_to_hex(&ref
->old_oid
), ref
->name
);
836 packet_buf_flush(&preamble
);
838 memset(&rpc
, 0, sizeof(rpc
));
839 rpc
.service_name
= "git-upload-pack",
840 rpc
.argv
= args
.argv
;
841 rpc
.stdin_preamble
= &preamble
;
842 rpc
.gzip_request
= 1;
844 err
= rpc_service(&rpc
, heads
);
846 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
847 strbuf_release(&rpc
.result
);
848 strbuf_release(&preamble
);
849 argv_array_clear(&args
);
853 static int fetch(int nr_heads
, struct ref
**to_fetch
)
855 struct discovery
*d
= discover_refs("git-upload-pack", 0);
857 return fetch_git(d
, nr_heads
, to_fetch
);
859 return fetch_dumb(nr_heads
, to_fetch
);
862 static void parse_fetch(struct strbuf
*buf
)
864 struct ref
**to_fetch
= NULL
;
865 struct ref
*list_head
= NULL
;
866 struct ref
**list
= &list_head
;
867 int alloc_heads
= 0, nr_heads
= 0;
871 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
874 struct object_id old_oid
;
876 if (get_oid_hex(p
, &old_oid
))
877 die("protocol error: expected sha/ref, got %s'", p
);
878 if (p
[GIT_SHA1_HEXSZ
] == ' ')
879 name
= p
+ GIT_SHA1_HEXSZ
+ 1;
880 else if (!p
[GIT_SHA1_HEXSZ
])
883 die("protocol error: expected sha/ref, got %s'", p
);
885 ref
= alloc_ref(name
);
886 oidcpy(&ref
->old_oid
, &old_oid
);
891 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
892 to_fetch
[nr_heads
++] = ref
;
895 die("http transport does not support %s", buf
->buf
);
898 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
904 if (fetch(nr_heads
, to_fetch
))
905 exit(128); /* error already reported */
906 free_refs(list_head
);
914 static int push_dav(int nr_spec
, char **specs
)
916 struct child_process child
= CHILD_PROCESS_INIT
;
920 argv_array_push(&child
.args
, "http-push");
921 argv_array_push(&child
.args
, "--helper-status");
923 argv_array_push(&child
.args
, "--dry-run");
924 if (options
.verbosity
> 1)
925 argv_array_push(&child
.args
, "--verbose");
926 argv_array_push(&child
.args
, url
.buf
);
927 for (i
= 0; i
< nr_spec
; i
++)
928 argv_array_push(&child
.args
, specs
[i
]);
930 if (run_command(&child
))
931 die("git-http-push failed");
935 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
937 struct rpc_state rpc
;
939 struct argv_array args
;
940 struct string_list_item
*cas_option
;
941 struct strbuf preamble
= STRBUF_INIT
;
943 argv_array_init(&args
);
944 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
948 argv_array_push(&args
, "--thin");
950 argv_array_push(&args
, "--dry-run");
951 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
952 argv_array_push(&args
, "--signed=yes");
953 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
954 argv_array_push(&args
, "--signed=if-asked");
955 if (options
.verbosity
== 0)
956 argv_array_push(&args
, "--quiet");
957 else if (options
.verbosity
> 1)
958 argv_array_push(&args
, "--verbose");
959 for (i
= 0; i
< options
.push_options
.nr
; i
++)
960 argv_array_pushf(&args
, "--push-option=%s",
961 options
.push_options
.items
[i
].string
);
962 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
963 for_each_string_list_item(cas_option
, &cas_options
)
964 argv_array_push(&args
, cas_option
->string
);
965 argv_array_push(&args
, url
.buf
);
967 argv_array_push(&args
, "--stdin");
968 for (i
= 0; i
< nr_spec
; i
++)
969 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
970 packet_buf_flush(&preamble
);
972 memset(&rpc
, 0, sizeof(rpc
));
973 rpc
.service_name
= "git-receive-pack",
974 rpc
.argv
= args
.argv
;
975 rpc
.stdin_preamble
= &preamble
;
977 err
= rpc_service(&rpc
, heads
);
979 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
980 strbuf_release(&rpc
.result
);
981 strbuf_release(&preamble
);
982 argv_array_clear(&args
);
986 static int push(int nr_spec
, char **specs
)
988 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
991 if (heads
->proto_git
)
992 ret
= push_git(heads
, nr_spec
, specs
);
994 ret
= push_dav(nr_spec
, specs
);
995 free_discovery(heads
);
999 static void parse_push(struct strbuf
*buf
)
1001 char **specs
= NULL
;
1002 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
1005 if (starts_with(buf
->buf
, "push ")) {
1006 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
1007 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
1010 die("http transport does not support %s", buf
->buf
);
1013 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1019 ret
= push(nr_spec
, specs
);
1024 exit(128); /* error already reported */
1027 for (i
= 0; i
< nr_spec
; i
++)
1032 int cmd_main(int argc
, const char **argv
)
1034 struct strbuf buf
= STRBUF_INIT
;
1037 setup_git_directory_gently(&nongit
);
1039 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1043 options
.verbosity
= 1;
1044 options
.progress
= !!isatty(2);
1046 string_list_init(&options
.deepen_not
, 1);
1047 string_list_init(&options
.push_options
, 1);
1049 remote
= remote_get(argv
[1]);
1052 end_url_with_slash(&url
, argv
[2]);
1054 end_url_with_slash(&url
, remote
->url
[0]);
1057 http_init(remote
, url
.buf
, 0);
1062 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1064 error("remote-curl: error reading command stream from git");
1069 if (starts_with(buf
.buf
, "fetch ")) {
1071 die("remote-curl: fetch attempted without a local repo");
1074 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1075 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1076 output_refs(get_refs(for_push
));
1078 } else if (starts_with(buf
.buf
, "push ")) {
1081 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1082 char *value
= strchr(arg
, ' ');
1090 result
= set_option(arg
, value
);
1093 else if (result
< 0)
1094 printf("error invalid value\n");
1096 printf("unsupported\n");
1099 } else if (!strcmp(buf
.buf
, "capabilities")) {
1103 printf("check-connectivity\n");
1107 error("remote-curl: unknown command '%s' from git", buf
.buf
);