THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1463 lines
37 KiB

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