transport.con commit git-sh-setup: Fix scripts whose PWD is a symlink into a git work-dir (08fc060)
   1#include "cache.h"
   2#include "transport.h"
   3#include "run-command.h"
   4#ifndef NO_CURL
   5#include "http.h"
   6#endif
   7#include "pkt-line.h"
   8#include "fetch-pack.h"
   9#include "send-pack.h"
  10#include "walker.h"
  11#include "bundle.h"
  12#include "dir.h"
  13#include "refs.h"
  14
  15/* rsync support */
  16
  17/*
  18 * We copy packed-refs and refs/ into a temporary file, then read the
  19 * loose refs recursively (sorting whenever possible), and then inserting
  20 * those packed refs that are not yet in the list (not validating, but
  21 * assuming that the file is sorted).
  22 *
  23 * Appears refactoring this from refs.c is too cumbersome.
  24 */
  25
  26static int str_cmp(const void *a, const void *b)
  27{
  28        const char *s1 = a;
  29        const char *s2 = b;
  30
  31        return strcmp(s1, s2);
  32}
  33
  34/* path->buf + name_offset is expected to point to "refs/" */
  35
  36static int read_loose_refs(struct strbuf *path, int name_offset,
  37                struct ref **tail)
  38{
  39        DIR *dir = opendir(path->buf);
  40        struct dirent *de;
  41        struct {
  42                char **entries;
  43                int nr, alloc;
  44        } list;
  45        int i, pathlen;
  46
  47        if (!dir)
  48                return -1;
  49
  50        memset (&list, 0, sizeof(list));
  51
  52        while ((de = readdir(dir))) {
  53                if (de->d_name[0] == '.' && (de->d_name[1] == '\0' ||
  54                                (de->d_name[1] == '.' &&
  55                                 de->d_name[2] == '\0')))
  56                        continue;
  57                ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
  58                list.entries[list.nr++] = xstrdup(de->d_name);
  59        }
  60        closedir(dir);
  61
  62        /* sort the list */
  63
  64        qsort(list.entries, list.nr, sizeof(char *), str_cmp);
  65
  66        pathlen = path->len;
  67        strbuf_addch(path, '/');
  68
  69        for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
  70                strbuf_addstr(path, list.entries[i]);
  71                if (read_loose_refs(path, name_offset, tail)) {
  72                        int fd = open(path->buf, O_RDONLY);
  73                        char buffer[40];
  74                        struct ref *next;
  75
  76                        if (fd < 0)
  77                                continue;
  78                        next = alloc_ref(path->buf + name_offset);
  79                        if (read_in_full(fd, buffer, 40) != 40 ||
  80                                        get_sha1_hex(buffer, next->old_sha1)) {
  81                                close(fd);
  82                                free(next);
  83                                continue;
  84                        }
  85                        close(fd);
  86                        (*tail)->next = next;
  87                        *tail = next;
  88                }
  89        }
  90        strbuf_setlen(path, pathlen);
  91
  92        for (i = 0; i < list.nr; i++)
  93                free(list.entries[i]);
  94        free(list.entries);
  95
  96        return 0;
  97}
  98
  99/* insert the packed refs for which no loose refs were found */
 100
 101static void insert_packed_refs(const char *packed_refs, struct ref **list)
 102{
 103        FILE *f = fopen(packed_refs, "r");
 104        static char buffer[PATH_MAX];
 105
 106        if (!f)
 107                return;
 108
 109        for (;;) {
 110                int cmp = cmp, len;
 111
 112                if (!fgets(buffer, sizeof(buffer), f)) {
 113                        fclose(f);
 114                        return;
 115                }
 116
 117                if (hexval(buffer[0]) > 0xf)
 118                        continue;
 119                len = strlen(buffer);
 120                if (len && buffer[len - 1] == '\n')
 121                        buffer[--len] = '\0';
 122                if (len < 41)
 123                        continue;
 124                while ((*list)->next &&
 125                                (cmp = strcmp(buffer + 41,
 126                                      (*list)->next->name)) > 0)
 127                        list = &(*list)->next;
 128                if (!(*list)->next || cmp < 0) {
 129                        struct ref *next = alloc_ref(buffer + 41);
 130                        buffer[40] = '\0';
 131                        if (get_sha1_hex(buffer, next->old_sha1)) {
 132                                warning ("invalid SHA-1: %s", buffer);
 133                                free(next);
 134                                continue;
 135                        }
 136                        next->next = (*list)->next;
 137                        (*list)->next = next;
 138                        list = &(*list)->next;
 139                }
 140        }
 141}
 142
 143static struct ref *get_refs_via_rsync(struct transport *transport)
 144{
 145        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
 146        struct ref dummy, *tail = &dummy;
 147        struct child_process rsync;
 148        const char *args[5];
 149        int temp_dir_len;
 150
 151        /* copy the refs to the temporary directory */
 152
 153        strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
 154        if (!mkdtemp(temp_dir.buf))
 155                die ("Could not make temporary directory");
 156        temp_dir_len = temp_dir.len;
 157
 158        strbuf_addstr(&buf, transport->url);
 159        strbuf_addstr(&buf, "/refs");
 160
 161        memset(&rsync, 0, sizeof(rsync));
 162        rsync.argv = args;
 163        rsync.stdout_to_stderr = 1;
 164        args[0] = "rsync";
 165        args[1] = (transport->verbose > 0) ? "-rv" : "-r";
 166        args[2] = buf.buf;
 167        args[3] = temp_dir.buf;
 168        args[4] = NULL;
 169
 170        if (run_command(&rsync))
 171                die ("Could not run rsync to get refs");
 172
 173        strbuf_reset(&buf);
 174        strbuf_addstr(&buf, transport->url);
 175        strbuf_addstr(&buf, "/packed-refs");
 176
 177        args[2] = buf.buf;
 178
 179        if (run_command(&rsync))
 180                die ("Could not run rsync to get refs");
 181
 182        /* read the copied refs */
 183
 184        strbuf_addstr(&temp_dir, "/refs");
 185        read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
 186        strbuf_setlen(&temp_dir, temp_dir_len);
 187
 188        tail = &dummy;
 189        strbuf_addstr(&temp_dir, "/packed-refs");
 190        insert_packed_refs(temp_dir.buf, &tail);
 191        strbuf_setlen(&temp_dir, temp_dir_len);
 192
 193        if (remove_dir_recursively(&temp_dir, 0))
 194                warning ("Error removing temporary directory %s.",
 195                                temp_dir.buf);
 196
 197        strbuf_release(&buf);
 198        strbuf_release(&temp_dir);
 199
 200        return dummy.next;
 201}
 202
 203static int fetch_objs_via_rsync(struct transport *transport,
 204                                int nr_objs, const struct ref **to_fetch)
 205{
 206        struct strbuf buf = STRBUF_INIT;
 207        struct child_process rsync;
 208        const char *args[8];
 209        int result;
 210
 211        strbuf_addstr(&buf, transport->url);
 212        strbuf_addstr(&buf, "/objects/");
 213
 214        memset(&rsync, 0, sizeof(rsync));
 215        rsync.argv = args;
 216        rsync.stdout_to_stderr = 1;
 217        args[0] = "rsync";
 218        args[1] = (transport->verbose > 0) ? "-rv" : "-r";
 219        args[2] = "--ignore-existing";
 220        args[3] = "--exclude";
 221        args[4] = "info";
 222        args[5] = buf.buf;
 223        args[6] = get_object_directory();
 224        args[7] = NULL;
 225
 226        /* NEEDSWORK: handle one level of alternates */
 227        result = run_command(&rsync);
 228
 229        strbuf_release(&buf);
 230
 231        return result;
 232}
 233
 234static int write_one_ref(const char *name, const unsigned char *sha1,
 235                int flags, void *data)
 236{
 237        struct strbuf *buf = data;
 238        int len = buf->len;
 239        FILE *f;
 240
 241        /* when called via for_each_ref(), flags is non-zero */
 242        if (flags && prefixcmp(name, "refs/heads/") &&
 243                        prefixcmp(name, "refs/tags/"))
 244                return 0;
 245
 246        strbuf_addstr(buf, name);
 247        if (safe_create_leading_directories(buf->buf) ||
 248                        !(f = fopen(buf->buf, "w")) ||
 249                        fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
 250                        fclose(f))
 251                return error("problems writing temporary file %s", buf->buf);
 252        strbuf_setlen(buf, len);
 253        return 0;
 254}
 255
 256static int write_refs_to_temp_dir(struct strbuf *temp_dir,
 257                int refspec_nr, const char **refspec)
 258{
 259        int i;
 260
 261        for (i = 0; i < refspec_nr; i++) {
 262                unsigned char sha1[20];
 263                char *ref;
 264
 265                if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
 266                        return error("Could not get ref %s", refspec[i]);
 267
 268                if (write_one_ref(ref, sha1, 0, temp_dir)) {
 269                        free(ref);
 270                        return -1;
 271                }
 272                free(ref);
 273        }
 274        return 0;
 275}
 276
 277static int rsync_transport_push(struct transport *transport,
 278                int refspec_nr, const char **refspec, int flags)
 279{
 280        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
 281        int result = 0, i;
 282        struct child_process rsync;
 283        const char *args[10];
 284
 285        if (flags & TRANSPORT_PUSH_MIRROR)
 286                return error("rsync transport does not support mirror mode");
 287
 288        /* first push the objects */
 289
 290        strbuf_addstr(&buf, transport->url);
 291        strbuf_addch(&buf, '/');
 292
 293        memset(&rsync, 0, sizeof(rsync));
 294        rsync.argv = args;
 295        rsync.stdout_to_stderr = 1;
 296        i = 0;
 297        args[i++] = "rsync";
 298        args[i++] = "-a";
 299        if (flags & TRANSPORT_PUSH_DRY_RUN)
 300                args[i++] = "--dry-run";
 301        if (transport->verbose > 0)
 302                args[i++] = "-v";
 303        args[i++] = "--ignore-existing";
 304        args[i++] = "--exclude";
 305        args[i++] = "info";
 306        args[i++] = get_object_directory();
 307        args[i++] = buf.buf;
 308        args[i++] = NULL;
 309
 310        if (run_command(&rsync))
 311                return error("Could not push objects to %s", transport->url);
 312
 313        /* copy the refs to the temporary directory; they could be packed. */
 314
 315        strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
 316        if (!mkdtemp(temp_dir.buf))
 317                die ("Could not make temporary directory");
 318        strbuf_addch(&temp_dir, '/');
 319
 320        if (flags & TRANSPORT_PUSH_ALL) {
 321                if (for_each_ref(write_one_ref, &temp_dir))
 322                        return -1;
 323        } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
 324                return -1;
 325
 326        i = 2;
 327        if (flags & TRANSPORT_PUSH_DRY_RUN)
 328                args[i++] = "--dry-run";
 329        if (!(flags & TRANSPORT_PUSH_FORCE))
 330                args[i++] = "--ignore-existing";
 331        args[i++] = temp_dir.buf;
 332        args[i++] = transport->url;
 333        args[i++] = NULL;
 334        if (run_command(&rsync))
 335                result = error("Could not push to %s", transport->url);
 336
 337        if (remove_dir_recursively(&temp_dir, 0))
 338                warning ("Could not remove temporary directory %s.",
 339                                temp_dir.buf);
 340
 341        strbuf_release(&buf);
 342        strbuf_release(&temp_dir);
 343
 344        return result;
 345}
 346
 347/* Generic functions for using commit walkers */
 348
 349#ifndef NO_CURL /* http fetch is the only user */
 350static int fetch_objs_via_walker(struct transport *transport,
 351                                 int nr_objs, const struct ref **to_fetch)
 352{
 353        char *dest = xstrdup(transport->url);
 354        struct walker *walker = transport->data;
 355        char **objs = xmalloc(nr_objs * sizeof(*objs));
 356        int i;
 357
 358        walker->get_all = 1;
 359        walker->get_tree = 1;
 360        walker->get_history = 1;
 361        walker->get_verbosely = transport->verbose >= 0;
 362        walker->get_recover = 0;
 363
 364        for (i = 0; i < nr_objs; i++)
 365                objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
 366
 367        if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
 368                die("Fetch failed.");
 369
 370        for (i = 0; i < nr_objs; i++)
 371                free(objs[i]);
 372        free(objs);
 373        free(dest);
 374        return 0;
 375}
 376#endif /* NO_CURL */
 377
 378static int disconnect_walker(struct transport *transport)
 379{
 380        struct walker *walker = transport->data;
 381        if (walker)
 382                walker_free(walker);
 383        return 0;
 384}
 385
 386#ifndef NO_CURL
 387static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
 388{
 389        const char **argv;
 390        int argc;
 391        int err;
 392
 393        if (flags & TRANSPORT_PUSH_MIRROR)
 394                return error("http transport does not support mirror mode");
 395
 396        argv = xmalloc((refspec_nr + 12) * sizeof(char *));
 397        argv[0] = "http-push";
 398        argc = 1;
 399        if (flags & TRANSPORT_PUSH_ALL)
 400                argv[argc++] = "--all";
 401        if (flags & TRANSPORT_PUSH_FORCE)
 402                argv[argc++] = "--force";
 403        if (flags & TRANSPORT_PUSH_DRY_RUN)
 404                argv[argc++] = "--dry-run";
 405        if (flags & TRANSPORT_PUSH_VERBOSE)
 406                argv[argc++] = "--verbose";
 407        argv[argc++] = transport->url;
 408        while (refspec_nr--)
 409                argv[argc++] = *refspec++;
 410        argv[argc] = NULL;
 411        err = run_command_v_opt(argv, RUN_GIT_CMD);
 412        switch (err) {
 413        case -ERR_RUN_COMMAND_FORK:
 414                error("unable to fork for %s", argv[0]);
 415        case -ERR_RUN_COMMAND_EXEC:
 416                error("unable to exec %s", argv[0]);
 417                break;
 418        case -ERR_RUN_COMMAND_WAITPID:
 419        case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
 420        case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
 421        case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
 422                error("%s died with strange error", argv[0]);
 423        }
 424        return !!err;
 425}
 426
 427static struct ref *get_refs_via_curl(struct transport *transport)
 428{
 429        struct strbuf buffer = STRBUF_INIT;
 430        char *data, *start, *mid;
 431        char *ref_name;
 432        char *refs_url;
 433        int i = 0;
 434
 435        struct active_request_slot *slot;
 436        struct slot_results results;
 437
 438        struct ref *refs = NULL;
 439        struct ref *ref = NULL;
 440        struct ref *last_ref = NULL;
 441
 442        struct walker *walker;
 443
 444        if (!transport->data)
 445                transport->data = get_http_walker(transport->url,
 446                                                transport->remote);
 447
 448        walker = transport->data;
 449
 450        refs_url = xmalloc(strlen(transport->url) + 11);
 451        sprintf(refs_url, "%s/info/refs", transport->url);
 452
 453        slot = get_active_slot();
 454        slot->results = &results;
 455        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
 456        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 457        curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
 458        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
 459
 460        if (start_active_slot(slot)) {
 461                run_active_slot(slot);
 462                if (results.curl_result != CURLE_OK) {
 463                        strbuf_release(&buffer);
 464                        if (missing_target(&results))
 465                                die("%s not found: did you run git update-server-info on the server?", refs_url);
 466                        else
 467                                die("%s download error - %s", refs_url, curl_errorstr);
 468                }
 469        } else {
 470                strbuf_release(&buffer);
 471                die("Unable to start HTTP request");
 472        }
 473
 474        data = buffer.buf;
 475        start = NULL;
 476        mid = data;
 477        while (i < buffer.len) {
 478                if (!start)
 479                        start = &data[i];
 480                if (data[i] == '\t')
 481                        mid = &data[i];
 482                if (data[i] == '\n') {
 483                        data[i] = 0;
 484                        ref_name = mid + 1;
 485                        ref = xmalloc(sizeof(struct ref) +
 486                                      strlen(ref_name) + 1);
 487                        memset(ref, 0, sizeof(struct ref));
 488                        strcpy(ref->name, ref_name);
 489                        get_sha1_hex(start, ref->old_sha1);
 490                        if (!refs)
 491                                refs = ref;
 492                        if (last_ref)
 493                                last_ref->next = ref;
 494                        last_ref = ref;
 495                        start = NULL;
 496                }
 497                i++;
 498        }
 499
 500        strbuf_release(&buffer);
 501
 502        ref = alloc_ref("HEAD");
 503        if (!walker->fetch_ref(walker, ref) &&
 504            !resolve_remote_symref(ref, refs)) {
 505                ref->next = refs;
 506                refs = ref;
 507        } else {
 508                free(ref);
 509        }
 510
 511        return refs;
 512}
 513
 514static int fetch_objs_via_curl(struct transport *transport,
 515                                 int nr_objs, const struct ref **to_fetch)
 516{
 517        if (!transport->data)
 518                transport->data = get_http_walker(transport->url,
 519                                                transport->remote);
 520        return fetch_objs_via_walker(transport, nr_objs, to_fetch);
 521}
 522
 523#endif
 524
 525struct bundle_transport_data {
 526        int fd;
 527        struct bundle_header header;
 528};
 529
 530static struct ref *get_refs_from_bundle(struct transport *transport)
 531{
 532        struct bundle_transport_data *data = transport->data;
 533        struct ref *result = NULL;
 534        int i;
 535
 536        if (data->fd > 0)
 537                close(data->fd);
 538        data->fd = read_bundle_header(transport->url, &data->header);
 539        if (data->fd < 0)
 540                die ("Could not read bundle '%s'.", transport->url);
 541        for (i = 0; i < data->header.references.nr; i++) {
 542                struct ref_list_entry *e = data->header.references.list + i;
 543                struct ref *ref = alloc_ref(e->name);
 544                hashcpy(ref->old_sha1, e->sha1);
 545                ref->next = result;
 546                result = ref;
 547        }
 548        return result;
 549}
 550
 551static int fetch_refs_from_bundle(struct transport *transport,
 552                               int nr_heads, const struct ref **to_fetch)
 553{
 554        struct bundle_transport_data *data = transport->data;
 555        return unbundle(&data->header, data->fd);
 556}
 557
 558static int close_bundle(struct transport *transport)
 559{
 560        struct bundle_transport_data *data = transport->data;
 561        if (data->fd > 0)
 562                close(data->fd);
 563        free(data);
 564        return 0;
 565}
 566
 567struct git_transport_data {
 568        unsigned thin : 1;
 569        unsigned keep : 1;
 570        unsigned followtags : 1;
 571        int depth;
 572        struct child_process *conn;
 573        int fd[2];
 574        const char *uploadpack;
 575        const char *receivepack;
 576};
 577
 578static int set_git_option(struct transport *connection,
 579                          const char *name, const char *value)
 580{
 581        struct git_transport_data *data = connection->data;
 582        if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
 583                data->uploadpack = value;
 584                return 0;
 585        } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
 586                data->receivepack = value;
 587                return 0;
 588        } else if (!strcmp(name, TRANS_OPT_THIN)) {
 589                data->thin = !!value;
 590                return 0;
 591        } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
 592                data->followtags = !!value;
 593                return 0;
 594        } else if (!strcmp(name, TRANS_OPT_KEEP)) {
 595                data->keep = !!value;
 596                return 0;
 597        } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
 598                if (!value)
 599                        data->depth = 0;
 600                else
 601                        data->depth = atoi(value);
 602                return 0;
 603        }
 604        return 1;
 605}
 606
 607static int connect_setup(struct transport *transport)
 608{
 609        struct git_transport_data *data = transport->data;
 610        data->conn = git_connect(data->fd, transport->url, data->uploadpack, 0);
 611        return 0;
 612}
 613
 614static struct ref *get_refs_via_connect(struct transport *transport)
 615{
 616        struct git_transport_data *data = transport->data;
 617        struct ref *refs;
 618
 619        connect_setup(transport);
 620        get_remote_heads(data->fd[0], &refs, 0, NULL, 0, NULL);
 621
 622        return refs;
 623}
 624
 625static int fetch_refs_via_pack(struct transport *transport,
 626                               int nr_heads, const struct ref **to_fetch)
 627{
 628        struct git_transport_data *data = transport->data;
 629        char **heads = xmalloc(nr_heads * sizeof(*heads));
 630        char **origh = xmalloc(nr_heads * sizeof(*origh));
 631        const struct ref *refs;
 632        char *dest = xstrdup(transport->url);
 633        struct fetch_pack_args args;
 634        int i;
 635        struct ref *refs_tmp = NULL;
 636
 637        memset(&args, 0, sizeof(args));
 638        args.uploadpack = data->uploadpack;
 639        args.keep_pack = data->keep;
 640        args.lock_pack = 1;
 641        args.use_thin_pack = data->thin;
 642        args.include_tag = data->followtags;
 643        args.verbose = (transport->verbose > 0);
 644        args.quiet = (transport->verbose < 0);
 645        args.no_progress = args.quiet || (!transport->progress && !isatty(1));
 646        args.depth = data->depth;
 647
 648        for (i = 0; i < nr_heads; i++)
 649                origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
 650
 651        if (!data->conn) {
 652                connect_setup(transport);
 653                get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
 654        }
 655
 656        refs = fetch_pack(&args, data->fd, data->conn,
 657                          refs_tmp ? refs_tmp : transport->remote_refs,
 658                          dest, nr_heads, heads, &transport->pack_lockfile);
 659        close(data->fd[0]);
 660        close(data->fd[1]);
 661        if (finish_connect(data->conn))
 662                refs = NULL;
 663        data->conn = NULL;
 664
 665        free_refs(refs_tmp);
 666
 667        for (i = 0; i < nr_heads; i++)
 668                free(origh[i]);
 669        free(origh);
 670        free(heads);
 671        free(dest);
 672        return (refs ? 0 : -1);
 673}
 674
 675static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
 676{
 677        struct git_transport_data *data = transport->data;
 678        struct send_pack_args args;
 679
 680        args.receivepack = data->receivepack;
 681        args.send_all = !!(flags & TRANSPORT_PUSH_ALL);
 682        args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
 683        args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
 684        args.use_thin_pack = data->thin;
 685        args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
 686        args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
 687
 688        return send_pack(&args, transport->url, transport->remote, refspec_nr, refspec);
 689}
 690
 691static int disconnect_git(struct transport *transport)
 692{
 693        struct git_transport_data *data = transport->data;
 694        if (data->conn) {
 695                packet_flush(data->fd[1]);
 696                close(data->fd[0]);
 697                close(data->fd[1]);
 698                finish_connect(data->conn);
 699        }
 700
 701        free(data);
 702        return 0;
 703}
 704
 705static int is_local(const char *url)
 706{
 707        const char *colon = strchr(url, ':');
 708        const char *slash = strchr(url, '/');
 709        return !colon || (slash && slash < colon) ||
 710                has_dos_drive_prefix(url);
 711}
 712
 713static int is_file(const char *url)
 714{
 715        struct stat buf;
 716        if (stat(url, &buf))
 717                return 0;
 718        return S_ISREG(buf.st_mode);
 719}
 720
 721struct transport *transport_get(struct remote *remote, const char *url)
 722{
 723        struct transport *ret = xcalloc(1, sizeof(*ret));
 724
 725        ret->remote = remote;
 726        ret->url = url;
 727
 728        if (!prefixcmp(url, "rsync://")) {
 729                ret->get_refs_list = get_refs_via_rsync;
 730                ret->fetch = fetch_objs_via_rsync;
 731                ret->push = rsync_transport_push;
 732
 733        } else if (!prefixcmp(url, "http://")
 734                || !prefixcmp(url, "https://")
 735                || !prefixcmp(url, "ftp://")) {
 736#ifdef NO_CURL
 737                error("git was compiled without libcurl support.");
 738#else
 739                ret->get_refs_list = get_refs_via_curl;
 740                ret->fetch = fetch_objs_via_curl;
 741                ret->push = curl_transport_push;
 742#endif
 743                ret->disconnect = disconnect_walker;
 744
 745        } else if (is_local(url) && is_file(url)) {
 746                struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
 747                ret->data = data;
 748                ret->get_refs_list = get_refs_from_bundle;
 749                ret->fetch = fetch_refs_from_bundle;
 750                ret->disconnect = close_bundle;
 751
 752        } else {
 753                struct git_transport_data *data = xcalloc(1, sizeof(*data));
 754                ret->data = data;
 755                ret->set_option = set_git_option;
 756                ret->get_refs_list = get_refs_via_connect;
 757                ret->fetch = fetch_refs_via_pack;
 758                ret->push = git_transport_push;
 759                ret->disconnect = disconnect_git;
 760
 761                data->thin = 1;
 762                data->conn = NULL;
 763                data->uploadpack = "git-upload-pack";
 764                if (remote && remote->uploadpack)
 765                        data->uploadpack = remote->uploadpack;
 766                data->receivepack = "git-receive-pack";
 767                if (remote && remote->receivepack)
 768                        data->receivepack = remote->receivepack;
 769        }
 770
 771        return ret;
 772}
 773
 774int transport_set_option(struct transport *transport,
 775                         const char *name, const char *value)
 776{
 777        if (transport->set_option)
 778                return transport->set_option(transport, name, value);
 779        return 1;
 780}
 781
 782int transport_push(struct transport *transport,
 783                   int refspec_nr, const char **refspec, int flags)
 784{
 785        if (!transport->push)
 786                return 1;
 787        return transport->push(transport, refspec_nr, refspec, flags);
 788}
 789
 790const struct ref *transport_get_remote_refs(struct transport *transport)
 791{
 792        if (!transport->remote_refs)
 793                transport->remote_refs = transport->get_refs_list(transport);
 794        return transport->remote_refs;
 795}
 796
 797int transport_fetch_refs(struct transport *transport, const struct ref *refs)
 798{
 799        int rc;
 800        int nr_heads = 0, nr_alloc = 0;
 801        const struct ref **heads = NULL;
 802        const struct ref *rm;
 803
 804        for (rm = refs; rm; rm = rm->next) {
 805                if (rm->peer_ref &&
 806                    !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
 807                        continue;
 808                ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
 809                heads[nr_heads++] = rm;
 810        }
 811
 812        rc = transport->fetch(transport, nr_heads, heads);
 813        free(heads);
 814        return rc;
 815}
 816
 817void transport_unlock_pack(struct transport *transport)
 818{
 819        if (transport->pack_lockfile) {
 820                unlink(transport->pack_lockfile);
 821                free(transport->pack_lockfile);
 822                transport->pack_lockfile = NULL;
 823        }
 824}
 825
 826int transport_disconnect(struct transport *transport)
 827{
 828        int ret = 0;
 829        if (transport->disconnect)
 830                ret = transport->disconnect(transport);
 831        free(transport);
 832        return ret;
 833}