7 #include "run-command.h"
9 #include "string-list.h"
11 #include "argv-array.h"
12 #include "credential.h"
13 #include "sha1-array.h"
15 static struct remote
*remote
;
16 /* always ends with a trailing slash */
17 static struct strbuf url
= STRBUF_INIT
;
22 unsigned progress
: 1,
23 check_self_contained_and_connected
: 1,
30 static struct options options
;
31 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
33 static int set_option(const char *name
, const char *value
)
35 if (!strcmp(name
, "verbosity")) {
37 int v
= strtol(value
, &end
, 10);
38 if (value
== end
|| *end
)
40 options
.verbosity
= v
;
43 else if (!strcmp(name
, "progress")) {
44 if (!strcmp(value
, "true"))
46 else if (!strcmp(value
, "false"))
52 else if (!strcmp(name
, "depth")) {
54 unsigned long v
= strtoul(value
, &end
, 10);
55 if (value
== end
|| *end
)
60 else if (!strcmp(name
, "followtags")) {
61 if (!strcmp(value
, "true"))
62 options
.followtags
= 1;
63 else if (!strcmp(value
, "false"))
64 options
.followtags
= 0;
69 else if (!strcmp(name
, "dry-run")) {
70 if (!strcmp(value
, "true"))
72 else if (!strcmp(value
, "false"))
78 else if (!strcmp(name
, "check-connectivity")) {
79 if (!strcmp(value
, "true"))
80 options
.check_self_contained_and_connected
= 1;
81 else if (!strcmp(value
, "false"))
82 options
.check_self_contained_and_connected
= 0;
87 else if (!strcmp(name
, "cas")) {
88 struct strbuf val
= STRBUF_INIT
;
89 strbuf_addf(&val
, "--" CAS_OPT_NAME
"=%s", value
);
90 string_list_append(&cas_options
, val
.buf
);
93 } else if (!strcmp(name
, "cloning")) {
94 if (!strcmp(value
, "true"))
96 else if (!strcmp(value
, "false"))
101 } else if (!strcmp(name
, "update-shallow")) {
102 if (!strcmp(value
, "true"))
103 options
.update_shallow
= 1;
104 else if (!strcmp(value
, "false"))
105 options
.update_shallow
= 0;
110 return 1 /* unsupported */;
120 struct sha1_array shallow
;
121 unsigned proto_git
: 1;
123 static struct discovery
*last_discovery
;
125 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
127 struct ref
*list
= NULL
;
128 get_remote_heads(-1, heads
->buf
, heads
->len
, &list
,
129 for_push
? REF_NORMAL
: 0, NULL
, &heads
->shallow
);
133 static struct ref
*parse_info_refs(struct discovery
*heads
)
135 char *data
, *start
, *mid
;
139 struct ref
*refs
= NULL
;
140 struct ref
*ref
= NULL
;
141 struct ref
*last_ref
= NULL
;
146 while (i
< heads
->len
) {
152 if (data
[i
] == '\n') {
153 if (mid
- start
!= 40)
154 die("%sinfo/refs not valid: is this a git repository?",
158 ref
= xmalloc(sizeof(struct ref
) +
159 strlen(ref_name
) + 1);
160 memset(ref
, 0, sizeof(struct ref
));
161 strcpy(ref
->name
, ref_name
);
162 get_sha1_hex(start
, ref
->old_sha1
);
166 last_ref
->next
= ref
;
173 ref
= alloc_ref("HEAD");
174 if (!http_fetch_ref(url
.buf
, ref
) &&
175 !resolve_remote_symref(ref
, refs
)) {
185 static void free_discovery(struct discovery
*d
)
188 if (d
== last_discovery
)
189 last_discovery
= NULL
;
190 free(d
->shallow
.sha1
);
197 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
203 * We only show text/plain parts, as other types are likely
204 * to be ugly to look at on the user's terminal.
206 if (strcmp(type
->buf
, "text/plain"))
209 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
217 eol
= strchrnul(p
, '\n');
218 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
224 static struct discovery
*discover_refs(const char *service
, int for_push
)
226 struct strbuf exp
= STRBUF_INIT
;
227 struct strbuf type
= STRBUF_INIT
;
228 struct strbuf charset
= STRBUF_INIT
;
229 struct strbuf buffer
= STRBUF_INIT
;
230 struct strbuf refs_url
= STRBUF_INIT
;
231 struct strbuf effective_url
= STRBUF_INIT
;
232 struct discovery
*last
= last_discovery
;
233 int http_ret
, maybe_smart
= 0;
234 struct http_get_options options
;
236 if (last
&& !strcmp(service
, last
->service
))
238 free_discovery(last
);
240 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
241 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
242 git_env_bool("GIT_SMART_HTTP", 1)) {
244 if (!strchr(url
.buf
, '?'))
245 strbuf_addch(&refs_url
, '?');
247 strbuf_addch(&refs_url
, '&');
248 strbuf_addf(&refs_url
, "service=%s", service
);
251 memset(&options
, 0, sizeof(options
));
252 options
.content_type
= &type
;
253 options
.charset
= &charset
;
254 options
.effective_url
= &effective_url
;
255 options
.base_url
= &url
;
256 options
.no_cache
= 1;
257 options
.keep_error
= 1;
259 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &options
);
263 case HTTP_MISSING_TARGET
:
264 show_http_message(&type
, &charset
, &buffer
);
265 die("repository '%s' not found", url
.buf
);
267 show_http_message(&type
, &charset
, &buffer
);
268 die("Authentication failed for '%s'", url
.buf
);
270 show_http_message(&type
, &charset
, &buffer
);
271 die("unable to access '%s': %s", url
.buf
, curl_errorstr
);
274 last
= xcalloc(1, sizeof(*last_discovery
));
275 last
->service
= service
;
276 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
277 last
->buf
= last
->buf_alloc
;
279 strbuf_addf(&exp
, "application/x-%s-advertisement", service
);
281 (5 <= last
->len
&& last
->buf
[4] == '#') &&
282 !strbuf_cmp(&exp
, &type
)) {
286 * smart HTTP response; validate that the service
287 * pkt-line matches our request.
289 line
= packet_read_line_buf(&last
->buf
, &last
->len
, NULL
);
292 strbuf_addf(&exp
, "# service=%s", service
);
293 if (strcmp(line
, exp
.buf
))
294 die("invalid server response; got '%s'", line
);
295 strbuf_release(&exp
);
297 /* The header can include additional metadata lines, up
298 * until a packet flush marker. Ignore these now, but
299 * in the future we might start to scan them.
301 while (packet_read_line_buf(&last
->buf
, &last
->len
, NULL
))
308 last
->refs
= parse_git_refs(last
, for_push
);
310 last
->refs
= parse_info_refs(last
);
312 strbuf_release(&refs_url
);
313 strbuf_release(&exp
);
314 strbuf_release(&type
);
315 strbuf_release(&charset
);
316 strbuf_release(&effective_url
);
317 strbuf_release(&buffer
);
318 last_discovery
= last
;
322 static struct ref
*get_refs(int for_push
)
324 struct discovery
*heads
;
327 heads
= discover_refs("git-receive-pack", for_push
);
329 heads
= discover_refs("git-upload-pack", for_push
);
334 static void output_refs(struct ref
*refs
)
337 for (posn
= refs
; posn
; posn
= posn
->next
) {
339 printf("@%s %s\n", posn
->symref
, posn
->name
);
341 printf("%s %s\n", sha1_to_hex(posn
->old_sha1
), posn
->name
);
348 const char *service_name
;
350 struct strbuf
*stdin_preamble
;
352 char *hdr_content_type
;
360 struct strbuf result
;
361 unsigned gzip_request
: 1;
362 unsigned initial_buffer
: 1;
365 static size_t rpc_out(void *ptr
, size_t eltsize
,
366 size_t nmemb
, void *buffer_
)
368 size_t max
= eltsize
* nmemb
;
369 struct rpc_state
*rpc
= buffer_
;
370 size_t avail
= rpc
->len
- rpc
->pos
;
373 rpc
->initial_buffer
= 0;
374 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
383 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
388 #ifndef NO_CURL_IOCTL
389 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
391 struct rpc_state
*rpc
= clientp
;
397 case CURLIOCMD_RESTARTREAD
:
398 if (rpc
->initial_buffer
) {
402 error("unable to rewind rpc post data - try increasing http.postBuffer");
403 return CURLIOE_FAILRESTART
;
406 return CURLIOE_UNKNOWNCMD
;
411 static size_t rpc_in(char *ptr
, size_t eltsize
,
412 size_t nmemb
, void *buffer_
)
414 size_t size
= eltsize
* nmemb
;
415 struct rpc_state
*rpc
= buffer_
;
416 write_or_die(rpc
->in
, ptr
, size
);
420 static int run_slot(struct active_request_slot
*slot
,
421 struct slot_results
*results
)
424 struct slot_results results_buf
;
427 results
= &results_buf
;
429 err
= run_one_slot(slot
, results
);
431 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
432 error("RPC failed; result=%d, HTTP code = %ld",
433 results
->curl_result
, results
->http_code
);
439 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
441 struct active_request_slot
*slot
;
442 struct curl_slist
*headers
= NULL
;
443 struct strbuf buf
= STRBUF_INIT
;
446 slot
= get_active_slot();
448 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
449 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
451 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
452 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
453 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
454 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
455 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
456 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
457 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
458 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
459 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
461 err
= run_slot(slot
, results
);
463 curl_slist_free_all(headers
);
464 strbuf_release(&buf
);
468 static int post_rpc(struct rpc_state
*rpc
)
470 struct active_request_slot
*slot
;
471 struct curl_slist
*headers
= NULL
;
472 int use_gzip
= rpc
->gzip_request
;
473 char *gzip_body
= NULL
;
474 size_t gzip_size
= 0;
475 int err
, large_request
= 0;
476 int needs_100_continue
= 0;
478 /* Try to load the entire request, if we can fit it into the
479 * allocated buffer space we can use HTTP/1.0 and avoid the
480 * chunked encoding mess.
483 size_t left
= rpc
->alloc
- rpc
->len
;
484 char *buf
= rpc
->buf
+ rpc
->len
;
487 if (left
< LARGE_PACKET_MAX
) {
493 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
500 struct slot_results results
;
503 err
= probe_rpc(rpc
, &results
);
504 if (err
== HTTP_REAUTH
)
505 credential_fill(&http_auth
);
506 } while (err
== HTTP_REAUTH
);
510 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
511 needs_100_continue
= 1;
514 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
515 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
516 headers
= curl_slist_append(headers
, needs_100_continue
?
517 "Expect: 100-continue" : "Expect:");
520 slot
= get_active_slot();
522 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
523 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
524 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
525 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
528 /* The request body is large and the size cannot be predicted.
529 * We must use chunked encoding to send it.
531 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
532 rpc
->initial_buffer
= 1;
533 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
534 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
535 #ifndef NO_CURL_IOCTL
536 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
537 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
539 if (options
.verbosity
> 1) {
540 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
544 } else if (gzip_body
) {
546 * If we are looping to retry authentication, then the previous
547 * run will have set up the headers and gzip buffer already,
548 * and we just need to send it.
550 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
551 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, gzip_size
);
553 } else if (use_gzip
&& 1024 < rpc
->len
) {
554 /* The client backend isn't giving us compressed data so
555 * we can try to deflate it ourselves, this may save on.
561 memset(&stream
, 0, sizeof(stream
));
562 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
563 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
564 gzip_body
= xmalloc(gzip_size
);
566 stream
.next_in
= (unsigned char *)rpc
->buf
;
567 stream
.avail_in
= rpc
->len
;
568 stream
.next_out
= (unsigned char *)gzip_body
;
569 stream
.avail_out
= gzip_size
;
571 ret
= git_deflate(&stream
, Z_FINISH
);
572 if (ret
!= Z_STREAM_END
)
573 die("cannot deflate request; zlib deflate error %d", ret
);
575 ret
= git_deflate_end_gently(&stream
);
577 die("cannot deflate request; zlib end error %d", ret
);
579 gzip_size
= stream
.total_out
;
581 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
582 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
583 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, gzip_size
);
585 if (options
.verbosity
> 1) {
586 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
588 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
592 /* We know the complete request size in advance, use the
593 * more normal Content-Length approach.
595 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
596 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, rpc
->len
);
597 if (options
.verbosity
> 1) {
598 fprintf(stderr
, "POST %s (%lu bytes)\n",
599 rpc
->service_name
, (unsigned long)rpc
->len
);
604 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
605 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
606 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
608 err
= run_slot(slot
, NULL
);
609 if (err
== HTTP_REAUTH
&& !large_request
) {
610 credential_fill(&http_auth
);
616 curl_slist_free_all(headers
);
621 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
623 const char *svc
= rpc
->service_name
;
624 struct strbuf buf
= STRBUF_INIT
;
625 struct strbuf
*preamble
= rpc
->stdin_preamble
;
626 struct child_process client
= CHILD_PROCESS_INIT
;
632 client
.argv
= rpc
->argv
;
633 if (start_command(&client
))
636 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
638 write_or_die(client
.in
, heads
->buf
, heads
->len
);
640 rpc
->alloc
= http_post_buffer
;
641 rpc
->buf
= xmalloc(rpc
->alloc
);
643 rpc
->out
= client
.out
;
644 strbuf_init(&rpc
->result
, 0);
646 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
647 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
649 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
650 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
652 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
653 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
656 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
661 err
|= post_rpc(rpc
);
667 strbuf_read(&rpc
->result
, client
.out
, 0);
671 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
678 err
|= finish_command(&client
);
679 free(rpc
->service_url
);
680 free(rpc
->hdr_content_type
);
681 free(rpc
->hdr_accept
);
683 strbuf_release(&buf
);
687 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
689 struct walker
*walker
;
690 char **targets
= xmalloc(nr_heads
* sizeof(char*));
694 die("dumb http transport does not support --depth");
695 for (i
= 0; i
< nr_heads
; i
++)
696 targets
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
698 walker
= get_http_walker(url
.buf
);
700 walker
->get_tree
= 1;
701 walker
->get_history
= 1;
702 walker
->get_verbosely
= options
.verbosity
>= 3;
703 walker
->get_recover
= 0;
704 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
707 for (i
= 0; i
< nr_heads
; i
++)
711 return ret
? error("fetch failed.") : 0;
714 static int fetch_git(struct discovery
*heads
,
715 int nr_heads
, struct ref
**to_fetch
)
717 struct rpc_state rpc
;
718 struct strbuf preamble
= STRBUF_INIT
;
719 char *depth_arg
= NULL
;
720 int argc
= 0, i
, err
;
721 const char *argv
[17];
723 argv
[argc
++] = "fetch-pack";
724 argv
[argc
++] = "--stateless-rpc";
725 argv
[argc
++] = "--stdin";
726 argv
[argc
++] = "--lock-pack";
727 if (options
.followtags
)
728 argv
[argc
++] = "--include-tag";
730 argv
[argc
++] = "--thin";
731 if (options
.verbosity
>= 3) {
735 if (options
.check_self_contained_and_connected
)
736 argv
[argc
++] = "--check-self-contained-and-connected";
738 argv
[argc
++] = "--cloning";
739 if (options
.update_shallow
)
740 argv
[argc
++] = "--update-shallow";
741 if (!options
.progress
)
742 argv
[argc
++] = "--no-progress";
744 struct strbuf buf
= STRBUF_INIT
;
745 strbuf_addf(&buf
, "--depth=%lu", options
.depth
);
746 depth_arg
= strbuf_detach(&buf
, NULL
);
747 argv
[argc
++] = depth_arg
;
749 argv
[argc
++] = url
.buf
;
752 for (i
= 0; i
< nr_heads
; i
++) {
753 struct ref
*ref
= to_fetch
[i
];
754 if (!ref
->name
|| !*ref
->name
)
755 die("cannot fetch by sha1 over smart http");
756 packet_buf_write(&preamble
, "%s %s\n",
757 sha1_to_hex(ref
->old_sha1
), ref
->name
);
759 packet_buf_flush(&preamble
);
761 memset(&rpc
, 0, sizeof(rpc
));
762 rpc
.service_name
= "git-upload-pack",
764 rpc
.stdin_preamble
= &preamble
;
765 rpc
.gzip_request
= 1;
767 err
= rpc_service(&rpc
, heads
);
769 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
770 strbuf_release(&rpc
.result
);
771 strbuf_release(&preamble
);
776 static int fetch(int nr_heads
, struct ref
**to_fetch
)
778 struct discovery
*d
= discover_refs("git-upload-pack", 0);
780 return fetch_git(d
, nr_heads
, to_fetch
);
782 return fetch_dumb(nr_heads
, to_fetch
);
785 static void parse_fetch(struct strbuf
*buf
)
787 struct ref
**to_fetch
= NULL
;
788 struct ref
*list_head
= NULL
;
789 struct ref
**list
= &list_head
;
790 int alloc_heads
= 0, nr_heads
= 0;
794 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
797 unsigned char old_sha1
[20];
799 if (strlen(p
) < 40 || get_sha1_hex(p
, old_sha1
))
800 die("protocol error: expected sha/ref, got %s'", p
);
806 die("protocol error: expected sha/ref, got %s'", p
);
808 ref
= alloc_ref(name
);
809 hashcpy(ref
->old_sha1
, old_sha1
);
814 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
815 to_fetch
[nr_heads
++] = ref
;
818 die("http transport does not support %s", buf
->buf
);
821 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
827 if (fetch(nr_heads
, to_fetch
))
828 exit(128); /* error already reported */
829 free_refs(list_head
);
837 static int push_dav(int nr_spec
, char **specs
)
839 const char **argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
842 argv
[argc
++] = "http-push";
843 argv
[argc
++] = "--helper-status";
845 argv
[argc
++] = "--dry-run";
846 if (options
.verbosity
> 1)
847 argv
[argc
++] = "--verbose";
848 argv
[argc
++] = url
.buf
;
849 for (i
= 0; i
< nr_spec
; i
++)
850 argv
[argc
++] = specs
[i
];
853 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
854 die("git-%s failed", argv
[0]);
859 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
861 struct rpc_state rpc
;
863 struct argv_array args
;
864 struct string_list_item
*cas_option
;
865 struct strbuf preamble
= STRBUF_INIT
;
867 argv_array_init(&args
);
868 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
872 argv_array_push(&args
, "--thin");
874 argv_array_push(&args
, "--dry-run");
875 if (options
.verbosity
== 0)
876 argv_array_push(&args
, "--quiet");
877 else if (options
.verbosity
> 1)
878 argv_array_push(&args
, "--verbose");
879 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
880 for_each_string_list_item(cas_option
, &cas_options
)
881 argv_array_push(&args
, cas_option
->string
);
882 argv_array_push(&args
, url
.buf
);
884 argv_array_push(&args
, "--stdin");
885 for (i
= 0; i
< nr_spec
; i
++)
886 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
887 packet_buf_flush(&preamble
);
889 memset(&rpc
, 0, sizeof(rpc
));
890 rpc
.service_name
= "git-receive-pack",
891 rpc
.argv
= args
.argv
;
892 rpc
.stdin_preamble
= &preamble
;
894 err
= rpc_service(&rpc
, heads
);
896 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
897 strbuf_release(&rpc
.result
);
898 strbuf_release(&preamble
);
899 argv_array_clear(&args
);
903 static int push(int nr_spec
, char **specs
)
905 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
908 if (heads
->proto_git
)
909 ret
= push_git(heads
, nr_spec
, specs
);
911 ret
= push_dav(nr_spec
, specs
);
912 free_discovery(heads
);
916 static void parse_push(struct strbuf
*buf
)
919 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
922 if (starts_with(buf
->buf
, "push ")) {
923 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
924 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
927 die("http transport does not support %s", buf
->buf
);
930 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
936 ret
= push(nr_spec
, specs
);
941 exit(128); /* error already reported */
944 for (i
= 0; i
< nr_spec
; i
++)
949 int main(int argc
, const char **argv
)
951 struct strbuf buf
= STRBUF_INIT
;
954 git_extract_argv0_path(argv
[0]);
955 setup_git_directory_gently(&nongit
);
957 error("remote-curl: usage: git remote-curl <remote> [<url>]");
961 options
.verbosity
= 1;
962 options
.progress
= !!isatty(2);
965 remote
= remote_get(argv
[1]);
968 end_url_with_slash(&url
, argv
[2]);
970 end_url_with_slash(&url
, remote
->url
[0]);
973 http_init(remote
, url
.buf
, 0);
978 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
) {
980 error("remote-curl: error reading command stream from git");
985 if (starts_with(buf
.buf
, "fetch ")) {
987 die("remote-curl: fetch attempted without a local repo");
990 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
991 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
992 output_refs(get_refs(for_push
));
994 } else if (starts_with(buf
.buf
, "push ")) {
997 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
998 char *value
= strchr(arg
, ' ');
1006 result
= set_option(arg
, value
);
1009 else if (result
< 0)
1010 printf("error invalid value\n");
1012 printf("unsupported\n");
1015 } else if (!strcmp(buf
.buf
, "capabilities")) {
1019 printf("check-connectivity\n");
1023 error("remote-curl: unknown command '%s' from git", buf
.buf
);