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