connect.con commit git-upload-pack: Support the multi_ack protocol (1bd8c8f)
   1#include "cache.h"
   2#include "pkt-line.h"
   3#include "quote.h"
   4#include "refs.h"
   5#include <sys/wait.h>
   6#include <sys/socket.h>
   7#include <netinet/in.h>
   8#include <arpa/inet.h>
   9#include <netdb.h>
  10
  11static char *server_capabilities = "";
  12
  13/*
  14 * Read all the refs from the other end
  15 */
  16struct ref **get_remote_heads(int in, struct ref **list,
  17                              int nr_match, char **match, int ignore_funny)
  18{
  19        *list = NULL;
  20        for (;;) {
  21                struct ref *ref;
  22                unsigned char old_sha1[20];
  23                static char buffer[1000];
  24                char *name;
  25                int len, name_len;
  26
  27                len = packet_read_line(in, buffer, sizeof(buffer));
  28                if (!len)
  29                        break;
  30                if (buffer[len-1] == '\n')
  31                        buffer[--len] = 0;
  32
  33                if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
  34                        die("protocol error: expected sha/ref, got '%s'", buffer);
  35                name = buffer + 41;
  36
  37                if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
  38                    check_ref_format(name + 5))
  39                        continue;
  40
  41                name_len = strlen(name);
  42                if (len != name_len + 41) {
  43                        if (server_capabilities)
  44                                free(server_capabilities);
  45                        server_capabilities = strdup(name + name_len + 1);
  46                }
  47
  48                if (nr_match && !path_match(name, nr_match, match))
  49                        continue;
  50                ref = xcalloc(1, sizeof(*ref) + len - 40);
  51                memcpy(ref->old_sha1, old_sha1, 20);
  52                memcpy(ref->name, buffer + 41, len - 40);
  53                *list = ref;
  54                list = &ref->next;
  55        }
  56        return list;
  57}
  58
  59int server_supports(const char *feature)
  60{
  61        return strstr(feature, server_capabilities) != NULL;
  62}
  63
  64int get_ack(int fd, unsigned char *result_sha1)
  65{
  66        static char line[1000];
  67        int len = packet_read_line(fd, line, sizeof(line));
  68
  69        if (!len)
  70                die("git-fetch-pack: expected ACK/NAK, got EOF");
  71        if (line[len-1] == '\n')
  72                line[--len] = 0;
  73        if (!strcmp(line, "NAK"))
  74                return 0;
  75        if (!strncmp(line, "ACK ", 3)) {
  76                if (!get_sha1_hex(line+4, result_sha1))
  77                        return 1;
  78        }
  79        die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
  80}
  81
  82int path_match(const char *path, int nr, char **match)
  83{
  84        int i;
  85        int pathlen = strlen(path);
  86
  87        for (i = 0; i < nr; i++) {
  88                char *s = match[i];
  89                int len = strlen(s);
  90
  91                if (!len || len > pathlen)
  92                        continue;
  93                if (memcmp(path + pathlen - len, s, len))
  94                        continue;
  95                if (pathlen > len && path[pathlen - len - 1] != '/')
  96                        continue;
  97                *s = 0;
  98                return 1;
  99        }
 100        return 0;
 101}
 102
 103struct refspec {
 104        char *src;
 105        char *dst;
 106        char force;
 107};
 108
 109/*
 110 * A:B means fast forward remote B with local A.
 111 * +A:B means overwrite remote B with local A.
 112 * +A is a shorthand for +A:A.
 113 * A is a shorthand for A:A.
 114 */
 115static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
 116{
 117        int i;
 118        struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
 119        for (i = 0; i < nr_refspec; i++) {
 120                char *sp, *dp, *ep;
 121                sp = refspec[i];
 122                if (*sp == '+') {
 123                        rs[i].force = 1;
 124                        sp++;
 125                }
 126                ep = strchr(sp, ':');
 127                if (ep) {
 128                        dp = ep + 1;
 129                        *ep = 0;
 130                }
 131                else
 132                        dp = sp;
 133                rs[i].src = sp;
 134                rs[i].dst = dp;
 135        }
 136        rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
 137        return rs;
 138}
 139
 140static int count_refspec_match(const char *pattern,
 141                               struct ref *refs,
 142                               struct ref **matched_ref)
 143{
 144        int match;
 145        int patlen = strlen(pattern);
 146
 147        for (match = 0; refs; refs = refs->next) {
 148                char *name = refs->name;
 149                int namelen = strlen(name);
 150                if (namelen < patlen ||
 151                    memcmp(name + namelen - patlen, pattern, patlen))
 152                        continue;
 153                if (namelen != patlen && name[namelen - patlen - 1] != '/')
 154                        continue;
 155                match++;
 156                *matched_ref = refs;
 157        }
 158        return match;
 159}
 160
 161static void link_dst_tail(struct ref *ref, struct ref ***tail)
 162{
 163        **tail = ref;
 164        *tail = &ref->next;
 165        **tail = NULL;
 166}
 167
 168static struct ref *try_explicit_object_name(const char *name)
 169{
 170        unsigned char sha1[20];
 171        struct ref *ref;
 172        int len;
 173        if (get_sha1(name, sha1))
 174                return NULL;
 175        len = strlen(name) + 1;
 176        ref = xcalloc(1, sizeof(*ref) + len);
 177        memcpy(ref->name, name, len);
 178        memcpy(ref->new_sha1, sha1, 20);
 179        return ref;
 180}
 181
 182static int match_explicit_refs(struct ref *src, struct ref *dst,
 183                               struct ref ***dst_tail, struct refspec *rs)
 184{
 185        int i, errs;
 186        for (i = errs = 0; rs[i].src; i++) {
 187                struct ref *matched_src, *matched_dst;
 188
 189                matched_src = matched_dst = NULL;
 190                switch (count_refspec_match(rs[i].src, src, &matched_src)) {
 191                case 1:
 192                        break;
 193                case 0:
 194                        /* The source could be in the get_sha1() format
 195                         * not a reference name.
 196                         */
 197                        matched_src = try_explicit_object_name(rs[i].src);
 198                        if (matched_src)
 199                                break;
 200                        errs = 1;
 201                        error("src refspec %s does not match any.",
 202                              rs[i].src);
 203                        break;
 204                default:
 205                        errs = 1;
 206                        error("src refspec %s matches more than one.",
 207                              rs[i].src);
 208                        break;
 209                }
 210                switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
 211                case 1:
 212                        break;
 213                case 0:
 214                        if (!memcmp(rs[i].dst, "refs/", 5)) {
 215                                int len = strlen(rs[i].dst) + 1;
 216                                matched_dst = xcalloc(1, sizeof(*dst) + len);
 217                                memcpy(matched_dst->name, rs[i].dst, len);
 218                                link_dst_tail(matched_dst, dst_tail);
 219                        }
 220                        else if (!strcmp(rs[i].src, rs[i].dst) &&
 221                                 matched_src) {
 222                                /* pushing "master:master" when
 223                                 * remote does not have master yet.
 224                                 */
 225                                int len = strlen(matched_src->name) + 1;
 226                                matched_dst = xcalloc(1, sizeof(*dst) + len);
 227                                memcpy(matched_dst->name, matched_src->name,
 228                                       len);
 229                                link_dst_tail(matched_dst, dst_tail);
 230                        }
 231                        else {
 232                                errs = 1;
 233                                error("dst refspec %s does not match any "
 234                                      "existing ref on the remote and does "
 235                                      "not start with refs/.", rs[i].dst);
 236                        }
 237                        break;
 238                default:
 239                        errs = 1;
 240                        error("dst refspec %s matches more than one.",
 241                              rs[i].dst);
 242                        break;
 243                }
 244                if (errs)
 245                        continue;
 246                if (matched_dst->peer_ref) {
 247                        errs = 1;
 248                        error("dst ref %s receives from more than one src.",
 249                              matched_dst->name);
 250                }
 251                else {
 252                        matched_dst->peer_ref = matched_src;
 253                        matched_dst->force = rs[i].force;
 254                }
 255        }
 256        return -errs;
 257}
 258
 259static struct ref *find_ref_by_name(struct ref *list, const char *name)
 260{
 261        for ( ; list; list = list->next)
 262                if (!strcmp(list->name, name))
 263                        return list;
 264        return NULL;
 265}
 266
 267int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
 268               int nr_refspec, char **refspec, int all)
 269{
 270        struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
 271
 272        if (nr_refspec)
 273                return match_explicit_refs(src, dst, dst_tail, rs);
 274
 275        /* pick the remainder */
 276        for ( ; src; src = src->next) {
 277                struct ref *dst_peer;
 278                if (src->peer_ref)
 279                        continue;
 280                dst_peer = find_ref_by_name(dst, src->name);
 281                if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
 282                        continue;
 283                if (!dst_peer) {
 284                        /* Create a new one and link it */
 285                        int len = strlen(src->name) + 1;
 286                        dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
 287                        memcpy(dst_peer->name, src->name, len);
 288                        memcpy(dst_peer->new_sha1, src->new_sha1, 20);
 289                        link_dst_tail(dst_peer, dst_tail);
 290                }
 291                dst_peer->peer_ref = src;
 292        }
 293        return 0;
 294}
 295
 296enum protocol {
 297        PROTO_LOCAL = 1,
 298        PROTO_SSH,
 299        PROTO_GIT,
 300};
 301
 302static enum protocol get_protocol(const char *name)
 303{
 304        if (!strcmp(name, "ssh"))
 305                return PROTO_SSH;
 306        if (!strcmp(name, "git"))
 307                return PROTO_GIT;
 308        if (!strcmp(name, "git+ssh"))
 309                return PROTO_SSH;
 310        if (!strcmp(name, "ssh+git"))
 311                return PROTO_SSH;
 312        die("I don't handle protocol '%s'", name);
 313}
 314
 315#define STR_(s) # s
 316#define STR(s)  STR_(s)
 317
 318#ifndef NO_IPV6
 319
 320static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
 321{
 322        int sockfd = -1;
 323        char *colon, *end;
 324        char *port = STR(DEFAULT_GIT_PORT);
 325        struct addrinfo hints, *ai0, *ai;
 326        int gai;
 327
 328        if (host[0] == '[') {
 329                end = strchr(host + 1, ']');
 330                if (end) {
 331                        *end = 0;
 332                        end++;
 333                        host++;
 334                } else
 335                        end = host;
 336        } else
 337                end = host;
 338        colon = strchr(end, ':');
 339
 340        if (colon) {
 341                *colon = 0;
 342                port = colon + 1;
 343        }
 344
 345        memset(&hints, 0, sizeof(hints));
 346        hints.ai_socktype = SOCK_STREAM;
 347        hints.ai_protocol = IPPROTO_TCP;
 348
 349        gai = getaddrinfo(host, port, &hints, &ai);
 350        if (gai)
 351                die("Unable to look up %s (%s)", host, gai_strerror(gai));
 352
 353        for (ai0 = ai; ai; ai = ai->ai_next) {
 354                sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
 355                if (sockfd < 0)
 356                        continue;
 357                if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
 358                        close(sockfd);
 359                        sockfd = -1;
 360                        continue;
 361                }
 362                break;
 363        }
 364
 365        freeaddrinfo(ai0);
 366
 367        if (sockfd < 0)
 368                die("unable to connect a socket (%s)", strerror(errno));
 369
 370        fd[0] = sockfd;
 371        fd[1] = sockfd;
 372        packet_write(sockfd, "%s %s\n", prog, path);
 373        return 0;
 374}
 375
 376#else /* NO_IPV6 */
 377
 378static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
 379{
 380        int sockfd = -1;
 381        char *colon, *end;
 382        char *port = STR(DEFAULT_GIT_PORT), *ep;
 383        struct hostent *he;
 384        struct sockaddr_in sa;
 385        char **ap;
 386        unsigned int nport;
 387
 388        if (host[0] == '[') {
 389                end = strchr(host + 1, ']');
 390                if (end) {
 391                        *end = 0;
 392                        end++;
 393                        host++;
 394                } else
 395                        end = host;
 396        } else
 397                end = host;
 398        colon = strchr(end, ':');
 399
 400        if (colon) {
 401                *colon = 0;
 402                port = colon + 1;
 403        }
 404
 405
 406        he = gethostbyname(host);
 407        if (!he)
 408                die("Unable to look up %s (%s)", host, hstrerror(h_errno));
 409        nport = strtoul(port, &ep, 10);
 410        if ( ep == port || *ep ) {
 411                /* Not numeric */
 412                struct servent *se = getservbyname(port,"tcp");
 413                if ( !se )
 414                        die("Unknown port %s\n", port);
 415                nport = se->s_port;
 416        }
 417
 418        for (ap = he->h_addr_list; *ap; ap++) {
 419                sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
 420                if (sockfd < 0)
 421                        continue;
 422
 423                memset(&sa, 0, sizeof sa);
 424                sa.sin_family = he->h_addrtype;
 425                sa.sin_port = htons(nport);
 426                memcpy(&sa.sin_addr, ap, he->h_length);
 427
 428                if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
 429                        close(sockfd);
 430                        sockfd = -1;
 431                        continue;
 432                }
 433                break;
 434        }
 435
 436        if (sockfd < 0)
 437                die("unable to connect a socket (%s)", strerror(errno));
 438
 439        fd[0] = sockfd;
 440        fd[1] = sockfd;
 441        packet_write(sockfd, "%s %s\n", prog, path);
 442        return 0;
 443}
 444
 445#endif /* NO_IPV6 */
 446
 447/*
 448 * Yeah, yeah, fixme. Need to pass in the heads etc.
 449 */
 450int git_connect(int fd[2], char *url, const char *prog)
 451{
 452        char command[1024];
 453        char *host, *path;
 454        char *colon;
 455        int pipefd[2][2];
 456        pid_t pid;
 457        enum protocol protocol;
 458
 459        host = NULL;
 460        path = url;
 461        colon = strchr(url, ':');
 462        protocol = PROTO_LOCAL;
 463        if (colon) {
 464                *colon = 0;
 465                host = url;
 466                path = colon+1;
 467                protocol = PROTO_SSH;
 468                if (!memcmp(path, "//", 2)) {
 469                        char *slash = strchr(path + 2, '/');
 470                        if (slash) {
 471                                int nr = slash - path - 2;
 472                                memmove(path, path+2, nr);
 473                                path[nr] = 0;
 474                                protocol = get_protocol(url);
 475                                host = path;
 476                                path = slash;
 477                        }
 478                }
 479        }
 480
 481        if (protocol == PROTO_GIT)
 482                return git_tcp_connect(fd, prog, host, path);
 483
 484        if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
 485                die("unable to create pipe pair for communication");
 486        pid = fork();
 487        if (!pid) {
 488                snprintf(command, sizeof(command), "%s %s", prog,
 489                         sq_quote(path));
 490                dup2(pipefd[1][0], 0);
 491                dup2(pipefd[0][1], 1);
 492                close(pipefd[0][0]);
 493                close(pipefd[0][1]);
 494                close(pipefd[1][0]);
 495                close(pipefd[1][1]);
 496                if (protocol == PROTO_SSH) {
 497                        const char *ssh, *ssh_basename;
 498                        ssh = getenv("GIT_SSH");
 499                        if (!ssh) ssh = "ssh";
 500                        ssh_basename = strrchr(ssh, '/');
 501                        if (!ssh_basename)
 502                                ssh_basename = ssh;
 503                        else
 504                                ssh_basename++;
 505                        execlp(ssh, ssh_basename, host, command, NULL);
 506                }
 507                else
 508                        execlp("sh", "sh", "-c", command, NULL);
 509                die("exec failed");
 510        }               
 511        fd[0] = pipefd[0][0];
 512        fd[1] = pipefd[1][1];
 513        close(pipefd[0][1]);
 514        close(pipefd[1][0]);
 515        return pid;
 516}
 517
 518int finish_connect(pid_t pid)
 519{
 520        int ret;
 521
 522        for (;;) {
 523                ret = waitpid(pid, NULL, 0);
 524                if (!ret)
 525                        break;
 526                if (errno != EINTR)
 527                        break;
 528        }
 529        return ret;
 530}