7 #include "run-command.h"
11 static struct remote
*remote
;
12 static const char *url
;
13 static struct walker
*walker
;
18 unsigned progress
: 1,
23 static struct options options
;
25 static void init_walker(void)
28 walker
= get_http_walker(url
, remote
);
31 static int set_option(const char *name
, const char *value
)
33 if (!strcmp(name
, "verbosity")) {
35 int v
= strtol(value
, &end
, 10);
36 if (value
== end
|| *end
)
38 options
.verbosity
= v
;
41 else if (!strcmp(name
, "progress")) {
42 if (!strcmp(value
, "true"))
44 else if (!strcmp(value
, "false"))
50 else if (!strcmp(name
, "depth")) {
52 unsigned long v
= strtoul(value
, &end
, 10);
53 if (value
== end
|| *end
)
58 else if (!strcmp(name
, "followtags")) {
59 if (!strcmp(value
, "true"))
60 options
.followtags
= 1;
61 else if (!strcmp(value
, "false"))
62 options
.followtags
= 0;
67 else if (!strcmp(name
, "dry-run")) {
68 if (!strcmp(value
, "true"))
70 else if (!strcmp(value
, "false"))
77 return 1 /* unsupported */;
86 unsigned proto_git
: 1;
88 static struct discovery
*last_discovery
;
90 static void free_discovery(struct discovery
*d
)
93 if (d
== last_discovery
)
94 last_discovery
= NULL
;
100 static struct discovery
* discover_refs(const char *service
)
102 struct strbuf buffer
= STRBUF_INIT
;
103 struct discovery
*last
= last_discovery
;
105 int http_ret
, is_http
= 0, proto_git_candidate
= 1;
107 if (last
&& !strcmp(service
, last
->service
))
109 free_discovery(last
);
111 strbuf_addf(&buffer
, "%s/info/refs", url
);
112 if (!prefixcmp(url
, "http://") || !prefixcmp(url
, "https://")) {
114 if (!strchr(url
, '?'))
115 strbuf_addch(&buffer
, '?');
117 strbuf_addch(&buffer
, '&');
118 strbuf_addf(&buffer
, "service=%s", service
);
120 refs_url
= strbuf_detach(&buffer
, NULL
);
123 http_ret
= http_get_strbuf(refs_url
, &buffer
, HTTP_NO_CACHE
);
125 /* try again with "plain" url (no ? or & appended) */
126 if (http_ret
!= HTTP_OK
) {
128 strbuf_reset(&buffer
);
130 proto_git_candidate
= 0;
131 strbuf_addf(&buffer
, "%s/info/refs", url
);
132 refs_url
= strbuf_detach(&buffer
, NULL
);
134 http_ret
= http_get_strbuf(refs_url
, &buffer
, HTTP_NO_CACHE
);
140 case HTTP_MISSING_TARGET
:
141 die("%s not found: did you run git update-server-info on the"
142 " server?", refs_url
);
144 http_error(refs_url
, http_ret
);
145 die("HTTP request failed");
148 last
= xcalloc(1, sizeof(*last_discovery
));
149 last
->service
= service
;
150 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
151 last
->buf
= last
->buf_alloc
;
153 if (is_http
&& proto_git_candidate
154 && 5 <= last
->len
&& last
->buf
[4] == '#') {
155 /* smart HTTP response; validate that the service
156 * pkt-line matches our request.
158 struct strbuf exp
= STRBUF_INIT
;
160 if (packet_get_line(&buffer
, &last
->buf
, &last
->len
) <= 0)
161 die("%s has invalid packet header", refs_url
);
162 if (buffer
.len
&& buffer
.buf
[buffer
.len
- 1] == '\n')
163 strbuf_setlen(&buffer
, buffer
.len
- 1);
165 strbuf_addf(&exp
, "# service=%s", service
);
166 if (strbuf_cmp(&exp
, &buffer
))
167 die("invalid server response; got '%s'", buffer
.buf
);
168 strbuf_release(&exp
);
170 /* The header can include additional metadata lines, up
171 * until a packet flush marker. Ignore these now, but
172 * in the future we might start to scan them.
174 strbuf_reset(&buffer
);
175 while (packet_get_line(&buffer
, &last
->buf
, &last
->len
) > 0)
176 strbuf_reset(&buffer
);
182 strbuf_release(&buffer
);
183 last_discovery
= last
;
187 static int write_discovery(int fd
, void *data
)
189 struct discovery
*heads
= data
;
191 if (write_in_full(fd
, heads
->buf
, heads
->len
) != heads
->len
)
197 static struct ref
*parse_git_refs(struct discovery
*heads
)
199 struct ref
*list
= NULL
;
202 memset(&async
, 0, sizeof(async
));
203 async
.proc
= write_discovery
;
206 if (start_async(&async
))
207 die("cannot start thread to parse advertised refs");
208 get_remote_heads(async
.out
, &list
, 0, NULL
, 0, NULL
);
210 if (finish_async(&async
))
211 die("ref parsing thread failed");
215 static struct ref
*parse_info_refs(struct discovery
*heads
)
217 char *data
, *start
, *mid
;
221 struct ref
*refs
= NULL
;
222 struct ref
*ref
= NULL
;
223 struct ref
*last_ref
= NULL
;
228 while (i
< heads
->len
) {
234 if (data
[i
] == '\n') {
237 ref
= xmalloc(sizeof(struct ref
) +
238 strlen(ref_name
) + 1);
239 memset(ref
, 0, sizeof(struct ref
));
240 strcpy(ref
->name
, ref_name
);
241 get_sha1_hex(start
, ref
->old_sha1
);
245 last_ref
->next
= ref
;
253 ref
= alloc_ref("HEAD");
254 if (!walker
->fetch_ref(walker
, ref
) &&
255 !resolve_remote_symref(ref
, refs
)) {
265 static struct ref
*get_refs(int for_push
)
267 struct discovery
*heads
;
270 heads
= discover_refs("git-receive-pack");
272 heads
= discover_refs("git-upload-pack");
274 if (heads
->proto_git
)
275 return parse_git_refs(heads
);
276 return parse_info_refs(heads
);
279 static void output_refs(struct ref
*refs
)
282 for (posn
= refs
; posn
; posn
= posn
->next
) {
284 printf("@%s %s\n", posn
->symref
, posn
->name
);
286 printf("%s %s\n", sha1_to_hex(posn
->old_sha1
), posn
->name
);
294 const char *service_name
;
297 char *hdr_content_type
;
305 struct strbuf result
;
306 unsigned gzip_request
: 1;
307 unsigned initial_buffer
: 1;
310 static size_t rpc_out(void *ptr
, size_t eltsize
,
311 size_t nmemb
, void *buffer_
)
313 size_t max
= eltsize
* nmemb
;
314 struct rpc_state
*rpc
= buffer_
;
315 size_t avail
= rpc
->len
- rpc
->pos
;
318 rpc
->initial_buffer
= 0;
319 avail
= packet_read_line(rpc
->out
, rpc
->buf
, rpc
->alloc
);
328 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
333 #ifndef NO_CURL_IOCTL
334 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
336 struct rpc_state
*rpc
= clientp
;
342 case CURLIOCMD_RESTARTREAD
:
343 if (rpc
->initial_buffer
) {
347 fprintf(stderr
, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
348 return CURLIOE_FAILRESTART
;
351 return CURLIOE_UNKNOWNCMD
;
356 static size_t rpc_in(const void *ptr
, size_t eltsize
,
357 size_t nmemb
, void *buffer_
)
359 size_t size
= eltsize
* nmemb
;
360 struct rpc_state
*rpc
= buffer_
;
361 write_or_die(rpc
->in
, ptr
, size
);
365 static int post_rpc(struct rpc_state
*rpc
)
367 struct active_request_slot
*slot
;
368 struct slot_results results
;
369 struct curl_slist
*headers
= NULL
;
370 int use_gzip
= rpc
->gzip_request
;
371 char *gzip_body
= NULL
;
372 int err
= 0, large_request
= 0;
374 /* Try to load the entire request, if we can fit it into the
375 * allocated buffer space we can use HTTP/1.0 and avoid the
376 * chunked encoding mess.
379 size_t left
= rpc
->alloc
- rpc
->len
;
380 char *buf
= rpc
->buf
+ rpc
->len
;
383 if (left
< LARGE_PACKET_MAX
) {
389 n
= packet_read_line(rpc
->out
, buf
, left
);
395 slot
= get_active_slot();
396 slot
->results
= &results
;
398 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
399 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
400 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
401 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
403 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
404 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
407 /* The request body is large and the size cannot be predicted.
408 * We must use chunked encoding to send it.
410 headers
= curl_slist_append(headers
, "Expect: 100-continue");
411 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
412 rpc
->initial_buffer
= 1;
413 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
414 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
415 #ifndef NO_CURL_IOCTL
416 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
417 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
419 if (options
.verbosity
> 1) {
420 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
424 } else if (use_gzip
&& 1024 < rpc
->len
) {
425 /* The client backend isn't giving us compressed data so
426 * we can try to deflate it ourselves, this may save on.
433 memset(&stream
, 0, sizeof(stream
));
434 ret
= deflateInit2(&stream
, Z_BEST_COMPRESSION
,
435 Z_DEFLATED
, (15 + 16),
436 8, Z_DEFAULT_STRATEGY
);
438 die("cannot deflate request; zlib init error %d", ret
);
439 size
= deflateBound(&stream
, rpc
->len
);
440 gzip_body
= xmalloc(size
);
442 stream
.next_in
= (unsigned char *)rpc
->buf
;
443 stream
.avail_in
= rpc
->len
;
444 stream
.next_out
= (unsigned char *)gzip_body
;
445 stream
.avail_out
= size
;
447 ret
= deflate(&stream
, Z_FINISH
);
448 if (ret
!= Z_STREAM_END
)
449 die("cannot deflate request; zlib deflate error %d", ret
);
451 ret
= deflateEnd(&stream
);
453 die("cannot deflate request; zlib end error %d", ret
);
455 size
= stream
.total_out
;
457 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
458 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
459 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, size
);
461 if (options
.verbosity
> 1) {
462 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
464 (unsigned long)rpc
->len
, (unsigned long)size
);
468 /* We know the complete request size in advance, use the
469 * more normal Content-Length approach.
471 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
472 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, rpc
->len
);
473 if (options
.verbosity
> 1) {
474 fprintf(stderr
, "POST %s (%lu bytes)\n",
475 rpc
->service_name
, (unsigned long)rpc
->len
);
480 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
481 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
482 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
484 slot
->curl_result
= curl_easy_perform(slot
->curl
);
485 finish_active_slot(slot
);
487 if (results
.curl_result
!= CURLE_OK
) {
488 err
|= error("RPC failed; result=%d, HTTP code = %ld",
489 results
.curl_result
, results
.http_code
);
492 curl_slist_free_all(headers
);
497 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
499 const char *svc
= rpc
->service_name
;
500 struct strbuf buf
= STRBUF_INIT
;
501 struct child_process client
;
505 memset(&client
, 0, sizeof(client
));
509 client
.argv
= rpc
->argv
;
510 if (start_command(&client
))
513 write_or_die(client
.in
, heads
->buf
, heads
->len
);
515 rpc
->alloc
= http_post_buffer
;
516 rpc
->buf
= xmalloc(rpc
->alloc
);
518 rpc
->out
= client
.out
;
519 strbuf_init(&rpc
->result
, 0);
521 strbuf_addf(&buf
, "%s/%s", url
, svc
);
522 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
524 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
525 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
527 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
528 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
531 int n
= packet_read_line(rpc
->out
, rpc
->buf
, rpc
->alloc
);
536 err
|= post_rpc(rpc
);
538 strbuf_read(&rpc
->result
, client
.out
, 0);
545 err
|= finish_command(&client
);
546 free(rpc
->service_url
);
547 free(rpc
->hdr_content_type
);
548 free(rpc
->hdr_accept
);
550 strbuf_release(&buf
);
554 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
556 char **targets
= xmalloc(nr_heads
* sizeof(char*));
560 die("dumb http transport does not support --depth");
561 for (i
= 0; i
< nr_heads
; i
++)
562 targets
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
566 walker
->get_tree
= 1;
567 walker
->get_history
= 1;
568 walker
->get_verbosely
= options
.verbosity
>= 3;
569 walker
->get_recover
= 0;
570 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
572 for (i
= 0; i
< nr_heads
; i
++)
576 return ret
? error("Fetch failed.") : 0;
579 static int fetch_git(struct discovery
*heads
,
580 int nr_heads
, struct ref
**to_fetch
)
582 struct rpc_state rpc
;
583 char *depth_arg
= NULL
;
585 int argc
= 0, i
, err
;
587 argv
= xmalloc((15 + nr_heads
) * sizeof(char*));
588 argv
[argc
++] = "fetch-pack";
589 argv
[argc
++] = "--stateless-rpc";
590 argv
[argc
++] = "--lock-pack";
591 if (options
.followtags
)
592 argv
[argc
++] = "--include-tag";
594 argv
[argc
++] = "--thin";
595 if (options
.verbosity
>= 3) {
599 if (!options
.progress
)
600 argv
[argc
++] = "--no-progress";
602 struct strbuf buf
= STRBUF_INIT
;
603 strbuf_addf(&buf
, "--depth=%lu", options
.depth
);
604 depth_arg
= strbuf_detach(&buf
, NULL
);
605 argv
[argc
++] = depth_arg
;
608 for (i
= 0; i
< nr_heads
; i
++) {
609 struct ref
*ref
= to_fetch
[i
];
610 if (!ref
->name
|| !*ref
->name
)
611 die("cannot fetch by sha1 over smart http");
612 argv
[argc
++] = ref
->name
;
616 memset(&rpc
, 0, sizeof(rpc
));
617 rpc
.service_name
= "git-upload-pack",
619 rpc
.gzip_request
= 1;
621 err
= rpc_service(&rpc
, heads
);
623 safe_write(1, rpc
.result
.buf
, rpc
.result
.len
);
624 strbuf_release(&rpc
.result
);
630 static int fetch(int nr_heads
, struct ref
**to_fetch
)
632 struct discovery
*d
= discover_refs("git-upload-pack");
634 return fetch_git(d
, nr_heads
, to_fetch
);
636 return fetch_dumb(nr_heads
, to_fetch
);
639 static void parse_fetch(struct strbuf
*buf
)
641 struct ref
**to_fetch
= NULL
;
642 struct ref
*list_head
= NULL
;
643 struct ref
**list
= &list_head
;
644 int alloc_heads
= 0, nr_heads
= 0;
647 if (!prefixcmp(buf
->buf
, "fetch ")) {
648 char *p
= buf
->buf
+ strlen("fetch ");
651 unsigned char old_sha1
[20];
653 if (strlen(p
) < 40 || get_sha1_hex(p
, old_sha1
))
654 die("protocol error: expected sha/ref, got %s'", p
);
660 die("protocol error: expected sha/ref, got %s'", p
);
662 ref
= alloc_ref(name
);
663 hashcpy(ref
->old_sha1
, old_sha1
);
668 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
669 to_fetch
[nr_heads
++] = ref
;
672 die("http transport does not support %s", buf
->buf
);
675 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
681 if (fetch(nr_heads
, to_fetch
))
682 exit(128); /* error already reported */
683 free_refs(list_head
);
691 static int push_dav(int nr_spec
, char **specs
)
693 const char **argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
696 argv
[argc
++] = "http-push";
697 argv
[argc
++] = "--helper-status";
699 argv
[argc
++] = "--dry-run";
700 if (options
.verbosity
> 1)
701 argv
[argc
++] = "--verbose";
703 for (i
= 0; i
< nr_spec
; i
++)
704 argv
[argc
++] = specs
[i
];
707 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
708 die("git-%s failed", argv
[0]);
713 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
715 struct rpc_state rpc
;
717 int argc
= 0, i
, err
;
719 argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
720 argv
[argc
++] = "send-pack";
721 argv
[argc
++] = "--stateless-rpc";
722 argv
[argc
++] = "--helper-status";
724 argv
[argc
++] = "--thin";
726 argv
[argc
++] = "--dry-run";
727 if (options
.verbosity
> 1)
728 argv
[argc
++] = "--verbose";
730 for (i
= 0; i
< nr_spec
; i
++)
731 argv
[argc
++] = specs
[i
];
734 memset(&rpc
, 0, sizeof(rpc
));
735 rpc
.service_name
= "git-receive-pack",
738 err
= rpc_service(&rpc
, heads
);
740 safe_write(1, rpc
.result
.buf
, rpc
.result
.len
);
741 strbuf_release(&rpc
.result
);
746 static int push(int nr_spec
, char **specs
)
748 struct discovery
*heads
= discover_refs("git-receive-pack");
751 if (heads
->proto_git
)
752 ret
= push_git(heads
, nr_spec
, specs
);
754 ret
= push_dav(nr_spec
, specs
);
755 free_discovery(heads
);
759 static void parse_push(struct strbuf
*buf
)
762 int alloc_spec
= 0, nr_spec
= 0, i
;
765 if (!prefixcmp(buf
->buf
, "push ")) {
766 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
767 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
770 die("http transport does not support %s", buf
->buf
);
773 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
779 if (push(nr_spec
, specs
))
780 exit(128); /* error already reported */
781 for (i
= 0; i
< nr_spec
; i
++)
789 int main(int argc
, const char **argv
)
791 struct strbuf buf
= STRBUF_INIT
;
794 git_extract_argv0_path(argv
[0]);
795 setup_git_directory_gently(&nongit
);
797 fprintf(stderr
, "Remote needed\n");
801 options
.verbosity
= 1;
802 options
.progress
= !!isatty(2);
805 remote
= remote_get(argv
[1]);
810 url
= remote
->url
[0];
814 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
)
816 if (!prefixcmp(buf
.buf
, "fetch ")) {
818 die("Fetch attempted without a local repo");
821 } else if (!strcmp(buf
.buf
, "list") || !prefixcmp(buf
.buf
, "list ")) {
822 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
823 output_refs(get_refs(for_push
));
825 } else if (!prefixcmp(buf
.buf
, "push ")) {
828 } else if (!prefixcmp(buf
.buf
, "option ")) {
829 char *name
= buf
.buf
+ strlen("option ");
830 char *value
= strchr(name
, ' ');
838 result
= set_option(name
, value
);
842 printf("error invalid value\n");
844 printf("unsupported\n");
847 } else if (!strcmp(buf
.buf
, "capabilities")) {