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
*msg
)
202 * We only show text/plain parts, as other types are likely
203 * to be ugly to look at on the user's terminal.
205 * TODO should handle "; charset=XXX", and re-encode into
208 if (strcasecmp(type
->buf
, "text/plain"))
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 buffer
= STRBUF_INIT
;
229 struct strbuf refs_url
= STRBUF_INIT
;
230 struct strbuf effective_url
= STRBUF_INIT
;
231 struct discovery
*last
= last_discovery
;
232 int http_ret
, maybe_smart
= 0;
233 struct http_get_options options
;
235 if (last
&& !strcmp(service
, last
->service
))
237 free_discovery(last
);
239 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
240 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
241 git_env_bool("GIT_SMART_HTTP", 1)) {
243 if (!strchr(url
.buf
, '?'))
244 strbuf_addch(&refs_url
, '?');
246 strbuf_addch(&refs_url
, '&');
247 strbuf_addf(&refs_url
, "service=%s", service
);
250 memset(&options
, 0, sizeof(options
));
251 options
.content_type
= &type
;
252 options
.effective_url
= &effective_url
;
253 options
.base_url
= &url
;
254 options
.no_cache
= 1;
255 options
.keep_error
= 1;
257 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &options
);
261 case HTTP_MISSING_TARGET
:
262 show_http_message(&type
, &buffer
);
263 die("repository '%s' not found", url
.buf
);
265 show_http_message(&type
, &buffer
);
266 die("Authentication failed for '%s'", url
.buf
);
268 show_http_message(&type
, &buffer
);
269 die("unable to access '%s': %s", url
.buf
, curl_errorstr
);
272 last
= xcalloc(1, sizeof(*last_discovery
));
273 last
->service
= service
;
274 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
275 last
->buf
= last
->buf_alloc
;
277 strbuf_addf(&exp
, "application/x-%s-advertisement", service
);
279 (5 <= last
->len
&& last
->buf
[4] == '#') &&
280 !strbuf_cmp(&exp
, &type
)) {
284 * smart HTTP response; validate that the service
285 * pkt-line matches our request.
287 line
= packet_read_line_buf(&last
->buf
, &last
->len
, NULL
);
290 strbuf_addf(&exp
, "# service=%s", service
);
291 if (strcmp(line
, exp
.buf
))
292 die("invalid server response; got '%s'", line
);
293 strbuf_release(&exp
);
295 /* The header can include additional metadata lines, up
296 * until a packet flush marker. Ignore these now, but
297 * in the future we might start to scan them.
299 while (packet_read_line_buf(&last
->buf
, &last
->len
, NULL
))
306 last
->refs
= parse_git_refs(last
, for_push
);
308 last
->refs
= parse_info_refs(last
);
310 strbuf_release(&refs_url
);
311 strbuf_release(&exp
);
312 strbuf_release(&type
);
313 strbuf_release(&effective_url
);
314 strbuf_release(&buffer
);
315 last_discovery
= last
;
319 static struct ref
*get_refs(int for_push
)
321 struct discovery
*heads
;
324 heads
= discover_refs("git-receive-pack", for_push
);
326 heads
= discover_refs("git-upload-pack", for_push
);
331 static void output_refs(struct ref
*refs
)
334 for (posn
= refs
; posn
; posn
= posn
->next
) {
336 printf("@%s %s\n", posn
->symref
, posn
->name
);
338 printf("%s %s\n", sha1_to_hex(posn
->old_sha1
), posn
->name
);
345 const char *service_name
;
347 struct strbuf
*stdin_preamble
;
349 char *hdr_content_type
;
357 struct strbuf result
;
358 unsigned gzip_request
: 1;
359 unsigned initial_buffer
: 1;
362 static size_t rpc_out(void *ptr
, size_t eltsize
,
363 size_t nmemb
, void *buffer_
)
365 size_t max
= eltsize
* nmemb
;
366 struct rpc_state
*rpc
= buffer_
;
367 size_t avail
= rpc
->len
- rpc
->pos
;
370 rpc
->initial_buffer
= 0;
371 avail
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
380 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
385 #ifndef NO_CURL_IOCTL
386 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
388 struct rpc_state
*rpc
= clientp
;
394 case CURLIOCMD_RESTARTREAD
:
395 if (rpc
->initial_buffer
) {
399 fprintf(stderr
, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
400 return CURLIOE_FAILRESTART
;
403 return CURLIOE_UNKNOWNCMD
;
408 static size_t rpc_in(char *ptr
, size_t eltsize
,
409 size_t nmemb
, void *buffer_
)
411 size_t size
= eltsize
* nmemb
;
412 struct rpc_state
*rpc
= buffer_
;
413 write_or_die(rpc
->in
, ptr
, size
);
417 static int run_slot(struct active_request_slot
*slot
,
418 struct slot_results
*results
)
421 struct slot_results results_buf
;
424 results
= &results_buf
;
426 err
= run_one_slot(slot
, results
);
428 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
429 error("RPC failed; result=%d, HTTP code = %ld",
430 results
->curl_result
, results
->http_code
);
436 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
438 struct active_request_slot
*slot
;
439 struct curl_slist
*headers
= NULL
;
440 struct strbuf buf
= STRBUF_INIT
;
443 slot
= get_active_slot();
445 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
446 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
448 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
449 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
450 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
451 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
452 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
453 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
454 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
455 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
456 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
458 err
= run_slot(slot
, results
);
460 curl_slist_free_all(headers
);
461 strbuf_release(&buf
);
465 static int post_rpc(struct rpc_state
*rpc
)
467 struct active_request_slot
*slot
;
468 struct curl_slist
*headers
= NULL
;
469 int use_gzip
= rpc
->gzip_request
;
470 char *gzip_body
= NULL
;
471 size_t gzip_size
= 0;
472 int err
, large_request
= 0;
473 int needs_100_continue
= 0;
475 /* Try to load the entire request, if we can fit it into the
476 * allocated buffer space we can use HTTP/1.0 and avoid the
477 * chunked encoding mess.
480 size_t left
= rpc
->alloc
- rpc
->len
;
481 char *buf
= rpc
->buf
+ rpc
->len
;
484 if (left
< LARGE_PACKET_MAX
) {
490 n
= packet_read(rpc
->out
, NULL
, NULL
, buf
, left
, 0);
497 struct slot_results results
;
500 err
= probe_rpc(rpc
, &results
);
501 if (err
== HTTP_REAUTH
)
502 credential_fill(&http_auth
);
503 } while (err
== HTTP_REAUTH
);
507 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
508 needs_100_continue
= 1;
511 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
512 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
513 headers
= curl_slist_append(headers
, needs_100_continue
?
514 "Expect: 100-continue" : "Expect:");
517 slot
= get_active_slot();
519 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
520 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
521 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
522 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
525 /* The request body is large and the size cannot be predicted.
526 * We must use chunked encoding to send it.
528 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
529 rpc
->initial_buffer
= 1;
530 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
531 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
532 #ifndef NO_CURL_IOCTL
533 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
534 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
536 if (options
.verbosity
> 1) {
537 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
541 } else if (gzip_body
) {
543 * If we are looping to retry authentication, then the previous
544 * run will have set up the headers and gzip buffer already,
545 * and we just need to send it.
547 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
548 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, gzip_size
);
550 } else if (use_gzip
&& 1024 < rpc
->len
) {
551 /* The client backend isn't giving us compressed data so
552 * we can try to deflate it ourselves, this may save on.
558 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
559 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
560 gzip_body
= xmalloc(gzip_size
);
562 stream
.next_in
= (unsigned char *)rpc
->buf
;
563 stream
.avail_in
= rpc
->len
;
564 stream
.next_out
= (unsigned char *)gzip_body
;
565 stream
.avail_out
= gzip_size
;
567 ret
= git_deflate(&stream
, Z_FINISH
);
568 if (ret
!= Z_STREAM_END
)
569 die("cannot deflate request; zlib deflate error %d", ret
);
571 ret
= git_deflate_end_gently(&stream
);
573 die("cannot deflate request; zlib end error %d", ret
);
575 gzip_size
= stream
.total_out
;
577 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
578 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
579 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, gzip_size
);
581 if (options
.verbosity
> 1) {
582 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
584 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
588 /* We know the complete request size in advance, use the
589 * more normal Content-Length approach.
591 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
592 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, rpc
->len
);
593 if (options
.verbosity
> 1) {
594 fprintf(stderr
, "POST %s (%lu bytes)\n",
595 rpc
->service_name
, (unsigned long)rpc
->len
);
600 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
601 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
602 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
604 err
= run_slot(slot
, NULL
);
605 if (err
== HTTP_REAUTH
&& !large_request
) {
606 credential_fill(&http_auth
);
612 curl_slist_free_all(headers
);
617 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
619 const char *svc
= rpc
->service_name
;
620 struct strbuf buf
= STRBUF_INIT
;
621 struct strbuf
*preamble
= rpc
->stdin_preamble
;
622 struct child_process client
;
625 memset(&client
, 0, sizeof(client
));
629 client
.argv
= rpc
->argv
;
630 if (start_command(&client
))
633 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
635 write_or_die(client
.in
, heads
->buf
, heads
->len
);
637 rpc
->alloc
= http_post_buffer
;
638 rpc
->buf
= xmalloc(rpc
->alloc
);
640 rpc
->out
= client
.out
;
641 strbuf_init(&rpc
->result
, 0);
643 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
644 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
646 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
647 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
649 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
650 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
653 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
658 err
|= post_rpc(rpc
);
664 strbuf_read(&rpc
->result
, client
.out
, 0);
668 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
675 err
|= finish_command(&client
);
676 free(rpc
->service_url
);
677 free(rpc
->hdr_content_type
);
678 free(rpc
->hdr_accept
);
680 strbuf_release(&buf
);
684 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
686 struct walker
*walker
;
687 char **targets
= xmalloc(nr_heads
* sizeof(char*));
691 die("dumb http transport does not support --depth");
692 for (i
= 0; i
< nr_heads
; i
++)
693 targets
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
695 walker
= get_http_walker(url
.buf
);
697 walker
->get_tree
= 1;
698 walker
->get_history
= 1;
699 walker
->get_verbosely
= options
.verbosity
>= 3;
700 walker
->get_recover
= 0;
701 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
704 for (i
= 0; i
< nr_heads
; i
++)
708 return ret
? error("Fetch failed.") : 0;
711 static int fetch_git(struct discovery
*heads
,
712 int nr_heads
, struct ref
**to_fetch
)
714 struct rpc_state rpc
;
715 struct strbuf preamble
= STRBUF_INIT
;
716 char *depth_arg
= NULL
;
717 int argc
= 0, i
, err
;
718 const char *argv
[17];
720 argv
[argc
++] = "fetch-pack";
721 argv
[argc
++] = "--stateless-rpc";
722 argv
[argc
++] = "--stdin";
723 argv
[argc
++] = "--lock-pack";
724 if (options
.followtags
)
725 argv
[argc
++] = "--include-tag";
727 argv
[argc
++] = "--thin";
728 if (options
.verbosity
>= 3) {
732 if (options
.check_self_contained_and_connected
)
733 argv
[argc
++] = "--check-self-contained-and-connected";
735 argv
[argc
++] = "--cloning";
736 if (options
.update_shallow
)
737 argv
[argc
++] = "--update-shallow";
738 if (!options
.progress
)
739 argv
[argc
++] = "--no-progress";
741 struct strbuf buf
= STRBUF_INIT
;
742 strbuf_addf(&buf
, "--depth=%lu", options
.depth
);
743 depth_arg
= strbuf_detach(&buf
, NULL
);
744 argv
[argc
++] = depth_arg
;
746 argv
[argc
++] = url
.buf
;
749 for (i
= 0; i
< nr_heads
; i
++) {
750 struct ref
*ref
= to_fetch
[i
];
751 if (!ref
->name
|| !*ref
->name
)
752 die("cannot fetch by sha1 over smart http");
753 packet_buf_write(&preamble
, "%s %s\n",
754 sha1_to_hex(ref
->old_sha1
), ref
->name
);
756 packet_buf_flush(&preamble
);
758 memset(&rpc
, 0, sizeof(rpc
));
759 rpc
.service_name
= "git-upload-pack",
761 rpc
.stdin_preamble
= &preamble
;
762 rpc
.gzip_request
= 1;
764 err
= rpc_service(&rpc
, heads
);
766 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
767 strbuf_release(&rpc
.result
);
768 strbuf_release(&preamble
);
773 static int fetch(int nr_heads
, struct ref
**to_fetch
)
775 struct discovery
*d
= discover_refs("git-upload-pack", 0);
777 return fetch_git(d
, nr_heads
, to_fetch
);
779 return fetch_dumb(nr_heads
, to_fetch
);
782 static void parse_fetch(struct strbuf
*buf
)
784 struct ref
**to_fetch
= NULL
;
785 struct ref
*list_head
= NULL
;
786 struct ref
**list
= &list_head
;
787 int alloc_heads
= 0, nr_heads
= 0;
790 if (starts_with(buf
->buf
, "fetch ")) {
791 char *p
= buf
->buf
+ strlen("fetch ");
794 unsigned char old_sha1
[20];
796 if (strlen(p
) < 40 || get_sha1_hex(p
, old_sha1
))
797 die("protocol error: expected sha/ref, got %s'", p
);
803 die("protocol error: expected sha/ref, got %s'", p
);
805 ref
= alloc_ref(name
);
806 hashcpy(ref
->old_sha1
, old_sha1
);
811 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
812 to_fetch
[nr_heads
++] = ref
;
815 die("http transport does not support %s", buf
->buf
);
818 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
824 if (fetch(nr_heads
, to_fetch
))
825 exit(128); /* error already reported */
826 free_refs(list_head
);
834 static int push_dav(int nr_spec
, char **specs
)
836 const char **argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
839 argv
[argc
++] = "http-push";
840 argv
[argc
++] = "--helper-status";
842 argv
[argc
++] = "--dry-run";
843 if (options
.verbosity
> 1)
844 argv
[argc
++] = "--verbose";
845 argv
[argc
++] = url
.buf
;
846 for (i
= 0; i
< nr_spec
; i
++)
847 argv
[argc
++] = specs
[i
];
850 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
851 die("git-%s failed", argv
[0]);
856 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
858 struct rpc_state rpc
;
860 struct argv_array args
;
861 struct string_list_item
*cas_option
;
863 argv_array_init(&args
);
864 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
868 argv_array_push(&args
, "--thin");
870 argv_array_push(&args
, "--dry-run");
871 if (options
.verbosity
== 0)
872 argv_array_push(&args
, "--quiet");
873 else if (options
.verbosity
> 1)
874 argv_array_push(&args
, "--verbose");
875 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
876 for_each_string_list_item(cas_option
, &cas_options
)
877 argv_array_push(&args
, cas_option
->string
);
878 argv_array_push(&args
, url
.buf
);
879 for (i
= 0; i
< nr_spec
; i
++)
880 argv_array_push(&args
, specs
[i
]);
882 memset(&rpc
, 0, sizeof(rpc
));
883 rpc
.service_name
= "git-receive-pack",
884 rpc
.argv
= args
.argv
;
886 err
= rpc_service(&rpc
, heads
);
888 write_or_die(1, rpc
.result
.buf
, rpc
.result
.len
);
889 strbuf_release(&rpc
.result
);
890 argv_array_clear(&args
);
894 static int push(int nr_spec
, char **specs
)
896 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
899 if (heads
->proto_git
)
900 ret
= push_git(heads
, nr_spec
, specs
);
902 ret
= push_dav(nr_spec
, specs
);
903 free_discovery(heads
);
907 static void parse_push(struct strbuf
*buf
)
910 int alloc_spec
= 0, nr_spec
= 0, i
, ret
;
913 if (starts_with(buf
->buf
, "push ")) {
914 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
915 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
918 die("http transport does not support %s", buf
->buf
);
921 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
927 ret
= push(nr_spec
, specs
);
932 exit(128); /* error already reported */
935 for (i
= 0; i
< nr_spec
; i
++)
940 int main(int argc
, const char **argv
)
942 struct strbuf buf
= STRBUF_INIT
;
945 git_extract_argv0_path(argv
[0]);
946 setup_git_directory_gently(&nongit
);
948 fprintf(stderr
, "Remote needed\n");
952 options
.verbosity
= 1;
953 options
.progress
= !!isatty(2);
956 remote
= remote_get(argv
[1]);
959 end_url_with_slash(&url
, argv
[2]);
961 end_url_with_slash(&url
, remote
->url
[0]);
964 http_init(remote
, url
.buf
, 0);
967 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
) {
969 fprintf(stderr
, "Error reading command stream\n");
971 fprintf(stderr
, "Unexpected end of command stream\n");
976 if (starts_with(buf
.buf
, "fetch ")) {
978 die("Fetch attempted without a local repo");
981 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
982 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
983 output_refs(get_refs(for_push
));
985 } else if (starts_with(buf
.buf
, "push ")) {
988 } else if (starts_with(buf
.buf
, "option ")) {
989 char *name
= buf
.buf
+ strlen("option ");
990 char *value
= strchr(name
, ' ');
998 result
= set_option(name
, value
);
1001 else if (result
< 0)
1002 printf("error invalid value\n");
1004 printf("unsupported\n");
1007 } else if (!strcmp(buf
.buf
, "capabilities")) {
1011 printf("check-connectivity\n");
1015 fprintf(stderr
, "Unknown command '%s'\n", buf
.buf
);