]>
Commit | Line | Data |
---|---|---|
a2d725b7 DB |
1 | #include "cache.h" |
2 | #include "remote.h" | |
3 | #include "strbuf.h" | |
4 | #include "walker.h" | |
5 | #include "http.h" | |
d01a8e32 | 6 | #include "exec_cmd.h" |
ae4efe19 | 7 | #include "run-command.h" |
97cc7bc4 | 8 | #include "pkt-line.h" |
de1a2fdd | 9 | #include "sideband.h" |
222b1212 | 10 | #include "argv-array.h" |
a2d725b7 | 11 | |
37a8768f | 12 | static struct remote *remote; |
d8fab072 | 13 | static const char *url; /* always ends with a trailing slash */ |
37a8768f | 14 | |
ef08ef9e SP |
15 | struct options { |
16 | int verbosity; | |
17 | unsigned long depth; | |
18 | unsigned progress : 1, | |
ae4efe19 | 19 | followtags : 1, |
de1a2fdd SP |
20 | dry_run : 1, |
21 | thin : 1; | |
ef08ef9e SP |
22 | }; |
23 | static struct options options; | |
24 | ||
ef08ef9e SP |
25 | static int set_option(const char *name, const char *value) |
26 | { | |
27 | if (!strcmp(name, "verbosity")) { | |
28 | char *end; | |
29 | int v = strtol(value, &end, 10); | |
30 | if (value == end || *end) | |
31 | return -1; | |
32 | options.verbosity = v; | |
33 | return 0; | |
34 | } | |
35 | else if (!strcmp(name, "progress")) { | |
36 | if (!strcmp(value, "true")) | |
37 | options.progress = 1; | |
38 | else if (!strcmp(value, "false")) | |
39 | options.progress = 0; | |
40 | else | |
41 | return -1; | |
249b2004 | 42 | return 0; |
ef08ef9e SP |
43 | } |
44 | else if (!strcmp(name, "depth")) { | |
45 | char *end; | |
46 | unsigned long v = strtoul(value, &end, 10); | |
47 | if (value == end || *end) | |
48 | return -1; | |
49 | options.depth = v; | |
249b2004 | 50 | return 0; |
ef08ef9e SP |
51 | } |
52 | else if (!strcmp(name, "followtags")) { | |
53 | if (!strcmp(value, "true")) | |
54 | options.followtags = 1; | |
55 | else if (!strcmp(value, "false")) | |
56 | options.followtags = 0; | |
57 | else | |
58 | return -1; | |
249b2004 | 59 | return 0; |
ef08ef9e | 60 | } |
ae4efe19 SP |
61 | else if (!strcmp(name, "dry-run")) { |
62 | if (!strcmp(value, "true")) | |
63 | options.dry_run = 1; | |
64 | else if (!strcmp(value, "false")) | |
65 | options.dry_run = 0; | |
66 | else | |
67 | return -1; | |
68 | return 0; | |
69 | } | |
ef08ef9e SP |
70 | else { |
71 | return 1 /* unsupported */; | |
72 | } | |
73 | } | |
74 | ||
97cc7bc4 SP |
75 | struct discovery { |
76 | const char *service; | |
77 | char *buf_alloc; | |
78 | char *buf; | |
79 | size_t len; | |
2a455202 | 80 | struct ref *refs; |
97cc7bc4 SP |
81 | unsigned proto_git : 1; |
82 | }; | |
83 | static struct discovery *last_discovery; | |
84 | ||
b8054bbe JK |
85 | static struct ref *parse_git_refs(struct discovery *heads, int for_push) |
86 | { | |
87 | struct ref *list = NULL; | |
88 | get_remote_heads(-1, heads->buf, heads->len, &list, | |
89 | for_push ? REF_NORMAL : 0, NULL); | |
90 | return list; | |
91 | } | |
92 | ||
93 | static struct ref *parse_info_refs(struct discovery *heads) | |
94 | { | |
95 | char *data, *start, *mid; | |
96 | char *ref_name; | |
97 | int i = 0; | |
98 | ||
99 | struct ref *refs = NULL; | |
100 | struct ref *ref = NULL; | |
101 | struct ref *last_ref = NULL; | |
102 | ||
103 | data = heads->buf; | |
104 | start = NULL; | |
105 | mid = data; | |
106 | while (i < heads->len) { | |
107 | if (!start) { | |
108 | start = &data[i]; | |
109 | } | |
110 | if (data[i] == '\t') | |
111 | mid = &data[i]; | |
112 | if (data[i] == '\n') { | |
113 | if (mid - start != 40) | |
114 | die("%sinfo/refs not valid: is this a git repository?", url); | |
115 | data[i] = 0; | |
116 | ref_name = mid + 1; | |
117 | ref = xmalloc(sizeof(struct ref) + | |
118 | strlen(ref_name) + 1); | |
119 | memset(ref, 0, sizeof(struct ref)); | |
120 | strcpy(ref->name, ref_name); | |
121 | get_sha1_hex(start, ref->old_sha1); | |
122 | if (!refs) | |
123 | refs = ref; | |
124 | if (last_ref) | |
125 | last_ref->next = ref; | |
126 | last_ref = ref; | |
127 | start = NULL; | |
128 | } | |
129 | i++; | |
130 | } | |
131 | ||
132 | ref = alloc_ref("HEAD"); | |
133 | if (!http_fetch_ref(url, ref) && | |
134 | !resolve_remote_symref(ref, refs)) { | |
135 | ref->next = refs; | |
136 | refs = ref; | |
137 | } else { | |
138 | free(ref); | |
139 | } | |
140 | ||
141 | return refs; | |
142 | } | |
143 | ||
97cc7bc4 SP |
144 | static void free_discovery(struct discovery *d) |
145 | { | |
146 | if (d) { | |
147 | if (d == last_discovery) | |
148 | last_discovery = NULL; | |
149 | free(d->buf_alloc); | |
2a455202 | 150 | free_refs(d->refs); |
97cc7bc4 SP |
151 | free(d); |
152 | } | |
153 | } | |
154 | ||
426e70d4 JK |
155 | static int show_http_message(struct strbuf *type, struct strbuf *msg) |
156 | { | |
157 | const char *p, *eol; | |
158 | ||
159 | /* | |
160 | * We only show text/plain parts, as other types are likely | |
161 | * to be ugly to look at on the user's terminal. | |
162 | * | |
163 | * TODO should handle "; charset=XXX", and re-encode into | |
164 | * logoutputencoding | |
165 | */ | |
166 | if (strcasecmp(type->buf, "text/plain")) | |
167 | return -1; | |
168 | ||
169 | strbuf_trim(msg); | |
170 | if (!msg->len) | |
171 | return -1; | |
172 | ||
173 | p = msg->buf; | |
174 | do { | |
175 | eol = strchrnul(p, '\n'); | |
176 | fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p); | |
177 | p = eol + 1; | |
178 | } while(*eol); | |
179 | return 0; | |
180 | } | |
181 | ||
2a455202 | 182 | static struct discovery* discover_refs(const char *service, int for_push) |
a2d725b7 | 183 | { |
4656bf47 SP |
184 | struct strbuf exp = STRBUF_INIT; |
185 | struct strbuf type = STRBUF_INIT; | |
a2d725b7 | 186 | struct strbuf buffer = STRBUF_INIT; |
97cc7bc4 | 187 | struct discovery *last = last_discovery; |
a2d725b7 | 188 | char *refs_url; |
243c329c | 189 | int http_ret, maybe_smart = 0; |
1bbcc224 | 190 | struct http_get_options options; |
a2d725b7 | 191 | |
97cc7bc4 SP |
192 | if (last && !strcmp(service, last->service)) |
193 | return last; | |
194 | free_discovery(last); | |
a2d725b7 | 195 | |
d8fab072 | 196 | strbuf_addf(&buffer, "%sinfo/refs", url); |
02572c2e JK |
197 | if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) && |
198 | git_env_bool("GIT_SMART_HTTP", 1)) { | |
243c329c | 199 | maybe_smart = 1; |
97cc7bc4 SP |
200 | if (!strchr(url, '?')) |
201 | strbuf_addch(&buffer, '?'); | |
202 | else | |
203 | strbuf_addch(&buffer, '&'); | |
204 | strbuf_addf(&buffer, "service=%s", service); | |
205 | } | |
206 | refs_url = strbuf_detach(&buffer, NULL); | |
a2d725b7 | 207 | |
1bbcc224 JK |
208 | memset(&options, 0, sizeof(options)); |
209 | options.content_type = &type; | |
210 | options.no_cache = 1; | |
211 | options.keep_error = 1; | |
212 | ||
213 | http_ret = http_get_strbuf(refs_url, &buffer, &options); | |
a2d725b7 DB |
214 | switch (http_ret) { |
215 | case HTTP_OK: | |
216 | break; | |
217 | case HTTP_MISSING_TARGET: | |
cfa0f404 JK |
218 | show_http_message(&type, &buffer); |
219 | die("repository '%s' not found", url); | |
42653c09 | 220 | case HTTP_NOAUTH: |
426e70d4 | 221 | show_http_message(&type, &buffer); |
d5ccbe4d | 222 | die("Authentication failed for '%s'", url); |
a2d725b7 | 223 | default: |
426e70d4 | 224 | show_http_message(&type, &buffer); |
de89f0b2 | 225 | die("unable to access '%s': %s", url, curl_errorstr); |
a2d725b7 DB |
226 | } |
227 | ||
97cc7bc4 SP |
228 | last= xcalloc(1, sizeof(*last_discovery)); |
229 | last->service = service; | |
230 | last->buf_alloc = strbuf_detach(&buffer, &last->len); | |
231 | last->buf = last->buf_alloc; | |
232 | ||
4656bf47 SP |
233 | strbuf_addf(&exp, "application/x-%s-advertisement", service); |
234 | if (maybe_smart && | |
235 | (5 <= last->len && last->buf[4] == '#') && | |
236 | !strbuf_cmp(&exp, &type)) { | |
4981fe75 JK |
237 | char *line; |
238 | ||
4656bf47 SP |
239 | /* |
240 | * smart HTTP response; validate that the service | |
97cc7bc4 SP |
241 | * pkt-line matches our request. |
242 | */ | |
4981fe75 | 243 | line = packet_read_line_buf(&last->buf, &last->len, NULL); |
97cc7bc4 | 244 | |
4656bf47 | 245 | strbuf_reset(&exp); |
97cc7bc4 | 246 | strbuf_addf(&exp, "# service=%s", service); |
4981fe75 JK |
247 | if (strcmp(line, exp.buf)) |
248 | die("invalid server response; got '%s'", line); | |
97cc7bc4 SP |
249 | strbuf_release(&exp); |
250 | ||
251 | /* The header can include additional metadata lines, up | |
252 | * until a packet flush marker. Ignore these now, but | |
253 | * in the future we might start to scan them. | |
254 | */ | |
4981fe75 JK |
255 | while (packet_read_line_buf(&last->buf, &last->len, NULL)) |
256 | ; | |
97cc7bc4 SP |
257 | |
258 | last->proto_git = 1; | |
259 | } | |
260 | ||
2a455202 JK |
261 | if (last->proto_git) |
262 | last->refs = parse_git_refs(last, for_push); | |
263 | else | |
264 | last->refs = parse_info_refs(last); | |
265 | ||
97cc7bc4 | 266 | free(refs_url); |
4656bf47 SP |
267 | strbuf_release(&exp); |
268 | strbuf_release(&type); | |
97cc7bc4 SP |
269 | strbuf_release(&buffer); |
270 | last_discovery = last; | |
271 | return last; | |
272 | } | |
273 | ||
97cc7bc4 SP |
274 | static struct ref *get_refs(int for_push) |
275 | { | |
276 | struct discovery *heads; | |
277 | ||
278 | if (for_push) | |
2a455202 | 279 | heads = discover_refs("git-receive-pack", for_push); |
97cc7bc4 | 280 | else |
2a455202 | 281 | heads = discover_refs("git-upload-pack", for_push); |
97cc7bc4 | 282 | |
2a455202 | 283 | return heads->refs; |
97cc7bc4 SP |
284 | } |
285 | ||
ae4efe19 SP |
286 | static void output_refs(struct ref *refs) |
287 | { | |
288 | struct ref *posn; | |
289 | for (posn = refs; posn; posn = posn->next) { | |
290 | if (posn->symref) | |
291 | printf("@%s %s\n", posn->symref, posn->name); | |
292 | else | |
293 | printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name); | |
294 | } | |
295 | printf("\n"); | |
296 | fflush(stdout); | |
ae4efe19 SP |
297 | } |
298 | ||
de1a2fdd SP |
299 | struct rpc_state { |
300 | const char *service_name; | |
301 | const char **argv; | |
8150749d | 302 | struct strbuf *stdin_preamble; |
de1a2fdd SP |
303 | char *service_url; |
304 | char *hdr_content_type; | |
305 | char *hdr_accept; | |
306 | char *buf; | |
307 | size_t alloc; | |
308 | size_t len; | |
309 | size_t pos; | |
310 | int in; | |
311 | int out; | |
312 | struct strbuf result; | |
b8538603 | 313 | unsigned gzip_request : 1; |
6c81a990 | 314 | unsigned initial_buffer : 1; |
de1a2fdd SP |
315 | }; |
316 | ||
317 | static size_t rpc_out(void *ptr, size_t eltsize, | |
318 | size_t nmemb, void *buffer_) | |
319 | { | |
320 | size_t max = eltsize * nmemb; | |
321 | struct rpc_state *rpc = buffer_; | |
322 | size_t avail = rpc->len - rpc->pos; | |
323 | ||
324 | if (!avail) { | |
6c81a990 | 325 | rpc->initial_buffer = 0; |
4981fe75 | 326 | avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); |
de1a2fdd SP |
327 | if (!avail) |
328 | return 0; | |
329 | rpc->pos = 0; | |
330 | rpc->len = avail; | |
331 | } | |
332 | ||
48310608 | 333 | if (max < avail) |
de1a2fdd SP |
334 | avail = max; |
335 | memcpy(ptr, rpc->buf + rpc->pos, avail); | |
336 | rpc->pos += avail; | |
337 | return avail; | |
338 | } | |
339 | ||
6c81a990 | 340 | #ifndef NO_CURL_IOCTL |
5092d3ec | 341 | static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp) |
6c81a990 MS |
342 | { |
343 | struct rpc_state *rpc = clientp; | |
344 | ||
345 | switch (cmd) { | |
346 | case CURLIOCMD_NOP: | |
347 | return CURLIOE_OK; | |
348 | ||
349 | case CURLIOCMD_RESTARTREAD: | |
350 | if (rpc->initial_buffer) { | |
351 | rpc->pos = 0; | |
352 | return CURLIOE_OK; | |
353 | } | |
354 | fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n"); | |
355 | return CURLIOE_FAILRESTART; | |
356 | ||
357 | default: | |
358 | return CURLIOE_UNKNOWNCMD; | |
359 | } | |
360 | } | |
361 | #endif | |
362 | ||
a04ff3ec | 363 | static size_t rpc_in(char *ptr, size_t eltsize, |
de1a2fdd SP |
364 | size_t nmemb, void *buffer_) |
365 | { | |
366 | size_t size = eltsize * nmemb; | |
367 | struct rpc_state *rpc = buffer_; | |
368 | write_or_die(rpc->in, ptr, size); | |
369 | return size; | |
370 | } | |
371 | ||
206b099d SP |
372 | static int run_slot(struct active_request_slot *slot) |
373 | { | |
b81401c1 | 374 | int err; |
206b099d SP |
375 | struct slot_results results; |
376 | ||
377 | slot->results = &results; | |
378 | slot->curl_result = curl_easy_perform(slot->curl); | |
379 | finish_active_slot(slot); | |
380 | ||
1960897e | 381 | err = handle_curl_result(&results); |
b81401c1 JK |
382 | if (err != HTTP_OK && err != HTTP_REAUTH) { |
383 | error("RPC failed; result=%d, HTTP code = %ld", | |
384 | results.curl_result, results.http_code); | |
206b099d SP |
385 | } |
386 | ||
387 | return err; | |
388 | } | |
389 | ||
390 | static int probe_rpc(struct rpc_state *rpc) | |
391 | { | |
392 | struct active_request_slot *slot; | |
393 | struct curl_slist *headers = NULL; | |
394 | struct strbuf buf = STRBUF_INIT; | |
395 | int err; | |
396 | ||
397 | slot = get_active_slot(); | |
398 | ||
399 | headers = curl_slist_append(headers, rpc->hdr_content_type); | |
400 | headers = curl_slist_append(headers, rpc->hdr_accept); | |
401 | ||
402 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); | |
403 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); | |
404 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); | |
aa90b969 | 405 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL); |
206b099d SP |
406 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000"); |
407 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4); | |
408 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); | |
409 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); | |
410 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf); | |
411 | ||
412 | err = run_slot(slot); | |
413 | ||
414 | curl_slist_free_all(headers); | |
415 | strbuf_release(&buf); | |
416 | return err; | |
417 | } | |
418 | ||
de1a2fdd SP |
419 | static int post_rpc(struct rpc_state *rpc) |
420 | { | |
421 | struct active_request_slot *slot; | |
de1a2fdd | 422 | struct curl_slist *headers = NULL; |
b8538603 SP |
423 | int use_gzip = rpc->gzip_request; |
424 | char *gzip_body = NULL; | |
37711549 | 425 | size_t gzip_size = 0; |
206b099d | 426 | int err, large_request = 0; |
de1a2fdd SP |
427 | |
428 | /* Try to load the entire request, if we can fit it into the | |
429 | * allocated buffer space we can use HTTP/1.0 and avoid the | |
430 | * chunked encoding mess. | |
431 | */ | |
432 | while (1) { | |
433 | size_t left = rpc->alloc - rpc->len; | |
434 | char *buf = rpc->buf + rpc->len; | |
435 | int n; | |
436 | ||
437 | if (left < LARGE_PACKET_MAX) { | |
438 | large_request = 1; | |
b8538603 | 439 | use_gzip = 0; |
de1a2fdd SP |
440 | break; |
441 | } | |
442 | ||
4981fe75 | 443 | n = packet_read(rpc->out, NULL, NULL, buf, left, 0); |
de1a2fdd SP |
444 | if (!n) |
445 | break; | |
446 | rpc->len += n; | |
447 | } | |
448 | ||
206b099d | 449 | if (large_request) { |
b81401c1 JK |
450 | do { |
451 | err = probe_rpc(rpc); | |
452 | } while (err == HTTP_REAUTH); | |
453 | if (err != HTTP_OK) | |
454 | return -1; | |
206b099d SP |
455 | } |
456 | ||
abf8df86 JK |
457 | headers = curl_slist_append(headers, rpc->hdr_content_type); |
458 | headers = curl_slist_append(headers, rpc->hdr_accept); | |
459 | headers = curl_slist_append(headers, "Expect:"); | |
460 | ||
461 | retry: | |
de1a2fdd | 462 | slot = get_active_slot(); |
de1a2fdd | 463 | |
de1a2fdd | 464 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
d21f9794 | 465 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); |
de1a2fdd | 466 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); |
aa90b969 | 467 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip"); |
de1a2fdd | 468 | |
de1a2fdd SP |
469 | if (large_request) { |
470 | /* The request body is large and the size cannot be predicted. | |
471 | * We must use chunked encoding to send it. | |
472 | */ | |
de1a2fdd | 473 | headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); |
6c81a990 | 474 | rpc->initial_buffer = 1; |
de1a2fdd SP |
475 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out); |
476 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc); | |
6c81a990 MS |
477 | #ifndef NO_CURL_IOCTL |
478 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl); | |
479 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc); | |
480 | #endif | |
de1a2fdd SP |
481 | if (options.verbosity > 1) { |
482 | fprintf(stderr, "POST %s (chunked)\n", rpc->service_name); | |
483 | fflush(stderr); | |
484 | } | |
485 | ||
2e736fd5 JK |
486 | } else if (gzip_body) { |
487 | /* | |
488 | * If we are looping to retry authentication, then the previous | |
489 | * run will have set up the headers and gzip buffer already, | |
490 | * and we just need to send it. | |
491 | */ | |
492 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); | |
493 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size); | |
494 | ||
b8538603 SP |
495 | } else if (use_gzip && 1024 < rpc->len) { |
496 | /* The client backend isn't giving us compressed data so | |
497 | * we can try to deflate it ourselves, this may save on. | |
498 | * the transfer time. | |
499 | */ | |
ef49a7a0 | 500 | git_zstream stream; |
b8538603 SP |
501 | int ret; |
502 | ||
503 | memset(&stream, 0, sizeof(stream)); | |
55bb5c91 | 504 | git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION); |
df126e10 JK |
505 | gzip_size = git_deflate_bound(&stream, rpc->len); |
506 | gzip_body = xmalloc(gzip_size); | |
b8538603 SP |
507 | |
508 | stream.next_in = (unsigned char *)rpc->buf; | |
509 | stream.avail_in = rpc->len; | |
510 | stream.next_out = (unsigned char *)gzip_body; | |
df126e10 | 511 | stream.avail_out = gzip_size; |
b8538603 | 512 | |
55bb5c91 | 513 | ret = git_deflate(&stream, Z_FINISH); |
b8538603 SP |
514 | if (ret != Z_STREAM_END) |
515 | die("cannot deflate request; zlib deflate error %d", ret); | |
516 | ||
55bb5c91 | 517 | ret = git_deflate_end_gently(&stream); |
b8538603 SP |
518 | if (ret != Z_OK) |
519 | die("cannot deflate request; zlib end error %d", ret); | |
520 | ||
df126e10 | 521 | gzip_size = stream.total_out; |
b8538603 SP |
522 | |
523 | headers = curl_slist_append(headers, "Content-Encoding: gzip"); | |
524 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); | |
df126e10 | 525 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size); |
b8538603 SP |
526 | |
527 | if (options.verbosity > 1) { | |
528 | fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", | |
529 | rpc->service_name, | |
df126e10 | 530 | (unsigned long)rpc->len, (unsigned long)gzip_size); |
b8538603 SP |
531 | fflush(stderr); |
532 | } | |
de1a2fdd SP |
533 | } else { |
534 | /* We know the complete request size in advance, use the | |
535 | * more normal Content-Length approach. | |
536 | */ | |
537 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf); | |
538 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len); | |
539 | if (options.verbosity > 1) { | |
540 | fprintf(stderr, "POST %s (%lu bytes)\n", | |
541 | rpc->service_name, (unsigned long)rpc->len); | |
542 | fflush(stderr); | |
543 | } | |
544 | } | |
545 | ||
546 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); | |
547 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in); | |
548 | curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc); | |
549 | ||
abf8df86 | 550 | err = run_slot(slot); |
2e736fd5 | 551 | if (err == HTTP_REAUTH && !large_request) |
abf8df86 | 552 | goto retry; |
b81401c1 JK |
553 | if (err != HTTP_OK) |
554 | err = -1; | |
de1a2fdd SP |
555 | |
556 | curl_slist_free_all(headers); | |
b8538603 | 557 | free(gzip_body); |
de1a2fdd SP |
558 | return err; |
559 | } | |
560 | ||
561 | static int rpc_service(struct rpc_state *rpc, struct discovery *heads) | |
562 | { | |
563 | const char *svc = rpc->service_name; | |
564 | struct strbuf buf = STRBUF_INIT; | |
8150749d | 565 | struct strbuf *preamble = rpc->stdin_preamble; |
de1a2fdd SP |
566 | struct child_process client; |
567 | int err = 0; | |
568 | ||
de1a2fdd SP |
569 | memset(&client, 0, sizeof(client)); |
570 | client.in = -1; | |
571 | client.out = -1; | |
572 | client.git_cmd = 1; | |
573 | client.argv = rpc->argv; | |
574 | if (start_command(&client)) | |
575 | exit(1); | |
8150749d IT |
576 | if (preamble) |
577 | write_or_die(client.in, preamble->buf, preamble->len); | |
de1a2fdd SP |
578 | if (heads) |
579 | write_or_die(client.in, heads->buf, heads->len); | |
580 | ||
581 | rpc->alloc = http_post_buffer; | |
582 | rpc->buf = xmalloc(rpc->alloc); | |
583 | rpc->in = client.in; | |
584 | rpc->out = client.out; | |
585 | strbuf_init(&rpc->result, 0); | |
586 | ||
d8fab072 | 587 | strbuf_addf(&buf, "%s%s", url, svc); |
de1a2fdd SP |
588 | rpc->service_url = strbuf_detach(&buf, NULL); |
589 | ||
590 | strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); | |
591 | rpc->hdr_content_type = strbuf_detach(&buf, NULL); | |
592 | ||
8efa5f62 | 593 | strbuf_addf(&buf, "Accept: application/x-%s-result", svc); |
de1a2fdd SP |
594 | rpc->hdr_accept = strbuf_detach(&buf, NULL); |
595 | ||
596 | while (!err) { | |
4981fe75 | 597 | int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); |
de1a2fdd SP |
598 | if (!n) |
599 | break; | |
600 | rpc->pos = 0; | |
601 | rpc->len = n; | |
602 | err |= post_rpc(rpc); | |
603 | } | |
de1a2fdd SP |
604 | |
605 | close(client.in); | |
de1a2fdd | 606 | client.in = -1; |
6cdf0223 SP |
607 | if (!err) { |
608 | strbuf_read(&rpc->result, client.out, 0); | |
609 | } else { | |
610 | char buf[4096]; | |
611 | for (;;) | |
612 | if (xread(client.out, buf, sizeof(buf)) <= 0) | |
613 | break; | |
614 | } | |
b4ee10f6 SP |
615 | |
616 | close(client.out); | |
de1a2fdd SP |
617 | client.out = -1; |
618 | ||
619 | err |= finish_command(&client); | |
620 | free(rpc->service_url); | |
621 | free(rpc->hdr_content_type); | |
622 | free(rpc->hdr_accept); | |
623 | free(rpc->buf); | |
624 | strbuf_release(&buf); | |
625 | return err; | |
626 | } | |
627 | ||
292ce46b SP |
628 | static int fetch_dumb(int nr_heads, struct ref **to_fetch) |
629 | { | |
26e1e0b2 | 630 | struct walker *walker; |
292ce46b SP |
631 | char **targets = xmalloc(nr_heads * sizeof(char*)); |
632 | int ret, i; | |
633 | ||
249b2004 SP |
634 | if (options.depth) |
635 | die("dumb http transport does not support --depth"); | |
292ce46b SP |
636 | for (i = 0; i < nr_heads; i++) |
637 | targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1)); | |
638 | ||
26e1e0b2 | 639 | walker = get_http_walker(url); |
292ce46b SP |
640 | walker->get_all = 1; |
641 | walker->get_tree = 1; | |
642 | walker->get_history = 1; | |
ef08ef9e | 643 | walker->get_verbosely = options.verbosity >= 3; |
292ce46b SP |
644 | walker->get_recover = 0; |
645 | ret = walker_fetch(walker, nr_heads, targets, NULL, NULL); | |
26e1e0b2 | 646 | walker_free(walker); |
292ce46b SP |
647 | |
648 | for (i = 0; i < nr_heads; i++) | |
649 | free(targets[i]); | |
650 | free(targets); | |
651 | ||
652 | return ret ? error("Fetch failed.") : 0; | |
653 | } | |
654 | ||
249b2004 SP |
655 | static int fetch_git(struct discovery *heads, |
656 | int nr_heads, struct ref **to_fetch) | |
657 | { | |
658 | struct rpc_state rpc; | |
8150749d | 659 | struct strbuf preamble = STRBUF_INIT; |
249b2004 | 660 | char *depth_arg = NULL; |
249b2004 | 661 | int argc = 0, i, err; |
8150749d | 662 | const char *argv[15]; |
249b2004 | 663 | |
249b2004 SP |
664 | argv[argc++] = "fetch-pack"; |
665 | argv[argc++] = "--stateless-rpc"; | |
8150749d | 666 | argv[argc++] = "--stdin"; |
249b2004 SP |
667 | argv[argc++] = "--lock-pack"; |
668 | if (options.followtags) | |
669 | argv[argc++] = "--include-tag"; | |
670 | if (options.thin) | |
671 | argv[argc++] = "--thin"; | |
672 | if (options.verbosity >= 3) { | |
673 | argv[argc++] = "-v"; | |
674 | argv[argc++] = "-v"; | |
675 | } | |
676 | if (!options.progress) | |
677 | argv[argc++] = "--no-progress"; | |
678 | if (options.depth) { | |
679 | struct strbuf buf = STRBUF_INIT; | |
680 | strbuf_addf(&buf, "--depth=%lu", options.depth); | |
681 | depth_arg = strbuf_detach(&buf, NULL); | |
682 | argv[argc++] = depth_arg; | |
683 | } | |
684 | argv[argc++] = url; | |
8150749d IT |
685 | argv[argc++] = NULL; |
686 | ||
249b2004 SP |
687 | for (i = 0; i < nr_heads; i++) { |
688 | struct ref *ref = to_fetch[i]; | |
689 | if (!ref->name || !*ref->name) | |
690 | die("cannot fetch by sha1 over smart http"); | |
8150749d | 691 | packet_buf_write(&preamble, "%s\n", ref->name); |
249b2004 | 692 | } |
8150749d | 693 | packet_buf_flush(&preamble); |
249b2004 SP |
694 | |
695 | memset(&rpc, 0, sizeof(rpc)); | |
696 | rpc.service_name = "git-upload-pack", | |
697 | rpc.argv = argv; | |
8150749d | 698 | rpc.stdin_preamble = &preamble; |
b8538603 | 699 | rpc.gzip_request = 1; |
249b2004 SP |
700 | |
701 | err = rpc_service(&rpc, heads); | |
702 | if (rpc.result.len) | |
cdf4fb8e | 703 | write_or_die(1, rpc.result.buf, rpc.result.len); |
249b2004 | 704 | strbuf_release(&rpc.result); |
8150749d | 705 | strbuf_release(&preamble); |
249b2004 SP |
706 | free(depth_arg); |
707 | return err; | |
708 | } | |
709 | ||
710 | static int fetch(int nr_heads, struct ref **to_fetch) | |
711 | { | |
2a455202 | 712 | struct discovery *d = discover_refs("git-upload-pack", 0); |
249b2004 SP |
713 | if (d->proto_git) |
714 | return fetch_git(d, nr_heads, to_fetch); | |
715 | else | |
716 | return fetch_dumb(nr_heads, to_fetch); | |
717 | } | |
718 | ||
292ce46b SP |
719 | static void parse_fetch(struct strbuf *buf) |
720 | { | |
721 | struct ref **to_fetch = NULL; | |
722 | struct ref *list_head = NULL; | |
723 | struct ref **list = &list_head; | |
724 | int alloc_heads = 0, nr_heads = 0; | |
725 | ||
726 | do { | |
727 | if (!prefixcmp(buf->buf, "fetch ")) { | |
728 | char *p = buf->buf + strlen("fetch "); | |
729 | char *name; | |
730 | struct ref *ref; | |
731 | unsigned char old_sha1[20]; | |
732 | ||
733 | if (strlen(p) < 40 || get_sha1_hex(p, old_sha1)) | |
734 | die("protocol error: expected sha/ref, got %s'", p); | |
735 | if (p[40] == ' ') | |
736 | name = p + 41; | |
737 | else if (!p[40]) | |
738 | name = ""; | |
739 | else | |
740 | die("protocol error: expected sha/ref, got %s'", p); | |
741 | ||
742 | ref = alloc_ref(name); | |
743 | hashcpy(ref->old_sha1, old_sha1); | |
744 | ||
745 | *list = ref; | |
746 | list = &ref->next; | |
747 | ||
748 | ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads); | |
749 | to_fetch[nr_heads++] = ref; | |
750 | } | |
751 | else | |
752 | die("http transport does not support %s", buf->buf); | |
753 | ||
754 | strbuf_reset(buf); | |
755 | if (strbuf_getline(buf, stdin, '\n') == EOF) | |
756 | return; | |
757 | if (!*buf->buf) | |
758 | break; | |
759 | } while (1); | |
760 | ||
249b2004 | 761 | if (fetch(nr_heads, to_fetch)) |
292ce46b SP |
762 | exit(128); /* error already reported */ |
763 | free_refs(list_head); | |
764 | free(to_fetch); | |
765 | ||
766 | printf("\n"); | |
767 | fflush(stdout); | |
768 | strbuf_reset(buf); | |
769 | } | |
770 | ||
ae4efe19 SP |
771 | static int push_dav(int nr_spec, char **specs) |
772 | { | |
773 | const char **argv = xmalloc((10 + nr_spec) * sizeof(char*)); | |
774 | int argc = 0, i; | |
775 | ||
776 | argv[argc++] = "http-push"; | |
777 | argv[argc++] = "--helper-status"; | |
778 | if (options.dry_run) | |
779 | argv[argc++] = "--dry-run"; | |
780 | if (options.verbosity > 1) | |
781 | argv[argc++] = "--verbose"; | |
782 | argv[argc++] = url; | |
783 | for (i = 0; i < nr_spec; i++) | |
784 | argv[argc++] = specs[i]; | |
785 | argv[argc++] = NULL; | |
786 | ||
787 | if (run_command_v_opt(argv, RUN_GIT_CMD)) | |
788 | die("git-%s failed", argv[0]); | |
789 | free(argv); | |
790 | return 0; | |
791 | } | |
792 | ||
de1a2fdd SP |
793 | static int push_git(struct discovery *heads, int nr_spec, char **specs) |
794 | { | |
795 | struct rpc_state rpc; | |
222b1212 JH |
796 | int i, err; |
797 | struct argv_array args; | |
798 | ||
799 | argv_array_init(&args); | |
800 | argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status", | |
801 | NULL); | |
de1a2fdd | 802 | |
de1a2fdd | 803 | if (options.thin) |
222b1212 | 804 | argv_array_push(&args, "--thin"); |
de1a2fdd | 805 | if (options.dry_run) |
222b1212 | 806 | argv_array_push(&args, "--dry-run"); |
c207e34f | 807 | if (options.verbosity == 0) |
222b1212 | 808 | argv_array_push(&args, "--quiet"); |
c207e34f | 809 | else if (options.verbosity > 1) |
222b1212 JH |
810 | argv_array_push(&args, "--verbose"); |
811 | argv_array_push(&args, options.progress ? "--progress" : "--no-progress"); | |
812 | argv_array_push(&args, url); | |
de1a2fdd | 813 | for (i = 0; i < nr_spec; i++) |
222b1212 | 814 | argv_array_push(&args, specs[i]); |
de1a2fdd SP |
815 | |
816 | memset(&rpc, 0, sizeof(rpc)); | |
817 | rpc.service_name = "git-receive-pack", | |
222b1212 | 818 | rpc.argv = args.argv; |
de1a2fdd SP |
819 | |
820 | err = rpc_service(&rpc, heads); | |
821 | if (rpc.result.len) | |
cdf4fb8e | 822 | write_or_die(1, rpc.result.buf, rpc.result.len); |
de1a2fdd | 823 | strbuf_release(&rpc.result); |
222b1212 | 824 | argv_array_clear(&args); |
de1a2fdd SP |
825 | return err; |
826 | } | |
827 | ||
828 | static int push(int nr_spec, char **specs) | |
829 | { | |
2a455202 | 830 | struct discovery *heads = discover_refs("git-receive-pack", 1); |
de1a2fdd SP |
831 | int ret; |
832 | ||
833 | if (heads->proto_git) | |
834 | ret = push_git(heads, nr_spec, specs); | |
835 | else | |
836 | ret = push_dav(nr_spec, specs); | |
837 | free_discovery(heads); | |
838 | return ret; | |
839 | } | |
840 | ||
ae4efe19 SP |
841 | static void parse_push(struct strbuf *buf) |
842 | { | |
843 | char **specs = NULL; | |
5238cbf6 | 844 | int alloc_spec = 0, nr_spec = 0, i, ret; |
ae4efe19 SP |
845 | |
846 | do { | |
847 | if (!prefixcmp(buf->buf, "push ")) { | |
848 | ALLOC_GROW(specs, nr_spec + 1, alloc_spec); | |
849 | specs[nr_spec++] = xstrdup(buf->buf + 5); | |
850 | } | |
851 | else | |
852 | die("http transport does not support %s", buf->buf); | |
853 | ||
854 | strbuf_reset(buf); | |
855 | if (strbuf_getline(buf, stdin, '\n') == EOF) | |
dc4cd767 | 856 | goto free_specs; |
ae4efe19 SP |
857 | if (!*buf->buf) |
858 | break; | |
859 | } while (1); | |
860 | ||
5238cbf6 | 861 | ret = push(nr_spec, specs); |
ae4efe19 SP |
862 | printf("\n"); |
863 | fflush(stdout); | |
dc4cd767 | 864 | |
5238cbf6 SP |
865 | if (ret) |
866 | exit(128); /* error already reported */ | |
867 | ||
dc4cd767 JM |
868 | free_specs: |
869 | for (i = 0; i < nr_spec; i++) | |
870 | free(specs[i]); | |
871 | free(specs); | |
ae4efe19 SP |
872 | } |
873 | ||
a2d725b7 DB |
874 | int main(int argc, const char **argv) |
875 | { | |
a2d725b7 | 876 | struct strbuf buf = STRBUF_INIT; |
a45d3d7e | 877 | int nongit; |
a2d725b7 | 878 | |
c6dfb399 | 879 | git_extract_argv0_path(argv[0]); |
a45d3d7e | 880 | setup_git_directory_gently(&nongit); |
a2d725b7 DB |
881 | if (argc < 2) { |
882 | fprintf(stderr, "Remote needed\n"); | |
883 | return 1; | |
884 | } | |
885 | ||
ef08ef9e SP |
886 | options.verbosity = 1; |
887 | options.progress = !!isatty(2); | |
de1a2fdd | 888 | options.thin = 1; |
ef08ef9e | 889 | |
a2d725b7 DB |
890 | remote = remote_get(argv[1]); |
891 | ||
892 | if (argc > 2) { | |
d8fab072 | 893 | end_url_with_slash(&buf, argv[2]); |
a2d725b7 | 894 | } else { |
d8fab072 | 895 | end_url_with_slash(&buf, remote->url[0]); |
a2d725b7 DB |
896 | } |
897 | ||
d8fab072 TRC |
898 | url = strbuf_detach(&buf, NULL); |
899 | ||
a4ddbc33 | 900 | http_init(remote, url, 0); |
888692b7 | 901 | |
a2d725b7 | 902 | do { |
1843f0ce SR |
903 | if (strbuf_getline(&buf, stdin, '\n') == EOF) { |
904 | if (ferror(stdin)) | |
905 | fprintf(stderr, "Error reading command stream\n"); | |
906 | else | |
907 | fprintf(stderr, "Unexpected end of command stream\n"); | |
908 | return 1; | |
909 | } | |
910 | if (buf.len == 0) | |
a2d725b7 DB |
911 | break; |
912 | if (!prefixcmp(buf.buf, "fetch ")) { | |
a45d3d7e DB |
913 | if (nongit) |
914 | die("Fetch attempted without a local repo"); | |
292ce46b SP |
915 | parse_fetch(&buf); |
916 | ||
ae4efe19 | 917 | } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) { |
97cc7bc4 SP |
918 | int for_push = !!strstr(buf.buf + 4, "for-push"); |
919 | output_refs(get_refs(for_push)); | |
ae4efe19 SP |
920 | |
921 | } else if (!prefixcmp(buf.buf, "push ")) { | |
922 | parse_push(&buf); | |
923 | ||
ef08ef9e SP |
924 | } else if (!prefixcmp(buf.buf, "option ")) { |
925 | char *name = buf.buf + strlen("option "); | |
926 | char *value = strchr(name, ' '); | |
927 | int result; | |
928 | ||
929 | if (value) | |
930 | *value++ = '\0'; | |
931 | else | |
932 | value = "true"; | |
933 | ||
934 | result = set_option(name, value); | |
935 | if (!result) | |
936 | printf("ok\n"); | |
937 | else if (result < 0) | |
938 | printf("error invalid value\n"); | |
939 | else | |
940 | printf("unsupported\n"); | |
a2d725b7 | 941 | fflush(stdout); |
ef08ef9e | 942 | |
a2d725b7 DB |
943 | } else if (!strcmp(buf.buf, "capabilities")) { |
944 | printf("fetch\n"); | |
ef08ef9e | 945 | printf("option\n"); |
ae4efe19 | 946 | printf("push\n"); |
a2d725b7 DB |
947 | printf("\n"); |
948 | fflush(stdout); | |
949 | } else { | |
1843f0ce | 950 | fprintf(stderr, "Unknown command '%s'\n", buf.buf); |
a2d725b7 DB |
951 | return 1; |
952 | } | |
953 | strbuf_reset(&buf); | |
954 | } while (1); | |
888692b7 TRC |
955 | |
956 | http_cleanup(); | |
957 | ||
a2d725b7 DB |
958 | return 0; |
959 | } |