4b17aaa237d498dcaaac14df1730af5b67b47705
   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 "quote.h"
   9#include "remote.h"
  10
  11static int debug;
  12
  13struct helper_data
  14{
  15        const char *name;
  16        struct child_process *helper;
  17        FILE *out;
  18        unsigned fetch : 1,
  19                import : 1,
  20                option : 1,
  21                push : 1;
  22        /* These go from remote name (as in "list") to private name */
  23        struct refspec *refspecs;
  24        int refspec_nr;
  25};
  26
  27static void sendline(struct helper_data *helper, struct strbuf *buffer)
  28{
  29        if (debug)
  30                fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
  31        if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
  32                != buffer->len)
  33                die_errno("Full write to remote helper failed");
  34}
  35
  36static int recvline(struct helper_data *helper, struct strbuf *buffer)
  37{
  38        strbuf_reset(buffer);
  39        if (debug)
  40                fprintf(stderr, "Debug: Remote helper: Waiting...\n");
  41        if (strbuf_getline(buffer, helper->out, '\n') == EOF) {
  42                if (debug)
  43                        fprintf(stderr, "Debug: Remote helper quit.\n");
  44                exit(128);
  45        }
  46
  47        if (debug)
  48                fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
  49        return 0;
  50}
  51
  52static void xchgline(struct helper_data *helper, struct strbuf *buffer)
  53{
  54        sendline(helper, buffer);
  55        recvline(helper, buffer);
  56}
  57
  58static void write_constant(int fd, const char *str)
  59{
  60        if (debug)
  61                fprintf(stderr, "Debug: Remote helper: -> %s", str);
  62        if (write_in_full(fd, str, strlen(str)) != strlen(str))
  63                die_errno("Full write to remote helper failed");
  64}
  65
  66static struct child_process *get_helper(struct transport *transport)
  67{
  68        struct helper_data *data = transport->data;
  69        struct strbuf buf = STRBUF_INIT;
  70        struct child_process *helper;
  71        const char **refspecs = NULL;
  72        int refspec_nr = 0;
  73        int refspec_alloc = 0;
  74
  75        if (data->helper)
  76                return data->helper;
  77
  78        helper = xcalloc(1, sizeof(*helper));
  79        helper->in = -1;
  80        helper->out = -1;
  81        helper->err = 0;
  82        helper->argv = xcalloc(4, sizeof(*helper->argv));
  83        strbuf_addf(&buf, "remote-%s", data->name);
  84        helper->argv[0] = strbuf_detach(&buf, NULL);
  85        helper->argv[1] = transport->remote->name;
  86        helper->argv[2] = transport->url;
  87        helper->git_cmd = 1;
  88        if (start_command(helper))
  89                die("Unable to run helper: git %s", helper->argv[0]);
  90        data->helper = helper;
  91
  92        write_constant(helper->in, "capabilities\n");
  93
  94        data->out = xfdopen(helper->out, "r");
  95        while (1) {
  96                const char *capname;
  97                int mandatory = 0;
  98                recvline(data, &buf);
  99
 100                if (!*buf.buf)
 101                        break;
 102
 103                if (*buf.buf == '*') {
 104                        capname = buf.buf + 1;
 105                        mandatory = 1;
 106                } else
 107                        capname = buf.buf;
 108
 109                if (debug)
 110                        fprintf(stderr, "Debug: Got cap %s\n", capname);
 111                if (!strcmp(capname, "fetch"))
 112                        data->fetch = 1;
 113                else if (!strcmp(capname, "option"))
 114                        data->option = 1;
 115                else if (!strcmp(capname, "push"))
 116                        data->push = 1;
 117                else if (!strcmp(capname, "import"))
 118                        data->import = 1;
 119                else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
 120                        ALLOC_GROW(refspecs,
 121                                   refspec_nr + 1,
 122                                   refspec_alloc);
 123                        refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
 124                } else if (mandatory) {
 125                        die("Unknown madatory capability %s. This remote "
 126                            "helper probably needs newer version of Git.\n",
 127                            capname);
 128                }
 129        }
 130        if (refspecs) {
 131                int i;
 132                data->refspec_nr = refspec_nr;
 133                data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
 134                for (i = 0; i < refspec_nr; i++) {
 135                        free((char *)refspecs[i]);
 136                }
 137                free(refspecs);
 138        }
 139        strbuf_release(&buf);
 140        if (debug)
 141                fprintf(stderr, "Debug: Capabilities complete.\n");
 142        return data->helper;
 143}
 144
 145static int disconnect_helper(struct transport *transport)
 146{
 147        struct helper_data *data = transport->data;
 148        struct strbuf buf = STRBUF_INIT;
 149
 150        if (data->helper) {
 151                if (debug)
 152                        fprintf(stderr, "Debug: Disconnecting.\n");
 153                strbuf_addf(&buf, "\n");
 154                sendline(data, &buf);
 155                close(data->helper->in);
 156                fclose(data->out);
 157                finish_command(data->helper);
 158                free((char *)data->helper->argv[0]);
 159                free(data->helper->argv);
 160                free(data->helper);
 161                data->helper = NULL;
 162        }
 163        return 0;
 164}
 165
 166static const char *unsupported_options[] = {
 167        TRANS_OPT_UPLOADPACK,
 168        TRANS_OPT_RECEIVEPACK,
 169        TRANS_OPT_THIN,
 170        TRANS_OPT_KEEP
 171        };
 172static const char *boolean_options[] = {
 173        TRANS_OPT_THIN,
 174        TRANS_OPT_KEEP,
 175        TRANS_OPT_FOLLOWTAGS
 176        };
 177
 178static int set_helper_option(struct transport *transport,
 179                          const char *name, const char *value)
 180{
 181        struct helper_data *data = transport->data;
 182        struct strbuf buf = STRBUF_INIT;
 183        int i, ret, is_bool = 0;
 184
 185        get_helper(transport);
 186
 187        if (!data->option)
 188                return 1;
 189
 190        for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
 191                if (!strcmp(name, unsupported_options[i]))
 192                        return 1;
 193        }
 194
 195        for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
 196                if (!strcmp(name, boolean_options[i])) {
 197                        is_bool = 1;
 198                        break;
 199                }
 200        }
 201
 202        strbuf_addf(&buf, "option %s ", name);
 203        if (is_bool)
 204                strbuf_addstr(&buf, value ? "true" : "false");
 205        else
 206                quote_c_style(value, &buf, NULL, 0);
 207        strbuf_addch(&buf, '\n');
 208
 209        xchgline(data, &buf);
 210
 211        if (!strcmp(buf.buf, "ok"))
 212                ret = 0;
 213        else if (!prefixcmp(buf.buf, "error")) {
 214                ret = -1;
 215        } else if (!strcmp(buf.buf, "unsupported"))
 216                ret = 1;
 217        else {
 218                warning("%s unexpectedly said: '%s'", data->name, buf.buf);
 219                ret = 1;
 220        }
 221        strbuf_release(&buf);
 222        return ret;
 223}
 224
 225static void standard_options(struct transport *t)
 226{
 227        char buf[16];
 228        int n;
 229        int v = t->verbose;
 230        int no_progress = v < 0 || (!t->progress && !isatty(1));
 231
 232        set_helper_option(t, "progress", !no_progress ? "true" : "false");
 233
 234        n = snprintf(buf, sizeof(buf), "%d", v + 1);
 235        if (n >= sizeof(buf))
 236                die("impossibly large verbosity value");
 237        set_helper_option(t, "verbosity", buf);
 238}
 239
 240static int release_helper(struct transport *transport)
 241{
 242        struct helper_data *data = transport->data;
 243        free_refspec(data->refspec_nr, data->refspecs);
 244        data->refspecs = NULL;
 245        disconnect_helper(transport);
 246        free(transport->data);
 247        return 0;
 248}
 249
 250static int fetch_with_fetch(struct transport *transport,
 251                            int nr_heads, struct ref **to_fetch)
 252{
 253        struct helper_data *data = transport->data;
 254        int i;
 255        struct strbuf buf = STRBUF_INIT;
 256
 257        standard_options(transport);
 258
 259        for (i = 0; i < nr_heads; i++) {
 260                const struct ref *posn = to_fetch[i];
 261                if (posn->status & REF_STATUS_UPTODATE)
 262                        continue;
 263
 264                strbuf_addf(&buf, "fetch %s %s\n",
 265                            sha1_to_hex(posn->old_sha1), posn->name);
 266        }
 267
 268        strbuf_addch(&buf, '\n');
 269        sendline(data, &buf);
 270
 271        while (1) {
 272                recvline(data, &buf);
 273
 274                if (!prefixcmp(buf.buf, "lock ")) {
 275                        const char *name = buf.buf + 5;
 276                        if (transport->pack_lockfile)
 277                                warning("%s also locked %s", data->name, name);
 278                        else
 279                                transport->pack_lockfile = xstrdup(name);
 280                }
 281                else if (!buf.len)
 282                        break;
 283                else
 284                        warning("%s unexpectedly said: '%s'", data->name, buf.buf);
 285        }
 286        strbuf_release(&buf);
 287        return 0;
 288}
 289
 290static int get_importer(struct transport *transport, struct child_process *fastimport)
 291{
 292        struct child_process *helper = get_helper(transport);
 293        memset(fastimport, 0, sizeof(*fastimport));
 294        fastimport->in = helper->out;
 295        fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
 296        fastimport->argv[0] = "fast-import";
 297        fastimport->argv[1] = "--quiet";
 298
 299        fastimport->git_cmd = 1;
 300        return start_command(fastimport);
 301}
 302
 303static int fetch_with_import(struct transport *transport,
 304                             int nr_heads, struct ref **to_fetch)
 305{
 306        struct child_process fastimport;
 307        struct helper_data *data = transport->data;
 308        int i;
 309        struct ref *posn;
 310        struct strbuf buf = STRBUF_INIT;
 311
 312        get_helper(transport);
 313
 314        if (get_importer(transport, &fastimport))
 315                die("Couldn't run fast-import");
 316
 317        for (i = 0; i < nr_heads; i++) {
 318                posn = to_fetch[i];
 319                if (posn->status & REF_STATUS_UPTODATE)
 320                        continue;
 321
 322                strbuf_addf(&buf, "import %s\n", posn->name);
 323                sendline(data, &buf);
 324                strbuf_reset(&buf);
 325        }
 326        disconnect_helper(transport);
 327        finish_command(&fastimport);
 328        free(fastimport.argv);
 329        fastimport.argv = NULL;
 330
 331        for (i = 0; i < nr_heads; i++) {
 332                char *private;
 333                posn = to_fetch[i];
 334                if (posn->status & REF_STATUS_UPTODATE)
 335                        continue;
 336                if (data->refspecs)
 337                        private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
 338                else
 339                        private = strdup(posn->name);
 340                read_ref(private, posn->old_sha1);
 341                free(private);
 342        }
 343        strbuf_release(&buf);
 344        return 0;
 345}
 346
 347static int fetch(struct transport *transport,
 348                 int nr_heads, struct ref **to_fetch)
 349{
 350        struct helper_data *data = transport->data;
 351        int i, count;
 352
 353        count = 0;
 354        for (i = 0; i < nr_heads; i++)
 355                if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
 356                        count++;
 357
 358        if (!count)
 359                return 0;
 360
 361        if (data->fetch)
 362                return fetch_with_fetch(transport, nr_heads, to_fetch);
 363
 364        if (data->import)
 365                return fetch_with_import(transport, nr_heads, to_fetch);
 366
 367        return -1;
 368}
 369
 370static int push_refs(struct transport *transport,
 371                struct ref *remote_refs, int flags)
 372{
 373        int force_all = flags & TRANSPORT_PUSH_FORCE;
 374        int mirror = flags & TRANSPORT_PUSH_MIRROR;
 375        struct helper_data *data = transport->data;
 376        struct strbuf buf = STRBUF_INIT;
 377        struct child_process *helper;
 378        struct ref *ref;
 379
 380        if (!remote_refs)
 381                return 0;
 382
 383        helper = get_helper(transport);
 384        if (!data->push)
 385                return 1;
 386
 387        for (ref = remote_refs; ref; ref = ref->next) {
 388                if (ref->peer_ref)
 389                        hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
 390                else if (!mirror)
 391                        continue;
 392
 393                ref->deletion = is_null_sha1(ref->new_sha1);
 394                if (!ref->deletion &&
 395                        !hashcmp(ref->old_sha1, ref->new_sha1)) {
 396                        ref->status = REF_STATUS_UPTODATE;
 397                        continue;
 398                }
 399
 400                if (force_all)
 401                        ref->force = 1;
 402
 403                strbuf_addstr(&buf, "push ");
 404                if (!ref->deletion) {
 405                        if (ref->force)
 406                                strbuf_addch(&buf, '+');
 407                        if (ref->peer_ref)
 408                                strbuf_addstr(&buf, ref->peer_ref->name);
 409                        else
 410                                strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
 411                }
 412                strbuf_addch(&buf, ':');
 413                strbuf_addstr(&buf, ref->name);
 414                strbuf_addch(&buf, '\n');
 415        }
 416        if (buf.len == 0)
 417                return 0;
 418
 419        transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
 420        standard_options(transport);
 421
 422        if (flags & TRANSPORT_PUSH_DRY_RUN) {
 423                if (set_helper_option(transport, "dry-run", "true") != 0)
 424                        die("helper %s does not support dry-run", data->name);
 425        }
 426
 427        strbuf_addch(&buf, '\n');
 428        sendline(data, &buf);
 429
 430        ref = remote_refs;
 431        while (1) {
 432                char *refname, *msg;
 433                int status;
 434
 435                recvline(data, &buf);
 436                if (!buf.len)
 437                        break;
 438
 439                if (!prefixcmp(buf.buf, "ok ")) {
 440                        status = REF_STATUS_OK;
 441                        refname = buf.buf + 3;
 442                } else if (!prefixcmp(buf.buf, "error ")) {
 443                        status = REF_STATUS_REMOTE_REJECT;
 444                        refname = buf.buf + 6;
 445                } else
 446                        die("expected ok/error, helper said '%s'\n", buf.buf);
 447
 448                msg = strchr(refname, ' ');
 449                if (msg) {
 450                        struct strbuf msg_buf = STRBUF_INIT;
 451                        const char *end;
 452
 453                        *msg++ = '\0';
 454                        if (!unquote_c_style(&msg_buf, msg, &end))
 455                                msg = strbuf_detach(&msg_buf, NULL);
 456                        else
 457                                msg = xstrdup(msg);
 458                        strbuf_release(&msg_buf);
 459
 460                        if (!strcmp(msg, "no match")) {
 461                                status = REF_STATUS_NONE;
 462                                free(msg);
 463                                msg = NULL;
 464                        }
 465                        else if (!strcmp(msg, "up to date")) {
 466                                status = REF_STATUS_UPTODATE;
 467                                free(msg);
 468                                msg = NULL;
 469                        }
 470                        else if (!strcmp(msg, "non-fast forward")) {
 471                                status = REF_STATUS_REJECT_NONFASTFORWARD;
 472                                free(msg);
 473                                msg = NULL;
 474                        }
 475                }
 476
 477                if (ref)
 478                        ref = find_ref_by_name(ref, refname);
 479                if (!ref)
 480                        ref = find_ref_by_name(remote_refs, refname);
 481                if (!ref) {
 482                        warning("helper reported unexpected status of %s", refname);
 483                        continue;
 484                }
 485
 486                ref->status = status;
 487                ref->remote_status = msg;
 488        }
 489        strbuf_release(&buf);
 490        return 0;
 491}
 492
 493static int has_attribute(const char *attrs, const char *attr) {
 494        int len;
 495        if (!attrs)
 496                return 0;
 497
 498        len = strlen(attr);
 499        for (;;) {
 500                const char *space = strchrnul(attrs, ' ');
 501                if (len == space - attrs && !strncmp(attrs, attr, len))
 502                        return 1;
 503                if (!*space)
 504                        return 0;
 505                attrs = space + 1;
 506        }
 507}
 508
 509static struct ref *get_refs_list(struct transport *transport, int for_push)
 510{
 511        struct helper_data *data = transport->data;
 512        struct child_process *helper;
 513        struct ref *ret = NULL;
 514        struct ref **tail = &ret;
 515        struct ref *posn;
 516        struct strbuf buf = STRBUF_INIT;
 517
 518        helper = get_helper(transport);
 519
 520        if (data->push && for_push)
 521                write_str_in_full(helper->in, "list for-push\n");
 522        else
 523                write_str_in_full(helper->in, "list\n");
 524
 525        while (1) {
 526                char *eov, *eon;
 527                recvline(data, &buf);
 528
 529                if (!*buf.buf)
 530                        break;
 531
 532                eov = strchr(buf.buf, ' ');
 533                if (!eov)
 534                        die("Malformed response in ref list: %s", buf.buf);
 535                eon = strchr(eov + 1, ' ');
 536                *eov = '\0';
 537                if (eon)
 538                        *eon = '\0';
 539                *tail = alloc_ref(eov + 1);
 540                if (buf.buf[0] == '@')
 541                        (*tail)->symref = xstrdup(buf.buf + 1);
 542                else if (buf.buf[0] != '?')
 543                        get_sha1_hex(buf.buf, (*tail)->old_sha1);
 544                if (eon) {
 545                        if (has_attribute(eon + 1, "unchanged")) {
 546                                (*tail)->status |= REF_STATUS_UPTODATE;
 547                                read_ref((*tail)->name, (*tail)->old_sha1);
 548                        }
 549                }
 550                tail = &((*tail)->next);
 551        }
 552        if (debug)
 553                fprintf(stderr, "Debug: Read ref listing.\n");
 554        strbuf_release(&buf);
 555
 556        for (posn = ret; posn; posn = posn->next)
 557                resolve_remote_symref(posn, ret);
 558
 559        return ret;
 560}
 561
 562int transport_helper_init(struct transport *transport, const char *name)
 563{
 564        struct helper_data *data = xcalloc(sizeof(*data), 1);
 565        data->name = name;
 566
 567        if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
 568                debug = 1;
 569
 570        transport->data = data;
 571        transport->set_option = set_helper_option;
 572        transport->get_refs_list = get_refs_list;
 573        transport->fetch = fetch;
 574        transport->push_refs = push_refs;
 575        transport->disconnect = release_helper;
 576        return 0;
 577}