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