transport.con commit config: document the settings to colorize push errors/hints (79f62e7)
   1#include "cache.h"
   2#include "config.h"
   3#include "transport.h"
   4#include "run-command.h"
   5#include "pkt-line.h"
   6#include "fetch-pack.h"
   7#include "remote.h"
   8#include "connect.h"
   9#include "send-pack.h"
  10#include "walker.h"
  11#include "bundle.h"
  12#include "dir.h"
  13#include "refs.h"
  14#include "branch.h"
  15#include "url.h"
  16#include "submodule.h"
  17#include "string-list.h"
  18#include "sha1-array.h"
  19#include "sigchain.h"
  20#include "transport-internal.h"
  21#include "object-store.h"
  22#include "color.h"
  23
  24static int transport_use_color = -1;
  25static char transport_colors[][COLOR_MAXLEN] = {
  26        GIT_COLOR_RESET,
  27        GIT_COLOR_RED           /* REJECTED */
  28};
  29
  30enum color_transport {
  31        TRANSPORT_COLOR_RESET = 0,
  32        TRANSPORT_COLOR_REJECTED = 1
  33};
  34
  35static int transport_color_config(void)
  36{
  37        const char *keys[] = {
  38                "color.transport.reset",
  39                "color.transport.rejected"
  40        }, *key = "color.transport";
  41        char *value;
  42        int i;
  43        static int initialized;
  44
  45        if (initialized)
  46                return 0;
  47        initialized = 1;
  48
  49        if (!git_config_get_string(key, &value))
  50                transport_use_color = git_config_colorbool(key, value);
  51
  52        if (!want_color_stderr(transport_use_color))
  53                return 0;
  54
  55        for (i = 0; i < ARRAY_SIZE(keys); i++)
  56                if (!git_config_get_string(keys[i], &value)) {
  57                        if (!value)
  58                                return config_error_nonbool(keys[i]);
  59                        if (color_parse(value, transport_colors[i]) < 0)
  60                                return -1;
  61                }
  62
  63        return 0;
  64}
  65
  66static const char *transport_get_color(enum color_transport ix)
  67{
  68        if (want_color_stderr(transport_use_color))
  69                return transport_colors[ix];
  70        return "";
  71}
  72
  73static void set_upstreams(struct transport *transport, struct ref *refs,
  74        int pretend)
  75{
  76        struct ref *ref;
  77        for (ref = refs; ref; ref = ref->next) {
  78                const char *localname;
  79                const char *tmp;
  80                const char *remotename;
  81                int flag = 0;
  82                /*
  83                 * Check suitability for tracking. Must be successful /
  84                 * already up-to-date ref create/modify (not delete).
  85                 */
  86                if (ref->status != REF_STATUS_OK &&
  87                        ref->status != REF_STATUS_UPTODATE)
  88                        continue;
  89                if (!ref->peer_ref)
  90                        continue;
  91                if (is_null_oid(&ref->new_oid))
  92                        continue;
  93
  94                /* Follow symbolic refs (mainly for HEAD). */
  95                localname = ref->peer_ref->name;
  96                remotename = ref->name;
  97                tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
  98                                         NULL, &flag);
  99                if (tmp && flag & REF_ISSYMREF &&
 100                        starts_with(tmp, "refs/heads/"))
 101                        localname = tmp;
 102
 103                /* Both source and destination must be local branches. */
 104                if (!localname || !starts_with(localname, "refs/heads/"))
 105                        continue;
 106                if (!remotename || !starts_with(remotename, "refs/heads/"))
 107                        continue;
 108
 109                if (!pretend)
 110                        install_branch_config(BRANCH_CONFIG_VERBOSE,
 111                                localname + 11, transport->remote->name,
 112                                remotename);
 113                else
 114                        printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
 115                                localname + 11, remotename + 11,
 116                                transport->remote->name);
 117        }
 118}
 119
 120struct bundle_transport_data {
 121        int fd;
 122        struct bundle_header header;
 123};
 124
 125static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
 126{
 127        struct bundle_transport_data *data = transport->data;
 128        struct ref *result = NULL;
 129        int i;
 130
 131        if (for_push)
 132                return NULL;
 133
 134        if (data->fd > 0)
 135                close(data->fd);
 136        data->fd = read_bundle_header(transport->url, &data->header);
 137        if (data->fd < 0)
 138                die ("Could not read bundle '%s'.", transport->url);
 139        for (i = 0; i < data->header.references.nr; i++) {
 140                struct ref_list_entry *e = data->header.references.list + i;
 141                struct ref *ref = alloc_ref(e->name);
 142                oidcpy(&ref->old_oid, &e->oid);
 143                ref->next = result;
 144                result = ref;
 145        }
 146        return result;
 147}
 148
 149static int fetch_refs_from_bundle(struct transport *transport,
 150                               int nr_heads, struct ref **to_fetch)
 151{
 152        struct bundle_transport_data *data = transport->data;
 153        return unbundle(&data->header, data->fd,
 154                        transport->progress ? BUNDLE_VERBOSE : 0);
 155}
 156
 157static int close_bundle(struct transport *transport)
 158{
 159        struct bundle_transport_data *data = transport->data;
 160        if (data->fd > 0)
 161                close(data->fd);
 162        free(data);
 163        return 0;
 164}
 165
 166struct git_transport_data {
 167        struct git_transport_options options;
 168        struct child_process *conn;
 169        int fd[2];
 170        unsigned got_remote_heads : 1;
 171        struct oid_array extra_have;
 172        struct oid_array shallow;
 173};
 174
 175static int set_git_option(struct git_transport_options *opts,
 176                          const char *name, const char *value)
 177{
 178        if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
 179                opts->uploadpack = value;
 180                return 0;
 181        } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
 182                opts->receivepack = value;
 183                return 0;
 184        } else if (!strcmp(name, TRANS_OPT_THIN)) {
 185                opts->thin = !!value;
 186                return 0;
 187        } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
 188                opts->followtags = !!value;
 189                return 0;
 190        } else if (!strcmp(name, TRANS_OPT_KEEP)) {
 191                opts->keep = !!value;
 192                return 0;
 193        } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
 194                opts->update_shallow = !!value;
 195                return 0;
 196        } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
 197                if (!value)
 198                        opts->depth = 0;
 199                else {
 200                        char *end;
 201                        opts->depth = strtol(value, &end, 0);
 202                        if (*end)
 203                                die(_("transport: invalid depth option '%s'"), value);
 204                }
 205                return 0;
 206        } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
 207                opts->deepen_since = value;
 208                return 0;
 209        } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
 210                opts->deepen_not = (const struct string_list *)value;
 211                return 0;
 212        } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
 213                opts->deepen_relative = !!value;
 214                return 0;
 215        } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
 216                opts->from_promisor = !!value;
 217                return 0;
 218        } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
 219                opts->no_dependents = !!value;
 220                return 0;
 221        } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
 222                parse_list_objects_filter(&opts->filter_options, value);
 223                return 0;
 224        }
 225        return 1;
 226}
 227
 228static int connect_setup(struct transport *transport, int for_push)
 229{
 230        struct git_transport_data *data = transport->data;
 231        int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
 232
 233        if (data->conn)
 234                return 0;
 235
 236        switch (transport->family) {
 237        case TRANSPORT_FAMILY_ALL: break;
 238        case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
 239        case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
 240        }
 241
 242        data->conn = git_connect(data->fd, transport->url,
 243                                 for_push ? data->options.receivepack :
 244                                 data->options.uploadpack,
 245                                 flags);
 246
 247        return 0;
 248}
 249
 250static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
 251{
 252        struct git_transport_data *data = transport->data;
 253        struct ref *refs;
 254
 255        connect_setup(transport, for_push);
 256        get_remote_heads(data->fd[0], NULL, 0, &refs,
 257                         for_push ? REF_NORMAL : 0,
 258                         &data->extra_have,
 259                         &data->shallow);
 260        data->got_remote_heads = 1;
 261
 262        return refs;
 263}
 264
 265static int fetch_refs_via_pack(struct transport *transport,
 266                               int nr_heads, struct ref **to_fetch)
 267{
 268        int ret = 0;
 269        struct git_transport_data *data = transport->data;
 270        struct ref *refs;
 271        char *dest = xstrdup(transport->url);
 272        struct fetch_pack_args args;
 273        struct ref *refs_tmp = NULL;
 274
 275        memset(&args, 0, sizeof(args));
 276        args.uploadpack = data->options.uploadpack;
 277        args.keep_pack = data->options.keep;
 278        args.lock_pack = 1;
 279        args.use_thin_pack = data->options.thin;
 280        args.include_tag = data->options.followtags;
 281        args.verbose = (transport->verbose > 1);
 282        args.quiet = (transport->verbose < 0);
 283        args.no_progress = !transport->progress;
 284        args.depth = data->options.depth;
 285        args.deepen_since = data->options.deepen_since;
 286        args.deepen_not = data->options.deepen_not;
 287        args.deepen_relative = data->options.deepen_relative;
 288        args.check_self_contained_and_connected =
 289                data->options.check_self_contained_and_connected;
 290        args.cloning = transport->cloning;
 291        args.update_shallow = data->options.update_shallow;
 292        args.from_promisor = data->options.from_promisor;
 293        args.no_dependents = data->options.no_dependents;
 294        args.filter_options = data->options.filter_options;
 295
 296        if (!data->got_remote_heads) {
 297                connect_setup(transport, 0);
 298                get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
 299                                 NULL, &data->shallow);
 300                data->got_remote_heads = 1;
 301        }
 302
 303        refs = fetch_pack(&args, data->fd, data->conn,
 304                          refs_tmp ? refs_tmp : transport->remote_refs,
 305                          dest, to_fetch, nr_heads, &data->shallow,
 306                          &transport->pack_lockfile);
 307        close(data->fd[0]);
 308        close(data->fd[1]);
 309        if (finish_connect(data->conn))
 310                ret = -1;
 311        data->conn = NULL;
 312        data->got_remote_heads = 0;
 313        data->options.self_contained_and_connected =
 314                args.self_contained_and_connected;
 315
 316        if (refs == NULL)
 317                ret = -1;
 318        if (report_unmatched_refs(to_fetch, nr_heads))
 319                ret = -1;
 320
 321        free_refs(refs_tmp);
 322        free_refs(refs);
 323        free(dest);
 324        return ret;
 325}
 326
 327static int push_had_errors(struct ref *ref)
 328{
 329        for (; ref; ref = ref->next) {
 330                switch (ref->status) {
 331                case REF_STATUS_NONE:
 332                case REF_STATUS_UPTODATE:
 333                case REF_STATUS_OK:
 334                        break;
 335                default:
 336                        return 1;
 337                }
 338        }
 339        return 0;
 340}
 341
 342int transport_refs_pushed(struct ref *ref)
 343{
 344        for (; ref; ref = ref->next) {
 345                switch(ref->status) {
 346                case REF_STATUS_NONE:
 347                case REF_STATUS_UPTODATE:
 348                        break;
 349                default:
 350                        return 1;
 351                }
 352        }
 353        return 0;
 354}
 355
 356void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
 357{
 358        struct refspec rs;
 359
 360        if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
 361                return;
 362
 363        rs.src = ref->name;
 364        rs.dst = NULL;
 365
 366        if (!remote_find_tracking(remote, &rs)) {
 367                if (verbose)
 368                        fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
 369                if (ref->deletion) {
 370                        delete_ref(NULL, rs.dst, NULL, 0);
 371                } else
 372                        update_ref("update by push", rs.dst, &ref->new_oid,
 373                                   NULL, 0, 0);
 374                free(rs.dst);
 375        }
 376}
 377
 378static void print_ref_status(char flag, const char *summary,
 379                             struct ref *to, struct ref *from, const char *msg,
 380                             int porcelain, int summary_width)
 381{
 382        if (porcelain) {
 383                if (from)
 384                        fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
 385                else
 386                        fprintf(stdout, "%c\t:%s\t", flag, to->name);
 387                if (msg)
 388                        fprintf(stdout, "%s (%s)\n", summary, msg);
 389                else
 390                        fprintf(stdout, "%s\n", summary);
 391        } else {
 392                const char *red = "", *reset = "";
 393                if (push_had_errors(to)) {
 394                        red = transport_get_color(TRANSPORT_COLOR_REJECTED);
 395                        reset = transport_get_color(TRANSPORT_COLOR_RESET);
 396                }
 397                fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
 398                        summary, reset);
 399                if (from)
 400                        fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
 401                else
 402                        fputs(prettify_refname(to->name), stderr);
 403                if (msg) {
 404                        fputs(" (", stderr);
 405                        fputs(msg, stderr);
 406                        fputc(')', stderr);
 407                }
 408                fputc('\n', stderr);
 409        }
 410}
 411
 412static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
 413{
 414        if (ref->deletion)
 415                print_ref_status('-', "[deleted]", ref, NULL, NULL,
 416                                 porcelain, summary_width);
 417        else if (is_null_oid(&ref->old_oid))
 418                print_ref_status('*',
 419                        (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
 420                        "[new branch]"),
 421                        ref, ref->peer_ref, NULL, porcelain, summary_width);
 422        else {
 423                struct strbuf quickref = STRBUF_INIT;
 424                char type;
 425                const char *msg;
 426
 427                strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
 428                                         DEFAULT_ABBREV);
 429                if (ref->forced_update) {
 430                        strbuf_addstr(&quickref, "...");
 431                        type = '+';
 432                        msg = "forced update";
 433                } else {
 434                        strbuf_addstr(&quickref, "..");
 435                        type = ' ';
 436                        msg = NULL;
 437                }
 438                strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
 439                                         DEFAULT_ABBREV);
 440
 441                print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
 442                                 porcelain, summary_width);
 443                strbuf_release(&quickref);
 444        }
 445}
 446
 447static int print_one_push_status(struct ref *ref, const char *dest, int count,
 448                                 int porcelain, int summary_width)
 449{
 450        if (!count) {
 451                char *url = transport_anonymize_url(dest);
 452                fprintf(porcelain ? stdout : stderr, "To %s\n", url);
 453                free(url);
 454        }
 455
 456        switch(ref->status) {
 457        case REF_STATUS_NONE:
 458                print_ref_status('X', "[no match]", ref, NULL, NULL,
 459                                 porcelain, summary_width);
 460                break;
 461        case REF_STATUS_REJECT_NODELETE:
 462                print_ref_status('!', "[rejected]", ref, NULL,
 463                                 "remote does not support deleting refs",
 464                                 porcelain, summary_width);
 465                break;
 466        case REF_STATUS_UPTODATE:
 467                print_ref_status('=', "[up to date]", ref,
 468                                 ref->peer_ref, NULL, porcelain, summary_width);
 469                break;
 470        case REF_STATUS_REJECT_NONFASTFORWARD:
 471                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 472                                 "non-fast-forward", porcelain, summary_width);
 473                break;
 474        case REF_STATUS_REJECT_ALREADY_EXISTS:
 475                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 476                                 "already exists", porcelain, summary_width);
 477                break;
 478        case REF_STATUS_REJECT_FETCH_FIRST:
 479                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 480                                 "fetch first", porcelain, summary_width);
 481                break;
 482        case REF_STATUS_REJECT_NEEDS_FORCE:
 483                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 484                                 "needs force", porcelain, summary_width);
 485                break;
 486        case REF_STATUS_REJECT_STALE:
 487                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 488                                 "stale info", porcelain, summary_width);
 489                break;
 490        case REF_STATUS_REJECT_SHALLOW:
 491                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 492                                 "new shallow roots not allowed",
 493                                 porcelain, summary_width);
 494                break;
 495        case REF_STATUS_REMOTE_REJECT:
 496                print_ref_status('!', "[remote rejected]", ref,
 497                                 ref->deletion ? NULL : ref->peer_ref,
 498                                 ref->remote_status, porcelain, summary_width);
 499                break;
 500        case REF_STATUS_EXPECTING_REPORT:
 501                print_ref_status('!', "[remote failure]", ref,
 502                                 ref->deletion ? NULL : ref->peer_ref,
 503                                 "remote failed to report status",
 504                                 porcelain, summary_width);
 505                break;
 506        case REF_STATUS_ATOMIC_PUSH_FAILED:
 507                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 508                                 "atomic push failed", porcelain, summary_width);
 509                break;
 510        case REF_STATUS_OK:
 511                print_ok_ref_status(ref, porcelain, summary_width);
 512                break;
 513        }
 514
 515        return 1;
 516}
 517
 518static int measure_abbrev(const struct object_id *oid, int sofar)
 519{
 520        char hex[GIT_MAX_HEXSZ + 1];
 521        int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
 522
 523        return (w < sofar) ? sofar : w;
 524}
 525
 526int transport_summary_width(const struct ref *refs)
 527{
 528        int maxw = -1;
 529
 530        for (; refs; refs = refs->next) {
 531                maxw = measure_abbrev(&refs->old_oid, maxw);
 532                maxw = measure_abbrev(&refs->new_oid, maxw);
 533        }
 534        if (maxw < 0)
 535                maxw = FALLBACK_DEFAULT_ABBREV;
 536        return (2 * maxw + 3);
 537}
 538
 539void transport_print_push_status(const char *dest, struct ref *refs,
 540                                  int verbose, int porcelain, unsigned int *reject_reasons)
 541{
 542        struct ref *ref;
 543        int n = 0;
 544        char *head;
 545        int summary_width = transport_summary_width(refs);
 546
 547        if (transport_color_config() < 0)
 548                warning(_("could not parse transport.color.* config"));
 549
 550        head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
 551
 552        if (verbose) {
 553                for (ref = refs; ref; ref = ref->next)
 554                        if (ref->status == REF_STATUS_UPTODATE)
 555                                n += print_one_push_status(ref, dest, n,
 556                                                           porcelain, summary_width);
 557        }
 558
 559        for (ref = refs; ref; ref = ref->next)
 560                if (ref->status == REF_STATUS_OK)
 561                        n += print_one_push_status(ref, dest, n,
 562                                                   porcelain, summary_width);
 563
 564        *reject_reasons = 0;
 565        for (ref = refs; ref; ref = ref->next) {
 566                if (ref->status != REF_STATUS_NONE &&
 567                    ref->status != REF_STATUS_UPTODATE &&
 568                    ref->status != REF_STATUS_OK)
 569                        n += print_one_push_status(ref, dest, n,
 570                                                   porcelain, summary_width);
 571                if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
 572                        if (head != NULL && !strcmp(head, ref->name))
 573                                *reject_reasons |= REJECT_NON_FF_HEAD;
 574                        else
 575                                *reject_reasons |= REJECT_NON_FF_OTHER;
 576                } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
 577                        *reject_reasons |= REJECT_ALREADY_EXISTS;
 578                } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
 579                        *reject_reasons |= REJECT_FETCH_FIRST;
 580                } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
 581                        *reject_reasons |= REJECT_NEEDS_FORCE;
 582                }
 583        }
 584        free(head);
 585}
 586
 587void transport_verify_remote_names(int nr_heads, const char **heads)
 588{
 589        int i;
 590
 591        for (i = 0; i < nr_heads; i++) {
 592                const char *local = heads[i];
 593                const char *remote = strrchr(heads[i], ':');
 594
 595                if (*local == '+')
 596                        local++;
 597
 598                /* A matching refspec is okay.  */
 599                if (remote == local && remote[1] == '\0')
 600                        continue;
 601
 602                remote = remote ? (remote + 1) : local;
 603                if (check_refname_format(remote,
 604                                REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
 605                        die("remote part of refspec is not a valid name in %s",
 606                                heads[i]);
 607        }
 608}
 609
 610static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
 611{
 612        struct git_transport_data *data = transport->data;
 613        struct send_pack_args args;
 614        int ret;
 615
 616        if (transport_color_config() < 0)
 617                return -1;
 618
 619        if (!data->got_remote_heads) {
 620                struct ref *tmp_refs;
 621                connect_setup(transport, 1);
 622
 623                get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
 624                                 NULL, &data->shallow);
 625                data->got_remote_heads = 1;
 626        }
 627
 628        memset(&args, 0, sizeof(args));
 629        args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
 630        args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
 631        args.use_thin_pack = data->options.thin;
 632        args.verbose = (transport->verbose > 0);
 633        args.quiet = (transport->verbose < 0);
 634        args.progress = transport->progress;
 635        args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
 636        args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
 637        args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
 638        args.push_options = transport->push_options;
 639        args.url = transport->url;
 640
 641        if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
 642                args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
 643        else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
 644                args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
 645        else
 646                args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
 647
 648        ret = send_pack(&args, data->fd, data->conn, remote_refs,
 649                        &data->extra_have);
 650
 651        close(data->fd[1]);
 652        close(data->fd[0]);
 653        ret |= finish_connect(data->conn);
 654        data->conn = NULL;
 655        data->got_remote_heads = 0;
 656
 657        return ret;
 658}
 659
 660static int connect_git(struct transport *transport, const char *name,
 661                       const char *executable, int fd[2])
 662{
 663        struct git_transport_data *data = transport->data;
 664        data->conn = git_connect(data->fd, transport->url,
 665                                 executable, 0);
 666        fd[0] = data->fd[0];
 667        fd[1] = data->fd[1];
 668        return 0;
 669}
 670
 671static int disconnect_git(struct transport *transport)
 672{
 673        struct git_transport_data *data = transport->data;
 674        if (data->conn) {
 675                if (data->got_remote_heads)
 676                        packet_flush(data->fd[1]);
 677                close(data->fd[0]);
 678                close(data->fd[1]);
 679                finish_connect(data->conn);
 680        }
 681
 682        free(data);
 683        return 0;
 684}
 685
 686static struct transport_vtable taken_over_vtable = {
 687        NULL,
 688        get_refs_via_connect,
 689        fetch_refs_via_pack,
 690        git_transport_push,
 691        NULL,
 692        disconnect_git
 693};
 694
 695void transport_take_over(struct transport *transport,
 696                         struct child_process *child)
 697{
 698        struct git_transport_data *data;
 699
 700        if (!transport->smart_options)
 701                die("BUG: taking over transport requires non-NULL "
 702                    "smart_options field.");
 703
 704        data = xcalloc(1, sizeof(*data));
 705        data->options = *transport->smart_options;
 706        data->conn = child;
 707        data->fd[0] = data->conn->out;
 708        data->fd[1] = data->conn->in;
 709        data->got_remote_heads = 0;
 710        transport->data = data;
 711
 712        transport->vtable = &taken_over_vtable;
 713        transport->smart_options = &(data->options);
 714
 715        transport->cannot_reuse = 1;
 716}
 717
 718static int is_file(const char *url)
 719{
 720        struct stat buf;
 721        if (stat(url, &buf))
 722                return 0;
 723        return S_ISREG(buf.st_mode);
 724}
 725
 726static int external_specification_len(const char *url)
 727{
 728        return strchr(url, ':') - url;
 729}
 730
 731static const struct string_list *protocol_whitelist(void)
 732{
 733        static int enabled = -1;
 734        static struct string_list allowed = STRING_LIST_INIT_DUP;
 735
 736        if (enabled < 0) {
 737                const char *v = getenv("GIT_ALLOW_PROTOCOL");
 738                if (v) {
 739                        string_list_split(&allowed, v, ':', -1);
 740                        string_list_sort(&allowed);
 741                        enabled = 1;
 742                } else {
 743                        enabled = 0;
 744                }
 745        }
 746
 747        return enabled ? &allowed : NULL;
 748}
 749
 750enum protocol_allow_config {
 751        PROTOCOL_ALLOW_NEVER = 0,
 752        PROTOCOL_ALLOW_USER_ONLY,
 753        PROTOCOL_ALLOW_ALWAYS
 754};
 755
 756static enum protocol_allow_config parse_protocol_config(const char *key,
 757                                                        const char *value)
 758{
 759        if (!strcasecmp(value, "always"))
 760                return PROTOCOL_ALLOW_ALWAYS;
 761        else if (!strcasecmp(value, "never"))
 762                return PROTOCOL_ALLOW_NEVER;
 763        else if (!strcasecmp(value, "user"))
 764                return PROTOCOL_ALLOW_USER_ONLY;
 765
 766        die("unknown value for config '%s': %s", key, value);
 767}
 768
 769static enum protocol_allow_config get_protocol_config(const char *type)
 770{
 771        char *key = xstrfmt("protocol.%s.allow", type);
 772        char *value;
 773
 774        /* first check the per-protocol config */
 775        if (!git_config_get_string(key, &value)) {
 776                enum protocol_allow_config ret =
 777                        parse_protocol_config(key, value);
 778                free(key);
 779                free(value);
 780                return ret;
 781        }
 782        free(key);
 783
 784        /* if defined, fallback to user-defined default for unknown protocols */
 785        if (!git_config_get_string("protocol.allow", &value)) {
 786                enum protocol_allow_config ret =
 787                        parse_protocol_config("protocol.allow", value);
 788                free(value);
 789                return ret;
 790        }
 791
 792        /* fallback to built-in defaults */
 793        /* known safe */
 794        if (!strcmp(type, "http") ||
 795            !strcmp(type, "https") ||
 796            !strcmp(type, "git") ||
 797            !strcmp(type, "ssh") ||
 798            !strcmp(type, "file"))
 799                return PROTOCOL_ALLOW_ALWAYS;
 800
 801        /* known scary; err on the side of caution */
 802        if (!strcmp(type, "ext"))
 803                return PROTOCOL_ALLOW_NEVER;
 804
 805        /* unknown; by default let them be used only directly by the user */
 806        return PROTOCOL_ALLOW_USER_ONLY;
 807}
 808
 809int is_transport_allowed(const char *type, int from_user)
 810{
 811        const struct string_list *whitelist = protocol_whitelist();
 812        if (whitelist)
 813                return string_list_has_string(whitelist, type);
 814
 815        switch (get_protocol_config(type)) {
 816        case PROTOCOL_ALLOW_ALWAYS:
 817                return 1;
 818        case PROTOCOL_ALLOW_NEVER:
 819                return 0;
 820        case PROTOCOL_ALLOW_USER_ONLY:
 821                if (from_user < 0)
 822                        from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
 823                return from_user;
 824        }
 825
 826        die("BUG: invalid protocol_allow_config type");
 827}
 828
 829void transport_check_allowed(const char *type)
 830{
 831        if (!is_transport_allowed(type, -1))
 832                die("transport '%s' not allowed", type);
 833}
 834
 835static struct transport_vtable bundle_vtable = {
 836        NULL,
 837        get_refs_from_bundle,
 838        fetch_refs_from_bundle,
 839        NULL,
 840        NULL,
 841        close_bundle
 842};
 843
 844static struct transport_vtable builtin_smart_vtable = {
 845        NULL,
 846        get_refs_via_connect,
 847        fetch_refs_via_pack,
 848        git_transport_push,
 849        connect_git,
 850        disconnect_git
 851};
 852
 853struct transport *transport_get(struct remote *remote, const char *url)
 854{
 855        const char *helper;
 856        struct transport *ret = xcalloc(1, sizeof(*ret));
 857
 858        ret->progress = isatty(2);
 859
 860        if (!remote)
 861                die("No remote provided to transport_get()");
 862
 863        ret->got_remote_refs = 0;
 864        ret->remote = remote;
 865        helper = remote->foreign_vcs;
 866
 867        if (!url && remote->url)
 868                url = remote->url[0];
 869        ret->url = url;
 870
 871        /* maybe it is a foreign URL? */
 872        if (url) {
 873                const char *p = url;
 874
 875                while (is_urlschemechar(p == url, *p))
 876                        p++;
 877                if (starts_with(p, "::"))
 878                        helper = xstrndup(url, p - url);
 879        }
 880
 881        if (helper) {
 882                transport_helper_init(ret, helper);
 883        } else if (starts_with(url, "rsync:")) {
 884                die("git-over-rsync is no longer supported");
 885        } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
 886                struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
 887                transport_check_allowed("file");
 888                ret->data = data;
 889                ret->vtable = &bundle_vtable;
 890                ret->smart_options = NULL;
 891        } else if (!is_url(url)
 892                || starts_with(url, "file://")
 893                || starts_with(url, "git://")
 894                || starts_with(url, "ssh://")
 895                || starts_with(url, "git+ssh://") /* deprecated - do not use */
 896                || starts_with(url, "ssh+git://") /* deprecated - do not use */
 897                ) {
 898                /*
 899                 * These are builtin smart transports; "allowed" transports
 900                 * will be checked individually in git_connect.
 901                 */
 902                struct git_transport_data *data = xcalloc(1, sizeof(*data));
 903                ret->data = data;
 904                ret->vtable = &builtin_smart_vtable;
 905                ret->smart_options = &(data->options);
 906
 907                data->conn = NULL;
 908                data->got_remote_heads = 0;
 909        } else {
 910                /* Unknown protocol in URL. Pass to external handler. */
 911                int len = external_specification_len(url);
 912                char *handler = xmemdupz(url, len);
 913                transport_helper_init(ret, handler);
 914        }
 915
 916        if (ret->smart_options) {
 917                ret->smart_options->thin = 1;
 918                ret->smart_options->uploadpack = "git-upload-pack";
 919                if (remote->uploadpack)
 920                        ret->smart_options->uploadpack = remote->uploadpack;
 921                ret->smart_options->receivepack = "git-receive-pack";
 922                if (remote->receivepack)
 923                        ret->smart_options->receivepack = remote->receivepack;
 924        }
 925
 926        return ret;
 927}
 928
 929int transport_set_option(struct transport *transport,
 930                         const char *name, const char *value)
 931{
 932        int git_reports = 1, protocol_reports = 1;
 933
 934        if (transport->smart_options)
 935                git_reports = set_git_option(transport->smart_options,
 936                                             name, value);
 937
 938        if (transport->vtable->set_option)
 939                protocol_reports = transport->vtable->set_option(transport,
 940                                                                 name, value);
 941
 942        /* If either report is 0, report 0 (success). */
 943        if (!git_reports || !protocol_reports)
 944                return 0;
 945        /* If either reports -1 (invalid value), report -1. */
 946        if ((git_reports == -1) || (protocol_reports == -1))
 947                return -1;
 948        /* Otherwise if both report unknown, report unknown. */
 949        return 1;
 950}
 951
 952void transport_set_verbosity(struct transport *transport, int verbosity,
 953        int force_progress)
 954{
 955        if (verbosity >= 1)
 956                transport->verbose = verbosity <= 3 ? verbosity : 3;
 957        if (verbosity < 0)
 958                transport->verbose = -1;
 959
 960        /**
 961         * Rules used to determine whether to report progress (processing aborts
 962         * when a rule is satisfied):
 963         *
 964         *   . Report progress, if force_progress is 1 (ie. --progress).
 965         *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
 966         *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
 967         *   . Report progress if isatty(2) is 1.
 968         **/
 969        if (force_progress >= 0)
 970                transport->progress = !!force_progress;
 971        else
 972                transport->progress = verbosity >= 0 && isatty(2);
 973}
 974
 975static void die_with_unpushed_submodules(struct string_list *needs_pushing)
 976{
 977        int i;
 978
 979        fprintf(stderr, _("The following submodule paths contain changes that can\n"
 980                        "not be found on any remote:\n"));
 981        for (i = 0; i < needs_pushing->nr; i++)
 982                fprintf(stderr, "  %s\n", needs_pushing->items[i].string);
 983        fprintf(stderr, _("\nPlease try\n\n"
 984                          "     git push --recurse-submodules=on-demand\n\n"
 985                          "or cd to the path and use\n\n"
 986                          "     git push\n\n"
 987                          "to push them to a remote.\n\n"));
 988
 989        string_list_clear(needs_pushing, 0);
 990
 991        die(_("Aborting."));
 992}
 993
 994static int run_pre_push_hook(struct transport *transport,
 995                             struct ref *remote_refs)
 996{
 997        int ret = 0, x;
 998        struct ref *r;
 999        struct child_process proc = CHILD_PROCESS_INIT;
1000        struct strbuf buf;
1001        const char *argv[4];
1002
1003        if (!(argv[0] = find_hook("pre-push")))
1004                return 0;
1005
1006        argv[1] = transport->remote->name;
1007        argv[2] = transport->url;
1008        argv[3] = NULL;
1009
1010        proc.argv = argv;
1011        proc.in = -1;
1012
1013        if (start_command(&proc)) {
1014                finish_command(&proc);
1015                return -1;
1016        }
1017
1018        sigchain_push(SIGPIPE, SIG_IGN);
1019
1020        strbuf_init(&buf, 256);
1021
1022        for (r = remote_refs; r; r = r->next) {
1023                if (!r->peer_ref) continue;
1024                if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1025                if (r->status == REF_STATUS_REJECT_STALE) continue;
1026                if (r->status == REF_STATUS_UPTODATE) continue;
1027
1028                strbuf_reset(&buf);
1029                strbuf_addf( &buf, "%s %s %s %s\n",
1030                         r->peer_ref->name, oid_to_hex(&r->new_oid),
1031                         r->name, oid_to_hex(&r->old_oid));
1032
1033                if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1034                        /* We do not mind if a hook does not read all refs. */
1035                        if (errno != EPIPE)
1036                                ret = -1;
1037                        break;
1038                }
1039        }
1040
1041        strbuf_release(&buf);
1042
1043        x = close(proc.in);
1044        if (!ret)
1045                ret = x;
1046
1047        sigchain_pop(SIGPIPE);
1048
1049        x = finish_command(&proc);
1050        if (!ret)
1051                ret = x;
1052
1053        return ret;
1054}
1055
1056int transport_push(struct transport *transport,
1057                   int refspec_nr, const char **refspec, int flags,
1058                   unsigned int *reject_reasons)
1059{
1060        *reject_reasons = 0;
1061        transport_verify_remote_names(refspec_nr, refspec);
1062
1063        if (transport_color_config() < 0)
1064                return -1;
1065
1066        if (transport->vtable->push_refs) {
1067                struct ref *remote_refs;
1068                struct ref *local_refs = get_local_heads();
1069                int match_flags = MATCH_REFS_NONE;
1070                int verbose = (transport->verbose > 0);
1071                int quiet = (transport->verbose < 0);
1072                int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1073                int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1074                int push_ret, ret, err;
1075
1076                if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1077                        return -1;
1078
1079                remote_refs = transport->vtable->get_refs_list(transport, 1);
1080
1081                if (flags & TRANSPORT_PUSH_ALL)
1082                        match_flags |= MATCH_REFS_ALL;
1083                if (flags & TRANSPORT_PUSH_MIRROR)
1084                        match_flags |= MATCH_REFS_MIRROR;
1085                if (flags & TRANSPORT_PUSH_PRUNE)
1086                        match_flags |= MATCH_REFS_PRUNE;
1087                if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1088                        match_flags |= MATCH_REFS_FOLLOW_TAGS;
1089
1090                if (match_push_refs(local_refs, &remote_refs,
1091                                    refspec_nr, refspec, match_flags)) {
1092                        return -1;
1093                }
1094
1095                if (transport->smart_options &&
1096                    transport->smart_options->cas &&
1097                    !is_empty_cas(transport->smart_options->cas))
1098                        apply_push_cas(transport->smart_options->cas,
1099                                       transport->remote, remote_refs);
1100
1101                set_ref_status_for_push(remote_refs,
1102                        flags & TRANSPORT_PUSH_MIRROR,
1103                        flags & TRANSPORT_PUSH_FORCE);
1104
1105                if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1106                        if (run_pre_push_hook(transport, remote_refs))
1107                                return -1;
1108
1109                if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1110                              TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1111                    !is_bare_repository()) {
1112                        struct ref *ref = remote_refs;
1113                        struct oid_array commits = OID_ARRAY_INIT;
1114
1115                        for (; ref; ref = ref->next)
1116                                if (!is_null_oid(&ref->new_oid))
1117                                        oid_array_append(&commits,
1118                                                          &ref->new_oid);
1119
1120                        if (!push_unpushed_submodules(&commits,
1121                                                      transport->remote,
1122                                                      refspec, refspec_nr,
1123                                                      transport->push_options,
1124                                                      pretend)) {
1125                                oid_array_clear(&commits);
1126                                die("Failed to push all needed submodules!");
1127                        }
1128                        oid_array_clear(&commits);
1129                }
1130
1131                if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1132                     ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1133                                TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1134                      !pretend)) && !is_bare_repository()) {
1135                        struct ref *ref = remote_refs;
1136                        struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1137                        struct oid_array commits = OID_ARRAY_INIT;
1138
1139                        for (; ref; ref = ref->next)
1140                                if (!is_null_oid(&ref->new_oid))
1141                                        oid_array_append(&commits,
1142                                                          &ref->new_oid);
1143
1144                        if (find_unpushed_submodules(&commits, transport->remote->name,
1145                                                &needs_pushing)) {
1146                                oid_array_clear(&commits);
1147                                die_with_unpushed_submodules(&needs_pushing);
1148                        }
1149                        string_list_clear(&needs_pushing, 0);
1150                        oid_array_clear(&commits);
1151                }
1152
1153                if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1154                        push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1155                else
1156                        push_ret = 0;
1157                err = push_had_errors(remote_refs);
1158                ret = push_ret | err;
1159
1160                if (!quiet || err)
1161                        transport_print_push_status(transport->url, remote_refs,
1162                                        verbose | porcelain, porcelain,
1163                                        reject_reasons);
1164
1165                if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1166                        set_upstreams(transport, remote_refs, pretend);
1167
1168                if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1169                               TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1170                        struct ref *ref;
1171                        for (ref = remote_refs; ref; ref = ref->next)
1172                                transport_update_tracking_ref(transport->remote, ref, verbose);
1173                }
1174
1175                if (porcelain && !push_ret)
1176                        puts("Done");
1177                else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1178                        fprintf(stderr, "Everything up-to-date\n");
1179
1180                return ret;
1181        }
1182        return 1;
1183}
1184
1185const struct ref *transport_get_remote_refs(struct transport *transport)
1186{
1187        if (!transport->got_remote_refs) {
1188                transport->remote_refs = transport->vtable->get_refs_list(transport, 0);
1189                transport->got_remote_refs = 1;
1190        }
1191
1192        return transport->remote_refs;
1193}
1194
1195int transport_fetch_refs(struct transport *transport, struct ref *refs)
1196{
1197        int rc;
1198        int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1199        struct ref **heads = NULL;
1200        struct ref *rm;
1201
1202        for (rm = refs; rm; rm = rm->next) {
1203                nr_refs++;
1204                if (rm->peer_ref &&
1205                    !is_null_oid(&rm->old_oid) &&
1206                    !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1207                        continue;
1208                ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1209                heads[nr_heads++] = rm;
1210        }
1211
1212        if (!nr_heads) {
1213                /*
1214                 * When deepening of a shallow repository is requested,
1215                 * then local and remote refs are likely to still be equal.
1216                 * Just feed them all to the fetch method in that case.
1217                 * This condition shouldn't be met in a non-deepening fetch
1218                 * (see builtin/fetch.c:quickfetch()).
1219                 */
1220                ALLOC_ARRAY(heads, nr_refs);
1221                for (rm = refs; rm; rm = rm->next)
1222                        heads[nr_heads++] = rm;
1223        }
1224
1225        rc = transport->vtable->fetch(transport, nr_heads, heads);
1226
1227        free(heads);
1228        return rc;
1229}
1230
1231void transport_unlock_pack(struct transport *transport)
1232{
1233        if (transport->pack_lockfile) {
1234                unlink_or_warn(transport->pack_lockfile);
1235                FREE_AND_NULL(transport->pack_lockfile);
1236        }
1237}
1238
1239int transport_connect(struct transport *transport, const char *name,
1240                      const char *exec, int fd[2])
1241{
1242        if (transport->vtable->connect)
1243                return transport->vtable->connect(transport, name, exec, fd);
1244        else
1245                die("Operation not supported by protocol");
1246}
1247
1248int transport_disconnect(struct transport *transport)
1249{
1250        int ret = 0;
1251        if (transport->vtable->disconnect)
1252                ret = transport->vtable->disconnect(transport);
1253        free(transport);
1254        return ret;
1255}
1256
1257/*
1258 * Strip username (and password) from a URL and return
1259 * it in a newly allocated string.
1260 */
1261char *transport_anonymize_url(const char *url)
1262{
1263        char *scheme_prefix, *anon_part;
1264        size_t anon_len, prefix_len = 0;
1265
1266        anon_part = strchr(url, '@');
1267        if (url_is_local_not_ssh(url) || !anon_part)
1268                goto literal_copy;
1269
1270        anon_len = strlen(++anon_part);
1271        scheme_prefix = strstr(url, "://");
1272        if (!scheme_prefix) {
1273                if (!strchr(anon_part, ':'))
1274                        /* cannot be "me@there:/path/name" */
1275                        goto literal_copy;
1276        } else {
1277                const char *cp;
1278                /* make sure scheme is reasonable */
1279                for (cp = url; cp < scheme_prefix; cp++) {
1280                        switch (*cp) {
1281                                /* RFC 1738 2.1 */
1282                        case '+': case '.': case '-':
1283                                break; /* ok */
1284                        default:
1285                                if (isalnum(*cp))
1286                                        break;
1287                                /* it isn't */
1288                                goto literal_copy;
1289                        }
1290                }
1291                /* @ past the first slash does not count */
1292                cp = strchr(scheme_prefix + 3, '/');
1293                if (cp && cp < anon_part)
1294                        goto literal_copy;
1295                prefix_len = scheme_prefix - url + 3;
1296        }
1297        return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1298                       (int)anon_len, anon_part);
1299literal_copy:
1300        return xstrdup(url);
1301}
1302
1303static void read_alternate_refs(const char *path,
1304                                alternate_ref_fn *cb,
1305                                void *data)
1306{
1307        struct child_process cmd = CHILD_PROCESS_INIT;
1308        struct strbuf line = STRBUF_INIT;
1309        FILE *fh;
1310
1311        cmd.git_cmd = 1;
1312        argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1313        argv_array_push(&cmd.args, "for-each-ref");
1314        argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1315        cmd.env = local_repo_env;
1316        cmd.out = -1;
1317
1318        if (start_command(&cmd))
1319                return;
1320
1321        fh = xfdopen(cmd.out, "r");
1322        while (strbuf_getline_lf(&line, fh) != EOF) {
1323                struct object_id oid;
1324
1325                if (get_oid_hex(line.buf, &oid) ||
1326                    line.buf[GIT_SHA1_HEXSZ] != ' ') {
1327                        warning("invalid line while parsing alternate refs: %s",
1328                                line.buf);
1329                        break;
1330                }
1331
1332                cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1333        }
1334
1335        fclose(fh);
1336        finish_command(&cmd);
1337}
1338
1339struct alternate_refs_data {
1340        alternate_ref_fn *fn;
1341        void *data;
1342};
1343
1344static int refs_from_alternate_cb(struct alternate_object_database *e,
1345                                  void *data)
1346{
1347        struct strbuf path = STRBUF_INIT;
1348        size_t base_len;
1349        struct alternate_refs_data *cb = data;
1350
1351        if (!strbuf_realpath(&path, e->path, 0))
1352                goto out;
1353        if (!strbuf_strip_suffix(&path, "/objects"))
1354                goto out;
1355        base_len = path.len;
1356
1357        /* Is this a git repository with refs? */
1358        strbuf_addstr(&path, "/refs");
1359        if (!is_directory(path.buf))
1360                goto out;
1361        strbuf_setlen(&path, base_len);
1362
1363        read_alternate_refs(path.buf, cb->fn, cb->data);
1364
1365out:
1366        strbuf_release(&path);
1367        return 0;
1368}
1369
1370void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1371{
1372        struct alternate_refs_data cb;
1373        cb.fn = fn;
1374        cb.data = data;
1375        foreach_alt_odb(refs_from_alternate_cb, &cb);
1376}