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