7 #include "run-command.h"
11 static struct remote
*remote
;
12 static const char *url
; /* always ends with a trailing slash */
17 unsigned progress
: 1,
22 static struct options options
;
24 static int set_option(const char *name
, const char *value
)
26 if (!strcmp(name
, "verbosity")) {
28 int v
= strtol(value
, &end
, 10);
29 if (value
== end
|| *end
)
31 options
.verbosity
= v
;
34 else if (!strcmp(name
, "progress")) {
35 if (!strcmp(value
, "true"))
37 else if (!strcmp(value
, "false"))
43 else if (!strcmp(name
, "depth")) {
45 unsigned long v
= strtoul(value
, &end
, 10);
46 if (value
== end
|| *end
)
51 else if (!strcmp(name
, "followtags")) {
52 if (!strcmp(value
, "true"))
53 options
.followtags
= 1;
54 else if (!strcmp(value
, "false"))
55 options
.followtags
= 0;
60 else if (!strcmp(name
, "dry-run")) {
61 if (!strcmp(value
, "true"))
63 else if (!strcmp(value
, "false"))
70 return 1 /* unsupported */;
79 unsigned proto_git
: 1;
81 static struct discovery
*last_discovery
;
83 static void free_discovery(struct discovery
*d
)
86 if (d
== last_discovery
)
87 last_discovery
= NULL
;
93 static struct discovery
* discover_refs(const char *service
)
95 struct strbuf buffer
= STRBUF_INIT
;
96 struct discovery
*last
= last_discovery
;
98 int http_ret
, is_http
= 0, proto_git_candidate
= 1;
100 if (last
&& !strcmp(service
, last
->service
))
102 free_discovery(last
);
104 strbuf_addf(&buffer
, "%sinfo/refs", url
);
105 if (!prefixcmp(url
, "http://") || !prefixcmp(url
, "https://")) {
107 if (!strchr(url
, '?'))
108 strbuf_addch(&buffer
, '?');
110 strbuf_addch(&buffer
, '&');
111 strbuf_addf(&buffer
, "service=%s", service
);
113 refs_url
= strbuf_detach(&buffer
, NULL
);
115 http_ret
= http_get_strbuf(refs_url
, &buffer
, HTTP_NO_CACHE
);
117 /* try again with "plain" url (no ? or & appended) */
118 if (http_ret
!= HTTP_OK
&& http_ret
!= HTTP_NOAUTH
) {
120 strbuf_reset(&buffer
);
122 proto_git_candidate
= 0;
123 strbuf_addf(&buffer
, "%sinfo/refs", url
);
124 refs_url
= strbuf_detach(&buffer
, NULL
);
126 http_ret
= http_get_strbuf(refs_url
, &buffer
, HTTP_NO_CACHE
);
132 case HTTP_MISSING_TARGET
:
133 die("%s not found: did you run git update-server-info on the"
134 " server?", refs_url
);
136 die("Authentication failed");
138 http_error(refs_url
, http_ret
);
139 die("HTTP request failed");
142 last
= xcalloc(1, sizeof(*last_discovery
));
143 last
->service
= service
;
144 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
145 last
->buf
= last
->buf_alloc
;
147 if (is_http
&& proto_git_candidate
148 && 5 <= last
->len
&& last
->buf
[4] == '#') {
149 /* smart HTTP response; validate that the service
150 * pkt-line matches our request.
152 struct strbuf exp
= STRBUF_INIT
;
154 if (packet_get_line(&buffer
, &last
->buf
, &last
->len
) <= 0)
155 die("%s has invalid packet header", refs_url
);
156 if (buffer
.len
&& buffer
.buf
[buffer
.len
- 1] == '\n')
157 strbuf_setlen(&buffer
, buffer
.len
- 1);
159 strbuf_addf(&exp
, "# service=%s", service
);
160 if (strbuf_cmp(&exp
, &buffer
))
161 die("invalid server response; got '%s'", buffer
.buf
);
162 strbuf_release(&exp
);
164 /* The header can include additional metadata lines, up
165 * until a packet flush marker. Ignore these now, but
166 * in the future we might start to scan them.
168 strbuf_reset(&buffer
);
169 while (packet_get_line(&buffer
, &last
->buf
, &last
->len
) > 0)
170 strbuf_reset(&buffer
);
176 strbuf_release(&buffer
);
177 last_discovery
= last
;
181 static int write_discovery(int in
, int out
, void *data
)
183 struct discovery
*heads
= data
;
185 if (write_in_full(out
, heads
->buf
, heads
->len
) != heads
->len
)
191 static struct ref
*parse_git_refs(struct discovery
*heads
)
193 struct ref
*list
= NULL
;
196 memset(&async
, 0, sizeof(async
));
197 async
.proc
= write_discovery
;
201 if (start_async(&async
))
202 die("cannot start thread to parse advertised refs");
203 get_remote_heads(async
.out
, &list
, 0, NULL
);
205 if (finish_async(&async
))
206 die("ref parsing thread failed");
210 static struct ref
*parse_info_refs(struct discovery
*heads
)
212 char *data
, *start
, *mid
;
216 struct ref
*refs
= NULL
;
217 struct ref
*ref
= NULL
;
218 struct ref
*last_ref
= NULL
;
223 while (i
< heads
->len
) {
229 if (data
[i
] == '\n') {
230 if (mid
- start
!= 40)
231 die("%sinfo/refs not valid: is this a git repository?", url
);
234 ref
= xmalloc(sizeof(struct ref
) +
235 strlen(ref_name
) + 1);
236 memset(ref
, 0, sizeof(struct ref
));
237 strcpy(ref
->name
, ref_name
);
238 get_sha1_hex(start
, ref
->old_sha1
);
242 last_ref
->next
= ref
;
249 ref
= alloc_ref("HEAD");
250 if (!http_fetch_ref(url
, ref
) &&
251 !resolve_remote_symref(ref
, refs
)) {
261 static struct ref
*get_refs(int for_push
)
263 struct discovery
*heads
;
266 heads
= discover_refs("git-receive-pack");
268 heads
= discover_refs("git-upload-pack");
270 if (heads
->proto_git
)
271 return parse_git_refs(heads
);
272 return parse_info_refs(heads
);
275 static void output_refs(struct ref
*refs
)
278 for (posn
= refs
; posn
; posn
= posn
->next
) {
280 printf("@%s %s\n", posn
->symref
, posn
->name
);
282 printf("%s %s\n", sha1_to_hex(posn
->old_sha1
), posn
->name
);
290 const char *service_name
;
293 char *hdr_content_type
;
301 struct strbuf result
;
302 unsigned gzip_request
: 1;
303 unsigned initial_buffer
: 1;
306 static size_t rpc_out(void *ptr
, size_t eltsize
,
307 size_t nmemb
, void *buffer_
)
309 size_t max
= eltsize
* nmemb
;
310 struct rpc_state
*rpc
= buffer_
;
311 size_t avail
= rpc
->len
- rpc
->pos
;
314 rpc
->initial_buffer
= 0;
315 avail
= packet_read_line(rpc
->out
, rpc
->buf
, rpc
->alloc
);
324 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
329 #ifndef NO_CURL_IOCTL
330 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
332 struct rpc_state
*rpc
= clientp
;
338 case CURLIOCMD_RESTARTREAD
:
339 if (rpc
->initial_buffer
) {
343 fprintf(stderr
, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
344 return CURLIOE_FAILRESTART
;
347 return CURLIOE_UNKNOWNCMD
;
352 static size_t rpc_in(char *ptr
, size_t eltsize
,
353 size_t nmemb
, void *buffer_
)
355 size_t size
= eltsize
* nmemb
;
356 struct rpc_state
*rpc
= buffer_
;
357 write_or_die(rpc
->in
, ptr
, size
);
361 static int run_slot(struct active_request_slot
*slot
)
364 struct slot_results results
;
366 slot
->results
= &results
;
367 slot
->curl_result
= curl_easy_perform(slot
->curl
);
368 finish_active_slot(slot
);
370 if (results
.curl_result
!= CURLE_OK
) {
371 err
|= error("RPC failed; result=%d, HTTP code = %ld",
372 results
.curl_result
, results
.http_code
);
378 static int probe_rpc(struct rpc_state
*rpc
)
380 struct active_request_slot
*slot
;
381 struct curl_slist
*headers
= NULL
;
382 struct strbuf buf
= STRBUF_INIT
;
385 slot
= get_active_slot();
387 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
388 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
390 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
391 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
392 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
393 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
394 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
395 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
396 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
397 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
398 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
400 err
= run_slot(slot
);
402 curl_slist_free_all(headers
);
403 strbuf_release(&buf
);
407 static int post_rpc(struct rpc_state
*rpc
)
409 struct active_request_slot
*slot
;
410 struct curl_slist
*headers
= NULL
;
411 int use_gzip
= rpc
->gzip_request
;
412 char *gzip_body
= NULL
;
413 int err
, large_request
= 0;
415 /* Try to load the entire request, if we can fit it into the
416 * allocated buffer space we can use HTTP/1.0 and avoid the
417 * chunked encoding mess.
420 size_t left
= rpc
->alloc
- rpc
->len
;
421 char *buf
= rpc
->buf
+ rpc
->len
;
424 if (left
< LARGE_PACKET_MAX
) {
430 n
= packet_read_line(rpc
->out
, buf
, left
);
437 err
= probe_rpc(rpc
);
442 slot
= get_active_slot();
444 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
445 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
446 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
447 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
449 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
450 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
451 headers
= curl_slist_append(headers
, "Expect:");
454 /* The request body is large and the size cannot be predicted.
455 * We must use chunked encoding to send it.
457 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
458 rpc
->initial_buffer
= 1;
459 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
460 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
461 #ifndef NO_CURL_IOCTL
462 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
463 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
465 if (options
.verbosity
> 1) {
466 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
470 } else if (use_gzip
&& 1024 < rpc
->len
) {
471 /* The client backend isn't giving us compressed data so
472 * we can try to deflate it ourselves, this may save on.
479 memset(&stream
, 0, sizeof(stream
));
480 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
481 size
= git_deflate_bound(&stream
, rpc
->len
);
482 gzip_body
= xmalloc(size
);
484 stream
.next_in
= (unsigned char *)rpc
->buf
;
485 stream
.avail_in
= rpc
->len
;
486 stream
.next_out
= (unsigned char *)gzip_body
;
487 stream
.avail_out
= size
;
489 ret
= git_deflate(&stream
, Z_FINISH
);
490 if (ret
!= Z_STREAM_END
)
491 die("cannot deflate request; zlib deflate error %d", ret
);
493 ret
= git_deflate_end_gently(&stream
);
495 die("cannot deflate request; zlib end error %d", ret
);
497 size
= stream
.total_out
;
499 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
500 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
501 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, size
);
503 if (options
.verbosity
> 1) {
504 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
506 (unsigned long)rpc
->len
, (unsigned long)size
);
510 /* We know the complete request size in advance, use the
511 * more normal Content-Length approach.
513 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
514 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, rpc
->len
);
515 if (options
.verbosity
> 1) {
516 fprintf(stderr
, "POST %s (%lu bytes)\n",
517 rpc
->service_name
, (unsigned long)rpc
->len
);
522 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
523 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
524 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
526 err
= run_slot(slot
);
528 curl_slist_free_all(headers
);
533 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
535 const char *svc
= rpc
->service_name
;
536 struct strbuf buf
= STRBUF_INIT
;
537 struct child_process client
;
540 memset(&client
, 0, sizeof(client
));
544 client
.argv
= rpc
->argv
;
545 if (start_command(&client
))
548 write_or_die(client
.in
, heads
->buf
, heads
->len
);
550 rpc
->alloc
= http_post_buffer
;
551 rpc
->buf
= xmalloc(rpc
->alloc
);
553 rpc
->out
= client
.out
;
554 strbuf_init(&rpc
->result
, 0);
556 strbuf_addf(&buf
, "%s%s", url
, svc
);
557 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
559 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
560 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
562 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
563 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
566 int n
= packet_read_line(rpc
->out
, rpc
->buf
, rpc
->alloc
);
571 err
|= post_rpc(rpc
);
577 strbuf_read(&rpc
->result
, client
.out
, 0);
581 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
588 err
|= finish_command(&client
);
589 free(rpc
->service_url
);
590 free(rpc
->hdr_content_type
);
591 free(rpc
->hdr_accept
);
593 strbuf_release(&buf
);
597 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
599 struct walker
*walker
;
600 char **targets
= xmalloc(nr_heads
* sizeof(char*));
604 die("dumb http transport does not support --depth");
605 for (i
= 0; i
< nr_heads
; i
++)
606 targets
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
608 walker
= get_http_walker(url
);
610 walker
->get_tree
= 1;
611 walker
->get_history
= 1;
612 walker
->get_verbosely
= options
.verbosity
>= 3;
613 walker
->get_recover
= 0;
614 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
617 for (i
= 0; i
< nr_heads
; i
++)
621 return ret
? error("Fetch failed.") : 0;
624 static int fetch_git(struct discovery
*heads
,
625 int nr_heads
, struct ref
**to_fetch
)
627 struct rpc_state rpc
;
628 char *depth_arg
= NULL
;
630 int argc
= 0, i
, err
;
632 argv
= xmalloc((15 + nr_heads
) * sizeof(char*));
633 argv
[argc
++] = "fetch-pack";
634 argv
[argc
++] = "--stateless-rpc";
635 argv
[argc
++] = "--lock-pack";
636 if (options
.followtags
)
637 argv
[argc
++] = "--include-tag";
639 argv
[argc
++] = "--thin";
640 if (options
.verbosity
>= 3) {
644 if (!options
.progress
)
645 argv
[argc
++] = "--no-progress";
647 struct strbuf buf
= STRBUF_INIT
;
648 strbuf_addf(&buf
, "--depth=%lu", options
.depth
);
649 depth_arg
= strbuf_detach(&buf
, NULL
);
650 argv
[argc
++] = depth_arg
;
653 for (i
= 0; i
< nr_heads
; i
++) {
654 struct ref
*ref
= to_fetch
[i
];
655 if (!ref
->name
|| !*ref
->name
)
656 die("cannot fetch by sha1 over smart http");
657 argv
[argc
++] = ref
->name
;
661 memset(&rpc
, 0, sizeof(rpc
));
662 rpc
.service_name
= "git-upload-pack",
664 rpc
.gzip_request
= 1;
666 err
= rpc_service(&rpc
, heads
);
668 safe_write(1, rpc
.result
.buf
, rpc
.result
.len
);
669 strbuf_release(&rpc
.result
);
675 static int fetch(int nr_heads
, struct ref
**to_fetch
)
677 struct discovery
*d
= discover_refs("git-upload-pack");
679 return fetch_git(d
, nr_heads
, to_fetch
);
681 return fetch_dumb(nr_heads
, to_fetch
);
684 static void parse_fetch(struct strbuf
*buf
)
686 struct ref
**to_fetch
= NULL
;
687 struct ref
*list_head
= NULL
;
688 struct ref
**list
= &list_head
;
689 int alloc_heads
= 0, nr_heads
= 0;
692 if (!prefixcmp(buf
->buf
, "fetch ")) {
693 char *p
= buf
->buf
+ strlen("fetch ");
696 unsigned char old_sha1
[20];
698 if (strlen(p
) < 40 || get_sha1_hex(p
, old_sha1
))
699 die("protocol error: expected sha/ref, got %s'", p
);
705 die("protocol error: expected sha/ref, got %s'", p
);
707 ref
= alloc_ref(name
);
708 hashcpy(ref
->old_sha1
, old_sha1
);
713 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
714 to_fetch
[nr_heads
++] = ref
;
717 die("http transport does not support %s", buf
->buf
);
720 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
726 if (fetch(nr_heads
, to_fetch
))
727 exit(128); /* error already reported */
728 free_refs(list_head
);
736 static int push_dav(int nr_spec
, char **specs
)
738 const char **argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
741 argv
[argc
++] = "http-push";
742 argv
[argc
++] = "--helper-status";
744 argv
[argc
++] = "--dry-run";
745 if (options
.verbosity
> 1)
746 argv
[argc
++] = "--verbose";
748 for (i
= 0; i
< nr_spec
; i
++)
749 argv
[argc
++] = specs
[i
];
752 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
753 die("git-%s failed", argv
[0]);
758 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
760 struct rpc_state rpc
;
762 int argc
= 0, i
, err
;
764 argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
765 argv
[argc
++] = "send-pack";
766 argv
[argc
++] = "--stateless-rpc";
767 argv
[argc
++] = "--helper-status";
769 argv
[argc
++] = "--thin";
771 argv
[argc
++] = "--dry-run";
772 if (options
.verbosity
> 1)
773 argv
[argc
++] = "--verbose";
775 for (i
= 0; i
< nr_spec
; i
++)
776 argv
[argc
++] = specs
[i
];
779 memset(&rpc
, 0, sizeof(rpc
));
780 rpc
.service_name
= "git-receive-pack",
783 err
= rpc_service(&rpc
, heads
);
785 safe_write(1, rpc
.result
.buf
, rpc
.result
.len
);
786 strbuf_release(&rpc
.result
);
791 static int push(int nr_spec
, char **specs
)
793 struct discovery
*heads
= discover_refs("git-receive-pack");
796 if (heads
->proto_git
)
797 ret
= push_git(heads
, nr_spec
, specs
);
799 ret
= push_dav(nr_spec
, specs
);
800 free_discovery(heads
);
804 static void parse_push(struct strbuf
*buf
)
807 int alloc_spec
= 0, nr_spec
= 0, i
;
810 if (!prefixcmp(buf
->buf
, "push ")) {
811 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
812 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
815 die("http transport does not support %s", buf
->buf
);
818 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
824 if (push(nr_spec
, specs
))
825 exit(128); /* error already reported */
831 for (i
= 0; i
< nr_spec
; i
++)
836 int main(int argc
, const char **argv
)
838 struct strbuf buf
= STRBUF_INIT
;
841 git_extract_argv0_path(argv
[0]);
842 setup_git_directory_gently(&nongit
);
844 fprintf(stderr
, "Remote needed\n");
848 options
.verbosity
= 1;
849 options
.progress
= !!isatty(2);
852 remote
= remote_get(argv
[1]);
855 end_url_with_slash(&buf
, argv
[2]);
857 end_url_with_slash(&buf
, remote
->url
[0]);
860 url
= strbuf_detach(&buf
, NULL
);
862 http_init(remote
, url
);
865 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
) {
867 fprintf(stderr
, "Error reading command stream\n");
869 fprintf(stderr
, "Unexpected end of command stream\n");
874 if (!prefixcmp(buf
.buf
, "fetch ")) {
876 die("Fetch attempted without a local repo");
879 } else if (!strcmp(buf
.buf
, "list") || !prefixcmp(buf
.buf
, "list ")) {
880 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
881 output_refs(get_refs(for_push
));
883 } else if (!prefixcmp(buf
.buf
, "push ")) {
886 } else if (!prefixcmp(buf
.buf
, "option ")) {
887 char *name
= buf
.buf
+ strlen("option ");
888 char *value
= strchr(name
, ' ');
896 result
= set_option(name
, value
);
900 printf("error invalid value\n");
902 printf("unsupported\n");
905 } else if (!strcmp(buf
.buf
, "capabilities")) {
912 fprintf(stderr
, "Unknown command '%s'\n", buf
.buf
);