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