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