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