]>
Commit | Line | Data |
---|---|---|
a2d725b7 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
a2d725b7 | 3 | #include "remote.h" |
ad6ac124 | 4 | #include "connect.h" |
a2d725b7 DB |
5 | #include "strbuf.h" |
6 | #include "walker.h" | |
7 | #include "http.h" | |
d01a8e32 | 8 | #include "exec_cmd.h" |
ae4efe19 | 9 | #include "run-command.h" |
97cc7bc4 | 10 | #include "pkt-line.h" |
05c1eb10 | 11 | #include "string-list.h" |
de1a2fdd | 12 | #include "sideband.h" |
222b1212 | 13 | #include "argv-array.h" |
2501aff8 | 14 | #include "credential.h" |
16094885 | 15 | #include "sha1-array.h" |
30261094 | 16 | #include "send-pack.h" |
ad6ac124 | 17 | #include "protocol.h" |
a2d725b7 | 18 | |
37a8768f | 19 | static struct remote *remote; |
b227bbc4 JK |
20 | /* always ends with a trailing slash */ |
21 | static struct strbuf url = STRBUF_INIT; | |
37a8768f | 22 | |
ef08ef9e SP |
23 | struct options { |
24 | int verbosity; | |
25 | unsigned long depth; | |
508ea882 | 26 | char *deepen_since; |
a45a2600 | 27 | struct string_list deepen_not; |
511155db | 28 | struct string_list push_options; |
ef08ef9e | 29 | unsigned progress : 1, |
9ba38048 | 30 | check_self_contained_and_connected : 1, |
16094885 NTND |
31 | cloning : 1, |
32 | update_shallow : 1, | |
ae4efe19 | 33 | followtags : 1, |
de1a2fdd | 34 | dry_run : 1, |
0ea47f9d | 35 | thin : 1, |
30261094 | 36 | /* One of the SEND_PACK_PUSH_CERT_* constants. */ |
cccf74e2 NTND |
37 | push_cert : 2, |
38 | deepen_relative : 1; | |
ef08ef9e SP |
39 | }; |
40 | static struct options options; | |
05c1eb10 | 41 | static struct string_list cas_options = STRING_LIST_INIT_DUP; |
ef08ef9e | 42 | |
ef08ef9e SP |
43 | static int set_option(const char *name, const char *value) |
44 | { | |
45 | if (!strcmp(name, "verbosity")) { | |
46 | char *end; | |
47 | int v = strtol(value, &end, 10); | |
48 | if (value == end || *end) | |
49 | return -1; | |
50 | options.verbosity = v; | |
51 | return 0; | |
52 | } | |
53 | else if (!strcmp(name, "progress")) { | |
54 | if (!strcmp(value, "true")) | |
55 | options.progress = 1; | |
56 | else if (!strcmp(value, "false")) | |
57 | options.progress = 0; | |
58 | else | |
59 | return -1; | |
249b2004 | 60 | return 0; |
ef08ef9e SP |
61 | } |
62 | else if (!strcmp(name, "depth")) { | |
63 | char *end; | |
64 | unsigned long v = strtoul(value, &end, 10); | |
65 | if (value == end || *end) | |
66 | return -1; | |
67 | options.depth = v; | |
249b2004 | 68 | return 0; |
ef08ef9e | 69 | } |
508ea882 NTND |
70 | else if (!strcmp(name, "deepen-since")) { |
71 | options.deepen_since = xstrdup(value); | |
72 | return 0; | |
73 | } | |
a45a2600 NTND |
74 | else if (!strcmp(name, "deepen-not")) { |
75 | string_list_append(&options.deepen_not, value); | |
76 | return 0; | |
77 | } | |
cccf74e2 NTND |
78 | else if (!strcmp(name, "deepen-relative")) { |
79 | if (!strcmp(value, "true")) | |
80 | options.deepen_relative = 1; | |
81 | else if (!strcmp(value, "false")) | |
82 | options.deepen_relative = 0; | |
83 | else | |
84 | return -1; | |
85 | return 0; | |
86 | } | |
ef08ef9e SP |
87 | else if (!strcmp(name, "followtags")) { |
88 | if (!strcmp(value, "true")) | |
89 | options.followtags = 1; | |
90 | else if (!strcmp(value, "false")) | |
91 | options.followtags = 0; | |
92 | else | |
93 | return -1; | |
249b2004 | 94 | return 0; |
ef08ef9e | 95 | } |
ae4efe19 SP |
96 | else if (!strcmp(name, "dry-run")) { |
97 | if (!strcmp(value, "true")) | |
98 | options.dry_run = 1; | |
99 | else if (!strcmp(value, "false")) | |
100 | options.dry_run = 0; | |
101 | else | |
102 | return -1; | |
103 | return 0; | |
104 | } | |
9ba38048 NTND |
105 | else if (!strcmp(name, "check-connectivity")) { |
106 | if (!strcmp(value, "true")) | |
107 | options.check_self_contained_and_connected = 1; | |
108 | else if (!strcmp(value, "false")) | |
109 | options.check_self_contained_and_connected = 0; | |
110 | else | |
111 | return -1; | |
112 | return 0; | |
113 | } | |
05c1eb10 JH |
114 | else if (!strcmp(name, "cas")) { |
115 | struct strbuf val = STRBUF_INIT; | |
116 | strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value); | |
117 | string_list_append(&cas_options, val.buf); | |
118 | strbuf_release(&val); | |
119 | return 0; | |
16094885 NTND |
120 | } else if (!strcmp(name, "cloning")) { |
121 | if (!strcmp(value, "true")) | |
122 | options.cloning = 1; | |
123 | else if (!strcmp(value, "false")) | |
124 | options.cloning = 0; | |
125 | else | |
126 | return -1; | |
127 | return 0; | |
128 | } else if (!strcmp(name, "update-shallow")) { | |
129 | if (!strcmp(value, "true")) | |
130 | options.update_shallow = 1; | |
131 | else if (!strcmp(value, "false")) | |
132 | options.update_shallow = 0; | |
133 | else | |
134 | return -1; | |
135 | return 0; | |
0ea47f9d JH |
136 | } else if (!strcmp(name, "pushcert")) { |
137 | if (!strcmp(value, "true")) | |
30261094 | 138 | options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; |
0ea47f9d | 139 | else if (!strcmp(value, "false")) |
30261094 DB |
140 | options.push_cert = SEND_PACK_PUSH_CERT_NEVER; |
141 | else if (!strcmp(value, "if-asked")) | |
142 | options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED; | |
0ea47f9d JH |
143 | else |
144 | return -1; | |
145 | return 0; | |
511155db BW |
146 | } else if (!strcmp(name, "push-option")) { |
147 | string_list_append(&options.push_options, value); | |
148 | return 0; | |
c915f11e EW |
149 | |
150 | #if LIBCURL_VERSION_NUM >= 0x070a08 | |
151 | } else if (!strcmp(name, "family")) { | |
152 | if (!strcmp(value, "ipv4")) | |
153 | git_curl_ipresolve = CURL_IPRESOLVE_V4; | |
154 | else if (!strcmp(value, "ipv6")) | |
155 | git_curl_ipresolve = CURL_IPRESOLVE_V6; | |
156 | else if (!strcmp(value, "all")) | |
157 | git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER; | |
158 | else | |
159 | return -1; | |
160 | return 0; | |
161 | #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */ | |
16094885 | 162 | } else { |
ef08ef9e SP |
163 | return 1 /* unsupported */; |
164 | } | |
165 | } | |
166 | ||
97cc7bc4 SP |
167 | struct discovery { |
168 | const char *service; | |
169 | char *buf_alloc; | |
170 | char *buf; | |
171 | size_t len; | |
2a455202 | 172 | struct ref *refs; |
910650d2 | 173 | struct oid_array shallow; |
97cc7bc4 SP |
174 | unsigned proto_git : 1; |
175 | }; | |
176 | static struct discovery *last_discovery; | |
177 | ||
b8054bbe JK |
178 | static struct ref *parse_git_refs(struct discovery *heads, int for_push) |
179 | { | |
180 | struct ref *list = NULL; | |
ad6ac124 BW |
181 | struct packet_reader reader; |
182 | ||
183 | packet_reader_init(&reader, -1, heads->buf, heads->len, | |
184 | PACKET_READ_CHOMP_NEWLINE | | |
185 | PACKET_READ_GENTLE_ON_EOF); | |
186 | ||
187 | switch (discover_version(&reader)) { | |
8f6982b4 BW |
188 | case protocol_v2: |
189 | die("support for protocol v2 not implemented yet"); | |
190 | break; | |
ad6ac124 BW |
191 | case protocol_v1: |
192 | case protocol_v0: | |
193 | get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0, | |
194 | NULL, &heads->shallow); | |
195 | break; | |
196 | case protocol_unknown_version: | |
197 | BUG("unknown protocol version"); | |
198 | } | |
199 | ||
b8054bbe JK |
200 | return list; |
201 | } | |
202 | ||
203 | static struct ref *parse_info_refs(struct discovery *heads) | |
204 | { | |
205 | char *data, *start, *mid; | |
206 | char *ref_name; | |
207 | int i = 0; | |
208 | ||
209 | struct ref *refs = NULL; | |
210 | struct ref *ref = NULL; | |
211 | struct ref *last_ref = NULL; | |
212 | ||
213 | data = heads->buf; | |
214 | start = NULL; | |
215 | mid = data; | |
216 | while (i < heads->len) { | |
217 | if (!start) { | |
218 | start = &data[i]; | |
219 | } | |
220 | if (data[i] == '\t') | |
221 | mid = &data[i]; | |
222 | if (data[i] == '\n') { | |
223 | if (mid - start != 40) | |
b227bbc4 JK |
224 | die("%sinfo/refs not valid: is this a git repository?", |
225 | url.buf); | |
b8054bbe JK |
226 | data[i] = 0; |
227 | ref_name = mid + 1; | |
6f687c21 | 228 | ref = alloc_ref(ref_name); |
f4e54d02 | 229 | get_oid_hex(start, &ref->old_oid); |
b8054bbe JK |
230 | if (!refs) |
231 | refs = ref; | |
232 | if (last_ref) | |
233 | last_ref->next = ref; | |
234 | last_ref = ref; | |
235 | start = NULL; | |
236 | } | |
237 | i++; | |
238 | } | |
239 | ||
240 | ref = alloc_ref("HEAD"); | |
b227bbc4 | 241 | if (!http_fetch_ref(url.buf, ref) && |
b8054bbe JK |
242 | !resolve_remote_symref(ref, refs)) { |
243 | ref->next = refs; | |
244 | refs = ref; | |
245 | } else { | |
246 | free(ref); | |
247 | } | |
248 | ||
249 | return refs; | |
250 | } | |
251 | ||
97cc7bc4 SP |
252 | static void free_discovery(struct discovery *d) |
253 | { | |
254 | if (d) { | |
255 | if (d == last_discovery) | |
256 | last_discovery = NULL; | |
ee3051bd | 257 | free(d->shallow.oid); |
97cc7bc4 | 258 | free(d->buf_alloc); |
2a455202 | 259 | free_refs(d->refs); |
97cc7bc4 SP |
260 | free(d); |
261 | } | |
262 | } | |
263 | ||
fc1b774c JK |
264 | static int show_http_message(struct strbuf *type, struct strbuf *charset, |
265 | struct strbuf *msg) | |
426e70d4 JK |
266 | { |
267 | const char *p, *eol; | |
268 | ||
269 | /* | |
270 | * We only show text/plain parts, as other types are likely | |
271 | * to be ugly to look at on the user's terminal. | |
426e70d4 | 272 | */ |
bf197fd7 | 273 | if (strcmp(type->buf, "text/plain")) |
426e70d4 | 274 | return -1; |
fc1b774c JK |
275 | if (charset->len) |
276 | strbuf_reencode(msg, charset->buf, get_log_output_encoding()); | |
426e70d4 JK |
277 | |
278 | strbuf_trim(msg); | |
279 | if (!msg->len) | |
280 | return -1; | |
281 | ||
282 | p = msg->buf; | |
283 | do { | |
284 | eol = strchrnul(p, '\n'); | |
285 | fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p); | |
286 | p = eol + 1; | |
287 | } while(*eol); | |
288 | return 0; | |
289 | } | |
290 | ||
24d36f14 | 291 | static struct discovery *discover_refs(const char *service, int for_push) |
a2d725b7 | 292 | { |
4656bf47 SP |
293 | struct strbuf exp = STRBUF_INIT; |
294 | struct strbuf type = STRBUF_INIT; | |
fc1b774c | 295 | struct strbuf charset = STRBUF_INIT; |
a2d725b7 | 296 | struct strbuf buffer = STRBUF_INIT; |
c65d5692 | 297 | struct strbuf refs_url = STRBUF_INIT; |
050ef365 | 298 | struct strbuf effective_url = STRBUF_INIT; |
97cc7bc4 | 299 | struct discovery *last = last_discovery; |
243c329c | 300 | int http_ret, maybe_smart = 0; |
fcaa6e64 | 301 | struct http_get_options http_options; |
a2d725b7 | 302 | |
97cc7bc4 SP |
303 | if (last && !strcmp(service, last->service)) |
304 | return last; | |
305 | free_discovery(last); | |
a2d725b7 | 306 | |
b227bbc4 | 307 | strbuf_addf(&refs_url, "%sinfo/refs", url.buf); |
59556548 | 308 | if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) && |
02572c2e | 309 | git_env_bool("GIT_SMART_HTTP", 1)) { |
243c329c | 310 | maybe_smart = 1; |
b227bbc4 | 311 | if (!strchr(url.buf, '?')) |
c65d5692 | 312 | strbuf_addch(&refs_url, '?'); |
97cc7bc4 | 313 | else |
c65d5692 JK |
314 | strbuf_addch(&refs_url, '&'); |
315 | strbuf_addf(&refs_url, "service=%s", service); | |
97cc7bc4 | 316 | } |
a2d725b7 | 317 | |
fcaa6e64 JK |
318 | memset(&http_options, 0, sizeof(http_options)); |
319 | http_options.content_type = &type; | |
320 | http_options.charset = &charset; | |
321 | http_options.effective_url = &effective_url; | |
322 | http_options.base_url = &url; | |
50d34137 | 323 | http_options.initial_request = 1; |
fcaa6e64 JK |
324 | http_options.no_cache = 1; |
325 | http_options.keep_error = 1; | |
1bbcc224 | 326 | |
fcaa6e64 | 327 | http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options); |
a2d725b7 DB |
328 | switch (http_ret) { |
329 | case HTTP_OK: | |
330 | break; | |
331 | case HTTP_MISSING_TARGET: | |
fc1b774c | 332 | show_http_message(&type, &charset, &buffer); |
b227bbc4 | 333 | die("repository '%s' not found", url.buf); |
42653c09 | 334 | case HTTP_NOAUTH: |
fc1b774c | 335 | show_http_message(&type, &charset, &buffer); |
b227bbc4 | 336 | die("Authentication failed for '%s'", url.buf); |
a2d725b7 | 337 | default: |
fc1b774c | 338 | show_http_message(&type, &charset, &buffer); |
b227bbc4 | 339 | die("unable to access '%s': %s", url.buf, curl_errorstr); |
a2d725b7 DB |
340 | } |
341 | ||
50d34137 JK |
342 | if (options.verbosity && !starts_with(refs_url.buf, url.buf)) |
343 | warning(_("redirecting to %s"), url.buf); | |
344 | ||
97cc7bc4 SP |
345 | last= xcalloc(1, sizeof(*last_discovery)); |
346 | last->service = service; | |
347 | last->buf_alloc = strbuf_detach(&buffer, &last->len); | |
348 | last->buf = last->buf_alloc; | |
349 | ||
4656bf47 SP |
350 | strbuf_addf(&exp, "application/x-%s-advertisement", service); |
351 | if (maybe_smart && | |
352 | (5 <= last->len && last->buf[4] == '#') && | |
353 | !strbuf_cmp(&exp, &type)) { | |
4981fe75 JK |
354 | char *line; |
355 | ||
4656bf47 SP |
356 | /* |
357 | * smart HTTP response; validate that the service | |
97cc7bc4 SP |
358 | * pkt-line matches our request. |
359 | */ | |
4981fe75 | 360 | line = packet_read_line_buf(&last->buf, &last->len, NULL); |
97cc7bc4 | 361 | |
4656bf47 | 362 | strbuf_reset(&exp); |
97cc7bc4 | 363 | strbuf_addf(&exp, "# service=%s", service); |
4981fe75 JK |
364 | if (strcmp(line, exp.buf)) |
365 | die("invalid server response; got '%s'", line); | |
97cc7bc4 SP |
366 | strbuf_release(&exp); |
367 | ||
368 | /* The header can include additional metadata lines, up | |
369 | * until a packet flush marker. Ignore these now, but | |
370 | * in the future we might start to scan them. | |
371 | */ | |
4981fe75 JK |
372 | while (packet_read_line_buf(&last->buf, &last->len, NULL)) |
373 | ; | |
97cc7bc4 SP |
374 | |
375 | last->proto_git = 1; | |
376 | } | |
377 | ||
2a455202 JK |
378 | if (last->proto_git) |
379 | last->refs = parse_git_refs(last, for_push); | |
380 | else | |
381 | last->refs = parse_info_refs(last); | |
382 | ||
c65d5692 | 383 | strbuf_release(&refs_url); |
4656bf47 SP |
384 | strbuf_release(&exp); |
385 | strbuf_release(&type); | |
fc1b774c | 386 | strbuf_release(&charset); |
050ef365 | 387 | strbuf_release(&effective_url); |
97cc7bc4 SP |
388 | strbuf_release(&buffer); |
389 | last_discovery = last; | |
390 | return last; | |
391 | } | |
392 | ||
97cc7bc4 SP |
393 | static struct ref *get_refs(int for_push) |
394 | { | |
395 | struct discovery *heads; | |
396 | ||
397 | if (for_push) | |
2a455202 | 398 | heads = discover_refs("git-receive-pack", for_push); |
97cc7bc4 | 399 | else |
2a455202 | 400 | heads = discover_refs("git-upload-pack", for_push); |
97cc7bc4 | 401 | |
2a455202 | 402 | return heads->refs; |
97cc7bc4 SP |
403 | } |
404 | ||
ae4efe19 SP |
405 | static void output_refs(struct ref *refs) |
406 | { | |
407 | struct ref *posn; | |
408 | for (posn = refs; posn; posn = posn->next) { | |
409 | if (posn->symref) | |
410 | printf("@%s %s\n", posn->symref, posn->name); | |
411 | else | |
f4e54d02 | 412 | printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name); |
ae4efe19 SP |
413 | } |
414 | printf("\n"); | |
415 | fflush(stdout); | |
ae4efe19 SP |
416 | } |
417 | ||
de1a2fdd SP |
418 | struct rpc_state { |
419 | const char *service_name; | |
420 | const char **argv; | |
8150749d | 421 | struct strbuf *stdin_preamble; |
de1a2fdd SP |
422 | char *service_url; |
423 | char *hdr_content_type; | |
424 | char *hdr_accept; | |
425 | char *buf; | |
426 | size_t alloc; | |
427 | size_t len; | |
428 | size_t pos; | |
429 | int in; | |
430 | int out; | |
296b847c | 431 | int any_written; |
de1a2fdd | 432 | struct strbuf result; |
b8538603 | 433 | unsigned gzip_request : 1; |
6c81a990 | 434 | unsigned initial_buffer : 1; |
de1a2fdd SP |
435 | }; |
436 | ||
437 | static size_t rpc_out(void *ptr, size_t eltsize, | |
438 | size_t nmemb, void *buffer_) | |
439 | { | |
440 | size_t max = eltsize * nmemb; | |
441 | struct rpc_state *rpc = buffer_; | |
442 | size_t avail = rpc->len - rpc->pos; | |
443 | ||
444 | if (!avail) { | |
6c81a990 | 445 | rpc->initial_buffer = 0; |
4981fe75 | 446 | avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); |
de1a2fdd SP |
447 | if (!avail) |
448 | return 0; | |
449 | rpc->pos = 0; | |
450 | rpc->len = avail; | |
451 | } | |
452 | ||
48310608 | 453 | if (max < avail) |
de1a2fdd SP |
454 | avail = max; |
455 | memcpy(ptr, rpc->buf + rpc->pos, avail); | |
456 | rpc->pos += avail; | |
457 | return avail; | |
458 | } | |
459 | ||
6c81a990 | 460 | #ifndef NO_CURL_IOCTL |
5092d3ec | 461 | static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp) |
6c81a990 MS |
462 | { |
463 | struct rpc_state *rpc = clientp; | |
464 | ||
465 | switch (cmd) { | |
466 | case CURLIOCMD_NOP: | |
467 | return CURLIOE_OK; | |
468 | ||
469 | case CURLIOCMD_RESTARTREAD: | |
470 | if (rpc->initial_buffer) { | |
471 | rpc->pos = 0; | |
472 | return CURLIOE_OK; | |
473 | } | |
b725b270 | 474 | error("unable to rewind rpc post data - try increasing http.postBuffer"); |
6c81a990 MS |
475 | return CURLIOE_FAILRESTART; |
476 | ||
477 | default: | |
478 | return CURLIOE_UNKNOWNCMD; | |
479 | } | |
480 | } | |
481 | #endif | |
482 | ||
a04ff3ec | 483 | static size_t rpc_in(char *ptr, size_t eltsize, |
de1a2fdd SP |
484 | size_t nmemb, void *buffer_) |
485 | { | |
486 | size_t size = eltsize * nmemb; | |
487 | struct rpc_state *rpc = buffer_; | |
296b847c DT |
488 | if (size) |
489 | rpc->any_written = 1; | |
de1a2fdd SP |
490 | write_or_die(rpc->in, ptr, size); |
491 | return size; | |
492 | } | |
493 | ||
3a347ed7 JK |
494 | static int run_slot(struct active_request_slot *slot, |
495 | struct slot_results *results) | |
206b099d | 496 | { |
b81401c1 | 497 | int err; |
3a347ed7 | 498 | struct slot_results results_buf; |
206b099d | 499 | |
3a347ed7 JK |
500 | if (!results) |
501 | results = &results_buf; | |
502 | ||
beed336c | 503 | err = run_one_slot(slot, results); |
206b099d | 504 | |
b81401c1 | 505 | if (err != HTTP_OK && err != HTTP_REAUTH) { |
00540458 SP |
506 | struct strbuf msg = STRBUF_INIT; |
507 | if (results->http_code && results->http_code != 200) | |
508 | strbuf_addf(&msg, "HTTP %ld", results->http_code); | |
509 | if (results->curl_result != CURLE_OK) { | |
510 | if (msg.len) | |
511 | strbuf_addch(&msg, ' '); | |
512 | strbuf_addf(&msg, "curl %d", results->curl_result); | |
513 | if (curl_errorstr[0]) { | |
514 | strbuf_addch(&msg, ' '); | |
515 | strbuf_addstr(&msg, curl_errorstr); | |
516 | } | |
517 | } | |
518 | error("RPC failed; %s", msg.buf); | |
519 | strbuf_release(&msg); | |
206b099d SP |
520 | } |
521 | ||
522 | return err; | |
523 | } | |
524 | ||
3a347ed7 | 525 | static int probe_rpc(struct rpc_state *rpc, struct slot_results *results) |
206b099d SP |
526 | { |
527 | struct active_request_slot *slot; | |
8cb01e2f | 528 | struct curl_slist *headers = http_copy_default_headers(); |
206b099d SP |
529 | struct strbuf buf = STRBUF_INIT; |
530 | int err; | |
531 | ||
532 | slot = get_active_slot(); | |
533 | ||
534 | headers = curl_slist_append(headers, rpc->hdr_content_type); | |
535 | headers = curl_slist_append(headers, rpc->hdr_accept); | |
536 | ||
537 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); | |
538 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); | |
539 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); | |
aa90b969 | 540 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL); |
206b099d SP |
541 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000"); |
542 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4); | |
543 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); | |
544 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); | |
545 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf); | |
546 | ||
3a347ed7 | 547 | err = run_slot(slot, results); |
206b099d SP |
548 | |
549 | curl_slist_free_all(headers); | |
550 | strbuf_release(&buf); | |
551 | return err; | |
552 | } | |
553 | ||
37ee680d DT |
554 | static curl_off_t xcurl_off_t(ssize_t len) { |
555 | if (len > maximum_signed_value_of_type(curl_off_t)) | |
556 | die("cannot handle pushes this big"); | |
557 | return (curl_off_t) len; | |
558 | } | |
559 | ||
de1a2fdd SP |
560 | static int post_rpc(struct rpc_state *rpc) |
561 | { | |
562 | struct active_request_slot *slot; | |
8cb01e2f | 563 | struct curl_slist *headers = http_copy_default_headers(); |
b8538603 SP |
564 | int use_gzip = rpc->gzip_request; |
565 | char *gzip_body = NULL; | |
37711549 | 566 | size_t gzip_size = 0; |
206b099d | 567 | int err, large_request = 0; |
c80d96ca | 568 | int needs_100_continue = 0; |
de1a2fdd SP |
569 | |
570 | /* Try to load the entire request, if we can fit it into the | |
571 | * allocated buffer space we can use HTTP/1.0 and avoid the | |
572 | * chunked encoding mess. | |
573 | */ | |
574 | while (1) { | |
575 | size_t left = rpc->alloc - rpc->len; | |
576 | char *buf = rpc->buf + rpc->len; | |
577 | int n; | |
578 | ||
579 | if (left < LARGE_PACKET_MAX) { | |
580 | large_request = 1; | |
b8538603 | 581 | use_gzip = 0; |
de1a2fdd SP |
582 | break; |
583 | } | |
584 | ||
4981fe75 | 585 | n = packet_read(rpc->out, NULL, NULL, buf, left, 0); |
de1a2fdd SP |
586 | if (!n) |
587 | break; | |
588 | rpc->len += n; | |
589 | } | |
590 | ||
206b099d | 591 | if (large_request) { |
c80d96ca | 592 | struct slot_results results; |
593 | ||
b81401c1 | 594 | do { |
c80d96ca | 595 | err = probe_rpc(rpc, &results); |
2501aff8 JK |
596 | if (err == HTTP_REAUTH) |
597 | credential_fill(&http_auth); | |
b81401c1 JK |
598 | } while (err == HTTP_REAUTH); |
599 | if (err != HTTP_OK) | |
600 | return -1; | |
c80d96ca | 601 | |
602 | if (results.auth_avail & CURLAUTH_GSSNEGOTIATE) | |
603 | needs_100_continue = 1; | |
206b099d SP |
604 | } |
605 | ||
abf8df86 JK |
606 | headers = curl_slist_append(headers, rpc->hdr_content_type); |
607 | headers = curl_slist_append(headers, rpc->hdr_accept); | |
c80d96ca | 608 | headers = curl_slist_append(headers, needs_100_continue ? |
609 | "Expect: 100-continue" : "Expect:"); | |
abf8df86 JK |
610 | |
611 | retry: | |
de1a2fdd | 612 | slot = get_active_slot(); |
de1a2fdd | 613 | |
de1a2fdd | 614 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
d21f9794 | 615 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); |
de1a2fdd | 616 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); |
aa90b969 | 617 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip"); |
de1a2fdd | 618 | |
de1a2fdd SP |
619 | if (large_request) { |
620 | /* The request body is large and the size cannot be predicted. | |
621 | * We must use chunked encoding to send it. | |
622 | */ | |
de1a2fdd | 623 | headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); |
6c81a990 | 624 | rpc->initial_buffer = 1; |
de1a2fdd SP |
625 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out); |
626 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc); | |
6c81a990 MS |
627 | #ifndef NO_CURL_IOCTL |
628 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl); | |
629 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc); | |
630 | #endif | |
de1a2fdd SP |
631 | if (options.verbosity > 1) { |
632 | fprintf(stderr, "POST %s (chunked)\n", rpc->service_name); | |
633 | fflush(stderr); | |
634 | } | |
635 | ||
2e736fd5 JK |
636 | } else if (gzip_body) { |
637 | /* | |
638 | * If we are looping to retry authentication, then the previous | |
639 | * run will have set up the headers and gzip buffer already, | |
640 | * and we just need to send it. | |
641 | */ | |
642 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); | |
37ee680d | 643 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size)); |
2e736fd5 | 644 | |
b8538603 SP |
645 | } else if (use_gzip && 1024 < rpc->len) { |
646 | /* The client backend isn't giving us compressed data so | |
647 | * we can try to deflate it ourselves, this may save on. | |
648 | * the transfer time. | |
649 | */ | |
ef49a7a0 | 650 | git_zstream stream; |
b8538603 SP |
651 | int ret; |
652 | ||
55bb5c91 | 653 | git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION); |
df126e10 JK |
654 | gzip_size = git_deflate_bound(&stream, rpc->len); |
655 | gzip_body = xmalloc(gzip_size); | |
b8538603 SP |
656 | |
657 | stream.next_in = (unsigned char *)rpc->buf; | |
658 | stream.avail_in = rpc->len; | |
659 | stream.next_out = (unsigned char *)gzip_body; | |
df126e10 | 660 | stream.avail_out = gzip_size; |
b8538603 | 661 | |
55bb5c91 | 662 | ret = git_deflate(&stream, Z_FINISH); |
b8538603 SP |
663 | if (ret != Z_STREAM_END) |
664 | die("cannot deflate request; zlib deflate error %d", ret); | |
665 | ||
55bb5c91 | 666 | ret = git_deflate_end_gently(&stream); |
b8538603 SP |
667 | if (ret != Z_OK) |
668 | die("cannot deflate request; zlib end error %d", ret); | |
669 | ||
df126e10 | 670 | gzip_size = stream.total_out; |
b8538603 SP |
671 | |
672 | headers = curl_slist_append(headers, "Content-Encoding: gzip"); | |
673 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); | |
37ee680d | 674 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size)); |
b8538603 SP |
675 | |
676 | if (options.verbosity > 1) { | |
677 | fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", | |
678 | rpc->service_name, | |
df126e10 | 679 | (unsigned long)rpc->len, (unsigned long)gzip_size); |
b8538603 SP |
680 | fflush(stderr); |
681 | } | |
de1a2fdd SP |
682 | } else { |
683 | /* We know the complete request size in advance, use the | |
684 | * more normal Content-Length approach. | |
685 | */ | |
686 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf); | |
37ee680d | 687 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len)); |
de1a2fdd SP |
688 | if (options.verbosity > 1) { |
689 | fprintf(stderr, "POST %s (%lu bytes)\n", | |
690 | rpc->service_name, (unsigned long)rpc->len); | |
691 | fflush(stderr); | |
692 | } | |
693 | } | |
694 | ||
695 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); | |
696 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in); | |
697 | curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc); | |
698 | ||
296b847c DT |
699 | |
700 | rpc->any_written = 0; | |
3a347ed7 | 701 | err = run_slot(slot, NULL); |
2501aff8 JK |
702 | if (err == HTTP_REAUTH && !large_request) { |
703 | credential_fill(&http_auth); | |
abf8df86 | 704 | goto retry; |
2501aff8 | 705 | } |
b81401c1 JK |
706 | if (err != HTTP_OK) |
707 | err = -1; | |
de1a2fdd | 708 | |
296b847c DT |
709 | if (!rpc->any_written) |
710 | err = -1; | |
711 | ||
de1a2fdd | 712 | curl_slist_free_all(headers); |
b8538603 | 713 | free(gzip_body); |
de1a2fdd SP |
714 | return err; |
715 | } | |
716 | ||
717 | static int rpc_service(struct rpc_state *rpc, struct discovery *heads) | |
718 | { | |
719 | const char *svc = rpc->service_name; | |
720 | struct strbuf buf = STRBUF_INIT; | |
8150749d | 721 | struct strbuf *preamble = rpc->stdin_preamble; |
d3180279 | 722 | struct child_process client = CHILD_PROCESS_INIT; |
de1a2fdd SP |
723 | int err = 0; |
724 | ||
de1a2fdd SP |
725 | client.in = -1; |
726 | client.out = -1; | |
727 | client.git_cmd = 1; | |
728 | client.argv = rpc->argv; | |
729 | if (start_command(&client)) | |
730 | exit(1); | |
8150749d IT |
731 | if (preamble) |
732 | write_or_die(client.in, preamble->buf, preamble->len); | |
de1a2fdd SP |
733 | if (heads) |
734 | write_or_die(client.in, heads->buf, heads->len); | |
735 | ||
736 | rpc->alloc = http_post_buffer; | |
737 | rpc->buf = xmalloc(rpc->alloc); | |
738 | rpc->in = client.in; | |
739 | rpc->out = client.out; | |
740 | strbuf_init(&rpc->result, 0); | |
741 | ||
b227bbc4 | 742 | strbuf_addf(&buf, "%s%s", url.buf, svc); |
de1a2fdd SP |
743 | rpc->service_url = strbuf_detach(&buf, NULL); |
744 | ||
745 | strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); | |
746 | rpc->hdr_content_type = strbuf_detach(&buf, NULL); | |
747 | ||
8efa5f62 | 748 | strbuf_addf(&buf, "Accept: application/x-%s-result", svc); |
de1a2fdd SP |
749 | rpc->hdr_accept = strbuf_detach(&buf, NULL); |
750 | ||
751 | while (!err) { | |
4981fe75 | 752 | int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); |
de1a2fdd SP |
753 | if (!n) |
754 | break; | |
755 | rpc->pos = 0; | |
756 | rpc->len = n; | |
757 | err |= post_rpc(rpc); | |
758 | } | |
de1a2fdd SP |
759 | |
760 | close(client.in); | |
de1a2fdd | 761 | client.in = -1; |
6cdf0223 SP |
762 | if (!err) { |
763 | strbuf_read(&rpc->result, client.out, 0); | |
764 | } else { | |
765 | char buf[4096]; | |
766 | for (;;) | |
767 | if (xread(client.out, buf, sizeof(buf)) <= 0) | |
768 | break; | |
769 | } | |
b4ee10f6 SP |
770 | |
771 | close(client.out); | |
de1a2fdd SP |
772 | client.out = -1; |
773 | ||
774 | err |= finish_command(&client); | |
775 | free(rpc->service_url); | |
776 | free(rpc->hdr_content_type); | |
777 | free(rpc->hdr_accept); | |
778 | free(rpc->buf); | |
779 | strbuf_release(&buf); | |
780 | return err; | |
781 | } | |
782 | ||
292ce46b SP |
783 | static int fetch_dumb(int nr_heads, struct ref **to_fetch) |
784 | { | |
26e1e0b2 | 785 | struct walker *walker; |
b32fa95f | 786 | char **targets; |
292ce46b SP |
787 | int ret, i; |
788 | ||
b32fa95f | 789 | ALLOC_ARRAY(targets, nr_heads); |
508ea882 NTND |
790 | if (options.depth || options.deepen_since) |
791 | die("dumb http transport does not support shallow capabilities"); | |
292ce46b | 792 | for (i = 0; i < nr_heads; i++) |
f4e54d02 | 793 | targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid)); |
292ce46b | 794 | |
b227bbc4 | 795 | walker = get_http_walker(url.buf); |
292ce46b SP |
796 | walker->get_all = 1; |
797 | walker->get_tree = 1; | |
798 | walker->get_history = 1; | |
ef08ef9e | 799 | walker->get_verbosely = options.verbosity >= 3; |
292ce46b SP |
800 | walker->get_recover = 0; |
801 | ret = walker_fetch(walker, nr_heads, targets, NULL, NULL); | |
26e1e0b2 | 802 | walker_free(walker); |
292ce46b SP |
803 | |
804 | for (i = 0; i < nr_heads; i++) | |
805 | free(targets[i]); | |
806 | free(targets); | |
807 | ||
b725b270 | 808 | return ret ? error("fetch failed.") : 0; |
292ce46b SP |
809 | } |
810 | ||
249b2004 SP |
811 | static int fetch_git(struct discovery *heads, |
812 | int nr_heads, struct ref **to_fetch) | |
813 | { | |
814 | struct rpc_state rpc; | |
8150749d | 815 | struct strbuf preamble = STRBUF_INIT; |
b5f62ebe NTND |
816 | int i, err; |
817 | struct argv_array args = ARGV_ARRAY_INIT; | |
818 | ||
819 | argv_array_pushl(&args, "fetch-pack", "--stateless-rpc", | |
820 | "--stdin", "--lock-pack", NULL); | |
249b2004 | 821 | if (options.followtags) |
b5f62ebe | 822 | argv_array_push(&args, "--include-tag"); |
249b2004 | 823 | if (options.thin) |
b5f62ebe NTND |
824 | argv_array_push(&args, "--thin"); |
825 | if (options.verbosity >= 3) | |
826 | argv_array_pushl(&args, "-v", "-v", NULL); | |
9ba38048 | 827 | if (options.check_self_contained_and_connected) |
b5f62ebe | 828 | argv_array_push(&args, "--check-self-contained-and-connected"); |
16094885 | 829 | if (options.cloning) |
b5f62ebe | 830 | argv_array_push(&args, "--cloning"); |
16094885 | 831 | if (options.update_shallow) |
b5f62ebe | 832 | argv_array_push(&args, "--update-shallow"); |
249b2004 | 833 | if (!options.progress) |
b5f62ebe NTND |
834 | argv_array_push(&args, "--no-progress"); |
835 | if (options.depth) | |
836 | argv_array_pushf(&args, "--depth=%lu", options.depth); | |
508ea882 NTND |
837 | if (options.deepen_since) |
838 | argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since); | |
a45a2600 NTND |
839 | for (i = 0; i < options.deepen_not.nr; i++) |
840 | argv_array_pushf(&args, "--shallow-exclude=%s", | |
841 | options.deepen_not.items[i].string); | |
cccf74e2 NTND |
842 | if (options.deepen_relative && options.depth) |
843 | argv_array_push(&args, "--deepen-relative"); | |
b5f62ebe | 844 | argv_array_push(&args, url.buf); |
8150749d | 845 | |
249b2004 SP |
846 | for (i = 0; i < nr_heads; i++) { |
847 | struct ref *ref = to_fetch[i]; | |
94ee8e2c | 848 | if (!*ref->name) |
249b2004 | 849 | die("cannot fetch by sha1 over smart http"); |
58f2ed05 | 850 | packet_buf_write(&preamble, "%s %s\n", |
f4e54d02 | 851 | oid_to_hex(&ref->old_oid), ref->name); |
249b2004 | 852 | } |
8150749d | 853 | packet_buf_flush(&preamble); |
249b2004 SP |
854 | |
855 | memset(&rpc, 0, sizeof(rpc)); | |
856 | rpc.service_name = "git-upload-pack", | |
b5f62ebe | 857 | rpc.argv = args.argv; |
8150749d | 858 | rpc.stdin_preamble = &preamble; |
b8538603 | 859 | rpc.gzip_request = 1; |
249b2004 SP |
860 | |
861 | err = rpc_service(&rpc, heads); | |
862 | if (rpc.result.len) | |
cdf4fb8e | 863 | write_or_die(1, rpc.result.buf, rpc.result.len); |
249b2004 | 864 | strbuf_release(&rpc.result); |
8150749d | 865 | strbuf_release(&preamble); |
b5f62ebe | 866 | argv_array_clear(&args); |
249b2004 SP |
867 | return err; |
868 | } | |
869 | ||
870 | static int fetch(int nr_heads, struct ref **to_fetch) | |
871 | { | |
2a455202 | 872 | struct discovery *d = discover_refs("git-upload-pack", 0); |
249b2004 SP |
873 | if (d->proto_git) |
874 | return fetch_git(d, nr_heads, to_fetch); | |
875 | else | |
876 | return fetch_dumb(nr_heads, to_fetch); | |
877 | } | |
878 | ||
292ce46b SP |
879 | static void parse_fetch(struct strbuf *buf) |
880 | { | |
881 | struct ref **to_fetch = NULL; | |
882 | struct ref *list_head = NULL; | |
883 | struct ref **list = &list_head; | |
884 | int alloc_heads = 0, nr_heads = 0; | |
885 | ||
886 | do { | |
95b567c7 JK |
887 | const char *p; |
888 | if (skip_prefix(buf->buf, "fetch ", &p)) { | |
889 | const char *name; | |
292ce46b | 890 | struct ref *ref; |
8338c911 | 891 | struct object_id old_oid; |
292ce46b | 892 | |
8338c911 | 893 | if (get_oid_hex(p, &old_oid)) |
292ce46b | 894 | die("protocol error: expected sha/ref, got %s'", p); |
8338c911 | 895 | if (p[GIT_SHA1_HEXSZ] == ' ') |
896 | name = p + GIT_SHA1_HEXSZ + 1; | |
897 | else if (!p[GIT_SHA1_HEXSZ]) | |
292ce46b SP |
898 | name = ""; |
899 | else | |
900 | die("protocol error: expected sha/ref, got %s'", p); | |
901 | ||
902 | ref = alloc_ref(name); | |
8338c911 | 903 | oidcpy(&ref->old_oid, &old_oid); |
292ce46b SP |
904 | |
905 | *list = ref; | |
906 | list = &ref->next; | |
907 | ||
908 | ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads); | |
909 | to_fetch[nr_heads++] = ref; | |
910 | } | |
911 | else | |
912 | die("http transport does not support %s", buf->buf); | |
913 | ||
914 | strbuf_reset(buf); | |
8f309aeb | 915 | if (strbuf_getline_lf(buf, stdin) == EOF) |
292ce46b SP |
916 | return; |
917 | if (!*buf->buf) | |
918 | break; | |
919 | } while (1); | |
920 | ||
249b2004 | 921 | if (fetch(nr_heads, to_fetch)) |
292ce46b SP |
922 | exit(128); /* error already reported */ |
923 | free_refs(list_head); | |
924 | free(to_fetch); | |
925 | ||
926 | printf("\n"); | |
927 | fflush(stdout); | |
928 | strbuf_reset(buf); | |
929 | } | |
930 | ||
ae4efe19 SP |
931 | static int push_dav(int nr_spec, char **specs) |
932 | { | |
850d2fec JK |
933 | struct child_process child = CHILD_PROCESS_INIT; |
934 | size_t i; | |
ae4efe19 | 935 | |
850d2fec JK |
936 | child.git_cmd = 1; |
937 | argv_array_push(&child.args, "http-push"); | |
938 | argv_array_push(&child.args, "--helper-status"); | |
ae4efe19 | 939 | if (options.dry_run) |
850d2fec | 940 | argv_array_push(&child.args, "--dry-run"); |
ae4efe19 | 941 | if (options.verbosity > 1) |
850d2fec JK |
942 | argv_array_push(&child.args, "--verbose"); |
943 | argv_array_push(&child.args, url.buf); | |
ae4efe19 | 944 | for (i = 0; i < nr_spec; i++) |
850d2fec | 945 | argv_array_push(&child.args, specs[i]); |
ae4efe19 | 946 | |
850d2fec JK |
947 | if (run_command(&child)) |
948 | die("git-http-push failed"); | |
ae4efe19 SP |
949 | return 0; |
950 | } | |
951 | ||
de1a2fdd SP |
952 | static int push_git(struct discovery *heads, int nr_spec, char **specs) |
953 | { | |
954 | struct rpc_state rpc; | |
222b1212 JH |
955 | int i, err; |
956 | struct argv_array args; | |
05c1eb10 | 957 | struct string_list_item *cas_option; |
26be19ba | 958 | struct strbuf preamble = STRBUF_INIT; |
222b1212 JH |
959 | |
960 | argv_array_init(&args); | |
961 | argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status", | |
962 | NULL); | |
de1a2fdd | 963 | |
de1a2fdd | 964 | if (options.thin) |
222b1212 | 965 | argv_array_push(&args, "--thin"); |
de1a2fdd | 966 | if (options.dry_run) |
222b1212 | 967 | argv_array_push(&args, "--dry-run"); |
30261094 DB |
968 | if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS) |
969 | argv_array_push(&args, "--signed=yes"); | |
970 | else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) | |
971 | argv_array_push(&args, "--signed=if-asked"); | |
c207e34f | 972 | if (options.verbosity == 0) |
222b1212 | 973 | argv_array_push(&args, "--quiet"); |
c207e34f | 974 | else if (options.verbosity > 1) |
222b1212 | 975 | argv_array_push(&args, "--verbose"); |
511155db BW |
976 | for (i = 0; i < options.push_options.nr; i++) |
977 | argv_array_pushf(&args, "--push-option=%s", | |
978 | options.push_options.items[i].string); | |
222b1212 | 979 | argv_array_push(&args, options.progress ? "--progress" : "--no-progress"); |
05c1eb10 | 980 | for_each_string_list_item(cas_option, &cas_options) |
2233ad45 | 981 | argv_array_push(&args, cas_option->string); |
b227bbc4 | 982 | argv_array_push(&args, url.buf); |
26be19ba JK |
983 | |
984 | argv_array_push(&args, "--stdin"); | |
de1a2fdd | 985 | for (i = 0; i < nr_spec; i++) |
26be19ba JK |
986 | packet_buf_write(&preamble, "%s\n", specs[i]); |
987 | packet_buf_flush(&preamble); | |
de1a2fdd SP |
988 | |
989 | memset(&rpc, 0, sizeof(rpc)); | |
990 | rpc.service_name = "git-receive-pack", | |
222b1212 | 991 | rpc.argv = args.argv; |
26be19ba | 992 | rpc.stdin_preamble = &preamble; |
de1a2fdd SP |
993 | |
994 | err = rpc_service(&rpc, heads); | |
995 | if (rpc.result.len) | |
cdf4fb8e | 996 | write_or_die(1, rpc.result.buf, rpc.result.len); |
de1a2fdd | 997 | strbuf_release(&rpc.result); |
26be19ba | 998 | strbuf_release(&preamble); |
222b1212 | 999 | argv_array_clear(&args); |
de1a2fdd SP |
1000 | return err; |
1001 | } | |
1002 | ||
1003 | static int push(int nr_spec, char **specs) | |
1004 | { | |
2a455202 | 1005 | struct discovery *heads = discover_refs("git-receive-pack", 1); |
de1a2fdd SP |
1006 | int ret; |
1007 | ||
1008 | if (heads->proto_git) | |
1009 | ret = push_git(heads, nr_spec, specs); | |
1010 | else | |
1011 | ret = push_dav(nr_spec, specs); | |
1012 | free_discovery(heads); | |
1013 | return ret; | |
1014 | } | |
1015 | ||
ae4efe19 SP |
1016 | static void parse_push(struct strbuf *buf) |
1017 | { | |
1018 | char **specs = NULL; | |
5238cbf6 | 1019 | int alloc_spec = 0, nr_spec = 0, i, ret; |
ae4efe19 SP |
1020 | |
1021 | do { | |
59556548 | 1022 | if (starts_with(buf->buf, "push ")) { |
ae4efe19 SP |
1023 | ALLOC_GROW(specs, nr_spec + 1, alloc_spec); |
1024 | specs[nr_spec++] = xstrdup(buf->buf + 5); | |
1025 | } | |
1026 | else | |
1027 | die("http transport does not support %s", buf->buf); | |
1028 | ||
1029 | strbuf_reset(buf); | |
8f309aeb | 1030 | if (strbuf_getline_lf(buf, stdin) == EOF) |
dc4cd767 | 1031 | goto free_specs; |
ae4efe19 SP |
1032 | if (!*buf->buf) |
1033 | break; | |
1034 | } while (1); | |
1035 | ||
5238cbf6 | 1036 | ret = push(nr_spec, specs); |
ae4efe19 SP |
1037 | printf("\n"); |
1038 | fflush(stdout); | |
dc4cd767 | 1039 | |
5238cbf6 SP |
1040 | if (ret) |
1041 | exit(128); /* error already reported */ | |
1042 | ||
dc4cd767 JM |
1043 | free_specs: |
1044 | for (i = 0; i < nr_spec; i++) | |
1045 | free(specs[i]); | |
1046 | free(specs); | |
ae4efe19 SP |
1047 | } |
1048 | ||
3f2e2297 | 1049 | int cmd_main(int argc, const char **argv) |
a2d725b7 | 1050 | { |
a2d725b7 | 1051 | struct strbuf buf = STRBUF_INIT; |
a45d3d7e | 1052 | int nongit; |
a2d725b7 | 1053 | |
a45d3d7e | 1054 | setup_git_directory_gently(&nongit); |
a2d725b7 | 1055 | if (argc < 2) { |
cdaa4e98 | 1056 | error("remote-curl: usage: git remote-curl <remote> [<url>]"); |
a2d725b7 DB |
1057 | return 1; |
1058 | } | |
1059 | ||
ef08ef9e SP |
1060 | options.verbosity = 1; |
1061 | options.progress = !!isatty(2); | |
de1a2fdd | 1062 | options.thin = 1; |
a45a2600 | 1063 | string_list_init(&options.deepen_not, 1); |
511155db | 1064 | string_list_init(&options.push_options, 1); |
ef08ef9e | 1065 | |
a2d725b7 DB |
1066 | remote = remote_get(argv[1]); |
1067 | ||
1068 | if (argc > 2) { | |
b227bbc4 | 1069 | end_url_with_slash(&url, argv[2]); |
a2d725b7 | 1070 | } else { |
b227bbc4 | 1071 | end_url_with_slash(&url, remote->url[0]); |
a2d725b7 DB |
1072 | } |
1073 | ||
b227bbc4 | 1074 | http_init(remote, url.buf, 0); |
888692b7 | 1075 | |
a2d725b7 | 1076 | do { |
95b567c7 JK |
1077 | const char *arg; |
1078 | ||
8f309aeb | 1079 | if (strbuf_getline_lf(&buf, stdin) == EOF) { |
1843f0ce | 1080 | if (ferror(stdin)) |
cdaa4e98 | 1081 | error("remote-curl: error reading command stream from git"); |
1843f0ce SR |
1082 | return 1; |
1083 | } | |
1084 | if (buf.len == 0) | |
a2d725b7 | 1085 | break; |
59556548 | 1086 | if (starts_with(buf.buf, "fetch ")) { |
a45d3d7e | 1087 | if (nongit) |
cdaa4e98 | 1088 | die("remote-curl: fetch attempted without a local repo"); |
292ce46b SP |
1089 | parse_fetch(&buf); |
1090 | ||
59556548 | 1091 | } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) { |
97cc7bc4 SP |
1092 | int for_push = !!strstr(buf.buf + 4, "for-push"); |
1093 | output_refs(get_refs(for_push)); | |
ae4efe19 | 1094 | |
59556548 | 1095 | } else if (starts_with(buf.buf, "push ")) { |
ae4efe19 SP |
1096 | parse_push(&buf); |
1097 | ||
95b567c7 JK |
1098 | } else if (skip_prefix(buf.buf, "option ", &arg)) { |
1099 | char *value = strchr(arg, ' '); | |
ef08ef9e SP |
1100 | int result; |
1101 | ||
1102 | if (value) | |
1103 | *value++ = '\0'; | |
1104 | else | |
1105 | value = "true"; | |
1106 | ||
95b567c7 | 1107 | result = set_option(arg, value); |
ef08ef9e SP |
1108 | if (!result) |
1109 | printf("ok\n"); | |
1110 | else if (result < 0) | |
1111 | printf("error invalid value\n"); | |
1112 | else | |
1113 | printf("unsupported\n"); | |
a2d725b7 | 1114 | fflush(stdout); |
ef08ef9e | 1115 | |
a2d725b7 DB |
1116 | } else if (!strcmp(buf.buf, "capabilities")) { |
1117 | printf("fetch\n"); | |
ef08ef9e | 1118 | printf("option\n"); |
ae4efe19 | 1119 | printf("push\n"); |
9ba38048 | 1120 | printf("check-connectivity\n"); |
a2d725b7 DB |
1121 | printf("\n"); |
1122 | fflush(stdout); | |
1123 | } else { | |
cdaa4e98 | 1124 | error("remote-curl: unknown command '%s' from git", buf.buf); |
a2d725b7 DB |
1125 | return 1; |
1126 | } | |
1127 | strbuf_reset(&buf); | |
1128 | } while (1); | |
888692b7 TRC |
1129 | |
1130 | http_cleanup(); | |
1131 | ||
a2d725b7 DB |
1132 | return 0; |
1133 | } |