remote.con commit Cleanup unnecessary break in remote.c (7dfee37)
   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                branch = make_branch(name, subkey - name);
 224                if (!subkey)
 225                        return 0;
 226                if (!value)
 227                        return 0;
 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        }
 282        return 0;
 283}
 284
 285static void read_config(void)
 286{
 287        unsigned char sha1[20];
 288        const char *head_ref;
 289        int flag;
 290        if (default_remote_name) // did this already
 291                return;
 292        default_remote_name = xstrdup("origin");
 293        current_branch = NULL;
 294        head_ref = resolve_ref("HEAD", sha1, 0, &flag);
 295        if (head_ref && (flag & REF_ISSYMREF) &&
 296            !prefixcmp(head_ref, "refs/heads/")) {
 297                current_branch =
 298                        make_branch(head_ref + strlen("refs/heads/"), 0);
 299        }
 300        git_config(handle_config);
 301}
 302
 303struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
 304{
 305        int i;
 306        struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
 307        for (i = 0; i < nr_refspec; i++) {
 308                const char *sp, *ep, *gp;
 309                sp = refspec[i];
 310                if (*sp == '+') {
 311                        rs[i].force = 1;
 312                        sp++;
 313                }
 314                gp = strchr(sp, '*');
 315                ep = strchr(sp, ':');
 316                if (gp && ep && gp > ep)
 317                        gp = NULL;
 318                if (ep) {
 319                        if (ep[1]) {
 320                                const char *glob = strchr(ep + 1, '*');
 321                                if (!glob)
 322                                        gp = NULL;
 323                                if (gp)
 324                                        rs[i].dst = xstrndup(ep + 1,
 325                                                             glob - ep - 1);
 326                                else
 327                                        rs[i].dst = xstrdup(ep + 1);
 328                        }
 329                } else {
 330                        ep = sp + strlen(sp);
 331                }
 332                if (gp) {
 333                        rs[i].pattern = 1;
 334                        ep = gp;
 335                }
 336                rs[i].src = xstrndup(sp, ep - sp);
 337        }
 338        return rs;
 339}
 340
 341struct remote *remote_get(const char *name)
 342{
 343        struct remote *ret;
 344
 345        read_config();
 346        if (!name)
 347                name = default_remote_name;
 348        ret = make_remote(name, 0);
 349        if (name[0] != '/') {
 350                if (!ret->url)
 351                        read_remotes_file(ret);
 352                if (!ret->url)
 353                        read_branches_file(ret);
 354        }
 355        if (!ret->url)
 356                add_url(ret, name);
 357        if (!ret->url)
 358                return NULL;
 359        ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
 360        ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
 361        return ret;
 362}
 363
 364int for_each_remote(each_remote_fn fn, void *priv)
 365{
 366        int i, result = 0;
 367        read_config();
 368        for (i = 0; i < allocated_remotes && !result; i++) {
 369                struct remote *r = remotes[i];
 370                if (!r)
 371                        continue;
 372                if (!r->fetch)
 373                        r->fetch = parse_ref_spec(r->fetch_refspec_nr,
 374                                        r->fetch_refspec);
 375                if (!r->push)
 376                        r->push = parse_ref_spec(r->push_refspec_nr,
 377                                        r->push_refspec);
 378                result = fn(r, priv);
 379        }
 380        return result;
 381}
 382
 383int remote_has_url(struct remote *remote, const char *url)
 384{
 385        int i;
 386        for (i = 0; i < remote->url_nr; i++) {
 387                if (!strcmp(remote->url[i], url))
 388                        return 1;
 389        }
 390        return 0;
 391}
 392
 393/*
 394 * Returns true if, under the matching rules for fetching, name is the
 395 * same as the given full name.
 396 */
 397static int ref_matches_abbrev(const char *name, const char *full)
 398{
 399        if (!prefixcmp(name, "refs/") || !strcmp(name, "HEAD"))
 400                return !strcmp(name, full);
 401        if (prefixcmp(full, "refs/"))
 402                return 0;
 403        if (!prefixcmp(name, "heads/") ||
 404            !prefixcmp(name, "tags/") ||
 405            !prefixcmp(name, "remotes/"))
 406                return !strcmp(name, full + 5);
 407        if (prefixcmp(full + 5, "heads/"))
 408                return 0;
 409        return !strcmp(full + 11, name);
 410}
 411
 412int remote_find_tracking(struct remote *remote, struct refspec *refspec)
 413{
 414        int find_src = refspec->src == NULL;
 415        char *needle, **result;
 416        int i;
 417
 418        if (find_src) {
 419                if (!refspec->dst)
 420                        return error("find_tracking: need either src or dst");
 421                needle = refspec->dst;
 422                result = &refspec->src;
 423        } else {
 424                needle = refspec->src;
 425                result = &refspec->dst;
 426        }
 427
 428        for (i = 0; i < remote->fetch_refspec_nr; i++) {
 429                struct refspec *fetch = &remote->fetch[i];
 430                const char *key = find_src ? fetch->dst : fetch->src;
 431                const char *value = find_src ? fetch->src : fetch->dst;
 432                if (!fetch->dst)
 433                        continue;
 434                if (fetch->pattern) {
 435                        if (!prefixcmp(needle, key)) {
 436                                *result = xmalloc(strlen(value) +
 437                                                  strlen(needle) -
 438                                                  strlen(key) + 1);
 439                                strcpy(*result, value);
 440                                strcpy(*result + strlen(value),
 441                                       needle + strlen(key));
 442                                refspec->force = fetch->force;
 443                                return 0;
 444                        }
 445                } else if (!strcmp(needle, key)) {
 446                        *result = xstrdup(value);
 447                        refspec->force = fetch->force;
 448                        return 0;
 449                }
 450        }
 451        return -1;
 452}
 453
 454struct ref *alloc_ref(unsigned namelen)
 455{
 456        struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
 457        memset(ret, 0, sizeof(struct ref) + namelen);
 458        return ret;
 459}
 460
 461static struct ref *copy_ref(struct ref *ref)
 462{
 463        struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
 464        memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
 465        ret->next = NULL;
 466        return ret;
 467}
 468
 469void free_refs(struct ref *ref)
 470{
 471        struct ref *next;
 472        while (ref) {
 473                next = ref->next;
 474                if (ref->peer_ref)
 475                        free(ref->peer_ref);
 476                free(ref);
 477                ref = next;
 478        }
 479}
 480
 481static int count_refspec_match(const char *pattern,
 482                               struct ref *refs,
 483                               struct ref **matched_ref)
 484{
 485        int patlen = strlen(pattern);
 486        struct ref *matched_weak = NULL;
 487        struct ref *matched = NULL;
 488        int weak_match = 0;
 489        int match = 0;
 490
 491        for (weak_match = match = 0; refs; refs = refs->next) {
 492                char *name = refs->name;
 493                int namelen = strlen(name);
 494
 495                if (namelen < patlen ||
 496                    memcmp(name + namelen - patlen, pattern, patlen))
 497                        continue;
 498                if (namelen != patlen && name[namelen - patlen - 1] != '/')
 499                        continue;
 500
 501                /* A match is "weak" if it is with refs outside
 502                 * heads or tags, and did not specify the pattern
 503                 * in full (e.g. "refs/remotes/origin/master") or at
 504                 * least from the toplevel (e.g. "remotes/origin/master");
 505                 * otherwise "git push $URL master" would result in
 506                 * ambiguity between remotes/origin/master and heads/master
 507                 * at the remote site.
 508                 */
 509                if (namelen != patlen &&
 510                    patlen != namelen - 5 &&
 511                    prefixcmp(name, "refs/heads/") &&
 512                    prefixcmp(name, "refs/tags/")) {
 513                        /* We want to catch the case where only weak
 514                         * matches are found and there are multiple
 515                         * matches, and where more than one strong
 516                         * matches are found, as ambiguous.  One
 517                         * strong match with zero or more weak matches
 518                         * are acceptable as a unique match.
 519                         */
 520                        matched_weak = refs;
 521                        weak_match++;
 522                }
 523                else {
 524                        matched = refs;
 525                        match++;
 526                }
 527        }
 528        if (!matched) {
 529                *matched_ref = matched_weak;
 530                return weak_match;
 531        }
 532        else {
 533                *matched_ref = matched;
 534                return match;
 535        }
 536}
 537
 538static void tail_link_ref(struct ref *ref, struct ref ***tail)
 539{
 540        **tail = ref;
 541        while (ref->next)
 542                ref = ref->next;
 543        *tail = &ref->next;
 544}
 545
 546static struct ref *try_explicit_object_name(const char *name)
 547{
 548        unsigned char sha1[20];
 549        struct ref *ref;
 550        int len;
 551
 552        if (!*name) {
 553                ref = alloc_ref(20);
 554                strcpy(ref->name, "(delete)");
 555                hashclr(ref->new_sha1);
 556                return ref;
 557        }
 558        if (get_sha1(name, sha1))
 559                return NULL;
 560        len = strlen(name) + 1;
 561        ref = alloc_ref(len);
 562        memcpy(ref->name, name, len);
 563        hashcpy(ref->new_sha1, sha1);
 564        return ref;
 565}
 566
 567static struct ref *make_linked_ref(const char *name, struct ref ***tail)
 568{
 569        struct ref *ret;
 570        size_t len;
 571
 572        len = strlen(name) + 1;
 573        ret = alloc_ref(len);
 574        memcpy(ret->name, name, len);
 575        tail_link_ref(ret, tail);
 576        return ret;
 577}
 578
 579static int match_explicit(struct ref *src, struct ref *dst,
 580                          struct ref ***dst_tail,
 581                          struct refspec *rs,
 582                          int errs)
 583{
 584        struct ref *matched_src, *matched_dst;
 585
 586        const char *dst_value = rs->dst;
 587
 588        if (rs->pattern)
 589                return errs;
 590
 591        matched_src = matched_dst = NULL;
 592        switch (count_refspec_match(rs->src, src, &matched_src)) {
 593        case 1:
 594                break;
 595        case 0:
 596                /* The source could be in the get_sha1() format
 597                 * not a reference name.  :refs/other is a
 598                 * way to delete 'other' ref at the remote end.
 599                 */
 600                matched_src = try_explicit_object_name(rs->src);
 601                if (!matched_src)
 602                        error("src refspec %s does not match any.", rs->src);
 603                break;
 604        default:
 605                matched_src = NULL;
 606                error("src refspec %s matches more than one.", rs->src);
 607                break;
 608        }
 609
 610        if (!matched_src)
 611                errs = 1;
 612
 613        if (!dst_value)
 614                dst_value = matched_src->name;
 615
 616        switch (count_refspec_match(dst_value, dst, &matched_dst)) {
 617        case 1:
 618                break;
 619        case 0:
 620                if (!memcmp(dst_value, "refs/", 5))
 621                        matched_dst = make_linked_ref(dst_value, dst_tail);
 622                else
 623                        error("dst refspec %s does not match any "
 624                              "existing ref on the remote and does "
 625                              "not start with refs/.", dst_value);
 626                break;
 627        default:
 628                matched_dst = NULL;
 629                error("dst refspec %s matches more than one.",
 630                      dst_value);
 631                break;
 632        }
 633        if (errs || !matched_dst)
 634                return 1;
 635        if (matched_dst->peer_ref) {
 636                errs = 1;
 637                error("dst ref %s receives from more than one src.",
 638                      matched_dst->name);
 639        }
 640        else {
 641                matched_dst->peer_ref = matched_src;
 642                matched_dst->force = rs->force;
 643        }
 644        return errs;
 645}
 646
 647static int match_explicit_refs(struct ref *src, struct ref *dst,
 648                               struct ref ***dst_tail, struct refspec *rs,
 649                               int rs_nr)
 650{
 651        int i, errs;
 652        for (i = errs = 0; i < rs_nr; i++)
 653                errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
 654        return -errs;
 655}
 656
 657static struct ref *find_ref_by_name(struct ref *list, const char *name)
 658{
 659        for ( ; list; list = list->next)
 660                if (!strcmp(list->name, name))
 661                        return list;
 662        return NULL;
 663}
 664
 665static const struct refspec *check_pattern_match(const struct refspec *rs,
 666                                                 int rs_nr,
 667                                                 const struct ref *src)
 668{
 669        int i;
 670        for (i = 0; i < rs_nr; i++) {
 671                if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
 672                        return rs + i;
 673        }
 674        return NULL;
 675}
 676
 677/*
 678 * Note. This is used only by "push"; refspec matching rules for
 679 * push and fetch are subtly different, so do not try to reuse it
 680 * without thinking.
 681 */
 682int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
 683               int nr_refspec, char **refspec, int all)
 684{
 685        struct refspec *rs =
 686                parse_ref_spec(nr_refspec, (const char **) refspec);
 687
 688        if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
 689                return -1;
 690
 691        /* pick the remainder */
 692        for ( ; src; src = src->next) {
 693                struct ref *dst_peer;
 694                const struct refspec *pat = NULL;
 695                char *dst_name;
 696                if (src->peer_ref)
 697                        continue;
 698                if (nr_refspec) {
 699                        pat = check_pattern_match(rs, nr_refspec, src);
 700                        if (!pat)
 701                                continue;
 702                }
 703                else if (prefixcmp(src->name, "refs/heads/"))
 704                        /*
 705                         * "matching refs"; traditionally we pushed everything
 706                         * including refs outside refs/heads/ hierarchy, but
 707                         * that does not make much sense these days.
 708                         */
 709                        continue;
 710
 711                if (pat) {
 712                        const char *dst_side = pat->dst ? pat->dst : pat->src;
 713                        dst_name = xmalloc(strlen(dst_side) +
 714                                           strlen(src->name) -
 715                                           strlen(pat->src) + 2);
 716                        strcpy(dst_name, dst_side);
 717                        strcat(dst_name, src->name + strlen(pat->src));
 718                } else
 719                        dst_name = xstrdup(src->name);
 720                dst_peer = find_ref_by_name(dst, dst_name);
 721                if (dst_peer && dst_peer->peer_ref)
 722                        /* We're already sending something to this ref. */
 723                        goto free_name;
 724                if (!dst_peer && !nr_refspec && !all)
 725                        /* Remote doesn't have it, and we have no
 726                         * explicit pattern, and we don't have
 727                         * --all. */
 728                        goto free_name;
 729                if (!dst_peer) {
 730                        /* Create a new one and link it */
 731                        dst_peer = make_linked_ref(dst_name, dst_tail);
 732                        hashcpy(dst_peer->new_sha1, src->new_sha1);
 733                }
 734                dst_peer->peer_ref = src;
 735        free_name:
 736                free(dst_name);
 737        }
 738        return 0;
 739}
 740
 741struct branch *branch_get(const char *name)
 742{
 743        struct branch *ret;
 744
 745        read_config();
 746        if (!name || !*name || !strcmp(name, "HEAD"))
 747                ret = current_branch;
 748        else
 749                ret = make_branch(name, 0);
 750        if (ret && ret->remote_name) {
 751                ret->remote = remote_get(ret->remote_name);
 752                if (ret->merge_nr) {
 753                        int i;
 754                        ret->merge = xcalloc(sizeof(*ret->merge),
 755                                             ret->merge_nr);
 756                        for (i = 0; i < ret->merge_nr; i++) {
 757                                ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
 758                                ret->merge[i]->src = xstrdup(ret->merge_name[i]);
 759                                remote_find_tracking(ret->remote,
 760                                                     ret->merge[i]);
 761                        }
 762                }
 763        }
 764        return ret;
 765}
 766
 767int branch_has_merge_config(struct branch *branch)
 768{
 769        return branch && !!branch->merge;
 770}
 771
 772int branch_merge_matches(struct branch *branch,
 773                                 int i,
 774                                 const char *refname)
 775{
 776        if (!branch || i < 0 || i >= branch->merge_nr)
 777                return 0;
 778        return ref_matches_abbrev(branch->merge[i]->src, refname);
 779}
 780
 781static struct ref *get_expanded_map(struct ref *remote_refs,
 782                                    const struct refspec *refspec)
 783{
 784        struct ref *ref;
 785        struct ref *ret = NULL;
 786        struct ref **tail = &ret;
 787
 788        int remote_prefix_len = strlen(refspec->src);
 789        int local_prefix_len = strlen(refspec->dst);
 790
 791        for (ref = remote_refs; ref; ref = ref->next) {
 792                if (strchr(ref->name, '^'))
 793                        continue; /* a dereference item */
 794                if (!prefixcmp(ref->name, refspec->src)) {
 795                        char *match;
 796                        struct ref *cpy = copy_ref(ref);
 797                        match = ref->name + remote_prefix_len;
 798
 799                        cpy->peer_ref = alloc_ref(local_prefix_len +
 800                                                  strlen(match) + 1);
 801                        sprintf(cpy->peer_ref->name, "%s%s",
 802                                refspec->dst, match);
 803                        if (refspec->force)
 804                                cpy->peer_ref->force = 1;
 805                        *tail = cpy;
 806                        tail = &cpy->next;
 807                }
 808        }
 809
 810        return ret;
 811}
 812
 813static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)
 814{
 815        struct ref *ref;
 816        for (ref = refs; ref; ref = ref->next) {
 817                if (ref_matches_abbrev(name, ref->name))
 818                        return ref;
 819        }
 820        return NULL;
 821}
 822
 823struct ref *get_remote_ref(struct ref *remote_refs, const char *name)
 824{
 825        struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
 826
 827        if (!ref)
 828                die("Couldn't find remote ref %s\n", name);
 829
 830        return copy_ref(ref);
 831}
 832
 833static struct ref *get_local_ref(const char *name)
 834{
 835        struct ref *ret;
 836        if (!name)
 837                return NULL;
 838
 839        if (!prefixcmp(name, "refs/")) {
 840                ret = alloc_ref(strlen(name) + 1);
 841                strcpy(ret->name, name);
 842                return ret;
 843        }
 844
 845        if (!prefixcmp(name, "heads/") ||
 846            !prefixcmp(name, "tags/") ||
 847            !prefixcmp(name, "remotes/")) {
 848                ret = alloc_ref(strlen(name) + 6);
 849                sprintf(ret->name, "refs/%s", name);
 850                return ret;
 851        }
 852
 853        ret = alloc_ref(strlen(name) + 12);
 854        sprintf(ret->name, "refs/heads/%s", name);
 855        return ret;
 856}
 857
 858int get_fetch_map(struct ref *remote_refs,
 859                  const struct refspec *refspec,
 860                  struct ref ***tail)
 861{
 862        struct ref *ref_map, *rm;
 863
 864        if (refspec->pattern) {
 865                ref_map = get_expanded_map(remote_refs, refspec);
 866        } else {
 867                ref_map = get_remote_ref(remote_refs,
 868                                         refspec->src[0] ?
 869                                         refspec->src : "HEAD");
 870
 871                ref_map->peer_ref = get_local_ref(refspec->dst);
 872                if (ref_map->peer_ref && refspec->force)
 873                        ref_map->peer_ref->force = 1;
 874        }
 875
 876        for (rm = ref_map; rm; rm = rm->next) {
 877                if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
 878                        die("* refusing to create funny ref '%s' locally",
 879                            rm->peer_ref->name);
 880        }
 881
 882        tail_link_ref(ref_map, tail);
 883
 884        return 0;
 885}