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