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