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