]> git.ipfire.org Git - thirdparty/git.git/blobdiff - transport.c
Git 2.25
[thirdparty/git.git] / transport.c
index fc802260f61c7496f4250b15ef6f5ab8dd316898..83379a037d69c2f6d97004d41b96f82f10d209dc 100644 (file)
@@ -11,6 +11,7 @@
 #include "bundle.h"
 #include "dir.h"
 #include "refs.h"
+#include "refspec.h"
 #include "branch.h"
 #include "url.h"
 #include "submodule.h"
 #include "sha1-array.h"
 #include "sigchain.h"
 #include "transport-internal.h"
+#include "protocol.h"
+#include "object-store.h"
+#include "color.h"
+
+static int transport_use_color = -1;
+static char transport_colors[][COLOR_MAXLEN] = {
+       GIT_COLOR_RESET,
+       GIT_COLOR_RED           /* REJECTED */
+};
+
+enum color_transport {
+       TRANSPORT_COLOR_RESET = 0,
+       TRANSPORT_COLOR_REJECTED = 1
+};
+
+static int transport_color_config(void)
+{
+       const char *keys[] = {
+               "color.transport.reset",
+               "color.transport.rejected"
+       }, *key = "color.transport";
+       char *value;
+       int i;
+       static int initialized;
+
+       if (initialized)
+               return 0;
+       initialized = 1;
+
+       if (!git_config_get_string(key, &value))
+               transport_use_color = git_config_colorbool(key, value);
+
+       if (!want_color_stderr(transport_use_color))
+               return 0;
+
+       for (i = 0; i < ARRAY_SIZE(keys); i++)
+               if (!git_config_get_string(keys[i], &value)) {
+                       if (!value)
+                               return config_error_nonbool(keys[i]);
+                       if (color_parse(value, transport_colors[i]) < 0)
+                               return -1;
+               }
+
+       return 0;
+}
+
+static const char *transport_get_color(enum color_transport ix)
+{
+       if (want_color_stderr(transport_use_color))
+               return transport_colors[ix];
+       return "";
+}
 
 static void set_upstreams(struct transport *transport, struct ref *refs,
        int pretend)
@@ -69,9 +122,12 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
 struct bundle_transport_data {
        int fd;
        struct bundle_header header;
+       unsigned get_refs_from_bundle_called : 1;
 };
 
-static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
+static struct ref *get_refs_from_bundle(struct transport *transport,
+                                       int for_push,
+                                       const struct argv_array *ref_prefixes)
 {
        struct bundle_transport_data *data = transport->data;
        struct ref *result = NULL;
@@ -80,11 +136,13 @@ static struct ref *get_refs_from_bundle(struct transport *transport, int for_pus
        if (for_push)
                return NULL;
 
+       data->get_refs_from_bundle_called = 1;
+
        if (data->fd > 0)
                close(data->fd);
        data->fd = read_bundle_header(transport->url, &data->header);
        if (data->fd < 0)
-               die ("Could not read bundle '%s'.", transport->url);
+               die(_("could not read bundle '%s'"), transport->url);
        for (i = 0; i < data->header.references.nr; i++) {
                struct ref_list_entry *e = data->header.references.list + i;
                struct ref *ref = alloc_ref(e->name);
@@ -99,7 +157,10 @@ static int fetch_refs_from_bundle(struct transport *transport,
                               int nr_heads, struct ref **to_fetch)
 {
        struct bundle_transport_data *data = transport->data;
-       return unbundle(&data->header, data->fd,
+
+       if (!data->get_refs_from_bundle_called)
+               get_refs_from_bundle(transport, 0, NULL);
+       return unbundle(the_repository, &data->header, data->fd,
                        transport->progress ? BUNDLE_VERBOSE : 0);
 }
 
@@ -117,6 +178,7 @@ struct git_transport_data {
        struct child_process *conn;
        int fd[2];
        unsigned got_remote_heads : 1;
+       enum protocol_version version;
        struct oid_array extra_have;
        struct oid_array shallow;
 };
@@ -161,6 +223,16 @@ static int set_git_option(struct git_transport_options *opts,
        } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
                opts->deepen_relative = !!value;
                return 0;
+       } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
+               opts->from_promisor = !!value;
+               return 0;
+       } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
+               opts->no_dependents = !!value;
+               return 0;
+       } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
+               list_objects_filter_die_if_populated(&opts->filter_options);
+               parse_list_objects_filter(&opts->filter_options, value);
+               return 0;
        }
        return 1;
 }
