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