]> git.ipfire.org Git - thirdparty/git.git/blame - transport-helper.c
Merge branch 'jt/t5500-unflake'
[thirdparty/git.git] / transport-helper.c
CommitLineData
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
18static int debug;
19
9cba13ca 20struct 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
56static 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 64static 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
80static int recvline(struct helper_data *helper, struct strbuf *buffer)
81{
b1c2edfc 82 return recvline_fh(helper->out, buffer);
fa8c097c
IL
83}
84
bf3c523c
IL
85static 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 93static 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
103static 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
112static void standard_options(struct transport *t);
113
6eb996b5
DB
114static 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
226static 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
254static 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
261static 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
268static 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
290static 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
310static 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
350static 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
376static 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 386static 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) {
145136a9
JH
407 const char *name;
408
5931b33e
FC
409 if (recvline(data, &buf))
410 exit(128);
292ce46b 411
145136a9 412 if (skip_prefix(buf.buf, "lock ", &name)) {
292ce46b 413 if (transport->pack_lockfile)
6b5b309f 414 warning(_("%s also locked %s"), data->name, name);
292ce46b
SP
415 else
416 transport->pack_lockfile = xstrdup(name);
417 }
9ba38048
NTND
418 else if (data->check_connectivity &&
419 data->transport_options.check_self_contained_and_connected &&
420 !strcmp(buf.buf, "connectivity-ok"))
421 data->transport_options.self_contained_and_connected = 1;
292ce46b
SP
422 else if (!buf.len)
423 break;
424 else
6b5b309f 425 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
6eb996b5 426 }
292ce46b 427 strbuf_release(&buf);
6eb996b5
DB
428 return 0;
429}
430
e65e91ed
DB
431static int get_importer(struct transport *transport, struct child_process *fastimport)
432{
433 struct child_process *helper = get_helper(transport);
bfc366d9 434 struct helper_data *data = transport->data;
bfc366d9 435 int cat_blob_fd, code;
483bbd4e 436 child_process_init(fastimport);
8b355427 437 fastimport->in = xdup(helper->out);
173fd1a1 438 argv_array_push(&fastimport->args, "fast-import");
68061e34 439 argv_array_push(&fastimport->args, "--allow-unsafe-features");
173fd1a1 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
452static 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
481static 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 492 if (get_importer(transport, &fastimport))
6b5b309f 493 die(_("couldn't run fast-import"));
e65e91ed
DB
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 515 if (finish_command(&fastimport))
6b5b309f 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;
57f32ac2 539 if (data->rs.nr)
d000414e 540 private = apply_refspecs(&data->rs, name);
72ff8943 541 else
33cae542 542 private = xstrdup(name);
d51b720f 543 if (private) {
34c290a6 544 if (read_ref(private, &posn->old_oid) < 0)
6b5b309f 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 553static 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)
6b5b309f 571 die_errno(_("can't dup helper output fd"));
fa8c097c
IL
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 {
739fb716 590 die(_("unknown response to connect: %s"),
6b5b309f 591 cmdbuf->buf);
176e85c1
BW
592 }
593
594 fclose(input);
595 return ret;
596}
597
598static 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 611 if (r > 0)
6b5b309f 612 warning(_("setting remote service path not supported by protocol"));
fa8c097c 613 else if (r < 0)
6b5b309f 614 warning(_("invalid remote service path"));
fa8c097c
IL
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
633static 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
649static 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)
6b5b309f 657 die(_("operation not supported by protocol"));
b236752a
IL
658
659 if (!process_connect_service(transport, name, exec))
6b5b309f 660 die(_("can't connect to subservice %s"), name);
b236752a
IL
661
662 fd[0] = data->helper->out;
663 fd[1] = data->helper->in;
664 return 0;
665}
666
ac3fda82
JT
667static struct ref *get_refs_list_using_list(struct transport *transport,
668 int for_push);
669
6eb996b5 670static int fetch(struct transport *transport,
e2842b39 671 int nr_heads, struct ref **to_fetch)
6eb996b5
DB
672{
673 struct helper_data *data = transport->data;
674 int i, count;
675
ac3fda82
JT
676 get_helper(transport);
677
fa8c097c
IL
678 if (process_connect(transport, 0)) {
679 do_take_over(transport);
e2842b39 680 return transport->vtable->fetch(transport, nr_heads, to_fetch);
fa8c097c
IL
681 }
682
ac3fda82
JT
683 if (!data->get_refs_list_called)
684 get_refs_list_using_list(transport, 0);
685
6eb996b5
DB
686 count = 0;
687 for (i = 0; i < nr_heads; i++)
688 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
689 count++;
690
691 if (!count)
692 return 0;
693
aab1beb0
MH
694 if (data->check_connectivity &&
695 data->transport_options.check_self_contained_and_connected)
696 set_helper_option(transport, "check-connectivity", "true");
697
698 if (transport->cloning)
699 set_helper_option(transport, "cloning", "true");
700
701 if (data->transport_options.update_shallow)
702 set_helper_option(transport, "update-shallow", "true");
703
87c2d9d3 704 if (data->transport_options.filter_options.choice) {
cf9ceb5a
MD
705 const char *spec = expand_list_objects_filter_spec(
706 &data->transport_options.filter_options);
707 set_helper_option(transport, "filter", spec);
87c2d9d3 708 }
640d8b72 709
3390e42a
JT
710 if (data->transport_options.negotiation_tips)
711 warning("Ignoring --negotiation-tip because the protocol does not support it.");
712
6eb996b5
DB
713 if (data->fetch)
714 return fetch_with_fetch(transport, nr_heads, to_fetch);
715
e65e91ed
DB
716 if (data->import)
717 return fetch_with_import(transport, nr_heads, to_fetch);
718
6eb996b5
DB
719 return -1;
720}
721
664059fb 722static int push_update_ref_status(struct strbuf *buf,
d2e73c6f
SR
723 struct ref **ref,
724 struct ref *remote_refs)
725{
726 char *refname, *msg;
f9e3c6be 727 int status, forced = 0;
d2e73c6f 728
59556548 729 if (starts_with(buf->buf, "ok ")) {
d2e73c6f
SR
730 status = REF_STATUS_OK;
731 refname = buf->buf + 3;
59556548 732 } else if (starts_with(buf->buf, "error ")) {
d2e73c6f
SR
733 status = REF_STATUS_REMOTE_REJECT;
734 refname = buf->buf + 6;
735 } else
6b5b309f 736 die(_("expected ok/error, helper said '%s'"), buf->buf);
d2e73c6f
SR
737
738 msg = strchr(refname, ' ');
739 if (msg) {
740 struct strbuf msg_buf = STRBUF_INIT;
741 const char *end;
742
743 *msg++ = '\0';
744 if (!unquote_c_style(&msg_buf, msg, &end))
745 msg = strbuf_detach(&msg_buf, NULL);
746 else
747 msg = xstrdup(msg);
748 strbuf_release(&msg_buf);
749
750 if (!strcmp(msg, "no match")) {
751 status = REF_STATUS_NONE;
6a83d902 752 FREE_AND_NULL(msg);
d2e73c6f
SR
753 }
754 else if (!strcmp(msg, "up to date")) {
755 status = REF_STATUS_UPTODATE;
6a83d902 756 FREE_AND_NULL(msg);
d2e73c6f
SR
757 }
758 else if (!strcmp(msg, "non-fast forward")) {
759 status = REF_STATUS_REJECT_NONFASTFORWARD;
6a83d902 760 FREE_AND_NULL(msg);
d2e73c6f 761 }
dbfeddb1
CR
762 else if (!strcmp(msg, "already exists")) {
763 status = REF_STATUS_REJECT_ALREADY_EXISTS;
6a83d902 764 FREE_AND_NULL(msg);
dbfeddb1 765 }
75e5c0dc
JH
766 else if (!strcmp(msg, "fetch first")) {
767 status = REF_STATUS_REJECT_FETCH_FIRST;
6a83d902 768 FREE_AND_NULL(msg);
75e5c0dc
JH
769 }
770 else if (!strcmp(msg, "needs force")) {
771 status = REF_STATUS_REJECT_NEEDS_FORCE;
6a83d902 772 FREE_AND_NULL(msg);
75e5c0dc 773 }
631b5ef2
JH
774 else if (!strcmp(msg, "stale info")) {
775 status = REF_STATUS_REJECT_STALE;
6a83d902 776 FREE_AND_NULL(msg);
631b5ef2 777 }
f9e3c6be
FC
778 else if (!strcmp(msg, "forced update")) {
779 forced = 1;
6a83d902 780 FREE_AND_NULL(msg);
f9e3c6be 781 }
d2e73c6f
SR
782 }
783
784 if (*ref)
785 *ref = find_ref_by_name(*ref, refname);
786 if (!*ref)
787 *ref = find_ref_by_name(remote_refs, refname);
788 if (!*ref) {
6b5b309f 789 warning(_("helper reported unexpected status of %s"), refname);
664059fb 790 return 1;
d2e73c6f
SR
791 }
792
793 if ((*ref)->status != REF_STATUS_NONE) {
794 /*
795 * Earlier, the ref was marked not to be pushed, so ignore the ref
796 * status reported by the remote helper if the latter is 'no match'.
797 */
798 if (status == REF_STATUS_NONE)
664059fb 799 return 1;
d2e73c6f
SR
800 }
801
802 (*ref)->status = status;
cf31f70c 803 (*ref)->forced_update |= forced;
d2e73c6f 804 (*ref)->remote_status = msg;
126aac5c 805 return !(status == REF_STATUS_OK);
d2e73c6f
SR
806}
807
0551a06c 808static int push_update_refs_status(struct helper_data *data,
5a75353f
FC
809 struct ref *remote_refs,
810 int flags)
d2e73c6f
SR
811{
812 struct strbuf buf = STRBUF_INIT;
813 struct ref *ref = remote_refs;
0551a06c
FC
814 int ret = 0;
815
d2e73c6f 816 for (;;) {
664059fb
FC
817 char *private;
818
0551a06c
FC
819 if (recvline(data, &buf)) {
820 ret = 1;
821 break;
822 }
823
d2e73c6f
SR
824 if (!buf.len)
825 break;
826
664059fb
FC
827 if (push_update_ref_status(&buf, &ref, remote_refs))
828 continue;
829
57f32ac2 830 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
664059fb
FC
831 continue;
832
833 /* propagate back the update to the remote namespace */
d000414e 834 private = apply_refspecs(&data->rs, ref->name);
664059fb
FC
835 if (!private)
836 continue;
ae077771 837 update_ref("update by helper", private, &ref->new_oid, NULL,
838 0, 0);
664059fb 839 free(private);
d2e73c6f
SR
840 }
841 strbuf_release(&buf);
0551a06c 842 return ret;
d2e73c6f
SR
843}
844
30261094
DB
845static void set_common_push_options(struct transport *transport,
846 const char *name, int flags)
847{
848 if (flags & TRANSPORT_PUSH_DRY_RUN) {
849 if (set_helper_option(transport, "dry-run", "true") != 0)
6b5b309f 850 die(_("helper %s does not support dry-run"), name);
30261094
DB
851 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
852 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
6b5b309f 853 die(_("helper %s does not support --signed"), name);
30261094
DB
854 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
855 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
6b5b309f 856 die(_("helper %s does not support --signed=if-asked"), name);
30261094 857 }
438fc684 858
6f119424 859 if (flags & TRANSPORT_PUSH_ATOMIC)
860 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
861 die(_("helper %s does not support --atomic"), name);
862
438fc684
SB
863 if (flags & TRANSPORT_PUSH_OPTIONS) {
864 struct string_list_item *item;
865 for_each_string_list_item(item, transport->push_options)
866 if (set_helper_option(transport, "push-option", item->string) != 0)
6b5b309f 867 die(_("helper %s does not support 'push-option'"), name);
438fc684 868 }
30261094
DB
869}
870
73b49a75 871static int push_refs_with_push(struct transport *transport,
05c1eb10 872 struct ref *remote_refs, int flags)
ae4efe19
SP
873{
874 int force_all = flags & TRANSPORT_PUSH_FORCE;
875 int mirror = flags & TRANSPORT_PUSH_MIRROR;
3bca1e7f 876 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
ae4efe19
SP
877 struct helper_data *data = transport->data;
878 struct strbuf buf = STRBUF_INIT;
ae4efe19 879 struct ref *ref;
05c1eb10
JH
880 struct string_list cas_options = STRING_LIST_INIT_DUP;
881 struct string_list_item *cas_option;
ae4efe19 882
c0aa335c 883 get_helper(transport);
ae4efe19
SP
884 if (!data->push)
885 return 1;
886
887 for (ref = remote_refs; ref; ref = ref->next) {
20e8b465 888 if (!ref->peer_ref && !mirror)
ae4efe19
SP
889 continue;
890
20e8b465
TRC
891 /* Check for statuses set by set_ref_status_for_push() */
892 switch (ref->status) {
893 case REF_STATUS_REJECT_NONFASTFORWARD:
631b5ef2 894 case REF_STATUS_REJECT_STALE:
dbfeddb1 895 case REF_STATUS_REJECT_ALREADY_EXISTS:
3bca1e7f 896 if (atomic) {
dfe1b7f1 897 reject_atomic_push(remote_refs, mirror);
3bca1e7f
ES
898 string_list_clear(&cas_options, 0);
899 return 0;
900 } else
901 continue;
20e8b465 902 case REF_STATUS_UPTODATE:
ae4efe19 903 continue;
20e8b465
TRC
904 default:
905 ; /* do nothing */
ae4efe19
SP
906 }
907
908 if (force_all)
909 ref->force = 1;
910
911 strbuf_addstr(&buf, "push ");
912 if (!ref->deletion) {
913 if (ref->force)
914 strbuf_addch(&buf, '+');
915 if (ref->peer_ref)
916 strbuf_addstr(&buf, ref->peer_ref->name);
917 else
f4e54d02 918 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
ae4efe19
SP
919 }
920 strbuf_addch(&buf, ':');
921 strbuf_addstr(&buf, ref->name);
922 strbuf_addch(&buf, '\n');
05c1eb10
JH
923
924 /*
925 * The "--force-with-lease" options without explicit
926 * values to expect have already been expanded into
f4e54d02 927 * the ref->old_oid_expect[] field; we can ignore
05c1eb10
JH
928 * transport->smart_options->cas altogether and instead
929 * can enumerate them from the refs.
930 */
931 if (ref->expect_old_sha1) {
932 struct strbuf cas = STRBUF_INIT;
933 strbuf_addf(&cas, "%s:%s",
f4e54d02 934 ref->name, oid_to_hex(&ref->old_oid_expect));
176cb979
RS
935 string_list_append_nodup(&cas_options,
936 strbuf_detach(&cas, NULL));
05c1eb10 937 }
ae4efe19 938 }
05c1eb10
JH
939 if (buf.len == 0) {
940 string_list_clear(&cas_options, 0);
d8f67d20 941 return 0;
05c1eb10 942 }
ae4efe19 943
05c1eb10
JH
944 for_each_string_list_item(cas_option, &cas_options)
945 set_helper_option(transport, "cas", cas_option->string);
30261094 946 set_common_push_options(transport, data->name, flags);
ae4efe19
SP
947
948 strbuf_addch(&buf, '\n');
bf3c523c 949 sendline(data, &buf);
ae4efe19 950 strbuf_release(&buf);
176cb979 951 string_list_clear(&cas_options, 0);
d2e73c6f 952
0551a06c 953 return push_update_refs_status(data, remote_refs, flags);
ae4efe19
SP
954}
955
73b49a75
SR
956static int push_refs_with_export(struct transport *transport,
957 struct ref *remote_refs, int flags)
958{
959 struct ref *ref;
960 struct child_process *helper, exporter;
961 struct helper_data *data = transport->data;
d98c8153 962 struct string_list revlist_args = STRING_LIST_INIT_DUP;
73b49a75
SR
963 struct strbuf buf = STRBUF_INIT;
964
57f32ac2 965 if (!data->rs.nr)
6b5b309f 966 die(_("remote-helper doesn't support push; refspec needed"));
21610d82 967
30261094 968 set_common_push_options(transport, data->name, flags);
510fa6f5
FC
969 if (flags & TRANSPORT_PUSH_FORCE) {
970 if (set_helper_option(transport, "force", "true") != 0)
6b5b309f 971 warning(_("helper %s does not support 'force'"), data->name);
510fa6f5
FC
972 }
973
73b49a75
SR
974 helper = get_helper(transport);
975
976 write_constant(helper->in, "export\n");
977
73b49a75
SR
978 for (ref = remote_refs; ref; ref = ref->next) {
979 char *private;
27912a03 980 struct object_id oid;
73b49a75 981
d000414e 982 private = apply_refspecs(&data->rs, ref->name);
e82caf38 983 if (private && !get_oid(private, &oid)) {
73b49a75 984 strbuf_addf(&buf, "^%s", private);
176cb979
RS
985 string_list_append_nodup(&revlist_args,
986 strbuf_detach(&buf, NULL));
27912a03 987 oidcpy(&ref->old_oid, &oid);
73b49a75 988 }
2faa1527 989 free(private);
73b49a75 990
67c9c782 991 if (ref->peer_ref) {
d98c8153 992 if (strcmp(ref->name, ref->peer_ref->name)) {
f3d03763
FC
993 if (!ref->deletion) {
994 const char *name;
995 int flag;
996
997 /* Follow symbolic refs (mainly for HEAD). */
49e61479 998 name = resolve_ref_unsafe(ref->peer_ref->name,
999 RESOLVE_REF_READING,
1000 &oid, &flag);
f3d03763
FC
1001 if (!name || !(flag & REF_ISSYMREF))
1002 name = ref->peer_ref->name;
9193f742 1003
f3d03763
FC
1004 strbuf_addf(&buf, "%s:%s", name, ref->name);
1005 } else
1006 strbuf_addf(&buf, ":%s", ref->name);
9193f742 1007
d98c8153
FC
1008 string_list_append(&revlist_args, "--refspec");
1009 string_list_append(&revlist_args, buf.buf);
1010 strbuf_release(&buf);
1011 }
f3d03763
FC
1012 if (!ref->deletion)
1013 string_list_append(&revlist_args, ref->peer_ref->name);
67c9c782 1014 }
73b49a75
SR
1015 }
1016
a515ebe9 1017 if (get_exporter(transport, &exporter, &revlist_args))
6b5b309f 1018 die(_("couldn't run fast-export"));
73b49a75 1019
d98c8153
FC
1020 string_list_clear(&revlist_args, 1);
1021
cc567322 1022 if (finish_command(&exporter))
6b5b309f 1023 die(_("error while running fast-export"));
3994e64d
FC
1024 if (push_update_refs_status(data, remote_refs, flags))
1025 return 1;
1026
1027 if (data->export_marks) {
1028 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1029 rename(buf.buf, data->export_marks);
1030 strbuf_release(&buf);
1031 }
1032
1033 return 0;
73b49a75
SR
1034}
1035
1036static int push_refs(struct transport *transport,
1037 struct ref *remote_refs, int flags)
1038{
1039 struct helper_data *data = transport->data;
1040
1041 if (process_connect(transport, 1)) {
1042 do_take_over(transport);
e967ca38 1043 return transport->vtable->push_refs(transport, remote_refs, flags);
73b49a75
SR
1044 }
1045
1046 if (!remote_refs) {
6b5b309f
NTND
1047 fprintf(stderr,
1048 _("No refs in common and none specified; doing nothing.\n"
1049 "Perhaps you should specify a branch such as 'master'.\n"));
73b49a75
SR
1050 return 0;
1051 }
1052
1053 if (data->push)
1054 return push_refs_with_push(transport, remote_refs, flags);
1055
1056 if (data->export)
1057 return push_refs_with_export(transport, remote_refs, flags);
1058
1059 return -1;
1060}
1061
1062
3b335762
NTND
1063static int has_attribute(const char *attrs, const char *attr)
1064{
f8ec9167
DB
1065 int len;
1066 if (!attrs)
1067 return 0;
1068
1069 len = strlen(attr);
1070 for (;;) {
1071 const char *space = strchrnul(attrs, ' ');
1072 if (len == space - attrs && !strncmp(attrs, attr, len))
1073 return 1;
1074 if (!*space)
1075 return 0;
1076 attrs = space + 1;
1077 }
1078}
1079
834cf34b
BW
1080static struct ref *get_refs_list(struct transport *transport, int for_push,
1081 const struct argv_array *ref_prefixes)
ac3fda82
JT
1082{
1083 get_helper(transport);
1084
1085 if (process_connect(transport, for_push)) {
1086 do_take_over(transport);
1087 return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1088 }
1089
1090 return get_refs_list_using_list(transport, for_push);
1091}
1092
1093static struct ref *get_refs_list_using_list(struct transport *transport,
1094 int for_push)
6eb996b5 1095{
292ce46b 1096 struct helper_data *data = transport->data;
6eb996b5
DB
1097 struct child_process *helper;
1098 struct ref *ret = NULL;
1099 struct ref **tail = &ret;
1100 struct ref *posn;
1101 struct strbuf buf = STRBUF_INIT;
6eb996b5 1102
ac3fda82 1103 data->get_refs_list_called = 1;
6eb996b5 1104 helper = get_helper(transport);
2d14d65c 1105
ae4efe19
SP
1106 if (data->push && for_push)
1107 write_str_in_full(helper->in, "list for-push\n");
1108 else
1109 write_str_in_full(helper->in, "list\n");
6eb996b5 1110
6eb996b5
DB
1111 while (1) {
1112 char *eov, *eon;
5931b33e
FC
1113 if (recvline(data, &buf))
1114 exit(128);
6eb996b5
DB
1115
1116 if (!*buf.buf)
1117 break;
1118
1119 eov = strchr(buf.buf, ' ');
1120 if (!eov)
6b5b309f 1121 die(_("malformed response in ref list: %s"), buf.buf);
6eb996b5
DB
1122 eon = strchr(eov + 1, ' ');
1123 *eov = '\0';
1124 if (eon)
1125 *eon = '\0';
1126 *tail = alloc_ref(eov + 1);
1127 if (buf.buf[0] == '@')
1128 (*tail)->symref = xstrdup(buf.buf + 1);
1129 else if (buf.buf[0] != '?')
f4e54d02 1130 get_oid_hex(buf.buf, &(*tail)->old_oid);
f8ec9167
DB
1131 if (eon) {
1132 if (has_attribute(eon + 1, "unchanged")) {
1133 (*tail)->status |= REF_STATUS_UPTODATE;
34c290a6 1134 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
1a07e59c 1135 die(_("could not read ref %s"),
ae25fd39 1136 (*tail)->name);
f8ec9167
DB
1137 }
1138 }
6eb996b5
DB
1139 tail = &((*tail)->next);
1140 }
bf3c523c
IL
1141 if (debug)
1142 fprintf(stderr, "Debug: Read ref listing.\n");
6eb996b5
DB
1143 strbuf_release(&buf);
1144
1145 for (posn = ret; posn; posn = posn->next)
1146 resolve_remote_symref(posn, ret);
1147
1148 return ret;
1149}
1150
e967ca38
JT
1151static struct transport_vtable vtable = {
1152 set_helper_option,
1153 get_refs_list,
1154 fetch,
1155 push_refs,
1156 connect_helper,
1157 release_helper
1158};
1159
c9e388bb 1160int transport_helper_init(struct transport *transport, const char *name)
6eb996b5 1161{
92e25b6b 1162 struct helper_data *data = xcalloc(1, sizeof(*data));
c9e388bb 1163 data->name = name;
6eb996b5 1164
a5adaced
JK
1165 transport_check_allowed(name);
1166
bf3c523c
IL
1167 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1168 debug = 1;
1169
6eb996b5 1170 transport->data = data;
e967ca38 1171 transport->vtable = &vtable;
61b075bd 1172 transport->smart_options = &(data->transport_options);
6eb996b5
DB
1173 return 0;
1174}
419f37db
IL
1175
1176/*
1177 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1178 * buffer less), so attempt reads and writes with up to that size.
1179 */
1180#define BUFFERSIZE 65536
1181/* This should be enough to hold debugging message. */
1182#define PBUFFERSIZE 8192
1183
1184/* Print bidirectional transfer loop debug message. */
4621085b 1185__attribute__((format (printf, 1, 2)))
419f37db
IL
1186static void transfer_debug(const char *fmt, ...)
1187{
6cdf8a79
1188 /*
1189 * NEEDSWORK: This function is sometimes used from multiple threads, and
1190 * we end up using debug_enabled racily. That "should not matter" since
1191 * we always write the same value, but it's still wrong. This function
1192 * is listed in .tsan-suppressions for the time being.
1193 */
1194
419f37db
IL
1195 va_list args;
1196 char msgbuf[PBUFFERSIZE];
1197 static int debug_enabled = -1;
1198
1199 if (debug_enabled < 0)
1200 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1201 if (!debug_enabled)
1202 return;
1203
1204 va_start(args, fmt);
1205 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1206 va_end(args);
1207 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1208}
1209
1210/* Stream state: More data may be coming in this direction. */
2e3a16b2 1211#define SSTATE_TRANSFERRING 0
419f37db
IL
1212/*
1213 * Stream state: No more data coming in this direction, flushing rest of
1214 * data.
1215 */
1216#define SSTATE_FLUSHING 1
1217/* Stream state: Transfer in this direction finished. */
1218#define SSTATE_FINISHED 2
1219
2e3a16b2 1220#define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
419f37db
IL
1221#define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1222#define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1223
1224/* Unidirectional transfer. */
1225struct unidirectional_transfer {
1226 /* Source */
1227 int src;
1228 /* Destination */
1229 int dest;
1230 /* Is source socket? */
1231 int src_is_sock;
1232 /* Is destination socket? */
1233 int dest_is_sock;
41ccfdd9 1234 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
419f37db
IL
1235 int state;
1236 /* Buffer. */
1237 char buf[BUFFERSIZE];
1238 /* Buffer used. */
1239 size_t bufuse;
1240 /* Name of source. */
1241 const char *src_name;
1242 /* Name of destination. */
1243 const char *dest_name;
1244};
1245
1246/* Closes the target (for writing) if transfer has finished. */
1247static void udt_close_if_finished(struct unidirectional_transfer *t)
1248{
1249 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1250 t->state = SSTATE_FINISHED;
1251 if (t->dest_is_sock)
1252 shutdown(t->dest, SHUT_WR);
1253 else
1254 close(t->dest);
1255 transfer_debug("Closed %s.", t->dest_name);
1256 }
1257}
1258
1259/*
832c0e5e 1260 * Tries to read data from source into buffer. If buffer is full,
419f37db
IL
1261 * no data is read. Returns 0 on success, -1 on error.
1262 */
1263static int udt_do_read(struct unidirectional_transfer *t)
1264{
1265 ssize_t bytes;
1266
1267 if (t->bufuse == BUFFERSIZE)
1268 return 0; /* No space for more. */
1269
1270 transfer_debug("%s is readable", t->src_name);
c14e5a1a 1271 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
d4c81368 1272 if (bytes < 0) {
6b5b309f 1273 error_errno(_("read(%s) failed"), t->src_name);
419f37db
IL
1274 return -1;
1275 } else if (bytes == 0) {
1276 transfer_debug("%s EOF (with %i bytes in buffer)",
4621085b 1277 t->src_name, (int)t->bufuse);
419f37db
IL
1278 t->state = SSTATE_FLUSHING;
1279 } else if (bytes > 0) {
1280 t->bufuse += bytes;
1281 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1282 (int)bytes, t->src_name, (int)t->bufuse);
1283 }
1284 return 0;
1285}
1286
1287/* Tries to write data from buffer into destination. If buffer is empty,
1288 * no data is written. Returns 0 on success, -1 on error.
1289 */
1290static int udt_do_write(struct unidirectional_transfer *t)
1291{
803dbdb9 1292 ssize_t bytes;
419f37db
IL
1293
1294 if (t->bufuse == 0)
1295 return 0; /* Nothing to write. */
1296
1297 transfer_debug("%s is writable", t->dest_name);
7edc02f4 1298 bytes = xwrite(t->dest, t->buf, t->bufuse);
d4c81368 1299 if (bytes < 0) {
6b5b309f 1300 error_errno(_("write(%s) failed"), t->dest_name);
419f37db
IL
1301 return -1;
1302 } else if (bytes > 0) {
1303 t->bufuse -= bytes;
1304 if (t->bufuse)
1305 memmove(t->buf, t->buf + bytes, t->bufuse);
1306 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1307 (int)bytes, t->dest_name, (int)t->bufuse);
1308 }
1309 return 0;
1310}
1311
1312
1313/* State of bidirectional transfer loop. */
1314struct bidirectional_transfer_state {
1315 /* Direction from program to git. */
1316 struct unidirectional_transfer ptg;
1317 /* Direction from git to program. */
1318 struct unidirectional_transfer gtp;
1319};
1320
1321static void *udt_copy_task_routine(void *udt)
1322{
1323 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1324 while (t->state != SSTATE_FINISHED) {
1325 if (STATE_NEEDS_READING(t->state))
1326 if (udt_do_read(t))
1327 return NULL;
1328 if (STATE_NEEDS_WRITING(t->state))
1329 if (udt_do_write(t))
1330 return NULL;
1331 if (STATE_NEEDS_CLOSING(t->state))
1332 udt_close_if_finished(t);
1333 }
1334 return udt; /* Just some non-NULL value. */
1335}
1336
1337#ifndef NO_PTHREADS
1338
1339/*
98e023de 1340 * Join thread, with appropriate errors on failure. Name is name for the
419f37db
IL
1341 * thread (for error messages). Returns 0 on success, 1 on failure.
1342 */
1343static int tloop_join(pthread_t thread, const char *name)
1344{
1345 int err;
1346 void *tret;
1347 err = pthread_join(thread, &tret);
1348 if (!tret) {
6b5b309f 1349 error(_("%s thread failed"), name);
419f37db
IL
1350 return 1;
1351 }
1352 if (err) {
6b5b309f 1353 error(_("%s thread failed to join: %s"), name, strerror(err));
419f37db
IL
1354 return 1;
1355 }
1356 return 0;
1357}
1358
1359/*
1360 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1361 * -1 on failure.
1362 */
1363static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1364{
1365 pthread_t gtp_thread;
1366 pthread_t ptg_thread;
1367 int err;
1368 int ret = 0;
1369 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1370 &s->gtp);
1371 if (err)
6b5b309f 1372 die(_("can't start thread for copying data: %s"), strerror(err));
419f37db
IL
1373 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1374 &s->ptg);
1375 if (err)
6b5b309f 1376 die(_("can't start thread for copying data: %s"), strerror(err));
419f37db
IL
1377
1378 ret |= tloop_join(gtp_thread, "Git to program copy");
1379 ret |= tloop_join(ptg_thread, "Program to git copy");
1380 return ret;
1381}
1382#else
1383
1384/* Close the source and target (for writing) for transfer. */
1385static void udt_kill_transfer(struct unidirectional_transfer *t)
1386{
1387 t->state = SSTATE_FINISHED;
1388 /*
1389 * Socket read end left open isn't a disaster if nobody
1390 * attempts to read from it (mingw compat headers do not
1391 * have SHUT_RD)...
1392 *
1393 * We can't fully close the socket since otherwise gtp
1394 * task would first close the socket it sends data to
1395 * while closing the ptg file descriptors.
1396 */
1397 if (!t->src_is_sock)
1398 close(t->src);
1399 if (t->dest_is_sock)
1400 shutdown(t->dest, SHUT_WR);
1401 else
1402 close(t->dest);
1403}
1404
1405/*
98e023de 1406 * Join process, with appropriate errors on failure. Name is name for the
419f37db
IL
1407 * process (for error messages). Returns 0 on success, 1 on failure.
1408 */
1409static int tloop_join(pid_t pid, const char *name)
1410{
1411 int tret;
1412 if (waitpid(pid, &tret, 0) < 0) {
6b5b309f 1413 error_errno(_("%s process failed to wait"), name);
419f37db
IL
1414 return 1;
1415 }
1416 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
6b5b309f 1417 error(_("%s process failed"), name);
419f37db
IL
1418 return 1;
1419 }
1420 return 0;
1421}
1422
1423/*
1424 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1425 * -1 on failure.
1426 */
1427static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1428{
1429 pid_t pid1, pid2;
1430 int ret = 0;
1431
1432 /* Fork thread #1: git to program. */
1433 pid1 = fork();
1434 if (pid1 < 0)
6b5b309f 1435 die_errno(_("can't start thread for copying data"));
419f37db
IL
1436 else if (pid1 == 0) {
1437 udt_kill_transfer(&s->ptg);
1438 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1439 }
1440
1441 /* Fork thread #2: program to git. */
1442 pid2 = fork();
1443 if (pid2 < 0)
6b5b309f 1444 die_errno(_("can't start thread for copying data"));
419f37db
IL
1445 else if (pid2 == 0) {
1446 udt_kill_transfer(&s->gtp);
1447 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1448 }
1449
1450 /*
1451 * Close both streams in parent as to not interfere with
1452 * end of file detection and wait for both tasks to finish.
1453 */
1454 udt_kill_transfer(&s->gtp);
1455 udt_kill_transfer(&s->ptg);
1456 ret |= tloop_join(pid1, "Git to program copy");
1457 ret |= tloop_join(pid2, "Program to git copy");
1458 return ret;
1459}
1460#endif
1461
1462/*
1463 * Copies data from stdin to output and from input to stdout simultaneously.
1464 * Additionally filtering through given filter. If filter is NULL, uses
1465 * identity filter.
1466 */
1467int bidirectional_transfer_loop(int input, int output)
1468{
1469 struct bidirectional_transfer_state state;
1470
1471 /* Fill the state fields. */
1472 state.ptg.src = input;
1473 state.ptg.dest = 1;
1474 state.ptg.src_is_sock = (input == output);
1475 state.ptg.dest_is_sock = 0;
2e3a16b2 1476 state.ptg.state = SSTATE_TRANSFERRING;
419f37db
IL
1477 state.ptg.bufuse = 0;
1478 state.ptg.src_name = "remote input";
1479 state.ptg.dest_name = "stdout";
1480
1481 state.gtp.src = 0;
1482 state.gtp.dest = output;
1483 state.gtp.src_is_sock = 0;
1484 state.gtp.dest_is_sock = (input == output);
2e3a16b2 1485 state.gtp.state = SSTATE_TRANSFERRING;
419f37db
IL
1486 state.gtp.bufuse = 0;
1487 state.gtp.src_name = "stdin";
1488 state.gtp.dest_name = "remote output";
1489
1490 return tloop_spawnwait_tasks(&state);
1491}
dfe1b7f1
JX
1492
1493void reject_atomic_push(struct ref *remote_refs, int mirror_mode)
1494{
1495 struct ref *ref;
1496
1497 /* Mark other refs as failed */
1498 for (ref = remote_refs; ref; ref = ref->next) {
1499 if (!ref->peer_ref && !mirror_mode)
1500 continue;
1501
1502 switch (ref->status) {
1503 case REF_STATUS_NONE:
1504 case REF_STATUS_OK:
1505 case REF_STATUS_EXPECTING_REPORT:
1506 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
1507 continue;
1508 default:
1509 break; /* do nothing */
1510 }
1511 }
1512 return;
1513}