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