]> git.proxmox.com Git - libgit2.git/commitdiff
Move negotiation to the transport
authorCarlos Martín Nieto <carlos@cmartin.tk>
Wed, 10 Aug 2011 18:49:43 +0000 (20:49 +0200)
committerVicent Marti <tanoku@gmail.com>
Thu, 18 Aug 2011 00:34:10 +0000 (02:34 +0200)
There are many ways how a transport might negotiate with the server,
so instead of making it fit into the smart protocol model, let the
transport do its thing. For now, the git protocol limits itself to
send only 160 "have" lines so we don't flood the server.

Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
src/fetch.c
src/fetch.h
src/transport.c
src/transport.h
src/transport_git.c

index 044d4c9cc2f1fb454289859e3c59cb1ae4e9ca8d..03febe2790f12805c3b88a046fb5aaae9aa0572b 100644 (file)
@@ -98,14 +98,8 @@ cleanup:
  */
 int git_fetch_negotiate(git_remote *remote)
 {
-       git_revwalk *walk;
        int error;
-       unsigned int i;
-       git_reference *ref;
-       git_strarray refs;
        git_headarray *list = &remote->refs;
-       git_repository *repo = remote->repo;
-       git_oid oid;
 
        error = filter_wants(remote);
        if (error < GIT_SUCCESS)
@@ -119,47 +113,11 @@ int git_fetch_negotiate(git_remote *remote)
         * what we want and what we have.
         */
        remote->need_pack = 1;
-       git_transport_send_wants(remote->transport, list);
-
-       error = git_reference_listall(&refs, repo, GIT_REF_LISTALL);
-       if (error < GIT_ERROR)
-               return git__rethrow(error, "Failed to list all references");
-
-       error = git_revwalk_new(&walk, repo);
-       if (error < GIT_ERROR) {
-               error = git__rethrow(error, "Failed to list all references");
-               goto cleanup;
-       }
-       git_revwalk_sorting(walk, GIT_SORT_TIME);
-
-       for (i = 0; i < refs.count; ++i) {
-               error = git_reference_lookup(&ref, repo, refs.strings[i]);
-               if (error < GIT_ERROR) {
-                       error = git__rethrow(error, "Failed to lookup %s", refs.strings[i]);
-                       goto cleanup;
-               }
-
-               error = git_revwalk_push(walk, git_reference_oid(ref));
-               if (error < GIT_ERROR) {
-                       error = git__rethrow(error, "Failed to push %s", refs.strings[i]);
-                       goto cleanup;
-               }
-       }
-       git_strarray_free(&refs);
-
-       while ((error = git_revwalk_next(&oid, walk)) == GIT_SUCCESS) {
-               git_transport_send_have(remote->transport, &oid);
-       }
-       if (error == GIT_EREVWALKOVER)
-               error = GIT_SUCCESS;
-
-       /* TODO: git_pkt_send_flush(fd), or git_transport_flush() */
-       git_transport_send_flush(remote->transport);
-       git_transport_send_done(remote->transport);
+       error = git_transport_send_wants(remote->transport, list);
+       if (error < GIT_SUCCESS)
+               return git__rethrow(error, "Failed to send want list");
 
-cleanup:
-       git_revwalk_free(walk);
-       return error;
+       return git_transport_negotiate_fetch(remote->transport, remote->repo, &remote->refs);
 }
 
 int git_fetch_download_pack(char **out, git_remote *remote)
index 2856f12ee766f2eafa7c0c3019a481e8511eb192..ad4451ffe329ac321e9a9077267bc296f0548643 100644 (file)
@@ -2,5 +2,6 @@
 #define INCLUDE_fetch_h__
 
 int git_fetch_negotiate(git_remote *remote);
+int git_fetch_download_pack(char **out, git_remote *remote);
 
 #endif
index 1bd0c4e9e22cc228bcff7112547a5f7477ef623a..91723df73c303e0ca25ced5585f5ac76a437e31d 100644 (file)
@@ -90,6 +90,11 @@ int git_transport_send_have(struct git_transport *transport, git_oid *oid)
        return transport->send_have(transport, oid);
 }
 
