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