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