+int git_transport_negotiate_fetch(struct git_transport *transport, git_repository *repo, git_headarray *list)
+{
+       return transport->negotiate_fetch(transport, repo, list);
+}
+
 int git_transport_send_flush(struct git_transport *transport)
 {
        return transport->send_flush(transport);
index b14684bcbbb851b156da4814a84ea10c7690b2fb..69bec4c66a60969e0351b8b7b893da8d2a108d0d 100644 (file)
@@ -72,6 +72,11 @@ struct git_transport {
         * Send a 'done' message
         */
        int (*send_done)(struct git_transport *transport);
+       /**
+        * Negotiate the minimal amount of objects that need to be
+        * retrieved
+        */
+       int (*negotiate_fetch)(struct git_transport *transport, git_repository *repo, git_headarray *list);
        /**
         * Send a flush
         */
@@ -99,6 +104,7 @@ int git_transport_git(struct git_transport **transport);
 int git_transport_dummy(struct git_transport **transport);
 
 int git_transport_send_wants(struct git_transport *transport, git_headarray *array);
+int git_transport_negotiate_fetch(struct git_transport *transport, git_repository *repo, git_headarray *array);
 int git_transport_send_have(struct git_transport *transport, git_oid *oid);
 int git_transport_send_done(struct git_transport *transport);
 int git_transport_send_flush(struct git_transport *transport);
index 60958c9cc3d35e8fdaf6298ddde3e45805bd0780..bb759a1fe4f9f9c980ef95a004dbcb296ee234f3 100644 (file)
@@ -27,6 +27,8 @@
 #include "git2/common.h"
 #include "git2/types.h"
 #include "git2/errors.h"
+#include "git2/net.h"
+#include "git2/revwalk.h"
 
 #include "vector.h"
 #include "transport.h"
@@ -325,6 +327,70 @@ static int git_send_have(git_transport *transport, git_oid *oid)
        return git_pkt_send_have(oid, t->socket);
 }
 
+static int git_negotiate_fetch(git_transport *transport, git_repository *repo, git_headarray *list)
+{
+       transport_git *t = (transport_git *) transport;
+       git_revwalk *walk;
+       git_reference *ref;
+       git_strarray refs;
+       git_oid oid;
+       int error;
+       unsigned int i;
+
+       error = git_reference_listall(&refs, repo, GIT_REF_LISTALL);
+       if (error < GIT_ERROR)
+               return git__rethrow(error, "Failed to list all references");
+
+       error = git_revwalk_new(&walk, repo);
+       if (error < GIT_ERROR) {
+               error = git__rethrow(error, "Failed to list all references");
+               goto cleanup;
+       }
+       git_revwalk_sorting(walk, GIT_SORT_TIME);
+
+       for (i = 0; i < refs.count; ++i) {
+               error = git_reference_lookup(&ref, repo, refs.strings[i]);
+               if (error < GIT_ERROR) {
+                       error = git__rethrow(error, "Failed to lookup %s", refs.strings[i]);
+                       goto cleanup;
+               }
+
+               error = git_revwalk_push(walk, git_reference_oid(ref));
+               if (error < GIT_ERROR) {
+                       error = git__rethrow(error, "Failed to push %s", refs.strings[i]);
+                       goto cleanup;
+               }
+       }
+       git_strarray_free(&refs);
+
+       /*
+        * We don't support any kind of ACK extensions, so the negotiation
+        * boils down to sending what we have and listening for an ACK
+        * every once in a while.
+        */
+       i = 0;
+       while ((error = git_revwalk_next(&oid, walk)) == GIT_SUCCESS) {
+               error = git_pkt_send_have(&oid, t->socket);
+               i++;
+               /*
+                * This is a magic number so we don't flood the server. We
+                * should check every once in a while to see if the server has
+                * sent an ACK.
+                */
+               if (i % 160 == 0)
+                       break;
+       }
+       if (error == GIT_EREVWALKOVER)
+               error = GIT_SUCCESS;
+
+       git_pkt_send_flush(t->socket);
+       git_pkt_send_done(t->socket);
+
+cleanup:
+       git_revwalk_free(walk);
+       return error;
+}
+
 static int git_send_flush(git_transport *transport)
 {
        transport_git *t = (transport_git *) transport;
@@ -476,6 +542,7 @@ int git_transport_git(git_transport **out)
        t->parent.ls = git_ls;
        t->parent.send_wants = git_send_wants;
        t->parent.send_have = git_send_have;
+       t->parent.negotiate_fetch = git_negotiate_fetch;
        t->parent.send_flush = git_send_flush;
        t->parent.send_done = git_send_done;
        t->parent.download_pack = git_download_pack;