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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
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;