remote.con commit Add remote.<name>.proxy (14c9821)
   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        } 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
 422/*
 423 * Returns true if, under the matching rules for fetching, name is the
 424 * same as the given full name.
 425 */
 426static int ref_matches_abbrev(const char *name, const char *full)
 427{
 428        if (!prefixcmp(name, "refs/") || !strcmp(name, "HEAD"))
 429                return !strcmp(name, full);
 430        if (prefixcmp(full, "refs/"))
 431                return 0;
 432        if (!prefixcmp(name, "heads/") ||
 433            !prefixcmp(name, "tags/") ||
 434            !prefixcmp(name, "remotes/"))
 435                return !strcmp(name, full + 5);
 436        if (prefixcmp(full + 5, "heads/"))
 437                return 0;
 438        return !strcmp(full + 11, name);
 439}
 440
 441int remote_find_tracking(struct remote *remote, struct refspec *refspec)
 442{
 443        int find_src = refspec->src == NULL;
 444        char *needle, **result;
 445        int i;
 446
 447        if (find_src) {
 448                if (!refspec->dst)
 449                        return error("find_tracking: need either src or dst");
 450                needle = refspec->dst;
 451                result = &refspec->src;
 452        } else {
 453                needle = refspec->src;
 454                result = &refspec->dst;
 455        }
 456
 457        for (i = 0; i < remote->fetch_refspec_nr; i++) {
 458                struct refspec *fetch = &remote->fetch[i];
 459                const char *key = find_src ? fetch->dst : fetch->src;
 460                const char *value = find_src ? fetch->src : fetch->dst;
 461                if (!fetch->dst)
 462                        continue;
 463                if (fetch->pattern) {
 464                        if (!prefixcmp(needle, key)) {
 465                                *result = xmalloc(strlen(value) +
 466                                                  strlen(needle) -
 467                                                  strlen(key) + 1);
 468                                strcpy(*result, value);
 469                                strcpy(*result + strlen(value),
 470                                       needle + strlen(key));
 471                                refspec->force = fetch->force;
 472                                return 0;
 473                        }
 474                } else if (!strcmp(needle, key)) {
 475                        *result = xstrdup(value);
 476                        refspec->force = fetch->force;
 477                        return 0;
 478                }
 479        }
 480        return -1;
 481}
 482
 483struct ref *alloc_ref(unsigned namelen)
 484{
 485        struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
 486        memset(ret, 0, sizeof(struct ref) + namelen);
 487        return ret;
 488}
 489
 490static struct ref *copy_ref(const struct ref *ref)
 491{
 492        struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
 493        memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
 494        ret->next = NULL;
 495        return ret;
 496}
 497
 498struct ref *copy_ref_list(const struct ref *ref)
 499{
 500        struct ref *ret = NULL;
 501        struct ref **tail = &ret;
 502        while (ref) {
 503                *tail = copy_ref(ref);
 504                ref = ref->next;
 505                tail = &((*tail)->next);
 506        }
 507        return ret;
 508}
 509
 510void free_refs(struct ref *ref)
 511{
 512        struct ref *next;
 513        while (ref) {
 514                next = ref->next;
 515                if (ref->peer_ref)
 516                        free(ref->peer_ref);
 517                free(ref);
 518                ref = next;
 519        }
 520}
 521
 522static int count_refspec_match(const char *pattern,
 523                               struct ref *refs,
 524                               struct ref **matched_ref)
 525{
 526        int patlen = strlen(pattern);
 527        struct ref *matched_weak = NULL;
 528        struct ref *matched = NULL;
 529        int weak_match = 0;
 530        int match = 0;
 531
 532        for (weak_match = match = 0; refs; refs = refs->next) {
 533                char *name = refs->name;
 534                int namelen = strlen(name);
 535
 536                if (namelen < patlen ||
 537                    memcmp(name + namelen - patlen, pattern, patlen))
 538                        continue;
 539                if (namelen != patlen && name[namelen - patlen - 1] != '/')
 540                        continue;
 541
 542                /* A match is "weak" if it is with refs outside
 543                 * heads or tags, and did not specify the pattern
 544                 * in full (e.g. "refs/remotes/origin/master") or at
 545                 * least from the toplevel (e.g. "remotes/origin/master");
 546                 * otherwise "git push $URL master" would result in
 547                 * ambiguity between remotes/origin/master and heads/master
 548                 * at the remote site.
 549                 */
 550                if (namelen != patlen &&
 551                    patlen != namelen - 5 &&
 552                    prefixcmp(name, "refs/heads/") &&
 553                    prefixcmp(name, "refs/tags/")) {
 554                        /* We want to catch the case where only weak
 555                         * matches are found and there are multiple
 556                         * matches, and where more than one strong
 557                         * matches are found, as ambiguous.  One
 558                         * strong match with zero or more weak matches
 559                         * are acceptable as a unique match.
 560                         */
 561                        matched_weak = refs;
 562                        weak_match++;
 563                }
 564                else {
 565                        matched = refs;
 566                        match++;
 567                }
 568        }
 569        if (!matched) {
 570                *matched_ref = matched_weak;
 571                return weak_match;
 572        }
 573        else {
 574                *matched_ref = matched;
 575                return match;
 576        }
 577}
 578
 579static void tail_link_ref(struct ref *ref, struct ref ***tail)
 580{
 581        **tail = ref;
 582        while (ref->next)
 583                ref = ref->next;
 584        *tail = &ref->next;
 585}
 586
 587static struct ref *try_explicit_object_name(const char *name)
 588{
 589        unsigned char sha1[20];
 590        struct ref *ref;
 591        int len;
 592
 593        if (!*name) {
 594                ref = alloc_ref(20);
 595                strcpy(ref->name, "(delete)");
 596                hashclr(ref->new_sha1);
 597                return ref;
 598        }
 599        if (get_sha1(name, sha1))
 600                return NULL;
 601        len = strlen(name) + 1;
 602        ref = alloc_ref(len);
 603        memcpy(ref->name, name, len);
 604        hashcpy(ref->new_sha1, sha1);
 605        return ref;
 606}
 607
 608static struct ref *make_linked_ref(const char *name, struct ref ***tail)
 609{
 610        struct ref *ret;
 611        size_t len;
 612
 613        len = strlen(name) + 1;
 614        ret = alloc_ref(len);
 615        memcpy(ret->name, name, len);
 616        tail_link_ref(ret, tail);
 617        return ret;
 618}
 619
 620static int match_explicit(struct ref *src, struct ref *dst,
 621                          struct ref ***dst_tail,
 622                          struct refspec *rs,
 623                          int errs)
 624{
 625        struct ref *matched_src, *matched_dst;
 626
 627        const char *dst_value = rs->dst;
 628
 629        if (rs->pattern)
 630                return errs;
 631
 632        matched_src = matched_dst = NULL;
 633        switch (count_refspec_match(rs->src, src, &matched_src)) {
 634        case 1:
 635                break;
 636        case 0:
 637                /* The source could be in the get_sha1() format
 638                 * not a reference name.  :refs/other is a
 639                 * way to delete 'other' ref at the remote end.
 640                 */
 641                matched_src = try_explicit_object_name(rs->src);
 642                if (!matched_src)
 643                        error("src refspec %s does not match any.", rs->src);
 644                break;
 645        default:
 646                matched_src = NULL;
 647                error("src refspec %s matches more than one.", rs->src);
 648                break;
 649        }
 650
 651        if (!matched_src)
 652                errs = 1;
 653
 654        if (!dst_value) {
 655                if (!matched_src)
 656                        return errs;
 657                dst_value = matched_src->name;
 658        }
 659
 660        switch (count_refspec_match(dst_value, dst, &matched_dst)) {
 661        case 1:
 662                break;
 663        case 0:
 664                if (!memcmp(dst_value, "refs/", 5))
 665                        matched_dst = make_linked_ref(dst_value, dst_tail);
 666                else
 667                        error("dst refspec %s does not match any "
 668                              "existing ref on the remote and does "
 669                              "not start with refs/.", dst_value);
 670                break;
 671        default:
 672                matched_dst = NULL;
 673                error("dst refspec %s matches more than one.",
 674                      dst_value);
 675                break;
 676        }
 677        if (errs || !matched_dst)
 678                return 1;
 679        if (matched_dst->peer_ref) {
 680                errs = 1;
 681                error("dst ref %s receives from more than one src.",
 682                      matched_dst->name);
 683        }
 684        else {
 685                matched_dst->peer_ref = matched_src;
 686                matched_dst->force = rs->force;
 687        }
 688        return errs;
 689}
 690
 691static int match_explicit_refs(struct ref *src, struct ref *dst,
 692                               struct ref ***dst_tail, struct refspec *rs,
 693                               int rs_nr)
 694{
 695        int i, errs;
 696        for (i = errs = 0; i < rs_nr; i++)
 697                errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
 698        return -errs;
 699}
 700
 701static const struct refspec *check_pattern_match(const struct refspec *rs,
 702                                                 int rs_nr,
 703                                                 const struct ref *src)
 704{
 705        int i;
 706        for (i = 0; i < rs_nr; i++) {
 707                if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
 708                        return rs + i;
 709        }
 710        return NULL;
 711}
 712
 713/*
 714 * Note. This is used only by "push"; refspec matching rules for
 715 * push and fetch are subtly different, so do not try to reuse it
 716 * without thinking.
 717 */
 718int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
 719               int nr_refspec, const char **refspec, int flags)
 720{
 721        struct refspec *rs =
 722                parse_ref_spec(nr_refspec, (const char **) refspec);
 723        int send_all = flags & MATCH_REFS_ALL;
 724        int send_mirror = flags & MATCH_REFS_MIRROR;
 725
 726        if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
 727                return -1;
 728
 729        /* pick the remainder */
 730        for ( ; src; src = src->next) {
 731                struct ref *dst_peer;
 732                const struct refspec *pat = NULL;
 733                char *dst_name;
 734                if (src->peer_ref)
 735                        continue;
 736                if (nr_refspec) {
 737                        pat = check_pattern_match(rs, nr_refspec, src);
 738                        if (!pat)
 739                                continue;
 740                }
 741                else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
 742                        /*
 743                         * "matching refs"; traditionally we pushed everything
 744                         * including refs outside refs/heads/ hierarchy, but
 745                         * that does not make much sense these days.
 746                         */
 747                        continue;
 748
 749                if (pat) {
 750                        const char *dst_side = pat->dst ? pat->dst : pat->src;
 751                        dst_name = xmalloc(strlen(dst_side) +
 752                                           strlen(src->name) -
 753                                           strlen(pat->src) + 2);
 754                        strcpy(dst_name, dst_side);
 755                        strcat(dst_name, src->name + strlen(pat->src));
 756                } else
 757                        dst_name = xstrdup(src->name);
 758                dst_peer = find_ref_by_name(dst, dst_name);
 759                if (dst_peer && dst_peer->peer_ref)
 760                        /* We're already sending something to this ref. */
 761                        goto free_name;
 762
 763                if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
 764                        /*
 765                         * Remote doesn't have it, and we have no
 766                         * explicit pattern, and we don't have
 767                         * --all nor --mirror.
 768                         */
 769                        goto free_name;
 770                if (!dst_peer) {
 771                        /* Create a new one and link it */
 772                        dst_peer = make_linked_ref(dst_name, dst_tail);
 773                        hashcpy(dst_peer->new_sha1, src->new_sha1);
 774                }
 775                dst_peer->peer_ref = src;
 776                if (pat)
 777                        dst_peer->force = pat->force;
 778        free_name:
 779                free(dst_name);
 780        }
 781        return 0;
 782}
 783
 784struct branch *branch_get(const char *name)
 785{
 786        struct branch *ret;
 787
 788        read_config();
 789        if (!name || !*name || !strcmp(name, "HEAD"))
 790                ret = current_branch;
 791        else
 792                ret = make_branch(name, 0);
 793        if (ret && ret->remote_name) {
 794                ret->remote = remote_get(ret->remote_name);
 795                if (ret->merge_nr) {
 796                        int i;
 797                        ret->merge = xcalloc(sizeof(*ret->merge),
 798                                             ret->merge_nr);
 799                        for (i = 0; i < ret->merge_nr; i++) {
 800                                ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
 801                                ret->merge[i]->src = xstrdup(ret->merge_name[i]);
 802                                remote_find_tracking(ret->remote,
 803                                                     ret->merge[i]);
 804                        }
 805                }
 806        }
 807        return ret;
 808}
 809
 810int branch_has_merge_config(struct branch *branch)
 811{
 812        return branch && !!branch->merge;
 813}
 814
 815int branch_merge_matches(struct branch *branch,
 816                                 int i,
 817                                 const char *refname)
 818{
 819        if (!branch || i < 0 || i >= branch->merge_nr)
 820                return 0;
 821        return ref_matches_abbrev(branch->merge[i]->src, refname);
 822}
 823
 824static struct ref *get_expanded_map(const struct ref *remote_refs,
 825                                    const struct refspec *refspec)
 826{
 827        const struct ref *ref;
 828        struct ref *ret = NULL;
 829        struct ref **tail = &ret;
 830
 831        int remote_prefix_len = strlen(refspec->src);
 832        int local_prefix_len = strlen(refspec->dst);
 833
 834        for (ref = remote_refs; ref; ref = ref->next) {
 835                if (strchr(ref->name, '^'))
 836                        continue; /* a dereference item */
 837                if (!prefixcmp(ref->name, refspec->src)) {
 838                        const char *match;
 839                        struct ref *cpy = copy_ref(ref);
 840                        match = ref->name + remote_prefix_len;
 841
 842                        cpy->peer_ref = alloc_ref(local_prefix_len +
 843                                                  strlen(match) + 1);
 844                        sprintf(cpy->peer_ref->name, "%s%s",
 845                                refspec->dst, match);
 846                        if (refspec->force)
 847                                cpy->peer_ref->force = 1;
 848                        *tail = cpy;
 849                        tail = &cpy->next;
 850                }
 851        }
 852
 853        return ret;
 854}
 855
 856static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
 857{
 858        const struct ref *ref;
 859        for (ref = refs; ref; ref = ref->next) {
 860                if (ref_matches_abbrev(name, ref->name))
 861                        return ref;
 862        }
 863        return NULL;
 864}
 865
 866struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
 867{
 868        const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
 869
 870        if (!ref)
 871                return NULL;
 872
 873        return copy_ref(ref);
 874}
 875
 876static struct ref *get_local_ref(const char *name)
 877{
 878        struct ref *ret;
 879        if (!name)
 880                return NULL;
 881
 882        if (!prefixcmp(name, "refs/")) {
 883                ret = alloc_ref(strlen(name) + 1);
 884                strcpy(ret->name, name);
 885                return ret;
 886        }
 887
 888        if (!prefixcmp(name, "heads/") ||
 889            !prefixcmp(name, "tags/") ||
 890            !prefixcmp(name, "remotes/")) {
 891                ret = alloc_ref(strlen(name) + 6);
 892                sprintf(ret->name, "refs/%s", name);
 893                return ret;
 894        }
 895
 896        ret = alloc_ref(strlen(name) + 12);
 897        sprintf(ret->name, "refs/heads/%s", name);
 898        return ret;
 899}
 900
 901int get_fetch_map(const struct ref *remote_refs,
 902                  const struct refspec *refspec,
 903                  struct ref ***tail,
 904                  int missing_ok)
 905{
 906        struct ref *ref_map, *rm;
 907
 908        if (refspec->pattern) {
 909                ref_map = get_expanded_map(remote_refs, refspec);
 910        } else {
 911                const char *name = refspec->src[0] ? refspec->src : "HEAD";
 912
 913                ref_map = get_remote_ref(remote_refs, name);
 914                if (!missing_ok && !ref_map)
 915                        die("Couldn't find remote ref %s", name);
 916                if (ref_map) {
 917                        ref_map->peer_ref = get_local_ref(refspec->dst);
 918                        if (ref_map->peer_ref && refspec->force)
 919                                ref_map->peer_ref->force = 1;
 920                }
 921        }
 922
 923        for (rm = ref_map; rm; rm = rm->next) {
 924                if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
 925                        die("* refusing to create funny ref '%s' locally",
 926                            rm->peer_ref->name);
 927        }
 928
 929        if (ref_map)
 930                tail_link_ref(ref_map, tail);
 931
 932        return 0;
 933}