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