Merge branch 'jt/transport-hide-vtable'
authorJunio C Hamano <gitster@pobox.com>
Thu, 28 Dec 2017 22:08:47 +0000 (14:08 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Dec 2017 22:08:47 +0000 (14:08 -0800)
Code clean-up.

* jt/transport-hide-vtable:
transport: make transport vtable more private
clone, fetch: remove redundant transport check

1  2 
builtin/clone.c
builtin/fetch.c
transport-helper.c
transport.c
diff --combined builtin/clone.c
index 6ad0ab3fa40af1761482f5fc78b4b1c401f9717c,979af0383dc9e93430a105698ce8a55970907d76..2da71db10752bf95647052dcd5b7c4b28e7a50ac
@@@ -452,8 -452,7 +452,8 @@@ static void clone_local(const char *src
  {
        if (option_shared) {
                struct strbuf alt = STRBUF_INIT;
 -              strbuf_addf(&alt, "%s/objects", src_repo);
 +              get_common_dir(&alt, src_repo);
 +              strbuf_addstr(&alt, "/objects");
                add_to_alternates_file(alt.buf);
                strbuf_release(&alt);
        } else {
@@@ -589,7 -588,7 +589,7 @@@ static void write_remote_refs(const str
        for (r = local_refs; r; r = r->next) {
                if (!r->peer_ref)
                        continue;
 -              if (ref_transaction_create(t, r->peer_ref->name, r->old_oid.hash,
 +              if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
                                           0, NULL, &err))
                        die("%s", err.buf);
        }
@@@ -611,12 -610,12 +611,12 @@@ static void write_followtags(const stru
                        continue;
                if (!has_object_file(&ref->old_oid))
                        continue;
 -              update_ref(msg, ref->name, ref->old_oid.hash,
 -                         NULL, 0, UPDATE_REFS_DIE_ON_ERR);
 +              update_ref(msg, ref->name, &ref->old_oid, NULL, 0,
 +                         UPDATE_REFS_DIE_ON_ERR);
        }
  }
  
 -static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
 +static int iterate_ref_map(void *cb_data, struct object_id *oid)
  {
        struct ref **rm = cb_data;
        struct ref *ref = *rm;
        if (!ref)
                return -1;
  
 -      hashcpy(sha1, ref->old_oid.hash);
 +      oidcpy(oid, &ref->old_oid);
        *rm = ref->next;
        return 0;
  }
@@@ -683,23 -682,23 +683,23 @@@ static void update_head(const struct re
                if (create_symref("HEAD", our->name, NULL) < 0)
                        die(_("unable to update HEAD"));
                if (!option_bare) {
 -                      update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0,
 +                      update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
                                   UPDATE_REFS_DIE_ON_ERR);
                        install_branch_config(0, head, option_origin, our->name);
                }
        } else if (our) {
                struct commit *c = lookup_commit_reference(&our->old_oid);
                /* --branch specifies a non-branch (i.e. tags), detach HEAD */
 -              update_ref(msg, "HEAD", c->object.oid.hash,
 -                         NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
 +              update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
 +                         UPDATE_REFS_DIE_ON_ERR);
        } else if (remote) {
                /*
                 * We know remote HEAD points to a non-branch, or
                 * HEAD points to a branch but we don't know which one.
                 * Detach HEAD in all these cases.
                 */
 -              update_ref(msg, "HEAD", remote->old_oid.hash,
 -                         NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
 +              update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
 +                         UPDATE_REFS_DIE_ON_ERR);
        }
  }
  
@@@ -707,7 -706,7 +707,7 @@@ static int checkout(int submodule_progr
  {
        struct object_id oid;
        char *head;
 -      struct lock_file *lock_file;
 +      struct lock_file lock_file = LOCK_INIT;
        struct unpack_trees_options opts;
        struct tree *tree;
        struct tree_desc t;
        if (option_no_checkout)
                return 0;
  
 -      head = resolve_refdup("HEAD", RESOLVE_REF_READING, oid.hash, NULL);
 +      head = resolve_refdup("HEAD", RESOLVE_REF_READING, &oid, NULL);
        if (!head) {
                warning(_("remote HEAD refers to nonexistent ref, "
                          "unable to checkout.\n"));
        /* We need to be in the new work tree for the checkout */
        setup_work_tree();
  
 -      lock_file = xcalloc(1, sizeof(struct lock_file));
 -      hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
 +      hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
  
        memset(&opts, 0, sizeof opts);
        opts.update = 1;
        if (unpack_trees(1, &t, &opts) < 0)
                die(_("unable to checkout working tree"));
  
 -      if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
 +      if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                die(_("unable to write new index file"));
  
        err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
@@@ -1083,9 -1083,6 +1083,6 @@@ int cmd_clone(int argc, const char **ar
                warning(_("--local is ignored"));
        transport->cloning = 1;
  
-       if (!transport->get_refs_list || (!is_local && !transport->fetch))
-               die(_("Don't know how to clone %s"), transport->url);
        transport_set_option(transport, TRANS_OPT_KEEP, "yes");
  
        if (option_depth)
diff --combined builtin/fetch.c
index e656746ab9afd071c3dff49b6b04c878f11996fa,09eb1fc1712f20e93dbeefc979294c79f83684c9..7bbcd26faf1fe650ccc7dbbd526db5114aac0c8f
@@@ -3,7 -3,6 +3,7 @@@
   */
  #include "cache.h"
  #include "config.h"
 +#include "repository.h"
  #include "refs.h"
  #include "commit.h"
  #include "builtin.h"
@@@ -458,8 -457,8 +458,8 @@@ static int s_update_ref(const char *act
        transaction = ref_transaction_begin(&err);
        if (!transaction ||
            ref_transaction_update(transaction, ref->name,
 -                                 ref->new_oid.hash,
 -                                 check_old ? ref->old_oid.hash : NULL,
 +                                 &ref->new_oid,
 +                                 check_old ? &ref->old_oid : NULL,
                                   0, msg, &err))
                goto fail;
  
@@@ -728,7 -727,7 +728,7 @@@ static int update_local_ref(struct ref 
        }
  }
  
 -static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
 +static int iterate_ref_map(void *cb_data, struct object_id *oid)
  {
        struct ref **rm = cb_data;
        struct ref *ref = *rm;
        if (!ref)
                return -1; /* end of the list */
        *rm = ref->next;
 -      hashcpy(sha1, ref->old_oid.hash);
 +      oidcpy(oid, &ref->old_oid);
        return 0;
  }
  
@@@ -1095,9 -1094,6 +1095,6 @@@ static int do_fetch(struct transport *t
                        tags = TAGS_UNSET;
        }
  
-       if (!transport->get_refs_list || !transport->fetch)
-               die(_("Don't know how to fetch from %s"), transport->url);
        /* if not appending, truncate FETCH_HEAD */
        if (!append && !dry_run) {
                retcode = truncate_fetch_head();
@@@ -1398,8 -1394,7 +1395,8 @@@ int cmd_fetch(int argc, const char **ar
                struct argv_array options = ARGV_ARRAY_INIT;
  
                add_options_to_argv(&options);
 -              result = fetch_populated_submodules(&options,
 +              result = fetch_populated_submodules(the_repository,
 +                                                  &options,
                                                    submodule_prefix,
                                                    recurse_submodules,
                                                    recurse_submodules_default,
diff --combined transport-helper.c
index 413cd7b5518c930eda526f37f0797ffbf31e33c8,1a4b43ff15b85923821993757b7ecf5a5eb31126..508015023176290ec0f8d78043d852cdd37d5ce6
@@@ -11,6 -11,7 +11,7 @@@
  #include "sigchain.h"
  #include "argv-array.h"
  #include "refs.h"
+ #include "transport-internal.h"
  
  static int debug;
  
@@@ -535,7 -536,7 +536,7 @@@ static int fetch_with_import(struct tra
                else
                        private = xstrdup(name);
                if (private) {
 -                      if (read_ref(private, posn->old_oid.hash) < 0)
 +                      if (read_ref(private, &posn->old_oid) < 0)
                                die("Could not read ref %s", private);
                        free(private);
                }
@@@ -650,7 -651,7 +651,7 @@@ static int fetch(struct transport *tran
  
        if (process_connect(transport, 0)) {
                do_take_over(transport);
-               return transport->fetch(transport, nr_heads, to_fetch);
+               return transport->vtable->fetch(transport, nr_heads, to_fetch);
        }
  
        count = 0;
@@@ -795,8 -796,7 +796,8 @@@ static int push_update_refs_status(stru
                private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
                if (!private)
                        continue;
 -              update_ref("update by helper", private, ref->new_oid.hash, NULL, 0, 0);
 +              update_ref("update by helper", private, &ref->new_oid, NULL,
 +                         0, 0);
                free(private);
        }
        strbuf_release(&buf);
@@@ -882,8 -882,7 +883,8 @@@ static int push_refs_with_push(struct t
                        struct strbuf cas = STRBUF_INIT;
                        strbuf_addf(&cas, "%s:%s",
                                    ref->name, oid_to_hex(&ref->old_oid_expect));
 -                      string_list_append(&cas_options, strbuf_detach(&cas, NULL));
 +                      string_list_append_nodup(&cas_options,
 +                                               strbuf_detach(&cas, NULL));
                }
        }
        if (buf.len == 0) {
        strbuf_addch(&buf, '\n');
        sendline(data, &buf);
        strbuf_release(&buf);
 +      string_list_clear(&cas_options, 0);
  
        return push_update_refs_status(data, remote_refs, flags);
  }
@@@ -932,8 -930,7 +933,8 @@@ static int push_refs_with_export(struc
                private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
                if (private && !get_oid(private, &oid)) {
                        strbuf_addf(&buf, "^%s", private);
 -                      string_list_append(&revlist_args, strbuf_detach(&buf, NULL));
 +                      string_list_append_nodup(&revlist_args,
 +                                               strbuf_detach(&buf, NULL));
                        oidcpy(&ref->old_oid, &oid);
                }
                free(private);
                                        int flag;
  
                                        /* Follow symbolic refs (mainly for HEAD). */
 -                                      name = resolve_ref_unsafe(
 -                                               ref->peer_ref->name,
 -                                               RESOLVE_REF_READING,
 -                                               oid.hash, &flag);
 +                                      name = resolve_ref_unsafe(ref->peer_ref->name,
 +                                                                RESOLVE_REF_READING,
 +                                                                &oid, &flag);
                                        if (!name || !(flag & REF_ISSYMREF))
                                                name = ref->peer_ref->name;
  
@@@ -990,7 -988,7 +991,7 @@@ static int push_refs(struct transport *
  
        if (process_connect(transport, 1)) {
                do_take_over(transport);
-               return transport->push_refs(transport, remote_refs, flags);
+               return transport->vtable->push_refs(transport, remote_refs, flags);
        }
  
        if (!remote_refs) {
@@@ -1038,7 -1036,7 +1039,7 @@@ static struct ref *get_refs_list(struc
  
        if (process_connect(transport, for_push)) {
                do_take_over(transport);
-               return transport->get_refs_list(transport, for_push);
+               return transport->vtable->get_refs_list(transport, for_push);
        }
  
        if (data->push && for_push)
                if (eon) {
                        if (has_attribute(eon + 1, "unchanged")) {
                                (*tail)->status |= REF_STATUS_UPTODATE;
 -                              if (read_ref((*tail)->name,
 -                                           (*tail)->old_oid.hash) < 0)
 +                              if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
                                        die(_("Could not read ref %s"),
                                            (*tail)->name);
                        }
        return ret;
  }
  
+ static struct transport_vtable vtable = {
+       set_helper_option,
+       get_refs_list,
+       fetch,
+       push_refs,
+       connect_helper,
+       release_helper
+ };
  int transport_helper_init(struct transport *transport, const char *name)
  {
        struct helper_data *data = xcalloc(1, sizeof(*data));
                debug = 1;
  
        transport->data = data;
-       transport->set_option = set_helper_option;
-       transport->get_refs_list = get_refs_list;
-       transport->fetch = fetch;
-       transport->push_refs = push_refs;
-       transport->disconnect = release_helper;
-       transport->connect = connect_helper;
+       transport->vtable = &vtable;
        transport->smart_options = &(data->transport_options);
        return 0;
  }
diff --combined transport.c
index 7cc39b7c0b9c547752924c82038ed0ac226a22bf,31b6d4d0e02d696603e5ecd3ffbb0b27e68d856d..fc802260f61c7496f4250b15ef6f5ab8dd316898
@@@ -17,6 -17,7 +17,7 @@@
  #include "string-list.h"
  #include "sha1-array.h"
  #include "sigchain.h"
+ #include "transport-internal.h"
  
  static void set_upstreams(struct transport *transport, struct ref *refs,
        int pretend)
@@@ -305,8 -306,8 +306,8 @@@ void transport_update_tracking_ref(stru
                if (ref->deletion) {
                        delete_ref(NULL, rs.dst, NULL, 0);
                } else
 -                      update_ref("update by push", rs.dst,
 -                                      ref->new_oid.hash, NULL, 0, 0);
 +                      update_ref("update by push", rs.dst, &ref->new_oid,
 +                                 NULL, 0, 0);
                free(rs.dst);
        }
  }
@@@ -607,6 -608,15 +608,15 @@@ static int disconnect_git(struct transp
        return 0;
  }
  
+ static struct transport_vtable taken_over_vtable = {
+       NULL,
+       get_refs_via_connect,
+       fetch_refs_via_pack,
+       git_transport_push,
+       NULL,
+       disconnect_git
+ };
  void transport_take_over(struct transport *transport,
                         struct child_process *child)
  {
        data->got_remote_heads = 0;
        transport->data = data;
  
-       transport->set_option = NULL;
-       transport->get_refs_list = get_refs_via_connect;
-       transport->fetch = fetch_refs_via_pack;
-       transport->push_refs = git_transport_push;
-       transport->disconnect = disconnect_git;
+       transport->vtable = &taken_over_vtable;
        transport->smart_options = &(data->options);
  
        transport->cannot_reuse = 1;
@@@ -751,6 -757,24 +757,24 @@@ void transport_check_allowed(const cha
                die("transport '%s' not allowed", type);
  }
  
+ static struct transport_vtable bundle_vtable = {
+       NULL,
+       get_refs_from_bundle,
+       fetch_refs_from_bundle,
+       NULL,
+       NULL,
+       close_bundle
+ };
+ static struct transport_vtable builtin_smart_vtable = {
+       NULL,
+       get_refs_via_connect,
+       fetch_refs_via_pack,
+       git_transport_push,
+       connect_git,
+       disconnect_git
+ };
  struct transport *transport_get(struct remote *remote, const char *url)
  {
        const char *helper;
                struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
                transport_check_allowed("file");
                ret->data = data;
-               ret->get_refs_list = get_refs_from_bundle;
-               ret->fetch = fetch_refs_from_bundle;
-               ret->disconnect = close_bundle;
+               ret->vtable = &bundle_vtable;
                ret->smart_options = NULL;
        } else if (!is_url(url)
                || starts_with(url, "file://")
                 */
                struct git_transport_data *data = xcalloc(1, sizeof(*data));
                ret->data = data;
-               ret->set_option = NULL;
-               ret->get_refs_list = get_refs_via_connect;
-               ret->fetch = fetch_refs_via_pack;
-               ret->push_refs = git_transport_push;
-               ret->connect = connect_git;
-               ret->disconnect = disconnect_git;
+               ret->vtable = &builtin_smart_vtable;
                ret->smart_options = &(data->options);
  
                data->conn = NULL;
@@@ -843,9 -860,9 +860,9 @@@ int transport_set_option(struct transpo
                git_reports = set_git_option(transport->smart_options,
                                             name, value);
  
-       if (transport->set_option)
-               protocol_reports = transport->set_option(transport, name,
-                                                       value);
+       if (transport->vtable->set_option)
+               protocol_reports = transport->vtable->set_option(transport,
+                                                                name, value);
  
        /* If either report is 0, report 0 (success). */
        if (!git_reports || !protocol_reports)
@@@ -968,7 -985,7 +985,7 @@@ int transport_push(struct transport *tr
        *reject_reasons = 0;
        transport_verify_remote_names(refspec_nr, refspec);
  
-       if (transport->push_refs) {
+       if (transport->vtable->push_refs) {
                struct ref *remote_refs;
                struct ref *local_refs = get_local_heads();
                int match_flags = MATCH_REFS_NONE;
                if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
                        return -1;
  
-               remote_refs = transport->get_refs_list(transport, 1);
+               remote_refs = transport->vtable->get_refs_list(transport, 1);
  
                if (flags & TRANSPORT_PUSH_ALL)
                        match_flags |= MATCH_REFS_ALL;
                }
  
                if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
-                       push_ret = transport->push_refs(transport, remote_refs, flags);
+                       push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
                else
                        push_ret = 0;
                err = push_had_errors(remote_refs);
  const struct ref *transport_get_remote_refs(struct transport *transport)
  {
        if (!transport->got_remote_refs) {
-               transport->remote_refs = transport->get_refs_list(transport, 0);
+               transport->remote_refs = transport->vtable->get_refs_list(transport, 0);
                transport->got_remote_refs = 1;
        }
  
@@@ -1127,7 -1144,7 +1144,7 @@@ int transport_fetch_refs(struct transpo
                        heads[nr_heads++] = rm;
        }
  
-       rc = transport->fetch(transport, nr_heads, heads);
+       rc = transport->vtable->fetch(transport, nr_heads, heads);
  
        free(heads);
        return rc;
@@@ -1144,8 -1161,8 +1161,8 @@@ void transport_unlock_pack(struct trans
  int transport_connect(struct transport *transport, const char *name,
                      const char *exec, int fd[2])
  {
-       if (transport->connect)
-               return transport->connect(transport, name, exec, fd);
+       if (transport->vtable->connect)
+               return transport->vtable->connect(transport, name, exec, fd);
        else
                die("Operation not supported by protocol");
  }
  int transport_disconnect(struct transport *transport)
  {
        int ret = 0;
-       if (transport->disconnect)
-               ret = transport->disconnect(transport);
+       if (transport->vtable->disconnect)
+               ret = transport->vtable->disconnect(transport);
        free(transport);
        return ret;
  }