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
;
28 unsigned progress
: 1,
29 check_self_contained_and_connected
: 1,
35 /* One of the SEND_PACK_PUSH_CERT_* constants. */
41 static struct options options
;
42 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
44 static int set_option(const char *name
, const char *value
)
46 if (!strcmp(name
, "verbosity")) {
48 int v
= strtol(value
, &end
, 10);
49 if (value
== end
|| *end
)
51 options
.verbosity
= v
;
54 else if (!strcmp(name
, "progress")) {
55 if (!strcmp(value
, "true"))
57 else if (!strcmp(value
, "false"))
63 else if (!strcmp(name
, "depth")) {
65 unsigned long v
= strtoul(value
, &end
, 10);
66 if (value
== end
|| *end
)
71 else if (!strcmp(name
, "deepen-since")) {
72 options
.deepen_since
= xstrdup(value
);
75 else if (!strcmp(name
, "deepen-not")) {
76 string_list_append(&options
.deepen_not
, value
);
79 else if (!strcmp(name
, "deepen-relative")) {
80 if (!strcmp(value
, "true"))
81 options
.deepen_relative
= 1;
82 else if (!strcmp(value
, "false"))
83 options
.deepen_relative
= 0;
88 else if (!strcmp(name
, "followtags")) {
89 if (!strcmp(value
, "true"))
90 options
.followtags
= 1;
91 else if (!strcmp(value
, "false"))
92 options
.followtags
= 0;
97 else if (!strcmp(name
, "dry-run")) {
98 if (!strcmp(value
, "true"))
100 else if (!strcmp(value
, "false"))
106 else if (!strcmp(name
, "check-connectivity")) {
107 if (!strcmp(value
, "true"))
108 options
.check_self_contained_and_connected
= 1;
109 else if (!strcmp(value
, "false"))
110 options
.check_self_contained_and_connected
= 0;
115 else if (!strcmp(name
, "cas")) {
116 struct strbuf val
= STRBUF_INIT
;
117 strbuf_addf(&val
, "--" CAS_OPT_NAME
"=%s", value
);
118 string_list_append(&cas_options
, val
.buf
);
119 strbuf_release(&val
);
121 } else if (!strcmp(name
, "cloning")) {
122 if (!strcmp(value
, "true"))
124 else if (!strcmp(value
, "false"))
129 } else if (!strcmp(name
, "update-shallow")) {
130 if (!strcmp(value
, "true"))
131 options
.update_shallow
= 1;
132 else if (!strcmp(value
, "false"))
133 options
.update_shallow
= 0;
137 } else if (!strcmp(name
, "pushcert")) {
138 if (!strcmp(value
, "true"))
139 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
140 else if (!strcmp(value
, "false"))
141 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
142 else if (!strcmp(value
, "if-asked"))
143 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
147 } else if (!strcmp(name
, "push-option")) {
148 string_list_append(&options
.push_options
, value
);
151 #if LIBCURL_VERSION_NUM >= 0x070a08
152 } else if (!strcmp(name
, "family")) {
153 if (!strcmp(value
, "ipv4"))
154 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
155 else if (!strcmp(value
, "ipv6"))
156 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
157 else if (!strcmp(value
, "all"))
158 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
162 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
163 } else if (!strcmp(name
, "from-promisor")) {
164 options
.from_promisor
= 1;
166 } else if (!strcmp(name
, "no-dependents")) {
167 options
.no_dependents
= 1;
169 } else if (!strcmp(name
, "filter")) {
170 options
.filter
= xstrdup(value
);;
173 return 1 /* unsupported */;
183 struct oid_array shallow
;
184 unsigned proto_git
: 1;
186 static struct discovery
*last_discovery
;
188 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
190 struct ref
*list
= NULL
;
191 get_remote_heads(-1, heads
->buf
, heads
->len
, &list
,
192 for_push
? REF_NORMAL
: 0, NULL
, &heads
->shallow
);
196 static struct ref
*parse_info_refs(struct discovery
*heads
)
198 char *data
, *start
, *mid
;
202 struct ref
*refs
= NULL
;
203 struct ref
*ref
= NULL
;
204 struct ref
*last_ref
= NULL
;
209 while (i
< heads
->len
) {
215 if (data
[i
] == '\n') {
216 if (mid
- start
!= 40)
217 die("%sinfo/refs not valid: is this a git repository?",
221 ref
= alloc_ref(ref_name
);
222 get_oid_hex(start
, &ref
->old_oid
);
226 last_ref
->next
= ref
;
233 ref
= alloc_ref("HEAD");
234 if (!http_fetch_ref(url
.buf
, ref
) &&
235 !resolve_remote_symref(ref
, refs
)) {
245 static void free_discovery(struct discovery
*d
)
248 if (d
== last_discovery
)
249 last_discovery
= NULL
;
250 free(d
->shallow
.oid
);
257 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
263 * We only show text/plain parts, as other types are likely
264 * to be ugly to look at on the user's terminal.
266 if (strcmp(type
->buf
, "text/plain"))
269 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
277 eol
= strchrnul(p
, '\n');
278 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
284 static struct discovery
*discover_refs(const char *service
, int for_push
)
286 struct strbuf exp
= STRBUF_INIT
;
287 struct strbuf type
= STRBUF_INIT
;
288 struct strbuf charset
= STRBUF_INIT
;
289 struct strbuf buffer
= STRBUF_INIT
;
290 struct strbuf refs_url
= STRBUF_INIT
;
291 struct strbuf effective_url
= STRBUF_INIT
;
292 struct discovery
*last
= last_discovery
;
293 int http_ret
, maybe_smart
= 0;
294 struct http_get_options http_options
;
296 if (last
&& !strcmp(service
, last
->service
))
298 free_discovery(last
);
300 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
301 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
302 git_env_bool("GIT_SMART_HTTP", 1)) {
304 if (!strchr(url
.buf
, '?'))
305 strbuf_addch(&refs_url
, '?');
307 strbuf_addch(&refs_url
, '&');
308 strbuf_addf(&refs_url
, "service=%s", service
);
311 memset(&http_options
, 0, sizeof(http_options
));
312 http_options
.content_type
= &type
;
313 http_options
.charset
= &charset
;
314 http_options
.effective_url
= &effective_url
;
315 http_options
.base_url
= &url
;
316 http_options
.initial_request
= 1;
317 http_options
.no_cache
= 1;
318 http_options
.keep_error
= 1;
320 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
324 case HTTP_MISSING_TARGET
:
325 show_http_message(&type
, &charset
, &buffer
);
326 die("repository '%s' not found", url
.buf
);
328 show_http_message(&type
, &charset
, &buffer
);
329 die("Authentication failed for '%s'", url
.buf
);
331 show_http_message(&type
, &charset
, &buffer
);
332 die("unable to access '%s': %s", url
.buf
, curl_errorstr
);
335 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
))
336 warning(_("redirecting to %s"), url
.buf
);
338 last
= xcalloc(1, sizeof(*last_discovery
));
339 last
->service
= service
;
340 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
341 last
->buf
= last
->buf_alloc
;
343 strbuf_addf(&exp
, "application/x-%s-advertisement", service
);
345 (5 <= last
->len
&& last
->buf
[4] == '#') &&
346 !strbuf_cmp(&exp
, &type
)) {
350 * smart HTTP response; validate that the service
351 * pkt-line matches our request.
353 line
= packet_read_line_buf(&last
->buf
, &last
->len
, NULL
);
355 die("invalid server response; expected service, got flush packet");
358 strbuf_addf(&exp
, "# service=%s", service
);
359 if (strcmp(line
, exp
.buf
))
360 die("invalid server response; got '%s'", line
);
361 strbuf_release(&exp
);
363 /* The header can include additional metadata lines, up
364 * until a packet flush marker. Ignore these now, but
365 * in the future we might start to scan them.
367 while (packet_read_line_buf(&last
->buf
, &last
->len
, NULL
))
374 last
->refs
= parse_git_refs(last
, for_push
);
376 last
->refs
= parse_info_refs(last
);
378 strbuf_release(&refs_url
);
379 strbuf_release(&exp
);
380 strbuf_release(&type
);
381 strbuf_release(&charset
);
382 strbuf_release(&effective_url
);
383 strbuf_release(&buffer
);
384 last_discovery
= last
;
388 static struct ref
*get_refs(int for_push
)
390 struct discovery
*heads
;
393 heads
= discover_refs("git-receive-pack", for_push
);
395 heads
= discover_refs("git-upload-pack", for_push
);
400 static void output_refs(struct ref
*refs
)
403 for (posn
= refs
; posn
; posn
= posn
->next
) {
405 printf("@%s %s\n", posn
->symref
, posn
->name
);
407 printf("%s %s\n", oid_to_hex(&posn
->old_oid
), posn
->name
);
414 const char *service_name
;
416 struct strbuf
*stdin_preamble
;
418 char *hdr_content_type
;
427 struct strbuf result
;
428 unsigned gzip_request
: 1;
429 unsigned initial_buffer
: 1;
432 static size_t rpc_out(void *ptr
, size_t eltsize
,
433 size_t nmemb
, void *buffer_
)
435 size_t max
= eltsize
* nmemb
;
436 struct rpc_state
*rpc
= buffer_
;
437 size_t avail
= rpc
->len
- rpc
->pos
;
440 rpc
->initial_buffer
= 0;
441 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
450 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
455 #ifndef NO_CURL_IOCTL
456 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
458 struct rpc_state
*rpc
= clientp
;
464 case CURLIOCMD_RESTARTREAD
:
465 if (rpc
->initial_buffer
) {
469 error("unable to rewind rpc post data - try increasing http.postBuffer");
470 return CURLIOE_FAILRESTART
;
473 return CURLIOE_UNKNOWNCMD
;
478 static size_t rpc_in(char *ptr
, size_t eltsize
,
479 size_t nmemb
, void *buffer_
)
481 size_t size
= eltsize
* nmemb
;
482 struct rpc_state
*rpc
= buffer_
;
484 rpc
->any_written
= 1;
485 write_or_die(rpc
->in
, ptr
, size
);
489 static int run_slot(struct active_request_slot
*slot
,
490 struct slot_results
*results
)
493 struct slot_results results_buf
;
496 results
= &results_buf
;
498 err
= run_one_slot(slot
, results
);
500 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
501 struct strbuf msg
= STRBUF_INIT
;
502 if (results
->http_code
&& results
->http_code
!= 200)
503 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
504 if (results
->curl_result
!= CURLE_OK
) {
506 strbuf_addch(&msg
, ' ');
507 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
508 if (curl_errorstr
[0]) {
509 strbuf_addch(&msg
, ' ');
510 strbuf_addstr(&msg
, curl_errorstr
);
513 error("RPC failed; %s", msg
.buf
);
514 strbuf_release(&msg
);
520 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
522 struct active_request_slot
*slot
;
523 struct curl_slist
*headers
= http_copy_default_headers();
524 struct strbuf buf
= STRBUF_INIT
;
527 slot
= get_active_slot();
529 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
530 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
532 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
533 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
534 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
535 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
536 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
537 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
538 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
539 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
540 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
542 err
= run_slot(slot
, results
);
544 curl_slist_free_all(headers
);
545 strbuf_release(&buf
);
549 static curl_off_t
xcurl_off_t(ssize_t len
) {
550 if (len
> maximum_signed_value_of_type(curl_off_t
))
551 die("cannot handle pushes this big");
552 return (curl_off_t
) len
;
555 static int post_rpc(struct rpc_state
*rpc
)
557 struct active_request_slot
*slot
;
558 struct curl_slist
*headers
= http_copy_default_headers();
559 int use_gzip
= rpc
->gzip_request
;
560 char *gzip_body
= NULL
;
561 size_t gzip_size
= 0;
562 int err
, large_request
= 0;
563 int needs_100_continue
= 0;
565 /* Try to load the entire request, if we can fit it into the
566 * allocated buffer space we can use HTTP/1.0 and avoid the
567 * chunked encoding mess.
570 size_t left
= rpc
->alloc
- rpc
->len
;
571 char *buf
= rpc
->buf
+ rpc
->len
;
574 if (left
< LARGE_PACKET_MAX
) {
580 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
587 struct slot_results results
;
590 err
= probe_rpc(rpc
, &results
);
591 if (err
== HTTP_REAUTH
)
592 credential_fill(&http_auth
);
593 } while (err
== HTTP_REAUTH
);
597 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
598 needs_100_continue
= 1;
601 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
602 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
603 headers
= curl_slist_append(headers
, needs_100_continue
?
604 "Expect: 100-continue" : "Expect:");
607 slot
= get_active_slot();
609 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
610 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
611 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
612 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
615 /* The request body is large and the size cannot be predicted.
616 * We must use chunked encoding to send it.
618 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
619 rpc
->initial_buffer
= 1;
620 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
621 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
622 #ifndef NO_CURL_IOCTL
623 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
624 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
626 if (options
.verbosity
> 1) {
627 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
631 } else if (gzip_body
) {
633 * If we are looping to retry authentication, then the previous
634 * run will have set up the headers and gzip buffer already,
635 * and we just need to send it.
637 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
638 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
640 } else if (use_gzip
&& 1024 < rpc
->len
) {
641 /* The client backend isn't giving us compressed data so
642 * we can try to deflate it ourselves, this may save on.
648 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
649 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
650 gzip_body
= xmalloc(gzip_size
);
652 stream
.next_in
= (unsigned char *)rpc
->buf
;
653 stream
.avail_in
= rpc
->len
;
654 stream
.next_out
= (unsigned char *)gzip_body
;
655 stream
.avail_out
= gzip_size
;
657 ret
= git_deflate(&stream
, Z_FINISH
);
658 if (ret
!= Z_STREAM_END
)
659 die("cannot deflate request; zlib deflate error %d", ret
);
661 ret
= git_deflate_end_gently(&stream
);
663 die("cannot deflate request; zlib end error %d", ret
);
665 gzip_size
= stream
.total_out
;
667 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
668 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
669 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
671 if (options
.verbosity
> 1) {
672 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
674 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
678 /* We know the complete request size in advance, use the
679 * more normal Content-Length approach.
681 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
682 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
683 if (options
.verbosity
> 1) {
684 fprintf(stderr
, "POST %s (%lu bytes)\n",
685 rpc
->service_name
, (unsigned long)rpc
->len
);
690 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
691 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
692 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
695 rpc
->any_written
= 0;
696 err
= run_slot(slot
, NULL
);
697 if (err
== HTTP_REAUTH
&& !large_request
) {
698 credential_fill(&http_auth
);
704 if (!rpc
->any_written
)
707 curl_slist_free_all(headers
);
712 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
714 const char *svc
= rpc
->service_name
;
715 struct strbuf buf
= STRBUF_INIT
;
716 struct strbuf
*preamble
= rpc
->stdin_preamble
;
717 struct child_process client
= CHILD_PROCESS_INIT
;
723 client
.argv
= rpc
->argv
;
724 if (start_command(&client
))
727 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
729 write_or_die(client
.in
, heads
->buf
, heads
->len
);
731 rpc
->alloc
= http_post_buffer
;
732 rpc
->buf
= xmalloc(rpc
->alloc
);
734 rpc
->out
= client
.out
;
735 strbuf_init(&rpc
->result
, 0);
737 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
738 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
740 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
741 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
743 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
744 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
747 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
752 err
|= post_rpc(rpc
);
758 strbuf_read(&rpc
->result
, client
.out
, 0);
762 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
769 err
|= finish_command(&client
);
770 free(rpc
->service_url
);
771 free(rpc
->hdr_content_type
);
772 free(rpc
->hdr_accept
);
774 strbuf_release(&buf
);
778 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
780 struct walker
*walker
;
784 ALLOC_ARRAY(targets
, nr_heads
);
785 if (options
.depth
|| options
.deepen_since
)
786 die("dumb http transport does not support shallow capabilities");
787 for (i
= 0; i
< nr_heads
; i
++)
788 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
790 walker
= get_http_walker(url
.buf
);
792 walker
->get_tree
= 1;
793 walker
->get_history
= 1;
794 walker
->get_verbosely
= options
.verbosity
>= 3;
795 walker
->get_recover
= 0;
796 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
799 for (i
= 0; i
< nr_heads
; i
++)
803 return ret
? error("fetch failed.") : 0;
806 static int fetch_git(struct discovery
*heads
,
807 int nr_heads
, struct ref
**to_fetch
)
809 struct rpc_state rpc
;
810 struct strbuf preamble
= STRBUF_INIT
;
812 struct argv_array args
= ARGV_ARRAY_INIT
;
814 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
815 "--stdin", "--lock-pack", NULL
);
816 if (options
.followtags
)
817 argv_array_push(&args
, "--include-tag");
819 argv_array_push(&args
, "--thin");
820 if (options
.verbosity
>= 3)
821 argv_array_pushl(&args
, "-v", "-v", NULL
);
822 if (options
.check_self_contained_and_connected
)
823 argv_array_push(&args
, "--check-self-contained-and-connected");
825 argv_array_push(&args
, "--cloning");
826 if (options
.update_shallow
)
827 argv_array_push(&args
, "--update-shallow");
828 if (!options
.progress
)
829 argv_array_push(&args
, "--no-progress");
831 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
832 if (options
.deepen_since
)
833 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
834 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
835 argv_array_pushf(&args
, "--shallow-exclude=%s",
836 options
.deepen_not
.items
[i
].string
);
837 if (options
.deepen_relative
&& options
.depth
)
838 argv_array_push(&args
, "--deepen-relative");
839 if (options
.from_promisor
)
840 argv_array_push(&args
, "--from-promisor");
841 if (options
.no_dependents
)
842 argv_array_push(&args
, "--no-dependents");
844 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
845 argv_array_push(&args
, url
.buf
);
847 for (i
= 0; i
< nr_heads
; i
++) {
848 struct ref
*ref
= to_fetch
[i
];
850 die("cannot fetch by sha1 over smart http");
851 packet_buf_write(&preamble
, "%s %s\n",
852 oid_to_hex(&ref
->old_oid
), ref
->name
);
854 packet_buf_flush(&preamble
);
856 memset(&rpc
, 0, sizeof(rpc
));
857 rpc
.service_name
= "git-upload-pack",
858 rpc
.argv
= args
.argv
;
859 rpc
.stdin_preamble
= &preamble
;
860 rpc
.gzip_request
= 1;
862 err
= rpc_service(&rpc
, heads
);
864 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
865 strbuf_release(&rpc
.result
);
866 strbuf_release(&preamble
);
867 argv_array_clear(&args
);
871 static int fetch(int nr_heads
, struct ref
**to_fetch
)
873 struct discovery
*d
= discover_refs("git-upload-pack", 0);
875 return fetch_git(d
, nr_heads
, to_fetch
);
877 return fetch_dumb(nr_heads
, to_fetch
);
880 static void parse_fetch(struct strbuf
*buf
)
882 struct ref
**to_fetch
= NULL
;
883 struct ref
*list_head
= NULL
;
884 struct ref
**list
= &list_head
;
885 int alloc_heads
= 0, nr_heads
= 0;
889 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
892 struct object_id old_oid
;
894 if (get_oid_hex(p
, &old_oid
))
895 die("protocol error: expected sha/ref, got %s'", p
);
896 if (p
[GIT_SHA1_HEXSZ
] == ' ')
897 name
= p
+ GIT_SHA1_HEXSZ
+ 1;
898 else if (!p
[GIT_SHA1_HEXSZ
])
901 die("protocol error: expected sha/ref, got %s'", p
);
903 ref
= alloc_ref(name
);
904 oidcpy(&ref
->old_oid
, &old_oid
);
909 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
910 to_fetch
[nr_heads
++] = ref
;
913 die("http transport does not support %s", buf
->buf
);
916 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
922 if (fetch(nr_heads
, to_fetch
))
923 exit(128); /* error already reported */
924 free_refs(list_head
);
932 static int push_dav(int nr_spec
, char **specs
)
934 struct child_process child
= CHILD_PROCESS_INIT
;
938 argv_array_push(&child
.args
, "http-push");
939 argv_array_push(&child
.args
, "--helper-status");
941 argv_array_push(&child
.args
, "--dry-run");
942 if (options
.verbosity
> 1)
943 argv_array_push(&child
.args
, "--verbose");
944 argv_array_push(&child
.args
, url
.buf
);
945 for (i
= 0; i
< nr_spec
; i
++)
946 argv_array_push(&child
.args
, specs
[i
]);
948 if (run_command(&child
))
949 die("git-http-push failed");
953 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
955 struct rpc_state rpc
;
957 struct argv_array args
;
958 struct string_list_item
*cas_option
;
959 struct strbuf preamble
= STRBUF_INIT
;
961 argv_array_init(&args
);
962 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
966 argv_array_push(&args
, "--thin");
968 argv_array_push(&args
, "--dry-run");
969 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
970 argv_array_push(&args
, "--signed=yes");
971 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
972 argv_array_push(&args
, "--signed=if-asked");
973 if (options
.verbosity
== 0)
974 argv_array_push(&args
, "--quiet");
975 else if (options
.verbosity
> 1)
976 argv_array_push(&args
, "--verbose");
977 for (i
= 0; i
< options
.push_options
.nr
; i
++)
978 argv_array_pushf(&args
, "--push-option=%s",
979 options
.push_options
.items
[i
].string
);
980 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
981 for_each_string_list_item(cas_option
, &cas_options
)
982 argv_array_push(&args
, cas_option
->string
);
983 argv_array_push(&args
, url
.buf
);
985 argv_array_push(&args
, "--stdin");
986 for (i
= 0; i
< nr_spec
; i
++)
987 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
988 packet_buf_flush(&preamble
);
990 memset(&rpc
, 0, sizeof(rpc
));
991 rpc
.service_name
= "git-receive-pack",
992 rpc
.argv
= args
.argv
;
993 rpc
.stdin_preamble
= &preamble
;
995 err
= rpc_service(&rpc
, heads
);
997 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
998 strbuf_release(&rpc
.result
);
999 strbuf_release(&preamble
);
1000 argv_array_clear(&args
);
1004 static int push(int nr_spec
, char **specs
)
1006 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1009 if (heads
->proto_git
)
1010 ret
= push_git(heads
, nr_spec
, specs
);
1012 ret
= push_dav(nr_spec
, specs
);
1013 free_discovery(heads
);
1017 static void parse_push(struct strbuf
*buf
)
1019 char **specs
= NULL
;
1020 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
1023 if (starts_with(buf
->buf
, "push ")) {
1024 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
1025 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
1028 die("http transport does not support %s", buf
->buf
);
1031 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1037 ret
= push(nr_spec
, specs
);
1042 exit(128); /* error already reported */
1045 for (i
= 0; i
< nr_spec
; i
++)
1050 int cmd_main(int argc
, const char **argv
)
1052 struct strbuf buf
= STRBUF_INIT
;
1055 setup_git_directory_gently(&nongit
);
1057 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1061 options
.verbosity
= 1;
1062 options
.progress
= !!isatty(2);
1064 string_list_init(&options
.deepen_not
, 1);
1065 string_list_init(&options
.push_options
, 1);
1067 remote
= remote_get(argv
[1]);
1070 end_url_with_slash(&url
, argv
[2]);
1072 end_url_with_slash(&url
, remote
->url
[0]);
1075 http_init(remote
, url
.buf
, 0);
1080 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1082 error("remote-curl: error reading command stream from git");
1087 if (starts_with(buf
.buf
, "fetch ")) {
1089 die("remote-curl: fetch attempted without a local repo");
1092 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1093 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1094 output_refs(get_refs(for_push
));
1096 } else if (starts_with(buf
.buf
, "push ")) {
1099 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1100 char *value
= strchr(arg
, ' ');
1108 result
= set_option(arg
, value
);
1111 else if (result
< 0)
1112 printf("error invalid value\n");
1114 printf("unsupported\n");
1117 } else if (!strcmp(buf
.buf
, "capabilities")) {
1121 printf("check-connectivity\n");
1125 error("remote-curl: unknown command '%s' from git", buf
.buf
);