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"))
48 return 1 /* TODO implement later */;
50 else if (!strcmp(name
, "depth")) {
52 unsigned long v
= strtoul(value
, &end
, 10);
53 if (value
== end
|| *end
)
56 return 1 /* TODO implement later */;
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;
65 return 1 /* TODO implement later */;
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;
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
);
127 case HTTP_MISSING_TARGET
:
128 die("%s not found: did you run git update-server-info on the"
129 " server?", refs_url
);
131 http_error(refs_url
, http_ret
);
132 die("HTTP request failed");
135 last
= xcalloc(1, sizeof(*last_discovery
));
136 last
->service
= service
;
137 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
138 last
->buf
= last
->buf_alloc
;
140 if (is_http
&& 5 <= last
->len
&& last
->buf
[4] == '#') {
141 /* smart HTTP response; validate that the service
142 * pkt-line matches our request.
144 struct strbuf exp
= STRBUF_INIT
;
146 if (packet_get_line(&buffer
, &last
->buf
, &last
->len
) <= 0)
147 die("%s has invalid packet header", refs_url
);
148 if (buffer
.len
&& buffer
.buf
[buffer
.len
- 1] == '\n')
149 strbuf_setlen(&buffer
, buffer
.len
- 1);
151 strbuf_addf(&exp
, "# service=%s", service
);
152 if (strbuf_cmp(&exp
, &buffer
))
153 die("invalid server response; got '%s'", buffer
.buf
);
154 strbuf_release(&exp
);
156 /* The header can include additional metadata lines, up
157 * until a packet flush marker. Ignore these now, but
158 * in the future we might start to scan them.
160 strbuf_reset(&buffer
);
161 while (packet_get_line(&buffer
, &last
->buf
, &last
->len
) > 0)
162 strbuf_reset(&buffer
);
168 strbuf_release(&buffer
);
169 last_discovery
= last
;
173 static int write_discovery(int fd
, void *data
)
175 struct discovery
*heads
= data
;
177 if (write_in_full(fd
, heads
->buf
, heads
->len
) != heads
->len
)
183 static struct ref
*parse_git_refs(struct discovery
*heads
)
185 struct ref
*list
= NULL
;
188 memset(&async
, 0, sizeof(async
));
189 async
.proc
= write_discovery
;
192 if (start_async(&async
))
193 die("cannot start thread to parse advertised refs");
194 get_remote_heads(async
.out
, &list
, 0, NULL
, 0, NULL
);
196 if (finish_async(&async
))
197 die("ref parsing thread failed");
201 static struct ref
*parse_info_refs(struct discovery
*heads
)
203 char *data
, *start
, *mid
;
207 struct ref
*refs
= NULL
;
208 struct ref
*ref
= NULL
;
209 struct ref
*last_ref
= NULL
;
214 while (i
< heads
->len
) {
220 if (data
[i
] == '\n') {
223 ref
= xmalloc(sizeof(struct ref
) +
224 strlen(ref_name
) + 1);
225 memset(ref
, 0, sizeof(struct ref
));
226 strcpy(ref
->name
, ref_name
);
227 get_sha1_hex(start
, ref
->old_sha1
);
231 last_ref
->next
= ref
;
239 ref
= alloc_ref("HEAD");
240 if (!walker
->fetch_ref(walker
, ref
) &&
241 !resolve_remote_symref(ref
, refs
)) {
251 static struct ref
*get_refs(int for_push
)
253 struct discovery
*heads
;
256 heads
= discover_refs("git-receive-pack");
258 heads
= discover_refs("git-upload-pack");
260 if (heads
->proto_git
)
261 return parse_git_refs(heads
);
262 return parse_info_refs(heads
);
265 static void output_refs(struct ref
*refs
)
268 for (posn
= refs
; posn
; posn
= posn
->next
) {
270 printf("@%s %s\n", posn
->symref
, posn
->name
);
272 printf("%s %s\n", sha1_to_hex(posn
->old_sha1
), posn
->name
);
280 const char *service_name
;
283 char *hdr_content_type
;
291 struct strbuf result
;
294 static size_t rpc_out(void *ptr
, size_t eltsize
,
295 size_t nmemb
, void *buffer_
)
297 size_t max
= eltsize
* nmemb
;
298 struct rpc_state
*rpc
= buffer_
;
299 size_t avail
= rpc
->len
- rpc
->pos
;
302 avail
= packet_read_line(rpc
->out
, rpc
->buf
, rpc
->alloc
);
311 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
316 static size_t rpc_in(const void *ptr
, size_t eltsize
,
317 size_t nmemb
, void *buffer_
)
319 size_t size
= eltsize
* nmemb
;
320 struct rpc_state
*rpc
= buffer_
;
321 write_or_die(rpc
->in
, ptr
, size
);
325 static int post_rpc(struct rpc_state
*rpc
)
327 struct active_request_slot
*slot
;
328 struct slot_results results
;
329 struct curl_slist
*headers
= NULL
;
330 int err
= 0, large_request
= 0;
332 /* Try to load the entire request, if we can fit it into the
333 * allocated buffer space we can use HTTP/1.0 and avoid the
334 * chunked encoding mess.
337 size_t left
= rpc
->alloc
- rpc
->len
;
338 char *buf
= rpc
->buf
+ rpc
->len
;
341 if (left
< LARGE_PACKET_MAX
) {
346 n
= packet_read_line(rpc
->out
, buf
, left
);
352 slot
= get_active_slot();
353 slot
->results
= &results
;
355 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
356 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
357 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
359 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
360 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
363 /* The request body is large and the size cannot be predicted.
364 * We must use chunked encoding to send it.
366 headers
= curl_slist_append(headers
, "Expect: 100-continue");
367 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
368 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
369 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
370 if (options
.verbosity
> 1) {
371 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
376 /* We know the complete request size in advance, use the
377 * more normal Content-Length approach.
379 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
380 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, rpc
->len
);
381 if (options
.verbosity
> 1) {
382 fprintf(stderr
, "POST %s (%lu bytes)\n",
383 rpc
->service_name
, (unsigned long)rpc
->len
);
388 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
389 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
390 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, rpc
);
392 slot
->curl_result
= curl_easy_perform(slot
->curl
);
393 finish_active_slot(slot
);
395 if (results
.curl_result
!= CURLE_OK
) {
396 err
|= error("RPC failed; result=%d, HTTP code = %ld",
397 results
.curl_result
, results
.http_code
);
400 curl_slist_free_all(headers
);
404 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
)
406 const char *svc
= rpc
->service_name
;
407 struct strbuf buf
= STRBUF_INIT
;
408 struct child_process client
;
412 memset(&client
, 0, sizeof(client
));
416 client
.argv
= rpc
->argv
;
417 if (start_command(&client
))
420 write_or_die(client
.in
, heads
->buf
, heads
->len
);
422 rpc
->alloc
= http_post_buffer
;
423 rpc
->buf
= xmalloc(rpc
->alloc
);
425 rpc
->out
= client
.out
;
426 strbuf_init(&rpc
->result
, 0);
428 strbuf_addf(&buf
, "%s/%s", url
, svc
);
429 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
431 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
432 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
434 strbuf_addf(&buf
, "Accept: application/x-%s-response", svc
);
435 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
438 int n
= packet_read_line(rpc
->out
, rpc
->buf
, rpc
->alloc
);
443 err
|= post_rpc(rpc
);
445 strbuf_read(&rpc
->result
, client
.out
, 0);
452 err
|= finish_command(&client
);
453 free(rpc
->service_url
);
454 free(rpc
->hdr_content_type
);
455 free(rpc
->hdr_accept
);
457 strbuf_release(&buf
);
461 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
463 char **targets
= xmalloc(nr_heads
* sizeof(char*));
466 for (i
= 0; i
< nr_heads
; i
++)
467 targets
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
471 walker
->get_tree
= 1;
472 walker
->get_history
= 1;
473 walker
->get_verbosely
= options
.verbosity
>= 3;
474 walker
->get_recover
= 0;
475 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
477 for (i
= 0; i
< nr_heads
; i
++)
481 return ret
? error("Fetch failed.") : 0;
484 static void parse_fetch(struct strbuf
*buf
)
486 struct ref
**to_fetch
= NULL
;
487 struct ref
*list_head
= NULL
;
488 struct ref
**list
= &list_head
;
489 int alloc_heads
= 0, nr_heads
= 0;
492 if (!prefixcmp(buf
->buf
, "fetch ")) {
493 char *p
= buf
->buf
+ strlen("fetch ");
496 unsigned char old_sha1
[20];
498 if (strlen(p
) < 40 || get_sha1_hex(p
, old_sha1
))
499 die("protocol error: expected sha/ref, got %s'", p
);
505 die("protocol error: expected sha/ref, got %s'", p
);
507 ref
= alloc_ref(name
);
508 hashcpy(ref
->old_sha1
, old_sha1
);
513 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
514 to_fetch
[nr_heads
++] = ref
;
517 die("http transport does not support %s", buf
->buf
);
520 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
526 if (fetch_dumb(nr_heads
, to_fetch
))
527 exit(128); /* error already reported */
528 free_refs(list_head
);
536 static int push_dav(int nr_spec
, char **specs
)
538 const char **argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
541 argv
[argc
++] = "http-push";
542 argv
[argc
++] = "--helper-status";
544 argv
[argc
++] = "--dry-run";
545 if (options
.verbosity
> 1)
546 argv
[argc
++] = "--verbose";
548 for (i
= 0; i
< nr_spec
; i
++)
549 argv
[argc
++] = specs
[i
];
552 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
553 die("git-%s failed", argv
[0]);
558 static int push_git(struct discovery
*heads
, int nr_spec
, char **specs
)
560 struct rpc_state rpc
;
562 int argc
= 0, i
, err
;
564 argv
= xmalloc((10 + nr_spec
) * sizeof(char*));
565 argv
[argc
++] = "send-pack";
566 argv
[argc
++] = "--stateless-rpc";
567 argv
[argc
++] = "--helper-status";
569 argv
[argc
++] = "--thin";
571 argv
[argc
++] = "--dry-run";
572 if (options
.verbosity
> 1)
573 argv
[argc
++] = "--verbose";
575 for (i
= 0; i
< nr_spec
; i
++)
576 argv
[argc
++] = specs
[i
];
579 memset(&rpc
, 0, sizeof(rpc
));
580 rpc
.service_name
= "git-receive-pack",
583 err
= rpc_service(&rpc
, heads
);
585 safe_write(1, rpc
.result
.buf
, rpc
.result
.len
);
586 strbuf_release(&rpc
.result
);
591 static int push(int nr_spec
, char **specs
)
593 struct discovery
*heads
= discover_refs("git-receive-pack");
596 if (heads
->proto_git
)
597 ret
= push_git(heads
, nr_spec
, specs
);
599 ret
= push_dav(nr_spec
, specs
);
600 free_discovery(heads
);
604 static void parse_push(struct strbuf
*buf
)
607 int alloc_spec
= 0, nr_spec
= 0, i
;
610 if (!prefixcmp(buf
->buf
, "push ")) {
611 ALLOC_GROW(specs
, nr_spec
+ 1, alloc_spec
);
612 specs
[nr_spec
++] = xstrdup(buf
->buf
+ 5);
615 die("http transport does not support %s", buf
->buf
);
618 if (strbuf_getline(buf
, stdin
, '\n') == EOF
)
624 if (push(nr_spec
, specs
))
625 exit(128); /* error already reported */
626 for (i
= 0; i
< nr_spec
; i
++)
634 int main(int argc
, const char **argv
)
636 struct strbuf buf
= STRBUF_INIT
;
638 git_extract_argv0_path(argv
[0]);
639 setup_git_directory();
641 fprintf(stderr
, "Remote needed\n");
645 options
.verbosity
= 1;
646 options
.progress
= !!isatty(2);
649 remote
= remote_get(argv
[1]);
654 url
= remote
->url
[0];
658 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
)
660 if (!prefixcmp(buf
.buf
, "fetch ")) {
663 } else if (!strcmp(buf
.buf
, "list") || !prefixcmp(buf
.buf
, "list ")) {
664 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
665 output_refs(get_refs(for_push
));
667 } else if (!prefixcmp(buf
.buf
, "push ")) {
670 } else if (!prefixcmp(buf
.buf
, "option ")) {
671 char *name
= buf
.buf
+ strlen("option ");
672 char *value
= strchr(name
, ' ');
680 result
= set_option(name
, value
);
684 printf("error invalid value\n");
686 printf("unsupported\n");
689 } else if (!strcmp(buf
.buf
, "capabilities")) {