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.

1485 lines
38KB

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