connect.con commit add boolean diff.suppress-blank-empty config option (a624eaa)
   1#include "git-compat-util.h"
   2#include "cache.h"
   3#include "pkt-line.h"
   4#include "quote.h"
   5#include "refs.h"
   6#include "run-command.h"
   7#include "remote.h"
   8
   9static char *server_capabilities;
  10
  11static int check_ref(const char *name, int len, unsigned int flags)
  12{
  13        if (!flags)
  14                return 1;
  15
  16        if (len < 5 || memcmp(name, "refs/", 5))
  17                return 0;
  18
  19        /* Skip the "refs/" part */
  20        name += 5;
  21        len -= 5;
  22
  23        /* REF_NORMAL means that we don't want the magic fake tag refs */
  24        if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
  25                return 0;
  26
  27        /* REF_HEADS means that we want regular branch heads */
  28        if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
  29                return 1;
  30
  31        /* REF_TAGS means that we want tags */
  32        if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
  33                return 1;
  34
  35        /* All type bits clear means that we are ok with anything */
  36        return !(flags & ~REF_NORMAL);
  37}
  38
  39int check_ref_type(const struct ref *ref, int flags)
  40{
  41        return check_ref(ref->name, strlen(ref->name), flags);
  42}
  43
  44/*
  45 * Read all the refs from the other end
  46 */
  47struct ref **get_remote_heads(int in, struct ref **list,
  48                              int nr_match, char **match,
  49                              unsigned int flags)
  50{
  51        *list = NULL;
  52        for (;;) {
  53                struct ref *ref;
  54                unsigned char old_sha1[20];
  55                static char buffer[1000];
  56                char *name;
  57                int len, name_len;
  58
  59                len = packet_read_line(in, buffer, sizeof(buffer));
  60                if (!len)
  61                        break;
  62                if (buffer[len-1] == '\n')
  63                        buffer[--len] = 0;
  64
  65                if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
  66                        die("protocol error: expected sha/ref, got '%s'", buffer);
  67                name = buffer + 41;
  68
  69                name_len = strlen(name);
  70                if (len != name_len + 41) {
  71                        free(server_capabilities);
  72                        server_capabilities = xstrdup(name + name_len + 1);
  73                }
  74
  75                if (!check_ref(name, name_len, flags))
  76                        continue;
  77                if (nr_match && !path_match(name, nr_match, match))
  78                        continue;
  79                ref = alloc_ref(name_len + 1);
  80                hashcpy(ref->old_sha1, old_sha1);
  81                memcpy(ref->name, buffer + 41, name_len + 1);
  82                *list = ref;
  83                list = &ref->next;
  84        }
  85        return list;
  86}
  87
  88int server_supports(const char *feature)
  89{
  90        return server_capabilities &&
  91                strstr(server_capabilities, feature) != NULL;
  92}
  93
  94int get_ack(int fd, unsigned char *result_sha1)
  95{
  96        static char line[1000];
  97        int len = packet_read_line(fd, line, sizeof(line));
  98
  99        if (!len)
 100                die("git-fetch-pack: expected ACK/NAK, got EOF");
 101        if (line[len-1] == '\n')
 102                line[--len] = 0;
 103        if (!strcmp(line, "NAK"))
 104                return 0;
 105        if (!prefixcmp(line, "ACK ")) {
 106                if (!get_sha1_hex(line+4, result_sha1)) {
 107                        if (strstr(line+45, "continue"))
 108                                return 2;
 109                        return 1;
 110                }
 111        }
 112        die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
 113}
 114
 115int path_match(const char *path, int nr, char **match)
 116{
 117        int i;
 118        int pathlen = strlen(path);
 119
 120        for (i = 0; i < nr; i++) {
 121                char *s = match[i];
 122                int len = strlen(s);
 123
 124                if (!len || len > pathlen)
 125                        continue;
 126                if (memcmp(path + pathlen - len, s, len))
 127                        continue;
 128                if (pathlen > len && path[pathlen - len - 1] != '/')
 129                        continue;
 130                *s = 0;
 131                return (i + 1);
 132        }
 133        return 0;
 134}
 135
 136enum protocol {
 137        PROTO_LOCAL = 1,
 138        PROTO_SSH,
 139        PROTO_GIT,
 140};
 141
 142static enum protocol get_protocol(const char *name)
 143{
 144        if (!strcmp(name, "ssh"))
 145                return PROTO_SSH;
 146        if (!strcmp(name, "git"))
 147                return PROTO_GIT;
 148        if (!strcmp(name, "git+ssh"))
 149                return PROTO_SSH;
 150        if (!strcmp(name, "ssh+git"))
 151                return PROTO_SSH;
 152        if (!strcmp(name, "file"))
 153                return PROTO_LOCAL;
 154        die("I don't handle protocol '%s'", name);
 155}
 156
 157#define STR_(s) # s
 158#define STR(s)  STR_(s)
 159
 160#ifndef NO_IPV6
 161
 162static const char *ai_name(const struct addrinfo *ai)
 163{
 164        static char addr[INET_ADDRSTRLEN];
 165        if ( AF_INET == ai->ai_family ) {
 166                struct sockaddr_in *in;
 167                in = (struct sockaddr_in *)ai->ai_addr;
 168                inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
 169        } else if ( AF_INET6 == ai->ai_family ) {
 170                struct sockaddr_in6 *in;
 171                in = (struct sockaddr_in6 *)ai->ai_addr;
 172                inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
 173        } else {
 174                strcpy(addr, "(unknown)");
 175        }
 176        return addr;
 177}
 178
 179/*
 180 * Returns a connected socket() fd, or else die()s.
 181 */
 182static int git_tcp_connect_sock(char *host, int flags)
 183{
 184        int sockfd = -1, saved_errno = 0;
 185        char *colon, *end;
 186        const char *port = STR(DEFAULT_GIT_PORT);
 187        struct addrinfo hints, *ai0, *ai;
 188        int gai;
 189        int cnt = 0;
 190
 191        if (host[0] == '[') {
 192                end = strchr(host + 1, ']');
 193                if (end) {
 194                        *end = 0;
 195                        end++;
 196                        host++;
 197                } else
 198                        end = host;
 199        } else
 200                end = host;
 201        colon = strchr(end, ':');
 202
 203        if (colon) {
 204                *colon = 0;
 205                port = colon + 1;
 206                if (!*port)
 207                        port = "<none>";
 208        }
 209
 210        memset(&hints, 0, sizeof(hints));
 211        hints.ai_socktype = SOCK_STREAM;
 212        hints.ai_protocol = IPPROTO_TCP;
 213
 214        if (flags & CONNECT_VERBOSE)
 215                fprintf(stderr, "Looking up %s ... ", host);
 216
 217        gai = getaddrinfo(host, port, &hints, &ai);
 218        if (gai)
 219                die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
 220
 221        if (flags & CONNECT_VERBOSE)
 222                fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
 223
 224        for (ai0 = ai; ai; ai = ai->ai_next) {
 225                sockfd = socket(ai->ai_family,
 226                                ai->ai_socktype, ai->ai_protocol);
 227                if (sockfd < 0) {
 228                        saved_errno = errno;
 229                        continue;
 230                }
 231                if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
 232                        saved_errno = errno;
 233                        fprintf(stderr, "%s[%d: %s]: errno=%s\n",
 234                                host,
 235                                cnt,
 236                                ai_name(ai),
 237                                strerror(saved_errno));
 238                        close(sockfd);
 239                        sockfd = -1;
 240                        continue;
 241                }
 242                if (flags & CONNECT_VERBOSE)
 243                        fprintf(stderr, "%s ", ai_name(ai));
 244                break;
 245        }
 246
 247        freeaddrinfo(ai0);
 248
 249        if (sockfd < 0)
 250                die("unable to connect a socket (%s)", strerror(saved_errno));
 251
 252        if (flags & CONNECT_VERBOSE)
 253                fprintf(stderr, "done.\n");
 254
 255        return sockfd;
 256}
 257
 258#else /* NO_IPV6 */
 259
 260/*
 261 * Returns a connected socket() fd, or else die()s.
 262 */
 263static int git_tcp_connect_sock(char *host, int flags)
 264{
 265        int sockfd = -1, saved_errno = 0;
 266        char *colon, *end;
 267        char *port = STR(DEFAULT_GIT_PORT), *ep;
 268        struct hostent *he;
 269        struct sockaddr_in sa;
 270        char **ap;
 271        unsigned int nport;
 272        int cnt;
 273
 274        if (host[0] == '[') {
 275                end = strchr(host + 1, ']');
 276                if (end) {
 277                        *end = 0;
 278                        end++;
 279                        host++;
 280                } else
 281                        end = host;
 282        } else
 283                end = host;
 284        colon = strchr(end, ':');
 285
 286        if (colon) {
 287                *colon = 0;
 288                port = colon + 1;
 289        }
 290
 291        if (flags & CONNECT_VERBOSE)
 292                fprintf(stderr, "Looking up %s ... ", host);
 293
 294        he = gethostbyname(host);
 295        if (!he)
 296                die("Unable to look up %s (%s)", host, hstrerror(h_errno));
 297        nport = strtoul(port, &ep, 10);
 298        if ( ep == port || *ep ) {
 299                /* Not numeric */
 300                struct servent *se = getservbyname(port,"tcp");
 301                if ( !se )
 302                        die("Unknown port %s\n", port);
 303                nport = se->s_port;
 304        }
 305
 306        if (flags & CONNECT_VERBOSE)
 307                fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
 308
 309        for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
 310                sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
 311                if (sockfd < 0) {
 312                        saved_errno = errno;
 313                        continue;
 314                }
 315
 316                memset(&sa, 0, sizeof sa);
 317                sa.sin_family = he->h_addrtype;
 318                sa.sin_port = htons(nport);
 319                memcpy(&sa.sin_addr, *ap, he->h_length);
 320
 321                if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
 322                        saved_errno = errno;
 323                        fprintf(stderr, "%s[%d: %s]: errno=%s\n",
 324                                host,
 325                                cnt,
 326                                inet_ntoa(*(struct in_addr *)&sa.sin_addr),
 327                                strerror(saved_errno));
 328                        close(sockfd);
 329                        sockfd = -1;
 330                        continue;
 331                }
 332                if (flags & CONNECT_VERBOSE)
 333                        fprintf(stderr, "%s ",
 334                                inet_ntoa(*(struct in_addr *)&sa.sin_addr));
 335                break;
 336        }
 337
 338        if (sockfd < 0)
 339                die("unable to connect a socket (%s)", strerror(saved_errno));
 340
 341        if (flags & CONNECT_VERBOSE)
 342                fprintf(stderr, "done.\n");
 343
 344        return sockfd;
 345}
 346
 347#endif /* NO_IPV6 */
 348
 349
 350static void git_tcp_connect(int fd[2], char *host, int flags)
 351{
 352        int sockfd = git_tcp_connect_sock(host, flags);
 353
 354        fd[0] = sockfd;
 355        fd[1] = dup(sockfd);
 356}
 357
 358
 359static char *git_proxy_command;
 360static const char *rhost_name;
 361static int rhost_len;
 362
 363static int git_proxy_command_options(const char *var, const char *value,
 364                void *cb)
 365{
 366        if (!strcmp(var, "core.gitproxy")) {
 367                const char *for_pos;
 368                int matchlen = -1;
 369                int hostlen;
 370
 371                if (git_proxy_command)
 372                        return 0;
 373                if (!value)
 374                        return config_error_nonbool(var);
 375                /* [core]
 376                 * ;# matches www.kernel.org as well
 377                 * gitproxy = netcatter-1 for kernel.org
 378                 * gitproxy = netcatter-2 for sample.xz
 379                 * gitproxy = netcatter-default
 380                 */
 381                for_pos = strstr(value, " for ");
 382                if (!for_pos)
 383                        /* matches everybody */
 384                        matchlen = strlen(value);
 385                else {
 386                        hostlen = strlen(for_pos + 5);
 387                        if (rhost_len < hostlen)
 388                                matchlen = -1;
 389                        else if (!strncmp(for_pos + 5,
 390                                          rhost_name + rhost_len - hostlen,
 391                                          hostlen) &&
 392                                 ((rhost_len == hostlen) ||
 393                                  rhost_name[rhost_len - hostlen -1] == '.'))
 394                                matchlen = for_pos - value;
 395                        else
 396                                matchlen = -1;
 397                }
 398                if (0 <= matchlen) {
 399                        /* core.gitproxy = none for kernel.org */
 400                        if (matchlen == 4 &&
 401                            !memcmp(value, "none", 4))
 402                                matchlen = 0;
 403                        git_proxy_command = xmemdupz(value, matchlen);
 404                }
 405                return 0;
 406        }
 407
 408        return git_default_config(var, value, cb);
 409}
 410
 411static int git_use_proxy(const char *host)
 412{
 413        rhost_name = host;
 414        rhost_len = strlen(host);
 415        git_proxy_command = getenv("GIT_PROXY_COMMAND");
 416        git_config(git_proxy_command_options, NULL);
 417        rhost_name = NULL;
 418        return (git_proxy_command && *git_proxy_command);
 419}
 420
 421static void git_proxy_connect(int fd[2], char *host)
 422{
 423        const char *port = STR(DEFAULT_GIT_PORT);
 424        char *colon, *end;
 425        const char *argv[4];
 426        struct child_process proxy;
 427
 428        if (host[0] == '[') {
 429                end = strchr(host + 1, ']');
 430                if (end) {
 431                        *end = 0;
 432                        end++;
 433                        host++;
 434                } else
 435                        end = host;
 436        } else
 437                end = host;
 438        colon = strchr(end, ':');
 439
 440        if (colon) {
 441                *colon = 0;
 442                port = colon + 1;
 443        }
 444
 445        argv[0] = git_proxy_command;
 446        argv[1] = host;
 447        argv[2] = port;
 448        argv[3] = NULL;
 449        memset(&proxy, 0, sizeof(proxy));
 450        proxy.argv = argv;
 451        proxy.in = -1;
 452        proxy.out = -1;
 453        if (start_command(&proxy))
 454                die("cannot start proxy %s", argv[0]);
 455        fd[0] = proxy.out; /* read from proxy stdout */
 456        fd[1] = proxy.in;  /* write to proxy stdin */
 457}
 458
 459#define MAX_CMD_LEN 1024
 460
 461char *get_port(char *host)
 462{
 463        char *end;
 464        char *p = strchr(host, ':');
 465
 466        if (p) {
 467                strtol(p+1, &end, 10);
 468                if (*end == '\0') {
 469                        *p = '\0';
 470                        return p+1;
 471                }
 472        }
 473
 474        return NULL;
 475}
 476
 477static struct child_process no_fork;
 478
 479/*
 480 * This returns a dummy child_process if the transport protocol does not
 481 * need fork(2), or a struct child_process object if it does.  Once done,
 482 * finish the connection with finish_connect() with the value returned from
 483 * this function (it is safe to call finish_connect() with NULL to support
 484 * the former case).
 485 *
 486 * If it returns, the connect is successful; it just dies on errors (this
 487 * will hopefully be changed in a libification effort, to return NULL when
 488 * the connection failed).
 489 */
 490struct child_process *git_connect(int fd[2], const char *url_orig,
 491                                  const char *prog, int flags)
 492{
 493        char *url = xstrdup(url_orig);
 494        char *host, *path = url;
 495        char *end;
 496        int c;
 497        struct child_process *conn;
 498        enum protocol protocol = PROTO_LOCAL;
 499        int free_path = 0;
 500        char *port = NULL;
 501        const char **arg;
 502        struct strbuf cmd;
 503
 504        /* Without this we cannot rely on waitpid() to tell
 505         * what happened to our children.
 506         */
 507        signal(SIGCHLD, SIG_DFL);
 508
 509        host = strstr(url, "://");
 510        if(host) {
 511                *host = '\0';
 512                protocol = get_protocol(url);
 513                host += 3;
 514                c = '/';
 515        } else {
 516                host = url;
 517                c = ':';
 518        }
 519
 520        if (host[0] == '[') {
 521                end = strchr(host + 1, ']');
 522                if (end) {
 523                        *end = 0;
 524                        end++;
 525                        host++;
 526                } else
 527                        end = host;
 528        } else
 529                end = host;
 530
 531        path = strchr(end, c);
 532        if (path && !has_dos_drive_prefix(end)) {
 533                if (c == ':') {
 534                        protocol = PROTO_SSH;
 535                        *path++ = '\0';
 536                }
 537        } else
 538                path = end;
 539
 540        if (!path || !*path)
 541                die("No path specified. See 'man git-pull' for valid url syntax");
 542
 543        /*
 544         * null-terminate hostname and point path to ~ for URL's like this:
 545         *    ssh://host.xz/~user/repo
 546         */
 547        if (protocol != PROTO_LOCAL && host != url) {
 548                char *ptr = path;
 549                if (path[1] == '~')
 550                        path++;
 551                else {
 552                        path = xstrdup(ptr);
 553                        free_path = 1;
 554                }
 555
 556                *ptr = '\0';
 557        }
 558
 559        /*
 560         * Add support for ssh port: ssh://host.xy:<port>/...
 561         */
 562        if (protocol == PROTO_SSH && host != url)
 563                port = get_port(host);
 564
 565        if (protocol == PROTO_GIT) {
 566                /* These underlying connection commands die() if they
 567                 * cannot connect.
 568                 */
 569                char *target_host = xstrdup(host);
 570                if (git_use_proxy(host))
 571                        git_proxy_connect(fd, host);
 572                else
 573                        git_tcp_connect(fd, host, flags);
 574                /*
 575                 * Separate original protocol components prog and path
 576                 * from extended components with a NUL byte.
 577                 */
 578                packet_write(fd[1],
 579                             "%s %s%chost=%s%c",
 580                             prog, path, 0,
 581                             target_host, 0);
 582                free(target_host);
 583                free(url);
 584                if (free_path)
 585                        free(path);
 586                return &no_fork;
 587        }
 588
 589        conn = xcalloc(1, sizeof(*conn));
 590
 591        strbuf_init(&cmd, MAX_CMD_LEN);
 592        strbuf_addstr(&cmd, prog);
 593        strbuf_addch(&cmd, ' ');
 594        sq_quote_buf(&cmd, path);
 595        if (cmd.len >= MAX_CMD_LEN)
 596                die("command line too long");
 597
 598        conn->in = conn->out = -1;
 599        conn->argv = arg = xcalloc(6, sizeof(*arg));
 600        if (protocol == PROTO_SSH) {
 601                const char *ssh = getenv("GIT_SSH");
 602                if (!ssh) ssh = "ssh";
 603
 604                *arg++ = ssh;
 605                if (port) {
 606                        *arg++ = "-p";
 607                        *arg++ = port;
 608                }
 609                *arg++ = host;
 610        }
 611        else {
 612                /* remove these from the environment */
 613                const char *env[] = {
 614                        ALTERNATE_DB_ENVIRONMENT,
 615                        DB_ENVIRONMENT,
 616                        GIT_DIR_ENVIRONMENT,
 617                        GIT_WORK_TREE_ENVIRONMENT,
 618                        GRAFT_ENVIRONMENT,
 619                        INDEX_ENVIRONMENT,
 620                        NULL
 621                };
 622                conn->env = env;
 623                *arg++ = "sh";
 624                *arg++ = "-c";
 625        }
 626        *arg++ = cmd.buf;
 627        *arg = NULL;
 628
 629        if (start_command(conn))
 630                die("unable to fork");
 631
 632        fd[0] = conn->out; /* read from child's stdout */
 633        fd[1] = conn->in;  /* write to child's stdin */
 634        strbuf_release(&cmd);
 635        free(url);
 636        if (free_path)
 637                free(path);
 638        return conn;
 639}
 640
 641int finish_connect(struct child_process *conn)
 642{
 643        int code;
 644        if (!conn || conn == &no_fork)
 645                return 0;
 646
 647        code = finish_command(conn);
 648        free(conn->argv);
 649        free(conn);
 650        return code;
 651}