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