]>
Commit | Line | Data |
---|---|---|
1 | #include "git-compat-util.h" | |
2 | #include "transport.h" | |
3 | #include "quote.h" | |
4 | #include "run-command.h" | |
5 | #include "commit.h" | |
6 | #include "environment.h" | |
7 | #include "gettext.h" | |
8 | #include "hex.h" | |
9 | #include "object-name.h" | |
10 | #include "repository.h" | |
11 | #include "remote.h" | |
12 | #include "string-list.h" | |
13 | #include "thread-utils.h" | |
14 | #include "sigchain.h" | |
15 | #include "strvec.h" | |
16 | #include "refs.h" | |
17 | #include "refspec.h" | |
18 | #include "transport-internal.h" | |
19 | #include "protocol.h" | |
20 | #include "packfile.h" | |
21 | ||
22 | static int debug; | |
23 | ||
24 | struct helper_data { | |
25 | const char *name; | |
26 | struct child_process *helper; | |
27 | FILE *out; | |
28 | unsigned fetch : 1, | |
29 | import : 1, | |
30 | bidi_import : 1, | |
31 | export : 1, | |
32 | option : 1, | |
33 | push : 1, | |
34 | connect : 1, | |
35 | stateless_connect : 1, | |
36 | signed_tags : 1, | |
37 | check_connectivity : 1, | |
38 | no_disconnect_req : 1, | |
39 | no_private_update : 1, | |
40 | object_format : 1; | |
41 | ||
42 | /* | |
43 | * As an optimization, the transport code may invoke fetch before | |
44 | * get_refs_list. If this happens, and if the transport helper doesn't | |
45 | * support connect or stateless_connect, we need to invoke | |
46 | * get_refs_list ourselves if we haven't already done so. Keep track of | |
47 | * whether we have invoked get_refs_list. | |
48 | */ | |
49 | unsigned get_refs_list_called : 1; | |
50 | ||
51 | char *export_marks; | |
52 | char *import_marks; | |
53 | /* These go from remote name (as in "list") to private name */ | |
54 | struct refspec rs; | |
55 | /* Transport options for fetch-pack/send-pack (should one of | |
56 | * those be invoked). | |
57 | */ | |
58 | struct git_transport_options transport_options; | |
59 | }; | |
60 | ||
61 | static void sendline(struct helper_data *helper, struct strbuf *buffer) | |
62 | { | |
63 | if (debug) | |
64 | fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf); | |
65 | if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0) | |
66 | die_errno(_("full write to remote helper failed")); | |
67 | } | |
68 | ||
69 | static int recvline_fh(FILE *helper, struct strbuf *buffer) | |
70 | { | |
71 | strbuf_reset(buffer); | |
72 | if (debug) | |
73 | fprintf(stderr, "Debug: Remote helper: Waiting...\n"); | |
74 | if (strbuf_getline(buffer, helper) == EOF) { | |
75 | if (debug) | |
76 | fprintf(stderr, "Debug: Remote helper quit.\n"); | |
77 | return 1; | |
78 | } | |
79 | ||
80 | if (debug) | |
81 | fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf); | |
82 | return 0; | |
83 | } | |
84 | ||
85 | static int recvline(struct helper_data *helper, struct strbuf *buffer) | |
86 | { | |
87 | return recvline_fh(helper->out, buffer); | |
88 | } | |
89 | ||
90 | static void write_constant(int fd, const char *str) | |
91 | { | |
92 | if (debug) | |
93 | fprintf(stderr, "Debug: Remote helper: -> %s", str); | |
94 | if (write_in_full(fd, str, strlen(str)) < 0) | |
95 | die_errno(_("full write to remote helper failed")); | |
96 | } | |
97 | ||
98 | static const char *remove_ext_force(const char *url) | |
99 | { | |
100 | if (url) { | |
101 | const char *colon = strchr(url, ':'); | |
102 | if (colon && colon[1] == ':') | |
103 | return colon + 2; | |
104 | } | |
105 | return url; | |
106 | } | |
107 | ||
108 | static void do_take_over(struct transport *transport) | |
109 | { | |
110 | struct helper_data *data; | |
111 | data = (struct helper_data *)transport->data; | |
112 | transport_take_over(transport, data->helper); | |
113 | fclose(data->out); | |
114 | free(data); | |
115 | } | |
116 | ||
117 | static void standard_options(struct transport *t); | |
118 | ||
119 | static struct child_process *get_helper(struct transport *transport) | |
120 | { | |
121 | struct helper_data *data = transport->data; | |
122 | struct strbuf buf = STRBUF_INIT; | |
123 | struct child_process *helper; | |
124 | int duped; | |
125 | int code; | |
126 | ||
127 | if (data->helper) | |
128 | return data->helper; | |
129 | ||
130 | helper = xmalloc(sizeof(*helper)); | |
131 | child_process_init(helper); | |
132 | helper->in = -1; | |
133 | helper->out = -1; | |
134 | helper->err = 0; | |
135 | strvec_pushf(&helper->args, "remote-%s", data->name); | |
136 | strvec_push(&helper->args, transport->remote->name); | |
137 | strvec_push(&helper->args, remove_ext_force(transport->url)); | |
138 | helper->git_cmd = 1; | |
139 | helper->silent_exec_failure = 1; | |
140 | ||
141 | if (have_git_dir()) | |
142 | strvec_pushf(&helper->env, "%s=%s", | |
143 | GIT_DIR_ENVIRONMENT, get_git_dir()); | |
144 | ||
145 | helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */ | |
146 | ||
147 | code = start_command(helper); | |
148 | if (code < 0 && errno == ENOENT) | |
149 | die(_("unable to find remote helper for '%s'"), data->name); | |
150 | else if (code != 0) | |
151 | exit(code); | |
152 | ||
153 | data->helper = helper; | |
154 | data->no_disconnect_req = 0; | |
155 | refspec_init(&data->rs, REFSPEC_FETCH); | |
156 | ||
157 | /* | |
158 | * Open the output as FILE* so strbuf_getline_*() family of | |
159 | * functions can be used. | |
160 | * Do this with duped fd because fclose() will close the fd, | |
161 | * and stuff like taking over will require the fd to remain. | |
162 | */ | |
163 | duped = dup(helper->out); | |
164 | if (duped < 0) | |
165 | die_errno(_("can't dup helper output fd")); | |
166 | data->out = xfdopen(duped, "r"); | |
167 | ||
168 | write_constant(helper->in, "capabilities\n"); | |
169 | ||
170 | while (1) { | |
171 | const char *capname, *arg; | |
172 | int mandatory = 0; | |
173 | if (recvline(data, &buf)) | |
174 | exit(128); | |
175 | ||
176 | if (!*buf.buf) | |
177 | break; | |
178 | ||
179 | if (*buf.buf == '*') { | |
180 | capname = buf.buf + 1; | |
181 | mandatory = 1; | |
182 | } else | |
183 | capname = buf.buf; | |
184 | ||
185 | if (debug) | |
186 | fprintf(stderr, "Debug: Got cap %s\n", capname); | |
187 | if (!strcmp(capname, "fetch")) | |
188 | data->fetch = 1; | |
189 | else if (!strcmp(capname, "option")) | |
190 | data->option = 1; | |
191 | else if (!strcmp(capname, "push")) | |
192 | data->push = 1; | |
193 | else if (!strcmp(capname, "import")) | |
194 | data->import = 1; | |
195 | else if (!strcmp(capname, "bidi-import")) | |
196 | data->bidi_import = 1; | |
197 | else if (!strcmp(capname, "export")) | |
198 | data->export = 1; | |
199 | else if (!strcmp(capname, "check-connectivity")) | |
200 | data->check_connectivity = 1; | |
201 | else if (skip_prefix(capname, "refspec ", &arg)) { | |
202 | refspec_append(&data->rs, arg); | |
203 | } else if (!strcmp(capname, "connect")) { | |
204 | data->connect = 1; | |
205 | } else if (!strcmp(capname, "stateless-connect")) { | |
206 | data->stateless_connect = 1; | |
207 | } else if (!strcmp(capname, "signed-tags")) { | |
208 | data->signed_tags = 1; | |
209 | } else if (skip_prefix(capname, "export-marks ", &arg)) { | |
210 | data->export_marks = xstrdup(arg); | |
211 | } else if (skip_prefix(capname, "import-marks ", &arg)) { | |
212 | data->import_marks = xstrdup(arg); | |
213 | } else if (starts_with(capname, "no-private-update")) { | |
214 | data->no_private_update = 1; | |
215 | } else if (starts_with(capname, "object-format")) { | |
216 | data->object_format = 1; | |
217 | } else if (mandatory) { | |
218 | die(_("unknown mandatory capability %s; this remote " | |
219 | "helper probably needs newer version of Git"), | |
220 | capname); | |
221 | } | |
222 | } | |
223 | if (!data->rs.nr && (data->import || data->bidi_import || data->export)) { | |
224 | warning(_("this remote helper should implement refspec capability")); | |
225 | } | |
226 | strbuf_release(&buf); | |
227 | if (debug) | |
228 | fprintf(stderr, "Debug: Capabilities complete.\n"); | |
229 | standard_options(transport); | |
230 | return data->helper; | |
231 | } | |
232 | ||
233 | static int disconnect_helper(struct transport *transport) | |
234 | { | |
235 | struct helper_data *data = transport->data; | |
236 | int res = 0; | |
237 | ||
238 | if (data->helper) { | |
239 | if (debug) | |
240 | fprintf(stderr, "Debug: Disconnecting.\n"); | |
241 | if (!data->no_disconnect_req) { | |
242 | /* | |
243 | * Ignore write errors; there's nothing we can do, | |
244 | * since we're about to close the pipe anyway. And the | |
245 | * most likely error is EPIPE due to the helper dying | |
246 | * to report an error itself. | |
247 | */ | |
248 | sigchain_push(SIGPIPE, SIG_IGN); | |
249 | xwrite(data->helper->in, "\n", 1); | |
250 | sigchain_pop(SIGPIPE); | |
251 | } | |
252 | close(data->helper->in); | |
253 | close(data->helper->out); | |
254 | fclose(data->out); | |
255 | res = finish_command(data->helper); | |
256 | FREE_AND_NULL(data->helper); | |
257 | } | |
258 | return res; | |
259 | } | |
260 | ||
261 | static const char *unsupported_options[] = { | |
262 | TRANS_OPT_UPLOADPACK, | |
263 | TRANS_OPT_RECEIVEPACK, | |
264 | TRANS_OPT_THIN, | |
265 | TRANS_OPT_KEEP | |
266 | }; | |
267 | ||
268 | static const char *boolean_options[] = { | |
269 | TRANS_OPT_THIN, | |
270 | TRANS_OPT_KEEP, | |
271 | TRANS_OPT_FOLLOWTAGS, | |
272 | TRANS_OPT_DEEPEN_RELATIVE | |
273 | }; | |
274 | ||
275 | static int strbuf_set_helper_option(struct helper_data *data, | |
276 | struct strbuf *buf) | |
277 | { | |
278 | int ret; | |
279 | ||
280 | sendline(data, buf); | |
281 | if (recvline(data, buf)) | |
282 | exit(128); | |
283 | ||
284 | if (!strcmp(buf->buf, "ok")) | |
285 | ret = 0; | |
286 | else if (starts_with(buf->buf, "error")) | |
287 | ret = -1; | |
288 | else if (!strcmp(buf->buf, "unsupported")) | |
289 | ret = 1; | |
290 | else { | |
291 | warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf); | |
292 | ret = 1; | |
293 | } | |
294 | return ret; | |
295 | } | |
296 | ||
297 | static int string_list_set_helper_option(struct helper_data *data, | |
298 | const char *name, | |
299 | struct string_list *list) | |
300 | { | |
301 | struct strbuf buf = STRBUF_INIT; | |
302 | int i, ret = 0; | |
303 | ||
304 | for (i = 0; i < list->nr; i++) { | |
305 | strbuf_addf(&buf, "option %s ", name); | |
306 | quote_c_style(list->items[i].string, &buf, NULL, 0); | |
307 | strbuf_addch(&buf, '\n'); | |
308 | ||
309 | if ((ret = strbuf_set_helper_option(data, &buf))) | |
310 | break; | |
311 | strbuf_reset(&buf); | |
312 | } | |
313 | strbuf_release(&buf); | |
314 | return ret; | |
315 | } | |
316 | ||
317 | static int set_helper_option(struct transport *transport, | |
318 | const char *name, const char *value) | |
319 | { | |
320 | struct helper_data *data = transport->data; | |
321 | struct strbuf buf = STRBUF_INIT; | |
322 | int i, ret, is_bool = 0; | |
323 | ||
324 | get_helper(transport); | |
325 | ||
326 | if (!data->option) | |
327 | return 1; | |
328 | ||
329 | if (!strcmp(name, "deepen-not")) | |
330 | return string_list_set_helper_option(data, name, | |
331 | (struct string_list *)value); | |
332 | ||
333 | for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) { | |
334 | if (!strcmp(name, unsupported_options[i])) | |
335 | return 1; | |
336 | } | |
337 | ||
338 | for (i = 0; i < ARRAY_SIZE(boolean_options); i++) { | |
339 | if (!strcmp(name, boolean_options[i])) { | |
340 | is_bool = 1; | |
341 | break; | |
342 | } | |
343 | } | |
344 | ||
345 | strbuf_addf(&buf, "option %s ", name); | |
346 | if (is_bool) | |
347 | strbuf_addstr(&buf, value ? "true" : "false"); | |
348 | else | |
349 | quote_c_style(value, &buf, NULL, 0); | |
350 | strbuf_addch(&buf, '\n'); | |
351 | ||
352 | ret = strbuf_set_helper_option(data, &buf); | |
353 | strbuf_release(&buf); | |
354 | return ret; | |
355 | } | |
356 | ||
357 | static void standard_options(struct transport *t) | |
358 | { | |
359 | char buf[16]; | |
360 | int v = t->verbose; | |
361 | ||
362 | set_helper_option(t, "progress", t->progress ? "true" : "false"); | |
363 | ||
364 | xsnprintf(buf, sizeof(buf), "%d", v + 1); | |
365 | set_helper_option(t, "verbosity", buf); | |
366 | ||
367 | switch (t->family) { | |
368 | case TRANSPORT_FAMILY_ALL: | |
369 | /* | |
370 | * this is already the default, | |
371 | * do not break old remote helpers by setting "all" here | |
372 | */ | |
373 | break; | |
374 | case TRANSPORT_FAMILY_IPV4: | |
375 | set_helper_option(t, "family", "ipv4"); | |
376 | break; | |
377 | case TRANSPORT_FAMILY_IPV6: | |
378 | set_helper_option(t, "family", "ipv6"); | |
379 | break; | |
380 | } | |
381 | } | |
382 | ||
383 | static int release_helper(struct transport *transport) | |
384 | { | |
385 | int res = 0; | |
386 | struct helper_data *data = transport->data; | |
387 | refspec_clear(&data->rs); | |
388 | res = disconnect_helper(transport); | |
389 | free(transport->data); | |
390 | return res; | |
391 | } | |
392 | ||
393 | static int fetch_with_fetch(struct transport *transport, | |
394 | int nr_heads, struct ref **to_fetch) | |
395 | { | |
396 | struct helper_data *data = transport->data; | |
397 | int i; | |
398 | struct strbuf buf = STRBUF_INIT; | |
399 | ||
400 | for (i = 0; i < nr_heads; i++) { | |
401 | const struct ref *posn = to_fetch[i]; | |
402 | if (posn->status & REF_STATUS_UPTODATE) | |
403 | continue; | |
404 | ||
405 | strbuf_addf(&buf, "fetch %s %s\n", | |
406 | oid_to_hex(&posn->old_oid), | |
407 | posn->symref ? posn->symref : posn->name); | |
408 | } | |
409 | ||
410 | strbuf_addch(&buf, '\n'); | |
411 | sendline(data, &buf); | |
412 | ||
413 | while (1) { | |
414 | const char *name; | |
415 | ||
416 | if (recvline(data, &buf)) | |
417 | exit(128); | |
418 | ||
419 | if (skip_prefix(buf.buf, "lock ", &name)) { | |
420 | if (transport->pack_lockfiles.nr) | |
421 | warning(_("%s also locked %s"), data->name, name); | |
422 | else | |
423 | string_list_append(&transport->pack_lockfiles, | |
424 | name); | |
425 | } | |
426 | else if (data->check_connectivity && | |
427 | data->transport_options.check_self_contained_and_connected && | |
428 | !strcmp(buf.buf, "connectivity-ok")) | |
429 | data->transport_options.self_contained_and_connected = 1; | |
430 | else if (!buf.len) | |
431 | break; | |
432 | else | |
433 | warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf); | |
434 | } | |
435 | strbuf_release(&buf); | |
436 | ||
437 | reprepare_packed_git(the_repository); | |
438 | return 0; | |
439 | } | |
440 | ||
441 | static int get_importer(struct transport *transport, struct child_process *fastimport) | |
442 | { | |
443 | struct child_process *helper = get_helper(transport); | |
444 | struct helper_data *data = transport->data; | |
445 | int cat_blob_fd, code; | |
446 | child_process_init(fastimport); | |
447 | fastimport->in = xdup(helper->out); | |
448 | strvec_push(&fastimport->args, "fast-import"); | |
449 | strvec_push(&fastimport->args, "--allow-unsafe-features"); | |
450 | strvec_push(&fastimport->args, debug ? "--stats" : "--quiet"); | |
451 | ||
452 | if (data->bidi_import) { | |
453 | cat_blob_fd = xdup(helper->in); | |
454 | strvec_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd); | |
455 | } | |
456 | fastimport->git_cmd = 1; | |
457 | ||
458 | code = start_command(fastimport); | |
459 | return code; | |
460 | } | |
461 | ||
462 | static int get_exporter(struct transport *transport, | |
463 | struct child_process *fastexport, | |
464 | struct string_list *revlist_args) | |
465 | { | |
466 | struct helper_data *data = transport->data; | |
467 | struct child_process *helper = get_helper(transport); | |
468 | int i; | |
469 | ||
470 | child_process_init(fastexport); | |
471 | ||
472 | /* we need to duplicate helper->in because we want to use it after | |
473 | * fastexport is done with it. */ | |
474 | fastexport->out = dup(helper->in); | |
475 | strvec_push(&fastexport->args, "fast-export"); | |
476 | strvec_push(&fastexport->args, "--use-done-feature"); | |
477 | strvec_push(&fastexport->args, data->signed_tags ? | |
478 | "--signed-tags=verbatim" : "--signed-tags=warn-strip"); | |
479 | if (data->export_marks) | |
480 | strvec_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks); | |
481 | if (data->import_marks) | |
482 | strvec_pushf(&fastexport->args, "--import-marks=%s", data->import_marks); | |
483 | ||
484 | for (i = 0; i < revlist_args->nr; i++) | |
485 | strvec_push(&fastexport->args, revlist_args->items[i].string); | |
486 | ||
487 | fastexport->git_cmd = 1; | |
488 | return start_command(fastexport); | |
489 | } | |
490 | ||
491 | static int fetch_with_import(struct transport *transport, | |
492 | int nr_heads, struct ref **to_fetch) | |
493 | { | |
494 | struct child_process fastimport; | |
495 | struct helper_data *data = transport->data; | |
496 | int i; | |
497 | struct ref *posn; | |
498 | struct strbuf buf = STRBUF_INIT; | |
499 | ||
500 | get_helper(transport); | |
501 | ||
502 | if (get_importer(transport, &fastimport)) | |
503 | die(_("couldn't run fast-import")); | |
504 | ||
505 | for (i = 0; i < nr_heads; i++) { | |
506 | posn = to_fetch[i]; | |
507 | if (posn->status & REF_STATUS_UPTODATE) | |
508 | continue; | |
509 | ||
510 | strbuf_addf(&buf, "import %s\n", | |
511 | posn->symref ? posn->symref : posn->name); | |
512 | sendline(data, &buf); | |
513 | strbuf_reset(&buf); | |
514 | } | |
515 | ||
516 | write_constant(data->helper->in, "\n"); | |
517 | /* | |
518 | * remote-helpers that advertise the bidi-import capability are required to | |
519 | * buffer the complete batch of import commands until this newline before | |
520 | * sending data to fast-import. | |
521 | * These helpers read back data from fast-import on their stdin, which could | |
522 | * be mixed with import commands, otherwise. | |
523 | */ | |
524 | ||
525 | if (finish_command(&fastimport)) | |
526 | die(_("error while running fast-import")); | |
527 | ||
528 | /* | |
529 | * The fast-import stream of a remote helper that advertises | |
530 | * the "refspec" capability writes to the refs named after the | |
531 | * right hand side of the first refspec matching each ref we | |
532 | * were fetching. | |
533 | * | |
534 | * (If no "refspec" capability was specified, for historical | |
535 | * reasons we default to the equivalent of *:*.) | |
536 | * | |
537 | * Store the result in to_fetch[i].old_sha1. Callers such | |
538 | * as "git fetch" can use the value to write feedback to the | |
539 | * terminal, populate FETCH_HEAD, and determine what new value | |
540 | * should be written to peer_ref if the update is a | |
541 | * fast-forward or this is a forced update. | |
542 | */ | |
543 | for (i = 0; i < nr_heads; i++) { | |
544 | char *private, *name; | |
545 | posn = to_fetch[i]; | |
546 | if (posn->status & REF_STATUS_UPTODATE) | |
547 | continue; | |
548 | name = posn->symref ? posn->symref : posn->name; | |
549 | if (data->rs.nr) | |
550 | private = apply_refspecs(&data->rs, name); | |
551 | else | |
552 | private = xstrdup(name); | |
553 | if (private) { | |
554 | if (read_ref(private, &posn->old_oid) < 0) | |
555 | die(_("could not read ref %s"), private); | |
556 | free(private); | |
557 | } | |
558 | } | |
559 | strbuf_release(&buf); | |
560 | return 0; | |
561 | } | |
562 | ||
563 | static int run_connect(struct transport *transport, struct strbuf *cmdbuf) | |
564 | { | |
565 | struct helper_data *data = transport->data; | |
566 | int ret = 0; | |
567 | int duped; | |
568 | FILE *input; | |
569 | struct child_process *helper; | |
570 | ||
571 | helper = get_helper(transport); | |
572 | ||
573 | /* | |
574 | * Yes, dup the pipe another time, as we need unbuffered version | |
575 | * of input pipe as FILE*. fclose() closes the underlying fd and | |
576 | * stream buffering only can be changed before first I/O operation | |
577 | * on it. | |
578 | */ | |
579 | duped = dup(helper->out); | |
580 | if (duped < 0) | |
581 | die_errno(_("can't dup helper output fd")); | |
582 | input = xfdopen(duped, "r"); | |
583 | setvbuf(input, NULL, _IONBF, 0); | |
584 | ||
585 | sendline(data, cmdbuf); | |
586 | if (recvline_fh(input, cmdbuf)) | |
587 | exit(128); | |
588 | ||
589 | if (!strcmp(cmdbuf->buf, "")) { | |
590 | data->no_disconnect_req = 1; | |
591 | if (debug) | |
592 | fprintf(stderr, "Debug: Smart transport connection " | |
593 | "ready.\n"); | |
594 | ret = 1; | |
595 | } else if (!strcmp(cmdbuf->buf, "fallback")) { | |
596 | if (debug) | |
597 | fprintf(stderr, "Debug: Falling back to dumb " | |
598 | "transport.\n"); | |
599 | } else { | |
600 | die(_("unknown response to connect: %s"), | |
601 | cmdbuf->buf); | |
602 | } | |
603 | ||
604 | fclose(input); | |
605 | return ret; | |
606 | } | |
607 | ||
608 | static int process_connect_service(struct transport *transport, | |
609 | const char *name, const char *exec) | |
610 | { | |
611 | struct helper_data *data = transport->data; | |
612 | struct strbuf cmdbuf = STRBUF_INIT; | |
613 | int ret = 0; | |
614 | ||
615 | /* | |
616 | * Handle --upload-pack and friends. This is fire and forget... | |
617 | * just warn if it fails. | |
618 | */ | |
619 | if (strcmp(name, exec)) { | |
620 | int r = set_helper_option(transport, "servpath", exec); | |
621 | if (r > 0) | |
622 | warning(_("setting remote service path not supported by protocol")); | |
623 | else if (r < 0) | |
624 | warning(_("invalid remote service path")); | |
625 | } | |
626 | ||
627 | if (data->connect) { | |
628 | strbuf_addf(&cmdbuf, "connect %s\n", name); | |
629 | ret = run_connect(transport, &cmdbuf); | |
630 | } else if (data->stateless_connect && | |
631 | (get_protocol_version_config() == protocol_v2) && | |
632 | (!strcmp("git-upload-pack", name) || | |
633 | !strcmp("git-upload-archive", name))) { | |
634 | strbuf_addf(&cmdbuf, "stateless-connect %s\n", name); | |
635 | ret = run_connect(transport, &cmdbuf); | |
636 | if (ret) | |
637 | transport->stateless_rpc = 1; | |
638 | } | |
639 | ||
640 | strbuf_release(&cmdbuf); | |
641 | return ret; | |
642 | } | |
643 | ||
644 | static int process_connect(struct transport *transport, | |
645 | int for_push) | |
646 | { | |
647 | struct helper_data *data = transport->data; | |
648 | const char *name; | |
649 | const char *exec; | |
650 | int ret; | |
651 | ||
652 | name = for_push ? "git-receive-pack" : "git-upload-pack"; | |
653 | if (for_push) | |
654 | exec = data->transport_options.receivepack; | |
655 | else | |
656 | exec = data->transport_options.uploadpack; | |
657 | ||
658 | ret = process_connect_service(transport, name, exec); | |
659 | if (ret) | |
660 | do_take_over(transport); | |
661 | return ret; | |
662 | } | |
663 | ||
664 | static int connect_helper(struct transport *transport, const char *name, | |
665 | const char *exec, int fd[2]) | |
666 | { | |
667 | struct helper_data *data = transport->data; | |
668 | ||
669 | /* Get_helper so connect is inited. */ | |
670 | get_helper(transport); | |
671 | ||
672 | if (!process_connect_service(transport, name, exec)) | |
673 | die(_("can't connect to subservice %s"), name); | |
674 | ||
675 | fd[0] = data->helper->out; | |
676 | fd[1] = data->helper->in; | |
677 | ||
678 | do_take_over(transport); | |
679 | return 0; | |
680 | } | |
681 | ||
682 | static struct ref *get_refs_list_using_list(struct transport *transport, | |
683 | int for_push); | |
684 | ||
685 | static int fetch_refs(struct transport *transport, | |
686 | int nr_heads, struct ref **to_fetch) | |
687 | { | |
688 | struct helper_data *data = transport->data; | |
689 | int i, count; | |
690 | ||
691 | get_helper(transport); | |
692 | ||
693 | if (process_connect(transport, 0)) | |
694 | return transport->vtable->fetch_refs(transport, nr_heads, to_fetch); | |
695 | ||
696 | /* | |
697 | * If we reach here, then the server, the client, and/or the transport | |
698 | * helper does not support protocol v2. --negotiate-only requires | |
699 | * protocol v2. | |
700 | */ | |
701 | if (data->transport_options.acked_commits) { | |
702 | warning(_("--negotiate-only requires protocol v2")); | |
703 | return -1; | |
704 | } | |
705 | ||
706 | if (!data->get_refs_list_called) | |
707 | get_refs_list_using_list(transport, 0); | |
708 | ||
709 | count = 0; | |
710 | for (i = 0; i < nr_heads; i++) | |
711 | if (!(to_fetch[i]->status & REF_STATUS_UPTODATE)) | |
712 | count++; | |
713 | ||
714 | if (!count) | |
715 | return 0; | |
716 | ||
717 | if (data->check_connectivity && | |
718 | data->transport_options.check_self_contained_and_connected) | |
719 | set_helper_option(transport, "check-connectivity", "true"); | |
720 | ||
721 | if (transport->cloning) | |
722 | set_helper_option(transport, "cloning", "true"); | |
723 | ||
724 | if (data->transport_options.update_shallow) | |
725 | set_helper_option(transport, "update-shallow", "true"); | |
726 | ||
727 | if (data->transport_options.refetch) | |
728 | set_helper_option(transport, "refetch", "true"); | |
729 | ||
730 | if (data->transport_options.filter_options.choice) { | |
731 | const char *spec = expand_list_objects_filter_spec( | |
732 | &data->transport_options.filter_options); | |
733 | set_helper_option(transport, "filter", spec); | |
734 | } | |
735 | ||
736 | if (data->transport_options.negotiation_tips) | |
737 | warning("Ignoring --negotiation-tip because the protocol does not support it."); | |
738 | ||
739 | if (data->fetch) | |
740 | return fetch_with_fetch(transport, nr_heads, to_fetch); | |
741 | ||
742 | if (data->import) | |
743 | return fetch_with_import(transport, nr_heads, to_fetch); | |
744 | ||
745 | return -1; | |
746 | } | |
747 | ||
748 | struct push_update_ref_state { | |
749 | struct ref *hint; | |
750 | struct ref_push_report *report; | |
751 | int new_report; | |
752 | }; | |
753 | ||
754 | static int push_update_ref_status(struct strbuf *buf, | |
755 | struct push_update_ref_state *state, | |
756 | struct ref *remote_refs) | |
757 | { | |
758 | char *refname, *msg; | |
759 | int status, forced = 0; | |
760 | ||
761 | if (starts_with(buf->buf, "option ")) { | |
762 | struct object_id old_oid, new_oid; | |
763 | const char *key, *val; | |
764 | char *p; | |
765 | ||
766 | if (!state->hint || !(state->report || state->new_report)) | |
767 | die(_("'option' without a matching 'ok/error' directive")); | |
768 | if (state->new_report) { | |
769 | if (!state->hint->report) { | |
770 | CALLOC_ARRAY(state->hint->report, 1); | |
771 | state->report = state->hint->report; | |
772 | } else { | |
773 | state->report = state->hint->report; | |
774 | while (state->report->next) | |
775 | state->report = state->report->next; | |
776 | CALLOC_ARRAY(state->report->next, 1); | |
777 | state->report = state->report->next; | |
778 | } | |
779 | state->new_report = 0; | |
780 | } | |
781 | key = buf->buf + 7; | |
782 | p = strchr(key, ' '); | |
783 | if (p) | |
784 | *p++ = '\0'; | |
785 | val = p; | |
786 | if (!strcmp(key, "refname")) | |
787 | state->report->ref_name = xstrdup_or_null(val); | |
788 | else if (!strcmp(key, "old-oid") && val && | |
789 | !parse_oid_hex(val, &old_oid, &val)) | |
790 | state->report->old_oid = oiddup(&old_oid); | |
791 | else if (!strcmp(key, "new-oid") && val && | |
792 | !parse_oid_hex(val, &new_oid, &val)) | |
793 | state->report->new_oid = oiddup(&new_oid); | |
794 | else if (!strcmp(key, "forced-update")) | |
795 | state->report->forced_update = 1; | |
796 | /* Not update remote namespace again. */ | |
797 | return 1; | |
798 | } | |
799 | ||
800 | state->report = NULL; | |
801 | state->new_report = 0; | |
802 | ||
803 | if (starts_with(buf->buf, "ok ")) { | |
804 | status = REF_STATUS_OK; | |
805 | refname = buf->buf + 3; | |
806 | } else if (starts_with(buf->buf, "error ")) { | |
807 | status = REF_STATUS_REMOTE_REJECT; | |
808 | refname = buf->buf + 6; | |
809 | } else | |
810 | die(_("expected ok/error, helper said '%s'"), buf->buf); | |
811 | ||
812 | msg = strchr(refname, ' '); | |
813 | if (msg) { | |
814 | struct strbuf msg_buf = STRBUF_INIT; | |
815 | const char *end; | |
816 | ||
817 | *msg++ = '\0'; | |
818 | if (!unquote_c_style(&msg_buf, msg, &end)) | |
819 | msg = strbuf_detach(&msg_buf, NULL); | |
820 | else | |
821 | msg = xstrdup(msg); | |
822 | strbuf_release(&msg_buf); | |
823 | ||
824 | if (!strcmp(msg, "no match")) { | |
825 | status = REF_STATUS_NONE; | |
826 | FREE_AND_NULL(msg); | |
827 | } | |
828 | else if (!strcmp(msg, "up to date")) { | |
829 | status = REF_STATUS_UPTODATE; | |
830 | FREE_AND_NULL(msg); | |
831 | } | |
832 | else if (!strcmp(msg, "non-fast forward")) { | |
833 | status = REF_STATUS_REJECT_NONFASTFORWARD; | |
834 | FREE_AND_NULL(msg); | |
835 | } | |
836 | else if (!strcmp(msg, "already exists")) { | |
837 | status = REF_STATUS_REJECT_ALREADY_EXISTS; | |
838 | FREE_AND_NULL(msg); | |
839 | } | |
840 | else if (!strcmp(msg, "fetch first")) { | |
841 | status = REF_STATUS_REJECT_FETCH_FIRST; | |
842 | FREE_AND_NULL(msg); | |
843 | } | |
844 | else if (!strcmp(msg, "needs force")) { | |
845 | status = REF_STATUS_REJECT_NEEDS_FORCE; | |
846 | FREE_AND_NULL(msg); | |
847 | } | |
848 | else if (!strcmp(msg, "stale info")) { | |
849 | status = REF_STATUS_REJECT_STALE; | |
850 | FREE_AND_NULL(msg); | |
851 | } | |
852 | else if (!strcmp(msg, "remote ref updated since checkout")) { | |
853 | status = REF_STATUS_REJECT_REMOTE_UPDATED; | |
854 | FREE_AND_NULL(msg); | |
855 | } | |
856 | else if (!strcmp(msg, "forced update")) { | |
857 | forced = 1; | |
858 | FREE_AND_NULL(msg); | |
859 | } | |
860 | else if (!strcmp(msg, "expecting report")) { | |
861 | status = REF_STATUS_EXPECTING_REPORT; | |
862 | FREE_AND_NULL(msg); | |
863 | } | |
864 | } | |
865 | ||
866 | if (state->hint) | |
867 | state->hint = find_ref_by_name(state->hint, refname); | |
868 | if (!state->hint) | |
869 | state->hint = find_ref_by_name(remote_refs, refname); | |
870 | if (!state->hint) { | |
871 | warning(_("helper reported unexpected status of %s"), refname); | |
872 | return 1; | |
873 | } | |
874 | ||
875 | if (state->hint->status != REF_STATUS_NONE) { | |
876 | /* | |
877 | * Earlier, the ref was marked not to be pushed, so ignore the ref | |
878 | * status reported by the remote helper if the latter is 'no match'. | |
879 | */ | |
880 | if (status == REF_STATUS_NONE) | |
881 | return 1; | |
882 | } | |
883 | ||
884 | if (status == REF_STATUS_OK) | |
885 | state->new_report = 1; | |
886 | state->hint->status = status; | |
887 | state->hint->forced_update |= forced; | |
888 | state->hint->remote_status = msg; | |
889 | return !(status == REF_STATUS_OK); | |
890 | } | |
891 | ||
892 | static int push_update_refs_status(struct helper_data *data, | |
893 | struct ref *remote_refs, | |
894 | int flags) | |
895 | { | |
896 | struct ref *ref; | |
897 | struct ref_push_report *report; | |
898 | struct strbuf buf = STRBUF_INIT; | |
899 | struct push_update_ref_state state = { remote_refs, NULL, 0 }; | |
900 | ||
901 | for (;;) { | |
902 | if (recvline(data, &buf)) { | |
903 | strbuf_release(&buf); | |
904 | return 1; | |
905 | } | |
906 | if (!buf.len) | |
907 | break; | |
908 | push_update_ref_status(&buf, &state, remote_refs); | |
909 | } | |
910 | strbuf_release(&buf); | |
911 | ||
912 | if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update) | |
913 | return 0; | |
914 | ||
915 | /* propagate back the update to the remote namespace */ | |
916 | for (ref = remote_refs; ref; ref = ref->next) { | |
917 | char *private; | |
918 | ||
919 | if (ref->status != REF_STATUS_OK) | |
920 | continue; | |
921 | ||
922 | if (!ref->report) { | |
923 | private = apply_refspecs(&data->rs, ref->name); | |
924 | if (!private) | |
925 | continue; | |
926 | update_ref("update by helper", private, &(ref->new_oid), | |
927 | NULL, 0, 0); | |
928 | free(private); | |
929 | } else { | |
930 | for (report = ref->report; report; report = report->next) { | |
931 | private = apply_refspecs(&data->rs, | |
932 | report->ref_name | |
933 | ? report->ref_name | |
934 | : ref->name); | |
935 | if (!private) | |
936 | continue; | |
937 | update_ref("update by helper", private, | |
938 | report->new_oid | |
939 | ? report->new_oid | |
940 | : &(ref->new_oid), | |
941 | NULL, 0, 0); | |
942 | free(private); | |
943 | } | |
944 | } | |
945 | } | |
946 | return 0; | |
947 | } | |
948 | ||
949 | static void set_common_push_options(struct transport *transport, | |
950 | const char *name, int flags) | |
951 | { | |
952 | if (flags & TRANSPORT_PUSH_DRY_RUN) { | |
953 | if (set_helper_option(transport, "dry-run", "true") != 0) | |
954 | die(_("helper %s does not support dry-run"), name); | |
955 | } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) { | |
956 | if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0) | |
957 | die(_("helper %s does not support --signed"), name); | |
958 | } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) { | |
959 | if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0) | |
960 | die(_("helper %s does not support --signed=if-asked"), name); | |
961 | } | |
962 | ||
963 | if (flags & TRANSPORT_PUSH_ATOMIC) | |
964 | if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0) | |
965 | die(_("helper %s does not support --atomic"), name); | |
966 | ||
967 | if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES) | |
968 | if (set_helper_option(transport, TRANS_OPT_FORCE_IF_INCLUDES, "true") != 0) | |
969 | die(_("helper %s does not support --%s"), | |
970 | name, TRANS_OPT_FORCE_IF_INCLUDES); | |
971 | ||
972 | if (flags & TRANSPORT_PUSH_OPTIONS) { | |
973 | struct string_list_item *item; | |
974 | for_each_string_list_item(item, transport->push_options) | |
975 | if (set_helper_option(transport, "push-option", item->string) != 0) | |
976 | die(_("helper %s does not support 'push-option'"), name); | |
977 | } | |
978 | } | |
979 | ||
980 | static int push_refs_with_push(struct transport *transport, | |
981 | struct ref *remote_refs, int flags) | |
982 | { | |
983 | int force_all = flags & TRANSPORT_PUSH_FORCE; | |
984 | int mirror = flags & TRANSPORT_PUSH_MIRROR; | |
985 | int atomic = flags & TRANSPORT_PUSH_ATOMIC; | |
986 | struct helper_data *data = transport->data; | |
987 | struct strbuf buf = STRBUF_INIT; | |
988 | struct ref *ref; | |
989 | struct string_list cas_options = STRING_LIST_INIT_DUP; | |
990 | struct string_list_item *cas_option; | |
991 | ||
992 | get_helper(transport); | |
993 | if (!data->push) | |
994 | return 1; | |
995 | ||
996 | for (ref = remote_refs; ref; ref = ref->next) { | |
997 | if (!ref->peer_ref && !mirror) | |
998 | continue; | |
999 | ||
1000 | /* Check for statuses set by set_ref_status_for_push() */ | |
1001 | switch (ref->status) { | |
1002 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
1003 | case REF_STATUS_REJECT_STALE: | |
1004 | case REF_STATUS_REJECT_ALREADY_EXISTS: | |
1005 | case REF_STATUS_REJECT_REMOTE_UPDATED: | |
1006 | if (atomic) { | |
1007 | reject_atomic_push(remote_refs, mirror); | |
1008 | string_list_clear(&cas_options, 0); | |
1009 | return 0; | |
1010 | } else | |
1011 | continue; | |
1012 | case REF_STATUS_UPTODATE: | |
1013 | continue; | |
1014 | default: | |
1015 | ; /* do nothing */ | |
1016 | } | |
1017 | ||
1018 | if (force_all) | |
1019 | ref->force = 1; | |
1020 | ||
1021 | strbuf_addstr(&buf, "push "); | |
1022 | if (!ref->deletion) { | |
1023 | if (ref->force) | |
1024 | strbuf_addch(&buf, '+'); | |
1025 | if (ref->peer_ref) | |
1026 | strbuf_addstr(&buf, ref->peer_ref->name); | |
1027 | else | |
1028 | strbuf_addstr(&buf, oid_to_hex(&ref->new_oid)); | |
1029 | } | |
1030 | strbuf_addch(&buf, ':'); | |
1031 | strbuf_addstr(&buf, ref->name); | |
1032 | strbuf_addch(&buf, '\n'); | |
1033 | ||
1034 | /* | |
1035 | * The "--force-with-lease" options without explicit | |
1036 | * values to expect have already been expanded into | |
1037 | * the ref->old_oid_expect[] field; we can ignore | |
1038 | * transport->smart_options->cas altogether and instead | |
1039 | * can enumerate them from the refs. | |
1040 | */ | |
1041 | if (ref->expect_old_sha1) { | |
1042 | struct strbuf cas = STRBUF_INIT; | |
1043 | strbuf_addf(&cas, "%s:%s", | |
1044 | ref->name, oid_to_hex(&ref->old_oid_expect)); | |
1045 | string_list_append_nodup(&cas_options, | |
1046 | strbuf_detach(&cas, NULL)); | |
1047 | } | |
1048 | } | |
1049 | if (buf.len == 0) { | |
1050 | string_list_clear(&cas_options, 0); | |
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | for_each_string_list_item(cas_option, &cas_options) | |
1055 | set_helper_option(transport, "cas", cas_option->string); | |
1056 | set_common_push_options(transport, data->name, flags); | |
1057 | ||
1058 | strbuf_addch(&buf, '\n'); | |
1059 | sendline(data, &buf); | |
1060 | strbuf_release(&buf); | |
1061 | string_list_clear(&cas_options, 0); | |
1062 | ||
1063 | return push_update_refs_status(data, remote_refs, flags); | |
1064 | } | |
1065 | ||
1066 | static int push_refs_with_export(struct transport *transport, | |
1067 | struct ref *remote_refs, int flags) | |
1068 | { | |
1069 | struct ref *ref; | |
1070 | struct child_process *helper, exporter; | |
1071 | struct helper_data *data = transport->data; | |
1072 | struct string_list revlist_args = STRING_LIST_INIT_DUP; | |
1073 | struct strbuf buf = STRBUF_INIT; | |
1074 | ||
1075 | if (!data->rs.nr) | |
1076 | die(_("remote-helper doesn't support push; refspec needed")); | |
1077 | ||
1078 | set_common_push_options(transport, data->name, flags); | |
1079 | if (flags & TRANSPORT_PUSH_FORCE) { | |
1080 | if (set_helper_option(transport, "force", "true") != 0) | |
1081 | warning(_("helper %s does not support '--force'"), data->name); | |
1082 | } | |
1083 | ||
1084 | helper = get_helper(transport); | |
1085 | ||
1086 | write_constant(helper->in, "export\n"); | |
1087 | ||
1088 | for (ref = remote_refs; ref; ref = ref->next) { | |
1089 | char *private; | |
1090 | struct object_id oid; | |
1091 | ||
1092 | private = apply_refspecs(&data->rs, ref->name); | |
1093 | if (private && !repo_get_oid(the_repository, private, &oid)) { | |
1094 | strbuf_addf(&buf, "^%s", private); | |
1095 | string_list_append_nodup(&revlist_args, | |
1096 | strbuf_detach(&buf, NULL)); | |
1097 | oidcpy(&ref->old_oid, &oid); | |
1098 | } | |
1099 | free(private); | |
1100 | ||
1101 | if (ref->peer_ref) { | |
1102 | if (strcmp(ref->name, ref->peer_ref->name)) { | |
1103 | if (!ref->deletion) { | |
1104 | const char *name; | |
1105 | int flag; | |
1106 | ||
1107 | /* Follow symbolic refs (mainly for HEAD). */ | |
1108 | name = resolve_ref_unsafe(ref->peer_ref->name, | |
1109 | RESOLVE_REF_READING, | |
1110 | &oid, &flag); | |
1111 | if (!name || !(flag & REF_ISSYMREF)) | |
1112 | name = ref->peer_ref->name; | |
1113 | ||
1114 | strbuf_addf(&buf, "%s:%s", name, ref->name); | |
1115 | } else | |
1116 | strbuf_addf(&buf, ":%s", ref->name); | |
1117 | ||
1118 | string_list_append(&revlist_args, "--refspec"); | |
1119 | string_list_append(&revlist_args, buf.buf); | |
1120 | strbuf_release(&buf); | |
1121 | } | |
1122 | if (!ref->deletion) | |
1123 | string_list_append(&revlist_args, ref->peer_ref->name); | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | if (get_exporter(transport, &exporter, &revlist_args)) | |
1128 | die(_("couldn't run fast-export")); | |
1129 | ||
1130 | string_list_clear(&revlist_args, 1); | |
1131 | ||
1132 | if (finish_command(&exporter)) | |
1133 | die(_("error while running fast-export")); | |
1134 | if (push_update_refs_status(data, remote_refs, flags)) | |
1135 | return 1; | |
1136 | ||
1137 | if (data->export_marks) { | |
1138 | strbuf_addf(&buf, "%s.tmp", data->export_marks); | |
1139 | rename(buf.buf, data->export_marks); | |
1140 | strbuf_release(&buf); | |
1141 | } | |
1142 | ||
1143 | return 0; | |
1144 | } | |
1145 | ||
1146 | static int push_refs(struct transport *transport, | |
1147 | struct ref *remote_refs, int flags) | |
1148 | { | |
1149 | struct helper_data *data = transport->data; | |
1150 | ||
1151 | if (process_connect(transport, 1)) | |
1152 | return transport->vtable->push_refs(transport, remote_refs, flags); | |
1153 | ||
1154 | if (!remote_refs) { | |
1155 | fprintf(stderr, | |
1156 | _("No refs in common and none specified; doing nothing.\n" | |
1157 | "Perhaps you should specify a branch.\n")); | |
1158 | return 0; | |
1159 | } | |
1160 | ||
1161 | if (data->push) | |
1162 | return push_refs_with_push(transport, remote_refs, flags); | |
1163 | ||
1164 | if (data->export) | |
1165 | return push_refs_with_export(transport, remote_refs, flags); | |
1166 | ||
1167 | return -1; | |
1168 | } | |
1169 | ||
1170 | ||
1171 | static int has_attribute(const char *attrs, const char *attr) | |
1172 | { | |
1173 | int len; | |
1174 | if (!attrs) | |
1175 | return 0; | |
1176 | ||
1177 | len = strlen(attr); | |
1178 | for (;;) { | |
1179 | const char *space = strchrnul(attrs, ' '); | |
1180 | if (len == space - attrs && !strncmp(attrs, attr, len)) | |
1181 | return 1; | |
1182 | if (!*space) | |
1183 | return 0; | |
1184 | attrs = space + 1; | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | static struct ref *get_refs_list(struct transport *transport, int for_push, | |
1189 | struct transport_ls_refs_options *transport_options) | |
1190 | { | |
1191 | get_helper(transport); | |
1192 | ||
1193 | if (process_connect(transport, for_push)) | |
1194 | return transport->vtable->get_refs_list(transport, for_push, | |
1195 | transport_options); | |
1196 | ||
1197 | return get_refs_list_using_list(transport, for_push); | |
1198 | } | |
1199 | ||
1200 | static struct ref *get_refs_list_using_list(struct transport *transport, | |
1201 | int for_push) | |
1202 | { | |
1203 | struct helper_data *data = transport->data; | |
1204 | struct child_process *helper; | |
1205 | struct ref *ret = NULL; | |
1206 | struct ref **tail = &ret; | |
1207 | struct ref *posn; | |
1208 | struct strbuf buf = STRBUF_INIT; | |
1209 | ||
1210 | data->get_refs_list_called = 1; | |
1211 | helper = get_helper(transport); | |
1212 | ||
1213 | if (data->object_format) { | |
1214 | write_str_in_full(helper->in, "option object-format\n"); | |
1215 | if (recvline(data, &buf) || strcmp(buf.buf, "ok")) | |
1216 | exit(128); | |
1217 | } | |
1218 | ||
1219 | if (data->push && for_push) | |
1220 | write_str_in_full(helper->in, "list for-push\n"); | |
1221 | else | |
1222 | write_str_in_full(helper->in, "list\n"); | |
1223 | ||
1224 | while (1) { | |
1225 | char *eov, *eon; | |
1226 | if (recvline(data, &buf)) | |
1227 | exit(128); | |
1228 | ||
1229 | if (!*buf.buf) | |
1230 | break; | |
1231 | else if (buf.buf[0] == ':') { | |
1232 | const char *value; | |
1233 | if (skip_prefix(buf.buf, ":object-format ", &value)) { | |
1234 | int algo = hash_algo_by_name(value); | |
1235 | if (algo == GIT_HASH_UNKNOWN) | |
1236 | die(_("unsupported object format '%s'"), | |
1237 | value); | |
1238 | transport->hash_algo = &hash_algos[algo]; | |
1239 | } | |
1240 | continue; | |
1241 | } | |
1242 | ||
1243 | eov = strchr(buf.buf, ' '); | |
1244 | if (!eov) | |
1245 | die(_("malformed response in ref list: %s"), buf.buf); | |
1246 | eon = strchr(eov + 1, ' '); | |
1247 | *eov = '\0'; | |
1248 | if (eon) | |
1249 | *eon = '\0'; | |
1250 | *tail = alloc_ref(eov + 1); | |
1251 | if (buf.buf[0] == '@') | |
1252 | (*tail)->symref = xstrdup(buf.buf + 1); | |
1253 | else if (buf.buf[0] != '?') | |
1254 | get_oid_hex_algop(buf.buf, &(*tail)->old_oid, transport->hash_algo); | |
1255 | if (eon) { | |
1256 | if (has_attribute(eon + 1, "unchanged")) { | |
1257 | (*tail)->status |= REF_STATUS_UPTODATE; | |
1258 | if (read_ref((*tail)->name, &(*tail)->old_oid) < 0) | |
1259 | die(_("could not read ref %s"), | |
1260 | (*tail)->name); | |
1261 | } | |
1262 | } | |
1263 | tail = &((*tail)->next); | |
1264 | } | |
1265 | if (debug) | |
1266 | fprintf(stderr, "Debug: Read ref listing.\n"); | |
1267 | strbuf_release(&buf); | |
1268 | ||
1269 | for (posn = ret; posn; posn = posn->next) | |
1270 | resolve_remote_symref(posn, ret); | |
1271 | ||
1272 | return ret; | |
1273 | } | |
1274 | ||
1275 | static int get_bundle_uri(struct transport *transport) | |
1276 | { | |
1277 | get_helper(transport); | |
1278 | ||
1279 | if (process_connect(transport, 0)) | |
1280 | return transport->vtable->get_bundle_uri(transport); | |
1281 | ||
1282 | return -1; | |
1283 | } | |
1284 | ||
1285 | static struct transport_vtable vtable = { | |
1286 | .set_option = set_helper_option, | |
1287 | .get_refs_list = get_refs_list, | |
1288 | .get_bundle_uri = get_bundle_uri, | |
1289 | .fetch_refs = fetch_refs, | |
1290 | .push_refs = push_refs, | |
1291 | .connect = connect_helper, | |
1292 | .disconnect = release_helper | |
1293 | }; | |
1294 | ||
1295 | int transport_helper_init(struct transport *transport, const char *name) | |
1296 | { | |
1297 | struct helper_data *data = xcalloc(1, sizeof(*data)); | |
1298 | data->name = name; | |
1299 | ||
1300 | transport_check_allowed(name); | |
1301 | ||
1302 | if (getenv("GIT_TRANSPORT_HELPER_DEBUG")) | |
1303 | debug = 1; | |
1304 | ||
1305 | list_objects_filter_init(&data->transport_options.filter_options); | |
1306 | ||
1307 | transport->data = data; | |
1308 | transport->vtable = &vtable; | |
1309 | transport->smart_options = &(data->transport_options); | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | /* | |
1314 | * Linux pipes can buffer 65536 bytes at once (and most platforms can | |
1315 | * buffer less), so attempt reads and writes with up to that size. | |
1316 | */ | |
1317 | #define BUFFERSIZE 65536 | |
1318 | /* This should be enough to hold debugging message. */ | |
1319 | #define PBUFFERSIZE 8192 | |
1320 | ||
1321 | /* Print bidirectional transfer loop debug message. */ | |
1322 | __attribute__((format (printf, 1, 2))) | |
1323 | static void transfer_debug(const char *fmt, ...) | |
1324 | { | |
1325 | /* | |
1326 | * NEEDSWORK: This function is sometimes used from multiple threads, and | |
1327 | * we end up using debug_enabled racily. That "should not matter" since | |
1328 | * we always write the same value, but it's still wrong. This function | |
1329 | * is listed in .tsan-suppressions for the time being. | |
1330 | */ | |
1331 | ||
1332 | va_list args; | |
1333 | char msgbuf[PBUFFERSIZE]; | |
1334 | static int debug_enabled = -1; | |
1335 | ||
1336 | if (debug_enabled < 0) | |
1337 | debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0; | |
1338 | if (!debug_enabled) | |
1339 | return; | |
1340 | ||
1341 | va_start(args, fmt); | |
1342 | vsnprintf(msgbuf, PBUFFERSIZE, fmt, args); | |
1343 | va_end(args); | |
1344 | fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf); | |
1345 | } | |
1346 | ||
1347 | /* Stream state: More data may be coming in this direction. */ | |
1348 | #define SSTATE_TRANSFERRING 0 | |
1349 | /* | |
1350 | * Stream state: No more data coming in this direction, flushing rest of | |
1351 | * data. | |
1352 | */ | |
1353 | #define SSTATE_FLUSHING 1 | |
1354 | /* Stream state: Transfer in this direction finished. */ | |
1355 | #define SSTATE_FINISHED 2 | |
1356 | ||
1357 | #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING) | |
1358 | #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING) | |
1359 | #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING) | |
1360 | ||
1361 | /* Unidirectional transfer. */ | |
1362 | struct unidirectional_transfer { | |
1363 | /* Source */ | |
1364 | int src; | |
1365 | /* Destination */ | |
1366 | int dest; | |
1367 | /* Is source socket? */ | |
1368 | int src_is_sock; | |
1369 | /* Is destination socket? */ | |
1370 | int dest_is_sock; | |
1371 | /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */ | |
1372 | int state; | |
1373 | /* Buffer. */ | |
1374 | char buf[BUFFERSIZE]; | |
1375 | /* Buffer used. */ | |
1376 | size_t bufuse; | |
1377 | /* Name of source. */ | |
1378 | const char *src_name; | |
1379 | /* Name of destination. */ | |
1380 | const char *dest_name; | |
1381 | }; | |
1382 | ||
1383 | /* Closes the target (for writing) if transfer has finished. */ | |
1384 | static void udt_close_if_finished(struct unidirectional_transfer *t) | |
1385 | { | |
1386 | if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) { | |
1387 | t->state = SSTATE_FINISHED; | |
1388 | if (t->dest_is_sock) | |
1389 | shutdown(t->dest, SHUT_WR); | |
1390 | else | |
1391 | close(t->dest); | |
1392 | transfer_debug("Closed %s.", t->dest_name); | |
1393 | } | |
1394 | } | |
1395 | ||
1396 | /* | |
1397 | * Tries to read data from source into buffer. If buffer is full, | |
1398 | * no data is read. Returns 0 on success, -1 on error. | |
1399 | */ | |
1400 | static int udt_do_read(struct unidirectional_transfer *t) | |
1401 | { | |
1402 | ssize_t bytes; | |
1403 | ||
1404 | if (t->bufuse == BUFFERSIZE) | |
1405 | return 0; /* No space for more. */ | |
1406 | ||
1407 | transfer_debug("%s is readable", t->src_name); | |
1408 | bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse); | |
1409 | if (bytes < 0) { | |
1410 | error_errno(_("read(%s) failed"), t->src_name); | |
1411 | return -1; | |
1412 | } else if (bytes == 0) { | |
1413 | transfer_debug("%s EOF (with %i bytes in buffer)", | |
1414 | t->src_name, (int)t->bufuse); | |
1415 | t->state = SSTATE_FLUSHING; | |
1416 | } else if (bytes > 0) { | |
1417 | t->bufuse += bytes; | |
1418 | transfer_debug("Read %i bytes from %s (buffer now at %i)", | |
1419 | (int)bytes, t->src_name, (int)t->bufuse); | |
1420 | } | |
1421 | return 0; | |
1422 | } | |
1423 | ||
1424 | /* Tries to write data from buffer into destination. If buffer is empty, | |
1425 | * no data is written. Returns 0 on success, -1 on error. | |
1426 | */ | |
1427 | static int udt_do_write(struct unidirectional_transfer *t) | |
1428 | { | |
1429 | ssize_t bytes; | |
1430 | ||
1431 | if (t->bufuse == 0) | |
1432 | return 0; /* Nothing to write. */ | |
1433 | ||
1434 | transfer_debug("%s is writable", t->dest_name); | |
1435 | bytes = xwrite(t->dest, t->buf, t->bufuse); | |
1436 | if (bytes < 0) { | |
1437 | error_errno(_("write(%s) failed"), t->dest_name); | |
1438 | return -1; | |
1439 | } else if (bytes > 0) { | |
1440 | t->bufuse -= bytes; | |
1441 | if (t->bufuse) | |
1442 | memmove(t->buf, t->buf + bytes, t->bufuse); | |
1443 | transfer_debug("Wrote %i bytes to %s (buffer now at %i)", | |
1444 | (int)bytes, t->dest_name, (int)t->bufuse); | |
1445 | } | |
1446 | return 0; | |
1447 | } | |
1448 | ||
1449 | ||
1450 | /* State of bidirectional transfer loop. */ | |
1451 | struct bidirectional_transfer_state { | |
1452 | /* Direction from program to git. */ | |
1453 | struct unidirectional_transfer ptg; | |
1454 | /* Direction from git to program. */ | |
1455 | struct unidirectional_transfer gtp; | |
1456 | }; | |
1457 | ||
1458 | static void *udt_copy_task_routine(void *udt) | |
1459 | { | |
1460 | struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt; | |
1461 | while (t->state != SSTATE_FINISHED) { | |
1462 | if (STATE_NEEDS_READING(t->state)) | |
1463 | if (udt_do_read(t)) | |
1464 | return NULL; | |
1465 | if (STATE_NEEDS_WRITING(t->state)) | |
1466 | if (udt_do_write(t)) | |
1467 | return NULL; | |
1468 | if (STATE_NEEDS_CLOSING(t->state)) | |
1469 | udt_close_if_finished(t); | |
1470 | } | |
1471 | return udt; /* Just some non-NULL value. */ | |
1472 | } | |
1473 | ||
1474 | #ifndef NO_PTHREADS | |
1475 | ||
1476 | /* | |
1477 | * Join thread, with appropriate errors on failure. Name is name for the | |
1478 | * thread (for error messages). Returns 0 on success, 1 on failure. | |
1479 | */ | |
1480 | static int tloop_join(pthread_t thread, const char *name) | |
1481 | { | |
1482 | int err; | |
1483 | void *tret; | |
1484 | err = pthread_join(thread, &tret); | |
1485 | if (!tret) { | |
1486 | error(_("%s thread failed"), name); | |
1487 | return 1; | |
1488 | } | |
1489 | if (err) { | |
1490 | error(_("%s thread failed to join: %s"), name, strerror(err)); | |
1491 | return 1; | |
1492 | } | |
1493 | return 0; | |
1494 | } | |
1495 | ||
1496 | /* | |
1497 | * Spawn the transfer tasks and then wait for them. Returns 0 on success, | |
1498 | * -1 on failure. | |
1499 | */ | |
1500 | static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) | |
1501 | { | |
1502 | pthread_t gtp_thread; | |
1503 | pthread_t ptg_thread; | |
1504 | int err; | |
1505 | int ret = 0; | |
1506 | err = pthread_create(>p_thread, NULL, udt_copy_task_routine, | |
1507 | &s->gtp); | |
1508 | if (err) | |
1509 | die(_("can't start thread for copying data: %s"), strerror(err)); | |
1510 | err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine, | |
1511 | &s->ptg); | |
1512 | if (err) | |
1513 | die(_("can't start thread for copying data: %s"), strerror(err)); | |
1514 | ||
1515 | ret |= tloop_join(gtp_thread, "Git to program copy"); | |
1516 | ret |= tloop_join(ptg_thread, "Program to git copy"); | |
1517 | return ret; | |
1518 | } | |
1519 | #else | |
1520 | ||
1521 | /* Close the source and target (for writing) for transfer. */ | |
1522 | static void udt_kill_transfer(struct unidirectional_transfer *t) | |
1523 | { | |
1524 | t->state = SSTATE_FINISHED; | |
1525 | /* | |
1526 | * Socket read end left open isn't a disaster if nobody | |
1527 | * attempts to read from it (mingw compat headers do not | |
1528 | * have SHUT_RD)... | |
1529 | * | |
1530 | * We can't fully close the socket since otherwise gtp | |
1531 | * task would first close the socket it sends data to | |
1532 | * while closing the ptg file descriptors. | |
1533 | */ | |
1534 | if (!t->src_is_sock) | |
1535 | close(t->src); | |
1536 | if (t->dest_is_sock) | |
1537 | shutdown(t->dest, SHUT_WR); | |
1538 | else | |
1539 | close(t->dest); | |
1540 | } | |
1541 | ||
1542 | /* | |
1543 | * Join process, with appropriate errors on failure. Name is name for the | |
1544 | * process (for error messages). Returns 0 on success, 1 on failure. | |
1545 | */ | |
1546 | static int tloop_join(pid_t pid, const char *name) | |
1547 | { | |
1548 | int tret; | |
1549 | if (waitpid(pid, &tret, 0) < 0) { | |
1550 | error_errno(_("%s process failed to wait"), name); | |
1551 | return 1; | |
1552 | } | |
1553 | if (!WIFEXITED(tret) || WEXITSTATUS(tret)) { | |
1554 | error(_("%s process failed"), name); | |
1555 | return 1; | |
1556 | } | |
1557 | return 0; | |
1558 | } | |
1559 | ||
1560 | /* | |
1561 | * Spawn the transfer tasks and then wait for them. Returns 0 on success, | |
1562 | * -1 on failure. | |
1563 | */ | |
1564 | static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) | |
1565 | { | |
1566 | pid_t pid1, pid2; | |
1567 | int ret = 0; | |
1568 | ||
1569 | /* Fork thread #1: git to program. */ | |
1570 | pid1 = fork(); | |
1571 | if (pid1 < 0) | |
1572 | die_errno(_("can't start thread for copying data")); | |
1573 | else if (pid1 == 0) { | |
1574 | udt_kill_transfer(&s->ptg); | |
1575 | exit(udt_copy_task_routine(&s->gtp) ? 0 : 1); | |
1576 | } | |
1577 | ||
1578 | /* Fork thread #2: program to git. */ | |
1579 | pid2 = fork(); | |
1580 | if (pid2 < 0) | |
1581 | die_errno(_("can't start thread for copying data")); | |
1582 | else if (pid2 == 0) { | |
1583 | udt_kill_transfer(&s->gtp); | |
1584 | exit(udt_copy_task_routine(&s->ptg) ? 0 : 1); | |
1585 | } | |
1586 | ||
1587 | /* | |
1588 | * Close both streams in parent as to not interfere with | |
1589 | * end of file detection and wait for both tasks to finish. | |
1590 | */ | |
1591 | udt_kill_transfer(&s->gtp); | |
1592 | udt_kill_transfer(&s->ptg); | |
1593 | ret |= tloop_join(pid1, "Git to program copy"); | |
1594 | ret |= tloop_join(pid2, "Program to git copy"); | |
1595 | return ret; | |
1596 | } | |
1597 | #endif | |
1598 | ||
1599 | /* | |
1600 | * Copies data from stdin to output and from input to stdout simultaneously. | |
1601 | * Additionally filtering through given filter. If filter is NULL, uses | |
1602 | * identity filter. | |
1603 | */ | |
1604 | int bidirectional_transfer_loop(int input, int output) | |
1605 | { | |
1606 | struct bidirectional_transfer_state state; | |
1607 | ||
1608 | /* Fill the state fields. */ | |
1609 | state.ptg.src = input; | |
1610 | state.ptg.dest = 1; | |
1611 | state.ptg.src_is_sock = (input == output); | |
1612 | state.ptg.dest_is_sock = 0; | |
1613 | state.ptg.state = SSTATE_TRANSFERRING; | |
1614 | state.ptg.bufuse = 0; | |
1615 | state.ptg.src_name = "remote input"; | |
1616 | state.ptg.dest_name = "stdout"; | |
1617 | ||
1618 | state.gtp.src = 0; | |
1619 | state.gtp.dest = output; | |
1620 | state.gtp.src_is_sock = 0; | |
1621 | state.gtp.dest_is_sock = (input == output); | |
1622 | state.gtp.state = SSTATE_TRANSFERRING; | |
1623 | state.gtp.bufuse = 0; | |
1624 | state.gtp.src_name = "stdin"; | |
1625 | state.gtp.dest_name = "remote output"; | |
1626 | ||
1627 | return tloop_spawnwait_tasks(&state); | |
1628 | } | |
1629 | ||
1630 | void reject_atomic_push(struct ref *remote_refs, int mirror_mode) | |
1631 | { | |
1632 | struct ref *ref; | |
1633 | ||
1634 | /* Mark other refs as failed */ | |
1635 | for (ref = remote_refs; ref; ref = ref->next) { | |
1636 | if (!ref->peer_ref && !mirror_mode) | |
1637 | continue; | |
1638 | ||
1639 | switch (ref->status) { | |
1640 | case REF_STATUS_NONE: | |
1641 | case REF_STATUS_OK: | |
1642 | case REF_STATUS_EXPECTING_REPORT: | |
1643 | ref->status = REF_STATUS_ATOMIC_PUSH_FAILED; | |
1644 | continue; | |
1645 | default: | |
1646 | break; /* do nothing */ | |
1647 | } | |
1648 | } | |
1649 | return; | |
1650 | } |