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