remote.con commit Make fetch-pack a builtin with an internal API (2d4177c)
   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_uri(struct remote *remote, const char *uri)
  36{
  37        int nr = remote->uri_nr + 1;
  38        remote->uri =
  39                xrealloc(remote->uri, nr * sizeof(char *));
  40        remote->uri[nr-1] = uri;
  41        remote->uri_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_uri(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_uri(remote, p);
 210        add_fetch_refspec(remote, branch);
 211}
 212
 213static int handle_config(const char *key, const char *value)
 214{
 215        const char *name;
 216        const char *subkey;
 217        struct remote *remote;
 218        struct branch *branch;
 219        if (!prefixcmp(key, "branch.")) {
 220                name = key + 7;
 221                subkey = strrchr(name, '.');
 222                branch = make_branch(name, subkey - name);
 223                if (!subkey)
 224                        return 0;
 225                if (!value)
 226                        return 0;
 227                if (!strcmp(subkey, ".remote")) {
 228                        branch->remote_name = xstrdup(value);
 229                        if (branch == current_branch)
 230                                default_remote_name = branch->remote_name;
 231                } else if (!strcmp(subkey, ".merge"))
 232                        add_merge(branch, xstrdup(value));
 233                return 0;
 234        }
 235        if (prefixcmp(key,  "remote."))
 236                return 0;
 237        name = key + 7;
 238        subkey = strrchr(name, '.');
 239        if (!subkey)
 240                return error("Config with no key for remote %s", name);
 241        if (*subkey == '/') {
 242                warning("Config remote shorthand cannot begin with '/': %s", name);
 243                return 0;
 244        }
 245        remote = make_remote(name, subkey - name);
 246        if (!value) {
 247                /* if we ever have a boolean variable, e.g. "remote.*.disabled"
 248                 * [remote "frotz"]
 249                 *      disabled
 250                 * is a valid way to set it to true; we get NULL in value so
 251                 * we need to handle it here.
 252                 *
 253                 * if (!strcmp(subkey, ".disabled")) {
 254                 *      val = git_config_bool(key, value);
 255                 *      return 0;
 256                 * } else
 257                 *
 258                 */
 259                return 0; /* ignore unknown booleans */
 260        }
 261        if (!strcmp(subkey, ".url")) {
 262                add_uri(remote, xstrdup(value));
 263        } else if (!strcmp(subkey, ".push")) {
 264                add_push_refspec(remote, xstrdup(value));
 265        } else if (!strcmp(subkey, ".fetch")) {
 266                add_fetch_refspec(remote, xstrdup(value));
 267        } else if (!strcmp(subkey, ".receivepack")) {
 268                if (!remote->receivepack)
 269                        remote->receivepack = xstrdup(value);
 270                else
 271                        error("more than one receivepack given, using the first");
 272        } else if (!strcmp(subkey, ".uploadpack")) {
 273                if (!remote->uploadpack)
 274                        remote->uploadpack = xstrdup(value);
 275                else
 276                        error("more than one uploadpack given, using the first");
 277        }
 278        return 0;
 279}
 280
 281static void read_config(void)
 282{
 283        unsigned char sha1[20];
 284        const char *head_ref;
 285        int flag;
 286        if (default_remote_name) // did this already
 287                return;
 288        default_remote_name = xstrdup("origin");
 289        current_branch = NULL;
 290        head_ref = resolve_ref("HEAD", sha1, 0, &flag);
 291        if (head_ref && (flag & REF_ISSYMREF) &&
 292            !prefixcmp(head_ref, "refs/heads/")) {
 293                current_branch =
 294                        make_branch(head_ref + strlen("refs/heads/"), 0);
 295        }
 296        git_config(handle_config);
 297}
 298
 299static struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
 300{
 301        int i;
 302        struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
 303        for (i = 0; i < nr_refspec; i++) {
 304                const char *sp, *ep, *gp;
 305                sp = refspec[i];
 306                if (*sp == '+') {
 307                        rs[i].force = 1;
 308                        sp++;
 309                }
 310                gp = strchr(sp, '*');
 311                ep = strchr(sp, ':');
 312                if (gp && ep && gp > ep)
 313                        gp = NULL;
 314                if (ep) {
 315                        if (ep[1]) {
 316                                const char *glob = strchr(ep + 1, '*');
 317                                if (!glob)
 318                                        gp = NULL;
 319                                if (gp)
 320                                        rs[i].dst = xstrndup(ep + 1,
 321                                                             glob - ep - 1);
 322                                else
 323                                        rs[i].dst = xstrdup(ep + 1);
 324                        }
 325                } else {
 326                        ep = sp + strlen(sp);
 327                }
 328                if (gp) {
 329                        rs[i].pattern = 1;
 330                        ep = gp;
 331                }
 332                rs[i].src = xstrndup(sp, ep - sp);
 333        }
 334        return rs;
 335}
 336
 337struct remote *remote_get(const char *name)
 338{
 339        struct remote *ret;
 340
 341        read_config();
 342        if (!name)
 343                name = default_remote_name;
 344        ret = make_remote(name, 0);
 345        if (name[0] != '/') {
 346                if (!ret->uri)
 347                        read_remotes_file(ret);
 348                if (!ret->uri)
 349                        read_branches_file(ret);
 350        }
 351        if (!ret->uri)
 352                add_uri(ret, name);
 353        if (!ret->uri)
 354                return NULL;
 355        ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
 356        ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
 357        return ret;
 358}
 359
 360int for_each_remote(each_remote_fn fn, void *priv)
 361{
 362        int i, result = 0;
 363        read_config();
 364        for (i = 0; i < allocated_remotes && !result; i++) {
 365                struct remote *r = remotes[i];
 366                if (!r)
 367                        continue;
 368                if (!r->fetch)
 369                        r->fetch = parse_ref_spec(r->fetch_refspec_nr,
 370                                        r->fetch_refspec);
 371                if (!r->push)
 372                        r->push = parse_ref_spec(r->push_refspec_nr,
 373                                        r->push_refspec);
 374                result = fn(r, priv);
 375        }
 376        return result;
 377}
 378
 379int remote_has_uri(struct remote *remote, const char *uri)
 380{
 381        int i;
 382        for (i = 0; i < remote->uri_nr; i++) {
 383                if (!strcmp(remote->uri[i], uri))
 384                        return 1;
 385        }
 386        return 0;
 387}
 388
 389/*
 390 * Returns true if, under the matching rules for fetching, name is the
 391 * same as the given full name.
 392 */
 393static int ref_matches_abbrev(const char *name, const char *full)
 394{
 395        if (!prefixcmp(name, "refs/") || !strcmp(name, "HEAD"))
 396                return !strcmp(name, full);
 397        if (prefixcmp(full, "refs/"))
 398                return 0;
 399        if (!prefixcmp(name, "heads/") ||
 400            !prefixcmp(name, "tags/") ||
 401            !prefixcmp(name, "remotes/"))
 402                return !strcmp(name, full + 5);
 403        if (prefixcmp(full + 5, "heads/"))
 404                return 0;
 405        return !strcmp(full + 11, name);
 406}
 407
 408int remote_find_tracking(struct remote *remote, struct refspec *refspec)
 409{
 410        int find_src = refspec->src == NULL;
 411        char *needle, **result;
 412        int i;
 413
 414        if (find_src) {
 415                if (refspec->dst == NULL)
 416                        return error("find_tracking: need either src or dst");
 417                needle = refspec->dst;
 418                result = &refspec->src;
 419        } else {
 420                needle = refspec->src;
 421                result = &refspec->dst;
 422        }
 423
 424        for (i = 0; i < remote->fetch_refspec_nr; i++) {
 425                struct refspec *fetch = &remote->fetch[i];
 426                const char *key = find_src ? fetch->dst : fetch->src;
 427                const char *value = find_src ? fetch->src : fetch->dst;
 428                if (!fetch->dst)
 429                        continue;
 430                if (fetch->pattern) {
 431                        if (!prefixcmp(needle, key)) {
 432                                *result = xmalloc(strlen(value) +
 433                                                  strlen(needle) -
 434                                                  strlen(key) + 1);
 435                                strcpy(*result, value);
 436                                strcpy(*result + strlen(value),
 437                                       needle + strlen(key));
 438                                refspec->force = fetch->force;
 439                                return 0;
 440                        }
 441                } else if (!strcmp(needle, key)) {
 442                        *result = xstrdup(value);
 443                        refspec->force = fetch->force;
 444                        return 0;
 445                }
 446        }
 447        return -1;
 448}
 449
 450struct ref *alloc_ref(unsigned namelen)
 451{
 452        struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
 453        memset(ret, 0, sizeof(struct ref) + namelen);
 454        return ret;
 455}
 456
 457void free_refs(struct ref *ref)
 458{
 459        struct ref *next;
 460        while (ref) {
 461                next = ref->next;
 462                if (ref->peer_ref)
 463                        free(ref->peer_ref);
 464                free(ref);
 465                ref = next;
 466        }
 467}
 468
 469static int count_refspec_match(const char *pattern,
 470                               struct ref *refs,
 471                               struct ref **matched_ref)
 472{
 473        int patlen = strlen(pattern);
 474        struct ref *matched_weak = NULL;
 475        struct ref *matched = NULL;
 476        int weak_match = 0;
 477        int match = 0;
 478
 479        for (weak_match = match = 0; refs; refs = refs->next) {
 480                char *name = refs->name;
 481                int namelen = strlen(name);
 482
 483                if (namelen < patlen ||
 484                    memcmp(name + namelen - patlen, pattern, patlen))
 485                        continue;
 486                if (namelen != patlen && name[namelen - patlen - 1] != '/')
 487                        continue;
 488
 489                /* A match is "weak" if it is with refs outside
 490                 * heads or tags, and did not specify the pattern
 491                 * in full (e.g. "refs/remotes/origin/master") or at
 492                 * least from the toplevel (e.g. "remotes/origin/master");
 493                 * otherwise "git push $URL master" would result in
 494                 * ambiguity between remotes/origin/master and heads/master
 495                 * at the remote site.
 496                 */
 497                if (namelen != patlen &&
 498                    patlen != namelen - 5 &&
 499                    prefixcmp(name, "refs/heads/") &&
 500                    prefixcmp(name, "refs/tags/")) {
 501                        /* We want to catch the case where only weak
 502                         * matches are found and there are multiple
 503                         * matches, and where more than one strong
 504                         * matches are found, as ambiguous.  One
 505                         * strong match with zero or more weak matches
 506                         * are acceptable as a unique match.
 507                         */
 508                        matched_weak = refs;
 509                        weak_match++;
 510                }
 511                else {
 512                        matched = refs;
 513                        match++;
 514                }
 515        }
 516        if (!matched) {
 517                *matched_ref = matched_weak;
 518                return weak_match;
 519        }
 520        else {
 521                *matched_ref = matched;
 522                return match;
 523        }
 524}
 525
 526static void tail_link_ref(struct ref *ref, struct ref ***tail)
 527{
 528        **tail = ref;
 529        while (ref->next)
 530                ref = ref->next;
 531        *tail = &ref->next;
 532}
 533
 534static struct ref *try_explicit_object_name(const char *name)
 535{
 536        unsigned char sha1[20];
 537        struct ref *ref;
 538        int len;
 539
 540        if (!*name) {
 541                ref = alloc_ref(20);
 542                strcpy(ref->name, "(delete)");
 543                hashclr(ref->new_sha1);
 544                return ref;
 545        }
 546        if (get_sha1(name, sha1))
 547                return NULL;
 548        len = strlen(name) + 1;
 549        ref = alloc_ref(len);
 550        memcpy(ref->name, name, len);
 551        hashcpy(ref->new_sha1, sha1);
 552        return ref;
 553}
 554
 555static struct ref *make_linked_ref(const char *name, struct ref ***tail)
 556{
 557        struct ref *ret;
 558        size_t len;
 559
 560        len = strlen(name) + 1;
 561        ret = alloc_ref(len);
 562        memcpy(ret->name, name, len);
 563        tail_link_ref(ret, tail);
 564        return ret;
 565}
 566
 567static int match_explicit(struct ref *src, struct ref *dst,
 568                          struct ref ***dst_tail,
 569                          struct refspec *rs,
 570                          int errs)
 571{
 572        struct ref *matched_src, *matched_dst;
 573
 574        const char *dst_value = rs->dst;
 575
 576        if (rs->pattern)
 577                return errs;
 578
 579        matched_src = matched_dst = NULL;
 580        switch (count_refspec_match(rs->src, src, &matched_src)) {
 581        case 1:
 582                break;
 583        case 0:
 584                /* The source could be in the get_sha1() format
 585                 * not a reference name.  :refs/other is a
 586                 * way to delete 'other' ref at the remote end.
 587                 */
 588                matched_src = try_explicit_object_name(rs->src);
 589                if (matched_src)
 590                        break;
 591                error("src refspec %s does not match any.",
 592                      rs->src);
 593                break;
 594        default:
 595                matched_src = NULL;
 596                error("src refspec %s matches more than one.",
 597                      rs->src);
 598                break;
 599        }
 600
 601        if (!matched_src)
 602                errs = 1;
 603
 604        if (dst_value == NULL)
 605                dst_value = matched_src->name;
 606
 607        switch (count_refspec_match(dst_value, dst, &matched_dst)) {
 608        case 1:
 609                break;
 610        case 0:
 611                if (!memcmp(dst_value, "refs/", 5))
 612                        matched_dst = make_linked_ref(dst_value, dst_tail);
 613                else
 614                        error("dst refspec %s does not match any "
 615                              "existing ref on the remote and does "
 616                              "not start with refs/.", dst_value);
 617                break;
 618        default:
 619                matched_dst = NULL;
 620                error("dst refspec %s matches more than one.",
 621                      dst_value);
 622                break;
 623        }
 624        if (errs || matched_dst == NULL)
 625                return 1;
 626        if (matched_dst->peer_ref) {
 627                errs = 1;
 628                error("dst ref %s receives from more than one src.",
 629                      matched_dst->name);
 630        }
 631        else {
 632                matched_dst->peer_ref = matched_src;
 633                matched_dst->force = rs->force;
 634        }
 635        return errs;
 636}
 637
 638static int match_explicit_refs(struct ref *src, struct ref *dst,
 639                               struct ref ***dst_tail, struct refspec *rs,
 640                               int rs_nr)
 641{
 642        int i, errs;
 643        for (i = errs = 0; i < rs_nr; i++)
 644                errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
 645        return -errs;
 646}
 647
 648static struct ref *find_ref_by_name(struct ref *list, const char *name)
 649{
 650        for ( ; list; list = list->next)
 651                if (!strcmp(list->name, name))
 652                        return list;
 653        return NULL;
 654}
 655
 656static const struct refspec *check_pattern_match(const struct refspec *rs,
 657                                                 int rs_nr,
 658                                                 const struct ref *src)
 659{
 660        int i;
 661        for (i = 0; i < rs_nr; i++) {
 662                if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
 663                        return rs + i;
 664        }
 665        return NULL;
 666}
 667
 668/*
 669 * Note. This is used only by "push"; refspec matching rules for
 670 * push and fetch are subtly different, so do not try to reuse it
 671 * without thinking.
 672 */
 673int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
 674               int nr_refspec, char **refspec, int all)
 675{
 676        struct refspec *rs =
 677                parse_ref_spec(nr_refspec, (const char **) refspec);
 678
 679        if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
 680                return -1;
 681
 682        /* pick the remainder */
 683        for ( ; src; src = src->next) {
 684                struct ref *dst_peer;
 685                const struct refspec *pat = NULL;
 686                char *dst_name;
 687                if (src->peer_ref)
 688                        continue;
 689                if (nr_refspec) {
 690                        pat = check_pattern_match(rs, nr_refspec, src);
 691                        if (!pat)
 692                                continue;
 693                }
 694                else if (prefixcmp(src->name, "refs/heads/"))
 695                        /*
 696                         * "matching refs"; traditionally we pushed everything
 697                         * including refs outside refs/heads/ hierarchy, but
 698                         * that does not make much sense these days.
 699                         */
 700                        continue;
 701
 702                if (pat) {
 703                        const char *dst_side = pat->dst ? pat->dst : pat->src;
 704                        dst_name = xmalloc(strlen(dst_side) +
 705                                           strlen(src->name) -
 706                                           strlen(pat->src) + 2);
 707                        strcpy(dst_name, dst_side);
 708                        strcat(dst_name, src->name + strlen(pat->src));
 709                } else
 710                        dst_name = xstrdup(src->name);
 711                dst_peer = find_ref_by_name(dst, dst_name);
 712                if (dst_peer && dst_peer->peer_ref)
 713                        /* We're already sending something to this ref. */
 714                        goto free_name;
 715                if (!dst_peer && !nr_refspec && !all)
 716                        /* Remote doesn't have it, and we have no
 717                         * explicit pattern, and we don't have
 718                         * --all. */
 719                        goto free_name;
 720                if (!dst_peer) {
 721                        /* Create a new one and link it */
 722                        dst_peer = make_linked_ref(dst_name, dst_tail);
 723                        hashcpy(dst_peer->new_sha1, src->new_sha1);
 724                }
 725                dst_peer->peer_ref = src;
 726        free_name:
 727                free(dst_name);
 728        }
 729        return 0;
 730}
 731
 732struct branch *branch_get(const char *name)
 733{
 734        struct branch *ret;
 735
 736        read_config();
 737        if (!name || !*name || !strcmp(name, "HEAD"))
 738                ret = current_branch;
 739        else
 740                ret = make_branch(name, 0);
 741        if (ret && ret->remote_name) {
 742                ret->remote = remote_get(ret->remote_name);
 743                if (ret->merge_nr) {
 744                        int i;
 745                        ret->merge = xcalloc(sizeof(*ret->merge),
 746                                             ret->merge_nr);
 747                        for (i = 0; i < ret->merge_nr; i++) {
 748                                ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
 749                                ret->merge[i]->src = xstrdup(ret->merge_name[i]);
 750                                remote_find_tracking(ret->remote,
 751                                                     ret->merge[i]);
 752                        }
 753                }
 754        }
 755        return ret;
 756}
 757
 758int branch_has_merge_config(struct branch *branch)
 759{
 760        return branch && !!branch->merge;
 761}
 762
 763int branch_merges(struct branch *branch, const char *refname)
 764{
 765        int i;
 766        if (!branch)
 767                return 0;
 768        for (i = 0; i < branch->merge_nr; i++) {
 769                if (ref_matches_abbrev(branch->merge[i]->src, refname))
 770                        return 1;
 771        }
 772        return 0;
 773}