@@ -187,28 +259,77 @@ static int connect_setup(struct transport *transport, int for_push)
        return 0;
 }
 
-static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
+static void die_if_server_options(struct transport *transport)
+{
+       if (!transport->server_options || !transport->server_options->nr)
+               return;
+       advise(_("see protocol.version in 'git help config' for more details"));
+       die(_("server options require protocol version 2 or later"));
+}
+
+/*
+ * Obtains the protocol version from the transport and writes it to
+ * transport->data->version, first connecting if not already connected.
+ *
+ * If the protocol version is one that allows skipping the listing of remote
+ * refs, and must_list_refs is 0, the listing of remote refs is skipped and
+ * this function returns NULL. Otherwise, this function returns the list of
+ * remote refs.
+ */
+static struct ref *handshake(struct transport *transport, int for_push,
+                            const struct argv_array *ref_prefixes,
+                            int must_list_refs)
 {
        struct git_transport_data *data = transport->data;
-       struct ref *refs;
+       struct ref *refs = NULL;
+       struct packet_reader reader;
 
        connect_setup(transport, for_push);
-       get_remote_heads(data->fd[0], NULL, 0, &refs,
-                        for_push ? REF_NORMAL : 0,
-                        &data->extra_have,
-                        &data->shallow);
+
+       packet_reader_init(&reader, data->fd[0], NULL, 0,
+                          PACKET_READ_CHOMP_NEWLINE |
+                          PACKET_READ_GENTLE_ON_EOF |
+                          PACKET_READ_DIE_ON_ERR_PACKET);
+
+       data->version = discover_version(&reader);
+       switch (data->version) {
+       case protocol_v2:
+               if (must_list_refs)
+                       get_remote_refs(data->fd[1], &reader, &refs, for_push,
+                                       ref_prefixes,
+                                       transport->server_options);
+               break;
+       case protocol_v1:
+       case protocol_v0:
+               die_if_server_options(transport);
+               get_remote_heads(&reader, &refs,
+                                for_push ? REF_NORMAL : 0,
+                                &data->extra_have,
+                                &data->shallow);
+               break;
+       case protocol_unknown_version:
+               BUG("unknown protocol version");
+       }
        data->got_remote_heads = 1;
 
+       if (reader.line_peeked)
+               BUG("buffer must be empty at the end of handshake()");
+
        return refs;
 }
 
