Commit 058b753ceb8f6b25b77e57106b3a87997bc6362a

Carlos Martín Nieto 2015-04-22T15:45:21

remote: move the transport ctor to the callbacks Instead of having it set in a different place from every other callback, put it the main structure. This removes some state from the remote and makes it behave more like clone, where the constructors are passed via the options.

diff --git a/include/git2/remote.h b/include/git2/remote.h
index 41cf851..22aad5a 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -484,6 +484,12 @@ struct git_remote_callbacks {
 	git_push_negotiation push_negotiation;
 
 	/**
+	 * Create the transport to use for this operation. Leave NULL
+	 * to auto-detect.
+	 */
+	git_transport_cb transport;
+
+	/**
 	 * This will be passed to each of the callbacks in this struct
 	 * as the last parameter.
 	 */
diff --git a/include/git2/sys/transport.h b/include/git2/sys/transport.h
index d6ca8ff..867fbcb 100644
--- a/include/git2/sys/transport.h
+++ b/include/git2/sys/transport.h
@@ -30,8 +30,6 @@ typedef enum {
 	GIT_TRANSPORTFLAGS_NONE = 0,
 } git_transport_flags_t;
 
-typedef struct git_transport git_transport;
-
 struct git_transport {
 	unsigned int version;
 	/* Set progress and error callbacks */
@@ -142,9 +140,6 @@ GIT_EXTERN(int) git_transport_new(git_transport **out, git_remote *owner, const 
  */
 GIT_EXTERN(int) git_transport_ssh_with_paths(git_transport **out, git_remote *owner, void *payload);
 
-/* Signature of a function which creates a transport */
-typedef int (*git_transport_cb)(git_transport **out, git_remote *owner, void *param);
-
 /**
  * Add a custom transport definition, to be used in addition to the built-in
  * set of transports that come with libgit2.
@@ -353,21 +348,6 @@ GIT_EXTERN(int) git_smart_subtransport_ssh(
 	git_transport* owner,
 	void *param);
 
-/**
- * Sets a custom transport factory for the remote. The caller can use this
- * function to override the transport used for this remote when performing
- * network operations.
- *
- * @param remote the remote to configure
- * @param transport_cb the function to use to create a transport
- * @param payload opaque parameter passed to transport_cb
- * @return 0 or an error code
- */
-GIT_EXTERN(int) git_remote_set_transport(
-	git_remote *remote,
-	git_transport_cb transport_cb,
-	void *payload);
-
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/include/git2/transport.h b/include/git2/transport.h
index c10907f..99fd09a 100644
--- a/include/git2/transport.h
+++ b/include/git2/transport.h
@@ -20,6 +20,9 @@
  */
 GIT_BEGIN_DECL
 
+/** Signature of a function which creates a transport */
+typedef int (*git_transport_cb)(git_transport **out, git_remote *owner, void *param);
+
 /**
  * Type of SSH host fingerprint
  */
diff --git a/include/git2/types.h b/include/git2/types.h
index fdb5f2b..d1e7cd9 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -224,6 +224,12 @@ typedef struct git_refspec git_refspec;
 typedef struct git_remote git_remote;
 
 /**
+ * Interface which represents a transport to communicate with a
+ * remote.
+ */
+typedef struct git_transport git_transport;
+
+/**
  * Preparation for a push operation. Can be used to configure what to
  * push and the level of parallelism of the packfile builder.
  */
diff --git a/src/remote.c b/src/remote.c
index a29b8aa..95c316f 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -309,8 +309,6 @@ int git_remote_dup(git_remote **dest, git_remote *source)
 		GITERR_CHECK_ALLOC(remote->pushurl);
 	}
 
-	remote->transport_cb = source->transport_cb;
-	remote->transport_cb_payload = source->transport_cb_payload;
 	remote->repo = source->repo;
 	remote->download_tags = source->download_tags;
 	remote->update_fetchhead = source->update_fetchhead;
@@ -725,13 +723,15 @@ int git_remote_connect(git_remote *remote, git_direction direction, const git_re
 	int flags = GIT_TRANSPORTFLAGS_NONE;
 	int error;
 	void *payload = NULL;
-	git_cred_acquire_cb credentials;
+	git_cred_acquire_cb credentials = NULL;
+	git_transport_cb transport = NULL;
 
 	assert(remote);
 
 	if (callbacks) {
 		GITERR_CHECK_VERSION(callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks");
 		credentials = callbacks->credentials;
+		transport   = callbacks->transport;
 		payload     = callbacks->payload;
 	}
 
@@ -746,8 +746,8 @@ int git_remote_connect(git_remote *remote, git_direction direction, const git_re
 
 	/* If we don't have a transport object yet, and the caller specified a
 	 * custom transport factory, use that */
-	if (!t && remote->transport_cb &&
-		(error = remote->transport_cb(&t, remote, remote->transport_cb_payload)) < 0)
+	if (!t && transport &&
+		(error = transport(&t, remote, payload)) < 0)
 		return error;
 
 	/* If we still don't have a transport, then use the global
@@ -1664,23 +1664,6 @@ int git_remote_list(git_strarray *remotes_list, git_repository *repo)
 	return 0;
 }
 
-int git_remote_set_transport(
-	git_remote *remote,
-	git_transport_cb transport_cb,
-	void *payload)
-{
-	assert(remote);
-
-	if (remote->transport) {
-		giterr_set(GITERR_NET, "A transport is already bound to this remote");
-		return -1;
-	}
-
-	remote->transport_cb = transport_cb;
-	remote->transport_cb_payload = payload;
-	return 0;
-}
-
 const git_transfer_progress* git_remote_stats(git_remote *remote)
 {
 	assert(remote);
diff --git a/src/remote.h b/src/remote.h
index 4fb2351..3a5c0de 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -24,8 +24,6 @@ struct git_remote {
 	git_vector refspecs;
 	git_vector active_refspecs;
 	git_vector passive_refspecs;
-	git_transport_cb transport_cb;
-	void *transport_cb_payload;
 	git_transport *transport;
 	git_repository *repo;
 	git_push *push;
diff --git a/tests/clone/transport.c b/tests/clone/transport.c
index 46c16a2..cccaae2 100644
--- a/tests/clone/transport.c
+++ b/tests/clone/transport.c
@@ -24,10 +24,9 @@ static int custom_transport_remote_create(
 {
 	int error;
 
-	if ((error = git_remote_create(out, repo, name, url)) < 0)
-		return error;
+	GIT_UNUSED(payload);
 
-	if ((error = git_remote_set_transport(*out, custom_transport, payload)) < 0)
+	if ((error = git_remote_create(out, repo, name, url)) < 0)
 		return error;
 
 	return 0;
@@ -40,7 +39,8 @@ void test_clone_transport__custom_transport(void)
 	int custom_transport_used = 0;
 
 	clone_opts.remote_cb = custom_transport_remote_create;
-	clone_opts.remote_cb_payload = &custom_transport_used;
+	clone_opts.fetch_opts.callbacks.transport = custom_transport;
+	clone_opts.fetch_opts.callbacks.payload = &custom_transport_used;
 
 	cl_git_pass(git_clone(&repo, cl_fixture("testrepo.git"), "./custom_transport.git", &clone_opts));
 	git_repository_free(repo);
diff --git a/tests/network/remote/remotes.c b/tests/network/remote/remotes.c
index 8dbd68f..f319937 100644
--- a/tests/network/remote/remotes.c
+++ b/tests/network/remote/remotes.c
@@ -80,6 +80,7 @@ void test_network_remote_remotes__error_when_not_found(void)
 void test_network_remote_remotes__error_when_no_push_available(void)
 {
 	git_remote *r;
+	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
 	char *specs = {
 		"refs/heads/master",
 	};
@@ -91,9 +92,8 @@ void test_network_remote_remotes__error_when_no_push_available(void)
 
 	cl_git_pass(git_remote_create_anonymous(&r, _repo, cl_fixture("testrepo.git"), NULL));
 
-	cl_git_pass(git_remote_set_transport(r, git_transport_local, NULL));
-
-	cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH, NULL));
+	callbacks.transport = git_transport_local;
+	cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH, &callbacks));
 
 	/* Make sure that push is really not available */
 	r->transport->push = NULL;
diff --git a/tests/online/clone.c b/tests/online/clone.c
index 35ddbe9..1d41969 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -401,10 +401,9 @@ static int custom_remote_ssh_with_paths(
 {
 	int error;
 
-	if ((error = git_remote_create(out, repo, name, url)) < 0)
-		return error;
+	GIT_UNUSED(payload);
 
-	if ((error = git_remote_set_transport(*out, git_transport_ssh_with_paths, payload)) < 0)
+	if ((error = git_remote_create(out, repo, name, url)) < 0)
 		return error;
 
 	return 0;
@@ -435,7 +434,8 @@ void test_online_clone__ssh_with_paths(void)
 		clar__skip();
 
 	g_options.remote_cb = custom_remote_ssh_with_paths;
-	g_options.remote_cb_payload = &arr;
+	g_options.fetch_opts.callbacks.transport = git_transport_ssh_with_paths;
+	g_options.fetch_opts.callbacks.payload = &arr;
 
 	cl_git_fail(git_clone(&g_repo, remote_url, "./foo", &g_options));
 
diff --git a/tests/online/push_util.h b/tests/online/push_util.h
index 83d46b5..822341b 100644
--- a/tests/online/push_util.h
+++ b/tests/online/push_util.h
@@ -12,7 +12,7 @@ extern const git_oid OID_ZERO;
  * @param data pointer to a record_callbacks_data instance
  */
 #define RECORD_CALLBACKS_INIT(data) \
-	{ GIT_REMOTE_CALLBACKS_VERSION, NULL, NULL, cred_acquire_cb, NULL, NULL, record_update_tips_cb, NULL, NULL, NULL, NULL, data }
+	{ GIT_REMOTE_CALLBACKS_VERSION, NULL, NULL, cred_acquire_cb, NULL, NULL, record_update_tips_cb, NULL, NULL, NULL, NULL, NULL, data }
 
 typedef struct {
 	char *name;