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