+static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
+                                       const struct argv_array *ref_prefixes)
+{
+       return handshake(transport, for_push, ref_prefixes, 1);
+}
+
 static int fetch_refs_via_pack(struct transport *transport,
                               int nr_heads, struct ref **to_fetch)
 {
        int ret = 0;
        struct git_transport_data *data = transport->data;
-       struct ref *refs;
-       char *dest = xstrdup(transport->url);
+       struct ref *refs = NULL;
        struct fetch_pack_args args;
        struct ref *refs_tmp = NULL;
 
@@ -229,18 +350,44 @@ static int fetch_refs_via_pack(struct transport *transport,
                data->options.check_self_contained_and_connected;
        args.cloning = transport->cloning;
        args.update_shallow = data->options.update_shallow;
+       args.from_promisor = data->options.from_promisor;
+       args.no_dependents = data->options.no_dependents;
+       args.filter_options = data->options.filter_options;
+       args.stateless_rpc = transport->stateless_rpc;
+       args.server_options = transport->server_options;
+       args.negotiation_tips = data->options.negotiation_tips;
 
        if (!data->got_remote_heads) {
-               connect_setup(transport, 0);
-               get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
-                                NULL, &data->shallow);
-               data->got_remote_heads = 1;
+               int i;
+               int must_list_refs = 0;
+               for (i = 0; i < nr_heads; i++) {
+                       if (!to_fetch[i]->exact_oid) {
+                               must_list_refs = 1;
+                               break;
+                       }
+               }
+               refs_tmp = handshake(transport, 0, NULL, must_list_refs);
+       }
+
+       switch (data->version) {
+       case protocol_v2:
+               refs = fetch_pack(&args, data->fd,
+                                 refs_tmp ? refs_tmp : transport->remote_refs,
+                                 to_fetch, nr_heads, &data->shallow,
+                                 &transport->pack_lockfile, data->version);
+               break;
+       case protocol_v1:
+       case protocol_v0:
+               die_if_server_options(transport);
+               refs = fetch_pack(&args, data->fd,
+                                 refs_tmp ? refs_tmp : transport->remote_refs,
+                                 to_fetch, nr_heads, &data->shallow,
+                                 &transport->pack_lockfile, data->version);
+               break;
+       case protocol_unknown_version:
+               BUG("unknown protocol version");
        }
 
-       refs = fetch_pack(&args, data->fd, data->conn,
-                         refs_tmp ? refs_tmp : transport->remote_refs,
-                         dest, to_fetch, nr_heads, &data->shallow,
-                         &transport->pack_lockfile);
        close(data->fd[0]);
        close(data->fd[1]);
        if (finish_connect(data->conn))
@@ -249,6 +396,7 @@ static int fetch_refs_via_pack(struct transport *transport,
        data->got_remote_heads = 0;
        data->options.self_contained_and_connected =
                args.self_contained_and_connected;
+       data->options.connectivity_checked = args.connectivity_checked;
 
        if (refs == NULL)
                ret = -1;
@@ -257,7 +405,6 @@ static int fetch_refs_via_pack(struct transport *transport,
 
        free_refs(refs_tmp);
        free_refs(refs);
-       free(dest);
        return ret;
 }
 
@@ -292,7 +439,7 @@ int transport_refs_pushed(struct ref *ref)
 
 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
 {
-       struct refspec rs;
+       struct refspec_item rs;
 
        if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
                return;
@@ -326,7 +473,13 @@ static void print_ref_status(char flag, const char *summary,
                else
                        fprintf(stdout, "%s\n", summary);
        } else {
-               fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
+               const char *red = "", *reset = "";
+               if (push_had_errors(to)) {
+                       red = transport_get_color(TRANSPORT_COLOR_REJECTED);
+                       reset = transport_get_color(TRANSPORT_COLOR_RESET);
+               }
+               fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
+                       summary, reset);
                if (from)
                        fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
                else
@@ -355,7 +508,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
                char type;
                const char *msg;
 
-               strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
+               strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
                                         DEFAULT_ABBREV);
                if (ref->forced_update) {
                        strbuf_addstr(&quickref, "...");
@@ -366,7 +519,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
                        type = ' ';
                        msg = NULL;
                }
-               strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
+               strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
                                         DEFAULT_ABBREV);
 
                print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
@@ -449,7 +602,7 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count,
 static int measure_abbrev(const struct object_id *oid, int sofar)
 {
        char hex[GIT_MAX_HEXSZ + 1];
-       int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
+       int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
 
        return (w < sofar) ? sofar : w;
 }
@@ -475,6 +628,9 @@ void transport_print_push_status(const char *dest, struct ref *refs,
        char *head;
        int summary_width = transport_summary_width(refs);
 
+       if (transport_color_config() < 0)
+               warning(_("could not parse transport.color.* config"));
+
        head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
 
        if (verbose) {
@@ -512,43 +668,17 @@ void transport_print_push_status(const char *dest, struct ref *refs,
        free(head);
 }
 
-void transport_verify_remote_names(int nr_heads, const char **heads)
-{
-       int i;
-
-       for (i = 0; i < nr_heads; i++) {
-               const char *local = heads[i];
-               const char *remote = strrchr(heads[i], ':');
-
-               if (*local == '+')
-                       local++;
-
-               /* A matching refspec is okay.  */
-               if (remote == local && remote[1] == '\0')
-                       continue;
-
-               remote = remote ? (remote + 1) : local;
-               if (check_refname_format(remote,
-                               REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
-                       die("remote part of refspec is not a valid name in %s",
-                               heads[i]);
-       }
-}
-
 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
 {
        struct git_transport_data *data = transport->data;
        struct send_pack_args args;
-       int ret;
+       int ret = 0;
 
-       if (!data->got_remote_heads) {
-               struct ref *tmp_refs;
-               connect_setup(transport, 1);
+       if (transport_color_config() < 0)
+               return -1;
 
-               get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
-                                NULL, &data->shallow);
-               data->got_remote_heads = 1;
-       }
+       if (!data->got_remote_heads)
+               get_refs_via_connect(transport, 1, NULL);
 
        memset(&args, 0, sizeof(args));
        args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
@@ -570,8 +700,18 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
        else
                args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
 
-       ret = send_pack(&args, data->fd, data->conn, remote_refs,
-                       &data->extra_have);
+       switch (data->version) {
+       case protocol_v2:
+               die(_("support for protocol v2 not implemented yet"));
+               break;
+       case protocol_v1:
+       case protocol_v0:
+               ret = send_pack(&args, data->fd, data->conn, remote_refs,
+                               &data->extra_have);
+               break;
+       case protocol_unknown_version:
+               BUG("unknown protocol version");
+       }
 
        close(data->fd[1]);
        close(data->fd[0]);
@@ -623,7 +763,7 @@ void transport_take_over(struct transport *transport,
        struct git_transport_data *data;
 
        if (!transport->smart_options)
-               die("BUG: taking over transport requires non-NULL "
+               BUG("taking over transport requires non-NULL "
                    "smart_options field.");
 
        data = xcalloc(1, sizeof(*data));
@@ -688,7 +828,7 @@ static enum protocol_allow_config parse_protocol_config(const char *key,
        else if (!strcasecmp(value, "user"))
                return PROTOCOL_ALLOW_USER_ONLY;
 
-       die("unknown value for config '%s': %s", key, value);
+       die(_("unknown value for config '%s': %s"), key, value);
 }
 
 static enum protocol_allow_config get_protocol_config(const char *type)
@@ -748,13 +888,13 @@ int is_transport_allowed(const char *type, int from_user)
                return from_user;
        }
 
-       die("BUG: invalid protocol_allow_config type");
+       BUG("invalid protocol_allow_config type");
 }
 
 void transport_check_allowed(const char *type)
 {
        if (!is_transport_allowed(type, -1))
-               die("transport '%s' not allowed", type);
+               die(_("transport '%s' not allowed"), type);
 }
 
 static struct transport_vtable bundle_vtable = {
@@ -783,7 +923,7 @@ struct transport *transport_get(struct remote *remote, const char *url)
        ret->progress = isatty(2);
 
        if (!remote)
-               die("No remote provided to transport_get()");
+               BUG("No remote provided to transport_get()");
 
        ret->got_remote_refs = 0;
        ret->remote = remote;
@@ -806,7 +946,7 @@ struct transport *transport_get(struct remote *remote, const char *url)
        if (helper) {
                transport_helper_init(ret, helper);
        } else if (starts_with(url, "rsync:")) {
-               die("git-over-rsync is no longer supported");
+               die(_("git-over-rsync is no longer supported"));
        } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
                struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
                transport_check_allowed("file");
@@ -934,6 +1074,7 @@ static int run_pre_push_hook(struct transport *transport,
 
        proc.argv = argv;
        proc.in = -1;
+       proc.trace2_hook_name = "pre-push";
 
        if (start_command(&proc)) {
                finish_command(&proc);
@@ -978,12 +1119,15 @@ static int run_pre_push_hook(struct transport *transport,
        return ret;
 }
 
-int transport_push(struct transport *transport,
-                  int refspec_nr, const char **refspec, int flags,
+int transport_push(struct repository *r,
+                  struct transport *transport,
+                  struct refspec *rs, int flags,
                   unsigned int *reject_reasons)
 {
        *reject_reasons = 0;
-       transport_verify_remote_names(refspec_nr, refspec);
+
+       if (transport_color_config() < 0)
+               return -1;
 
        if (transport->vtable->push_refs) {
                struct ref *remote_refs;
@@ -994,11 +1138,19 @@ int transport_push(struct transport *transport,
                int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
                int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
                int push_ret, ret, err;
+               struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
 
-               if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
+               if (check_push_refs(local_refs, rs) < 0)
                        return -1;
 
-               remote_refs = transport->vtable->get_refs_list(transport, 1);
+               refspec_ref_prefixes(rs, &ref_prefixes);
+
+               trace2_region_enter("transport_push", "get_refs_list", r);
+               remote_refs = transport->vtable->get_refs_list(transport, 1,
+                                                              &ref_prefixes);
+               trace2_region_leave("transport_push", "get_refs_list", r);
+
+               argv_array_clear(&ref_prefixes);
 
                if (flags & TRANSPORT_PUSH_ALL)
                        match_flags |= MATCH_REFS_ALL;
@@ -1009,10 +1161,8 @@ int transport_push(struct transport *transport,
                if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
                        match_flags |= MATCH_REFS_FOLLOW_TAGS;
 
-               if (match_push_refs(local_refs, &remote_refs,
-                                   refspec_nr, refspec, match_flags)) {
+               if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
                        return -1;
-               }
 
                if (transport->smart_options &&
                    transport->smart_options->cas &&
@@ -1034,20 +1184,24 @@ int transport_push(struct transport *transport,
                        struct ref *ref = remote_refs;
                        struct oid_array commits = OID_ARRAY_INIT;
 
+                       trace2_region_enter("transport_push", "push_submodules", r);
                        for (; ref; ref = ref->next)
                                if (!is_null_oid(&ref->new_oid))
                                        oid_array_append(&commits,
                                                          &ref->new_oid);
 
-                       if (!push_unpushed_submodules(&commits,
+                       if (!push_unpushed_submodules(r,
+                                                     &commits,
                                                      transport->remote,
-                                                     refspec, refspec_nr,
+                                                     rs,
                                                      transport->push_options,
                                                      pretend)) {
                                oid_array_clear(&commits);
-                               die("Failed to push all needed submodules!");
+                               trace2_region_leave("transport_push", "push_submodules", r);
+                               die(_("failed to push all needed submodules"));
                        }
                        oid_array_clear(&commits);
+                       trace2_region_leave("transport_push", "push_submodules", r);
                }
 
                if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
@@ -1058,27 +1212,48 @@ int transport_push(struct transport *transport,
                        struct string_list needs_pushing = STRING_LIST_INIT_DUP;
                        struct oid_array commits = OID_ARRAY_INIT;
 
+                       trace2_region_enter("transport_push", "check_submodules", r);
                        for (; ref; ref = ref->next)
                                if (!is_null_oid(&ref->new_oid))
                                        oid_array_append(&commits,
                                                          &ref->new_oid);
 
-                       if (find_unpushed_submodules(&commits, transport->remote->name,
-                                               &needs_pushing)) {
+                       if (find_unpushed_submodules(r,
+                                                    &commits,
+                                                    transport->remote->name,
+                                                    &needs_pushing)) {
                                oid_array_clear(&commits);
+                               trace2_region_leave("transport_push", "check_submodules", r);
                                die_with_unpushed_submodules(&needs_pushing);
                        }
                        string_list_clear(&needs_pushing, 0);
                        oid_array_clear(&commits);
+                       trace2_region_leave("transport_push", "check_submodules", r);
                }
 
-               if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
+               if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
+                       trace2_region_enter("transport_push", "push_refs", r);
                        push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
-               else
+                       trace2_region_leave("transport_push", "push_refs", r);
+               } else
                        push_ret = 0;
                err = push_had_errors(remote_refs);
                ret = push_ret | err;
 
+               if ((flags & TRANSPORT_PUSH_ATOMIC) && err) {
+                       struct ref *it;
+                       for (it = remote_refs; it; it = it->next)
+                               switch (it->status) {
+                               case REF_STATUS_NONE:
+                               case REF_STATUS_UPTODATE:
+                               case REF_STATUS_OK:
+                                       it->status = REF_STATUS_ATOMIC_PUSH_FAILED;
+                                       break;
+                               default:
+                                       break;
+                               }
+               }
+
                if (!quiet || err)
                        transport_print_push_status(transport->url, remote_refs,
                                        verbose | porcelain, porcelain,
@@ -1104,10 +1279,13 @@ int transport_push(struct transport *transport,
        return 1;
 }
 
-const struct ref *transport_get_remote_refs(struct transport *transport)
+const struct ref *transport_get_remote_refs(struct transport *transport,
+                                           const struct argv_array *ref_prefixes)
 {
        if (!transport->got_remote_refs) {
-               transport->remote_refs = transport->vtable->get_refs_list(transport, 0);
+               transport->remote_refs =
+                       transport->vtable->get_refs_list(transport, 0,
+                                                        ref_prefixes);
                transport->got_remote_refs = 1;
        }
 
@@ -1125,7 +1303,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
                nr_refs++;
                if (rm->peer_ref &&
                    !is_null_oid(&rm->old_oid) &&
-                   !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
+                   oideq(&rm->peer_ref->old_oid, &rm->old_oid))
                        continue;
                ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
                heads[nr_heads++] = rm;
@@ -1164,7 +1342,7 @@ int transport_connect(struct transport *transport, const char *name,
        if (transport->vtable->connect)
                return transport->vtable->connect(transport, name, exec, fd);
        else
-               die("Operation not supported by protocol");
+               die(_("operation not supported by protocol"));
 }
 
 int transport_disconnect(struct transport *transport)
@@ -1221,78 +1399,3 @@ char *transport_anonymize_url(const char *url)
 literal_copy:
        return xstrdup(url);
 }
-
-static void read_alternate_refs(const char *path,
-                               alternate_ref_fn *cb,
-                               void *data)
-{
-       struct child_process cmd = CHILD_PROCESS_INIT;
-       struct strbuf line = STRBUF_INIT;
-       FILE *fh;
-
-       cmd.git_cmd = 1;
-       argv_array_pushf(&cmd.args, "--git-dir=%s", path);
-       argv_array_push(&cmd.args, "for-each-ref");
-       argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
-       cmd.env = local_repo_env;
-       cmd.out = -1;
-
-       if (start_command(&cmd))
-               return;
-
-       fh = xfdopen(cmd.out, "r");
-       while (strbuf_getline_lf(&line, fh) != EOF) {
-               struct object_id oid;
-
-               if (get_oid_hex(line.buf, &oid) ||
-                   line.buf[GIT_SHA1_HEXSZ] != ' ') {
-                       warning("invalid line while parsing alternate refs: %s",
-                               line.buf);
-                       break;
-               }
-
-               cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
-       }
-
-       fclose(fh);
-       finish_command(&cmd);
-}
-
-struct alternate_refs_data {
-       alternate_ref_fn *fn;
-       void *data;
-};
-
-static int refs_from_alternate_cb(struct alternate_object_database *e,
-                                 void *data)
-{
-       struct strbuf path = STRBUF_INIT;
-       size_t base_len;
-       struct alternate_refs_data *cb = data;
-
-       if (!strbuf_realpath(&path, e->path, 0))
-               goto out;
-       if (!strbuf_strip_suffix(&path, "/objects"))
-               goto out;
-       base_len = path.len;
-
-       /* Is this a git repository with refs? */
-       strbuf_addstr(&path, "/refs");
-       if (!is_directory(path.buf))
-               goto out;
-       strbuf_setlen(&path, base_len);
-
-       read_alternate_refs(path.buf, cb->fn, cb->data);
-
-out:
-       strbuf_release(&path);
-       return 0;
-}
-
-void for_each_alternate_ref(alternate_ref_fn fn, void *data)
-{
-       struct alternate_refs_data cb;
-       cb.fn = fn;
-       cb.data = data;
-       foreach_alt_odb(refs_from_alternate_cb, &cb);
-}