0012954b6f795d75d442f4c58f29dc194888d022
   1#include "cache.h"
   2#include "remote.h"
   3#include "refs.h"
   4
   5struct rewrite {
   6        const char *base;
   7        const char **instead_of;
   8        int instead_of_nr;
   9        int instead_of_alloc;
  10};
  11
  12static struct remote **remotes;
  13static int remotes_alloc;
  14static int remotes_nr;
  15
  16static struct branch **branches;
  17static int branches_alloc;
  18static int branches_nr;
  19
  20static struct branch *current_branch;
  21static const char *default_remote_name;
  22
  23static struct rewrite **rewrite;
  24static int rewrite_alloc;
  25static int rewrite_nr;
  26
  27#define BUF_SIZE (2048)
  28static char buffer[BUF_SIZE];
  29
  30static const char *alias_url(const char *url)
  31{
  32        int i, j;
  33        for (i = 0; i < rewrite_nr; i++) {
  34                if (!rewrite[i])
  35                        continue;
  36                for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
  37                        if (!prefixcmp(url, rewrite[i]->instead_of[j])) {
  38                                char *ret = malloc(strlen(rewrite[i]->base) -
  39                                                   strlen(rewrite[i]->instead_of[j]) +
  40                                                   strlen(url) + 1);
  41                                strcpy(ret, rewrite[i]->base);
  42                                strcat(ret, url + strlen(rewrite[i]->instead_of[j]));
  43                                return ret;
  44                        }
  45                }
  46        }
  47        return url;
  48}
  49
  50static void add_push_refspec(struct remote *remote, const char *ref)
  51{
  52        ALLOC_GROW(remote->push_refspec,
  53                   remote->push_refspec_nr + 1,
  54                   remote->push_refspec_alloc);
  55        remote->push_refspec[remote->push_refspec_nr++] = ref;
  56}
  57
  58static void add_fetch_refspec(struct remote *remote, const char *ref)
  59{
  60        ALLOC_GROW(remote->fetch_refspec,
  61                   remote->fetch_refspec_nr + 1,
  62                   remote->fetch_refspec_alloc);
  63        remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
  64}
  65
  66static void add_url(struct remote *remote, const char *url)
  67{
  68        ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
  69        remote->url[remote->url_nr++] = url;
  70}
  71
  72static void add_url_alias(struct remote *remote, const char *url)
  73{
  74        add_url(remote, alias_url(url));
  75}
  76
  77static struct remote *make_remote(const char *name, int len)
  78{
  79        struct remote *ret;
  80        int i;
  81
  82        for (i = 0; i < remotes_nr; i++) {
  83                if (len ? (!strncmp(name, remotes[i]->name, len) &&
  84                           !remotes[i]->name[len]) :
  85                    !strcmp(name, remotes[i]->name))
  86                        return remotes[i];
  87        }
  88
  89        ret = xcalloc(1, sizeof(struct remote));
  90        ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
  91        remotes[remotes_nr++] = ret;
  92        if (len)
  93                ret->name = xstrndup(name, len);
  94        else
  95                ret->name = xstrdup(name);
  96        return ret;
  97}
  98
  99static void add_merge(struct branch *branch, const char *name)
 100{
 101        ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
 102                   branch->merge_alloc);
 103        branch->merge_name[branch->merge_nr++] = name;
 104}
 105
 106static struct branch *make_branch(const char *name, int len)
 107{
 108        struct branch *ret;
 109        int i;
 110        char *refname;
 111
 112        for (i = 0; i < branches_nr; i++) {
 113                if (len ? (!strncmp(name, branches[i]->name, len) &&
 114                           !branches[i]->name[len]) :
 115                    !strcmp(name, branches[i]->name))
 116                        return branches[i];
 117        }
 118
 119        ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
 120        ret = xcalloc(1, sizeof(struct branch));
 121        branches[branches_nr++] = ret;
 122        if (len)
 123                ret->name = xstrndup(name, len);
 124        else
 125                ret->name = xstrdup(name);
 126        refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
 127        strcpy(refname, "refs/heads/");
 128        strcpy(refname + strlen("refs/heads/"), ret->name);
 129        ret->refname = refname;
 130
 131        return ret;
 132}
 133
 134static struct rewrite *make_rewrite(const char *base, int len)
 135{
 136        struct rewrite *ret;
 137        int i;
 138
 139        for (i = 0; i < rewrite_nr; i++) {
 140                if (len ? (!strncmp(base, rewrite[i]->base, len) &&
 141                           !rewrite[i]->base[len]) :
 142                    !strcmp(base, rewrite[i]->base))
 143                        return rewrite[i];
 144        }
 145
 146        ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
 147        ret = xcalloc(1, sizeof(struct rewrite));
 148        rewrite[rewrite_nr++] = ret;
 149        if (len)
 150                ret->base = xstrndup(base, len);
 151        else
 152                ret->base = xstrdup(base);
 153
 154        return ret;
 155}
 156
 157static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
 158{
 159        ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
 160        rewrite->instead_of[rewrite->instead_of_nr++] = instead_of;
 161}
 162
 163static void read_remotes_file(struct remote *remote)
 164{
 165        FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
 166
 167        if (!f)
 168                return;
 169        while (fgets(buffer, BUF_SIZE, f)) {
 170                int value_list;
 171                char *s, *p;
 172
 173                if (!prefixcmp(buffer, "URL:")) {
 174                        value_list = 0;
 175                        s = buffer + 4;
 176                } else if (!prefixcmp(buffer, "Push:")) {
 177                        value_list = 1;
 178                        s = buffer + 5;
 179                } else if (!prefixcmp(buffer, "Pull:")) {
 180                        value_list = 2;
 181                        s = buffer + 5;
 182                } else
 183                        continue;
 184
 185                while (isspace(*s))
 186                        s++;
 187                if (!*s)
 188                        continue;
 189
 190                p = s + strlen(s);
 191                while (isspace(p[-1]))
 192                        *--p = 0;
 193
 194                switch (value_list) {
 195                case 0:
 196                        add_url_alias(remote, xstrdup(s));
 197                        break;
 198                case 1:
 199                        add_push_refspec(remote, xstrdup(s));
 200                        break;
 201                case 2:
 202                        add_fetch_refspec(remote, xstrdup(s));
 203                        break;
 204                }
 205        }
 206        fclose(f);
 207}
 208
 209static void read_branches_file(struct remote *remote)
 210{
 211        const char *slash = strchr(remote->name, '/');
 212        char *frag;
 213        char *branch;
 214        int n = slash ? slash - remote->name : 1000;
 215        FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
 216        char *s, *p;
 217        int len;
 218
 219        if (!f)
 220                return;
 221        s = fgets(buffer, BUF_SIZE, f);
 222        fclose(f);
 223        if (!s)
 224                return;
 225        while (isspace(*s))
 226                s++;
 227        if (!*s)
 228                return;
 229        p = s + strlen(s);
 230        while (isspace(p[-1]))
 231                *--p = 0;
 232        len = p - s;
 233        if (slash)
 234                len += strlen(slash);
 235        p = xmalloc(len + 1);
 236        strcpy(p, s);
 237        if (slash)
 238                strcat(p, slash);
 239        frag = strchr(p, '#');
 240        if (frag) {
 241                *(frag++) = '\0';
 242                branch = xmalloc(strlen(frag) + 12);
 243                strcpy(branch, "refs/heads/");
 244                strcat(branch, frag);
 245        } else {
 246                branch = "refs/heads/master";
 247        }
 248        add_url_alias(remote, p);
 249        add_fetch_refspec(remote, branch);
 250        remote->fetch_tags = 1; /* always auto-follow */
 251}
 252
 253static int handle_config(const char *key, const char *value)
 254{
 255        const char *name;
 256        const char *subkey;
 257        struct remote *remote;
 258        struct branch *branch;
 259        if (!prefixcmp(key, "branch.")) {
 260                name = key + 7;
 261                subkey = strrchr(name, '.');
 262                if (!subkey)
 263                        return 0;
 264                branch = make_branch(name, subkey - name);
 265                if (!strcmp(subkey, ".remote")) {
 266                        if (!value)
 267                                return config_error_nonbool(key);
 268                        branch->remote_name = xstrdup(value);
 269                        if (branch == current_branch)
 270                                default_remote_name = branch->remote_name;
 271                } else if (!strcmp(subkey, ".merge")) {
 272                        if (!value)
 273                                return config_error_nonbool(key);
 274                        add_merge(branch, xstrdup(value));
 275                }
 276                return 0;
 277        }
 278        if (!prefixcmp(key, "url.")) {
 279                struct rewrite *rewrite;
 280                name = key + 5;
 281                subkey = strrchr(name, '.');
 282                if (!subkey)
 283                        return 0;
 284                rewrite = make_rewrite(name, subkey - name);
 285                if (!strcmp(subkey, ".insteadof")) {
 286                        if (!value)
 287                                return config_error_nonbool(key);
 288                        add_instead_of(rewrite, xstrdup(value));
 289                }
 290        }
 291        if (prefixcmp(key,  "remote."))
 292                return 0;
 293        name = key + 7;
 294        subkey = strrchr(name, '.');
 295        if (!subkey)
 296                return error("Config with no key for remote %s", name);
 297        if (*subkey == '/') {
 298                warning("Config remote shorthand cannot begin with '/': %s", name);
 299                return 0;
 300        }
 301        remote = make_remote(name, subkey - name);
 302        if (!value) {
 303                /* if we ever have a boolean variable, e.g. "remote.*.disabled"
 304                 * [remote "frotz"]
 305                 *      disabled
 306                 * is a valid way to set it to true; we get NULL in value so
 307                 * we need to handle it here.
 308                 *
 309                 * if (!strcmp(subkey, ".disabled")) {
 310                 *      val = git_config_bool(key, value);
 311                 *      return 0;
 312                 * } else
 313                 *
 314                 */
 315                return 0; /* ignore unknown booleans */
 316        }
 317        if (!strcmp(subkey, ".url")) {
 318                add_url(remote, xstrdup(value));
 319        } else if (!strcmp(subkey, ".push")) {
 320                add_push_refspec(remote, xstrdup(value));
 321        } else if (!strcmp(subkey, ".fetch")) {
 322                add_fetch_refspec(remote, xstrdup(value));
 323        } else if (!strcmp(subkey, ".receivepack")) {
 324                if (!remote->receivepack)
 325                        remote->receivepack = xstrdup(value);
 326                else
 327                        error("more than one receivepack given, using the first");
 328        } else if (!strcmp(subkey, ".uploadpack")) {
 329                if (!remote->uploadpack)
 330                        remote->uploadpack = xstrdup(value);
 331                else
 332                        error("more than one uploadpack given, using the first");
 333        } else if (!strcmp(subkey, ".tagopt")) {
 334                if (!strcmp(value, "--no-tags"))
 335                        remote->fetch_tags = -1;
 336        } else if (!strcmp(subkey, ".proxy")) {
 337                remote->http_proxy = xstrdup(value);
 338        }
 339        return 0;
 340}
 341
 342static void alias_all_urls(void)
 343{
 344        int i, j;
 345        for (i = 0; i < remotes_nr; i++) {
 346                if (!remotes[i])
 347                        continue;
 348                for (j = 0; j < remotes[i]->url_nr; j++) {
 349                        remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
 350                }
 351        }
 352}
 353
 354static void read_config(void)
 355{
 356        unsigned char sha1[20];
 357        const char *head_ref;
 358        int flag;
 359        if (default_remote_name) // did this already
 360                return;
 361        default_remote_name = xstrdup("origin");
 362        current_branch = NULL;
 363        head_ref = resolve_ref("HEAD", sha1, 0, &flag);
 364        if (head_ref && (flag & REF_ISSYMREF) &&
 365            !prefixcmp(head_ref, "refs/heads/")) {
 366                current_branch =
 367                        make_branch(head_ref + strlen("refs/heads/"), 0);
 368        }
 369        git_config(handle_config);
 370        alias_all_urls();
 371}
 372
 373struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
 374{
 375        int i;
 376        struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
 377        for (i = 0; i < nr_refspec; i++) {
 378                const char *sp, *ep, *gp;
 379                sp = refspec[i];
 380                if (*sp == '+') {
 381                        rs[i].force = 1;
 382                        sp++;
 383                }
 384                gp = strchr(sp, '*');
 385                ep = strchr(sp, ':');
 386                if (gp && ep && gp > ep)
 387                        gp = NULL;
 388                if (ep) {
 389                        if (ep[1]) {
 390                                const char *glob = strchr(ep + 1, '*');
 391                                if (!glob)
 392                                        gp = NULL;
 393                                if (gp)
 394                                        rs[i].dst = xstrndup(ep + 1,
 395                                                             glob - ep - 1);
 396                                else
 397                                        rs[i].dst = xstrdup(ep + 1);
 398                        }
 399                } else {
 400                        ep = sp + strlen(sp);
 401                }
 402                if (gp) {
 403                        rs[i].pattern = 1;
 404                        ep = gp;
 405                }
 406                rs[i].src = xstrndup(sp, ep - sp);
 407        }
 408        return rs;
 409}
 410
 411static int valid_remote_nick(const char *name)
 412{
 413        if (!name[0] || /* not empty */
 414            (name[0] == '.' && /* not "." */
 415             (!name[1] || /* not ".." */
 416              (name[1] == '.' && !name[2]))))
 417                return 0;
 418        return !strchr(name, '/'); /* no slash */
 419}
 420
 421struct remote *remote_get(const char *name)
 422{
 423        struct remote *ret;
 424
 425        read_config();
 426        if (!name)
 427                name = default_remote_name;
 428        ret = make_remote(name, 0);
 429        if (valid_remote_nick(name)) {
 430                if (!ret->url)
 431                        read_remotes_file(ret);
 432                if (!ret->url)
 433                        read_branches_file(ret);
 434        }
 435        if (!ret->url)
 436                add_url_alias(ret, name);
 437        if (!ret->url)
 438                return NULL;
 439        ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
 440        ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
 441        return ret;
 442}
 443
 444int for_each_remote(each_remote_fn fn, void *priv)
 445{
 446        int i, result = 0;
 447        read_config();
 448        for (i = 0; i < remotes_nr && !result; i++) {
 449                struct remote *r = remotes[i];
 450                if (!r)
 451                        continue;
 452                if (!r->fetch)
 453                        r->fetch = parse_ref_spec(r->fetch_refspec_nr,
 454                                        r->fetch_refspec);
 455                if (!r->push)
 456                        r->push = parse_ref_spec(r->push_refspec_nr,
 457                                        r->push_refspec);
 458                result = fn(r, priv);
 459        }
 460        return result;
 461}
 462
 463void ref_remove_duplicates(struct ref *ref_map)
 464{
 465        struct ref **posn;
 466        struct ref *next;
 467        for (; ref_map; ref_map = ref_map->next) {
 468                if (!ref_map->peer_ref)
 469                        continue;
 470                posn = &ref_map->next;
 471                while (*posn) {
 472                        if ((*posn)->peer_ref &&
 473                            !strcmp((*posn)->peer_ref->name,
 474                                    ref_map->peer_ref->name)) {
 475                                if (strcmp((*posn)->name, ref_map->name))
 476                                        die("%s tracks both %s and %s",
 477                                            ref_map->peer_ref->name,
 478                                            (*posn)->name, ref_map->name);
 479                                next = (*posn)->next;
 480                                free((*posn)->peer_ref);
 481                                free(*posn);
 482                                *posn = next;
 483                        } else {
 484                                posn = &(*posn)->next;
 485                        }
 486                }
 487        }
 488}
 489
 490int remote_has_url(struct remote *remote, const char *url)
 491{
 492        int i;
 493        for (i = 0; i < remote->url_nr; i++) {
 494                if (!strcmp(remote->url[i], url))
 495                        return 1;
 496        }
 497        return 0;
 498}
 499
 500int remote_find_tracking(struct remote *remote, struct refspec *refspec)
 501{
 502        int find_src = refspec->src == NULL;
 503        char *needle, **result;
 504        int i;
 505
 506        if (find_src) {
 507                if (!refspec->dst)
 508                        return error("find_tracking: need either src or dst");
 509                needle = refspec->dst;
 510                result = &refspec->src;
 511        } else {
 512                needle = refspec->src;
 513                result = &refspec->dst;
 514        }
 515
 516        for (i = 0; i < remote->fetch_refspec_nr; i++) {
 517                struct refspec *fetch = &remote->fetch[i];
 518                const char *key = find_src ? fetch->dst : fetch->src;
 519                const char *value = find_src ? fetch->src : fetch->dst;
 520                if (!fetch->dst)
 521                        continue;
 522                if (fetch->pattern) {
 523                        if (!prefixcmp(needle, key)) {
 524                                *result = xmalloc(strlen(value) +
 525                                                  strlen(needle) -
 526                                                  strlen(key) + 1);
 527                                strcpy(*result, value);
 528                                strcpy(*result + strlen(value),
 529                                       needle + strlen(key));
 530                                refspec->force = fetch->force;
 531                                return 0;
 532                        }
 533                } else if (!strcmp(needle, key)) {
 534                        *result = xstrdup(value);
 535                        refspec->force = fetch->force;
 536                        return 0;
 537                }
 538        }
 539        return -1;
 540}
 541
 542struct ref *alloc_ref(unsigned namelen)
 543{
 544        struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
 545        memset(ret, 0, sizeof(struct ref) + namelen);
 546        return ret;
 547}
 548
 549static struct ref *copy_ref(const struct ref *ref)
 550{
 551        struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
 552        memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
 553        ret->next = NULL;
 554        return ret;
 555}
 556
 557struct ref *copy_ref_list(const struct ref *ref)
 558{
 559        struct ref *ret = NULL;
 560        struct ref **tail = &ret;
 561        while (ref) {
 562                *tail = copy_ref(ref);
 563                ref = ref->next;
 564                tail = &((*tail)->next);
 565        }
 566        return ret;
 567}
 568
 569void free_refs(struct ref *ref)
 570{
 571        struct ref *next;
 572        while (ref) {
 573                next = ref->next;
 574                if (ref->peer_ref)
 575                        free(ref->peer_ref);
 576                free(ref);
 577                ref = next;
 578        }
 579}
 580
 581static int count_refspec_match(const char *pattern,
 582                               struct ref *refs,
 583                               struct ref **matched_ref)
 584{
 585        int patlen = strlen(pattern);
 586        struct ref *matched_weak = NULL;
 587        struct ref *matched = NULL;
 588        int weak_match = 0;
 589        int match = 0;
 590
 591        for (weak_match = match = 0; refs; refs = refs->next) {
 592                char *name = refs->name;
 593                int namelen = strlen(name);
 594
 595                if (!refname_match(pattern, name, ref_rev_parse_rules))
 596                        continue;
 597
 598                /* A match is "weak" if it is with refs outside
 599                 * heads or tags, and did not specify the pattern
 600                 * in full (e.g. "refs/remotes/origin/master") or at
 601                 * least from the toplevel (e.g. "remotes/origin/master");
 602                 * otherwise "git push $URL master" would result in
 603                 * ambiguity between remotes/origin/master and heads/master
 604                 * at the remote site.
 605                 */
 606                if (namelen != patlen &&
 607                    patlen != namelen - 5 &&
 608                    prefixcmp(name, "refs/heads/") &&
 609                    prefixcmp(name, "refs/tags/")) {
 610                        /* We want to catch the case where only weak
 611                         * matches are found and there are multiple
 612                         * matches, and where more than one strong
 613                         * matches are found, as ambiguous.  One
 614                         * strong match with zero or more weak matches
 615                         * are acceptable as a unique match.
 616                         */
 617                        matched_weak = refs;
 618                        weak_match++;
 619                }
 620                else {
 621                        matched = refs;
 622                        match++;
 623                }
 624        }
 625        if (!matched) {
 626                *matched_ref = matched_weak;
 627                return weak_match;
 628        }
 629        else {
 630                *matched_ref = matched;
 631                return match;
 632        }
 633}
 634
 635static void tail_link_ref(struct ref *ref, struct ref ***tail)
 636{
 637        **tail = ref;
 638        while (ref->next)
 639                ref = ref->next;
 640        *tail = &ref->next;
 641}
 642
 643static struct ref *try_explicit_object_name(const char *name)
 644{
 645        unsigned char sha1[20];
 646        struct ref *ref;
 647        int len;
 648
 649        if (!*name) {
 650                ref = alloc_ref(20);
 651                strcpy(ref->name, "(delete)");
 652                hashclr(ref->new_sha1);
 653                return ref;
 654        }
 655        if (get_sha1(name, sha1))
 656                return NULL;
 657        len = strlen(name) + 1;
 658        ref = alloc_ref(len);
 659        memcpy(ref->name, name, len);
 660        hashcpy(ref->new_sha1, sha1);
 661        return ref;
 662}
 663
 664static struct ref *make_linked_ref(const char *name, struct ref ***tail)
 665{
 666        struct ref *ret;
 667        size_t len;
 668
 669        len = strlen(name) + 1;
 670        ret = alloc_ref(len);
 671        memcpy(ret->name, name, len);
 672        tail_link_ref(ret, tail);
 673        return ret;
 674}
 675
 676static int match_explicit(struct ref *src, struct ref *dst,
 677                          struct ref ***dst_tail,
 678                          struct refspec *rs,
 679                          int errs)
 680{
 681        struct ref *matched_src, *matched_dst;
 682
 683        const char *dst_value = rs->dst;
 684
 685        if (rs->pattern)
 686                return errs;
 687
 688        matched_src = matched_dst = NULL;
 689        switch (count_refspec_match(rs->src, src, &matched_src)) {
 690        case 1:
 691                break;
 692        case 0:
 693                /* The source could be in the get_sha1() format
 694                 * not a reference name.  :refs/other is a
 695                 * way to delete 'other' ref at the remote end.
 696                 */
 697                matched_src = try_explicit_object_name(rs->src);
 698                if (!matched_src)
 699                        error("src refspec %s does not match any.", rs->src);
 700                break;
 701        default:
 702                matched_src = NULL;
 703                error("src refspec %s matches more than one.", rs->src);
 704                break;
 705        }
 706
 707        if (!matched_src)
 708                errs = 1;
 709
 710        if (!dst_value) {
 711                if (!matched_src)
 712                        return errs;
 713                dst_value = matched_src->name;
 714        }
 715
 716        switch (count_refspec_match(dst_value, dst, &matched_dst)) {
 717        case 1:
 718                break;
 719        case 0:
 720                if (!memcmp(dst_value, "refs/", 5))
 721                        matched_dst = make_linked_ref(dst_value, dst_tail);
 722                else
 723                        error("dst refspec %s does not match any "
 724                              "existing ref on the remote and does "
 725                              "not start with refs/.", dst_value);
 726                break;
 727        default:
 728                matched_dst = NULL;
 729                error("dst refspec %s matches more than one.",
 730                      dst_value);
 731                break;
 732        }
 733        if (errs || !matched_dst)
 734                return 1;
 735        if (matched_dst->peer_ref) {
 736                errs = 1;
 737                error("dst ref %s receives from more than one src.",
 738                      matched_dst->name);
 739        }
 740        else {
 741                matched_dst->peer_ref = matched_src;
 742                matched_dst->force = rs->force;
 743        }
 744        return errs;
 745}
 746
 747static int match_explicit_refs(struct ref *src, struct ref *dst,
 748                               struct ref ***dst_tail, struct refspec *rs,
 749                               int rs_nr)
 750{
 751        int i, errs;
 752        for (i = errs = 0; i < rs_nr; i++)
 753                errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
 754        return -errs;
 755}
 756
 757static const struct refspec *check_pattern_match(const struct refspec *rs,
 758                                                 int rs_nr,
 759                                                 const struct ref *src)
 760{
 761        int i;
 762        for (i = 0; i < rs_nr; i++) {
 763                if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
 764                        return rs + i;
 765        }
 766        return NULL;
 767}
 768
 769/*
 770 * Note. This is used only by "push"; refspec matching rules for
 771 * push and fetch are subtly different, so do not try to reuse it
 772 * without thinking.
 773 */
 774int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
 775               int nr_refspec, const char **refspec, int flags)
 776{
 777        struct refspec *rs =
 778                parse_ref_spec(nr_refspec, (const char **) refspec);
 779        int send_all = flags & MATCH_REFS_ALL;
 780        int send_mirror = flags & MATCH_REFS_MIRROR;
 781
 782        if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
 783                return -1;
 784
 785        /* pick the remainder */
 786        for ( ; src; src = src->next) {
 787                struct ref *dst_peer;
 788                const struct refspec *pat = NULL;
 789                char *dst_name;
 790                if (src->peer_ref)
 791                        continue;
 792                if (nr_refspec) {
 793                        pat = check_pattern_match(rs, nr_refspec, src);
 794                        if (!pat)
 795                                continue;
 796                }
 797                else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
 798                        /*
 799                         * "matching refs"; traditionally we pushed everything
 800                         * including refs outside refs/heads/ hierarchy, but
 801                         * that does not make much sense these days.
 802                         */
 803                        continue;
 804
 805                if (pat) {
 806                        const char *dst_side = pat->dst ? pat->dst : pat->src;
 807                        dst_name = xmalloc(strlen(dst_side) +
 808                                           strlen(src->name) -
 809                                           strlen(pat->src) + 2);
 810                        strcpy(dst_name, dst_side);
 811                        strcat(dst_name, src->name + strlen(pat->src));
 812                } else
 813                        dst_name = xstrdup(src->name);
 814                dst_peer = find_ref_by_name(dst, dst_name);
 815                if (dst_peer && dst_peer->peer_ref)
 816                        /* We're already sending something to this ref. */
 817                        goto free_name;
 818
 819                if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
 820                        /*
 821                         * Remote doesn't have it, and we have no
 822                         * explicit pattern, and we don't have
 823                         * --all nor --mirror.
 824                         */
 825                        goto free_name;
 826                if (!dst_peer) {
 827                        /* Create a new one and link it */
 828                        dst_peer = make_linked_ref(dst_name, dst_tail);
 829                        hashcpy(dst_peer->new_sha1, src->new_sha1);
 830                }
 831                dst_peer->peer_ref = src;
 832                if (pat)
 833                        dst_peer->force = pat->force;
 834        free_name:
 835                free(dst_name);
 836        }
 837        return 0;
 838}
 839
 840struct branch *branch_get(const char *name)
 841{
 842        struct branch *ret;
 843
 844        read_config();
 845        if (!name || !*name || !strcmp(name, "HEAD"))
 846                ret = current_branch;
 847        else
 848                ret = make_branch(name, 0);
 849        if (ret && ret->remote_name) {
 850                ret->remote = remote_get(ret->remote_name);
 851                if (ret->merge_nr) {
 852                        int i;
 853                        ret->merge = xcalloc(sizeof(*ret->merge),
 854                                             ret->merge_nr);
 855                        for (i = 0; i < ret->merge_nr; i++) {
 856                                ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
 857                                ret->merge[i]->src = xstrdup(ret->merge_name[i]);
 858                                remote_find_tracking(ret->remote,
 859                                                     ret->merge[i]);
 860                        }
 861                }
 862        }
 863        return ret;
 864}
 865
 866int branch_has_merge_config(struct branch *branch)
 867{
 868        return branch && !!branch->merge;
 869}
 870
 871int branch_merge_matches(struct branch *branch,
 872                                 int i,
 873                                 const char *refname)
 874{
 875        if (!branch || i < 0 || i >= branch->merge_nr)
 876                return 0;
 877        return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
 878}
 879
 880static struct ref *get_expanded_map(const struct ref *remote_refs,
 881                                    const struct refspec *refspec)
 882{
 883        const struct ref *ref;
 884        struct ref *ret = NULL;
 885        struct ref **tail = &ret;
 886
 887        int remote_prefix_len = strlen(refspec->src);
 888        int local_prefix_len = strlen(refspec->dst);
 889
 890        for (ref = remote_refs; ref; ref = ref->next) {
 891                if (strchr(ref->name, '^'))
 892                        continue; /* a dereference item */
 893                if (!prefixcmp(ref->name, refspec->src)) {
 894                        const char *match;
 895                        struct ref *cpy = copy_ref(ref);
 896                        match = ref->name + remote_prefix_len;
 897
 898                        cpy->peer_ref = alloc_ref(local_prefix_len +
 899                                                  strlen(match) + 1);
 900                        sprintf(cpy->peer_ref->name, "%s%s",
 901                                refspec->dst, match);
 902                        if (refspec->force)
 903                                cpy->peer_ref->force = 1;
 904                        *tail = cpy;
 905                        tail = &cpy->next;
 906                }
 907        }
 908
 909        return ret;
 910}
 911
 912static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
 913{
 914        const struct ref *ref;
 915        for (ref = refs; ref; ref = ref->next) {
 916                if (refname_match(name, ref->name, ref_fetch_rules))
 917                        return ref;
 918        }
 919        return NULL;
 920}
 921
 922struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
 923{
 924        const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
 925
 926        if (!ref)
 927                return NULL;
 928
 929        return copy_ref(ref);
 930}
 931
 932static struct ref *get_local_ref(const char *name)
 933{
 934        struct ref *ret;
 935        if (!name)
 936                return NULL;
 937
 938        if (!prefixcmp(name, "refs/")) {
 939                ret = alloc_ref(strlen(name) + 1);
 940                strcpy(ret->name, name);
 941                return ret;
 942        }
 943
 944        if (!prefixcmp(name, "heads/") ||
 945            !prefixcmp(name, "tags/") ||
 946            !prefixcmp(name, "remotes/")) {
 947                ret = alloc_ref(strlen(name) + 6);
 948                sprintf(ret->name, "refs/%s", name);
 949                return ret;
 950        }
 951
 952        ret = alloc_ref(strlen(name) + 12);
 953        sprintf(ret->name, "refs/heads/%s", name);
 954        return ret;
 955}
 956
 957int get_fetch_map(const struct ref *remote_refs,
 958                  const struct refspec *refspec,
 959                  struct ref ***tail,
 960                  int missing_ok)
 961{
 962        struct ref *ref_map, *rm;
 963
 964        if (refspec->pattern) {
 965                ref_map = get_expanded_map(remote_refs, refspec);
 966        } else {
 967                const char *name = refspec->src[0] ? refspec->src : "HEAD";
 968
 969                ref_map = get_remote_ref(remote_refs, name);
 970                if (!missing_ok && !ref_map)
 971                        die("Couldn't find remote ref %s", name);
 972                if (ref_map) {
 973                        ref_map->peer_ref = get_local_ref(refspec->dst);
 974                        if (ref_map->peer_ref && refspec->force)
 975                                ref_map->peer_ref->force = 1;
 976                }
 977        }
 978
 979        for (rm = ref_map; rm; rm = rm->next) {
 980                if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
 981                        die("* refusing to create funny ref '%s' locally",
 982                            rm->peer_ref->name);
 983        }
 984
 985        if (ref_map)
 986                tail_link_ref(ref_map, tail);
 987
 988        return 0;
 989}