transport: introduce `git_transport_smart_remote_connect_options` 6fc6eeb66c40310086c8f059cae41de69ad4c6da removed `git_transport_smart_proxy_option`, and there was nothing added to replace it. That made it hard for custom transports / smart subtransports to know what remote connect options to use (e.g. proxy options). This change introduces `git_transport_smart_remote_connect_options` to replace it.
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 263 264 265 266 267 268 269 270 271 272
diff --git a/include/git2/sys/remote.h b/include/git2/sys/remote.h
index dd243ca..bde1b16 100644
--- a/include/git2/sys/remote.h
+++ b/include/git2/sys/remote.h
@@ -8,6 +8,8 @@
#ifndef INCLUDE_sys_git_remote_h
#define INCLUDE_sys_git_remote_h
+#include "git2/remote.h"
+
/**
* @file git2/sys/remote.h
* @brief Low-level remote functionality for custom transports
@@ -26,6 +28,17 @@ typedef enum {
GIT_REMOTE_CAPABILITY_REACHABLE_OID = (1 << 1),
} git_remote_capability_t;
+/**
+ * Disposes libgit2-initialized fields from a git_remote_connect_options.
+ *
+ * Note that this does not free the `git_remote_connect_options` itself, just
+ * the memory pointed to by it.
+ *
+ * @param opts The `git_remote_connect_options` struct to dispose.
+ */
+GIT_EXTERN(void) git_remote_connect_options_dispose(
+ git_remote_connect_options *opts);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/include/git2/sys/transport.h b/include/git2/sys/transport.h
index f0c2a3e..8191f15 100644
--- a/include/git2/sys/transport.h
+++ b/include/git2/sys/transport.h
@@ -9,10 +9,11 @@
#define INCLUDE_sys_git_transport_h
#include "git2/net.h"
+#include "git2/proxy.h"
+#include "git2/remote.h"
+#include "git2/strarray.h"
#include "git2/transport.h"
#include "git2/types.h"
-#include "git2/strarray.h"
-#include "git2/proxy.h"
/**
* @file git2/sys/transport.h
@@ -270,6 +271,19 @@ GIT_EXTERN(int) git_transport_smart_credentials(git_credential **out, git_transp
*/
GIT_EXTERN(int) git_transport_smart_proxy_options(git_proxy_options *out, git_transport *transport);
+/**
+ * Get a copy of the remote connect options
+ *
+ * All data is copied and must be freed by the caller by calling
+ * `git_remote_connect_options_dispose`.
+ *
+ * @param out options struct to fill
+ * @param transport the transport to extract the data from.
+ */
+GIT_EXTERN(int) git_transport_smart_remote_connect_options(
+ git_remote_connect_options *out,
+ git_transport *transport);
+
/*
*** End of base transport interface ***
*** Begin interface for subtransports for the smart transport ***
diff --git a/src/libgit2/remote.h b/src/libgit2/remote.h
index ea9c7d1..41ee58e 100644
--- a/src/libgit2/remote.h
+++ b/src/libgit2/remote.h
@@ -11,6 +11,7 @@
#include "git2/remote.h"
#include "git2/transport.h"
+#include "git2/sys/remote.h"
#include "git2/sys/transport.h"
#include "refspec.h"
@@ -53,7 +54,6 @@ int git_remote_connect_options_normalize(
git_remote_connect_options *dst,
git_repository *repo,
const git_remote_connect_options *src);
-void git_remote_connect_options_dispose(git_remote_connect_options *opts);
int git_remote_capabilities(unsigned int *out, git_remote *remote);
diff --git a/src/libgit2/transports/smart.c b/src/libgit2/transports/smart.c
index 801fcbe..264f213 100644
--- a/src/libgit2/transports/smart.c
+++ b/src/libgit2/transports/smart.c
@@ -425,6 +425,18 @@ int git_transport_smart_credentials(git_credential **out, git_transport *transpo
return connect_opts->callbacks.credentials(out, t->url, user, methods, connect_opts->callbacks.payload);
}
+int git_transport_smart_remote_connect_options(
+ git_remote_connect_options *out,
+ git_transport *transport)
+{
+ transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
+
+ GIT_ASSERT_ARG(out);
+ GIT_ASSERT_ARG(transport);
+
+ return git_remote_connect_options_dup(out, &t->connect_opts);
+}
+
int git_transport_smart(git_transport **out, git_remote *owner, void *param)
{
transport_smart *t;
diff --git a/tests/libgit2/transport/register.c b/tests/libgit2/transport/register.c
index 88ba247..216f11e 100644
--- a/tests/libgit2/transport/register.c
+++ b/tests/libgit2/transport/register.c
@@ -1,6 +1,8 @@
#include "clar_libgit2.h"
+#include "git2/sys/remote.h"
#include "git2/sys/transport.h"
+static const char *proxy_url = "https://proxy";
static git_transport _transport = GIT_TRANSPORT_INIT;
static int dummy_transport(git_transport **transport, git_remote *owner, void *param)
@@ -77,3 +79,146 @@ void test_transport_register__custom_transport_ssh(void)
#endif
}
}
+
+static int custom_subtransport_stream__read(
+ git_smart_subtransport_stream *stream,
+ char *buffer,
+ size_t buf_size,
+ size_t *bytes_read)
+{
+ GIT_UNUSED(stream);
+ GIT_UNUSED(buffer);
+ GIT_UNUSED(buf_size);
+
+ *bytes_read = 0;
+
+ git_error_set_str(42, "unimplemented");
+ return GIT_EUSER;
+}
+
+static int custom_subtransport_stream__write(
+ git_smart_subtransport_stream *stream,
+ const char *buffer,
+ size_t len)
+{
+ GIT_UNUSED(stream);
+ GIT_UNUSED(buffer);
+ GIT_UNUSED(len);
+
+ git_error_set_str(42, "unimplemented");
+ return GIT_EUSER;
+}
+
+static void custom_subtransport_stream__free(
+ git_smart_subtransport_stream *stream)
+{
+ git__free(stream);
+}
+
+struct custom_subtransport {
+ git_smart_subtransport subtransport;
+ git_transport *owner;
+ int *called;
+};
+
+static int custom_subtransport__action(
+ git_smart_subtransport_stream **out,
+ git_smart_subtransport *transport,
+ const char *url,
+ git_smart_service_t action)
+{
+ struct custom_subtransport *t = (struct custom_subtransport *)transport;
+ git_remote_connect_options opts = GIT_REMOTE_CONNECT_OPTIONS_INIT;
+ int ret;
+
+ GIT_UNUSED(url);
+ GIT_UNUSED(action);
+
+ ret = git_transport_smart_remote_connect_options(&opts, t->owner);
+
+ /* increase the counter once if this function was called at all and once more if the URL matches. */
+ (*t->called)++;
+ if (strcmp(proxy_url, opts.proxy_opts.url) == 0)
+ (*t->called)++;
+
+ git_remote_connect_options_dispose(&opts);
+
+ *out = git__calloc(1, sizeof(git_smart_subtransport_stream));
+ (*out)->subtransport = transport;
+ (*out)->read = custom_subtransport_stream__read;
+ (*out)->write = custom_subtransport_stream__write;
+ (*out)->free = custom_subtransport_stream__free;
+
+ return ret;
+}
+
+static int custom_subtransport__close(git_smart_subtransport *transport)
+{
+ GIT_UNUSED(transport);
+
+ return 0;
+}
+
+static void custom_subtransport__free(git_smart_subtransport *transport)
+{
+ GIT_UNUSED(transport);
+
+ git__free(transport);
+}
+
+static int custom_transport_callback(git_smart_subtransport **out, git_transport *owner, void *param)
+{
+ struct custom_subtransport *subtransport = git__calloc(1, sizeof(struct custom_subtransport));
+ subtransport->called = (int *)param;
+ subtransport->owner = owner;
+ subtransport->subtransport.action = custom_subtransport__action;
+ subtransport->subtransport.close = custom_subtransport__close;
+ subtransport->subtransport.free = custom_subtransport__free;
+
+ *out = &subtransport->subtransport;
+
+ return 0;
+}
+
+
+static int custom_transport(git_transport **out, git_remote *owner, void *param)
+{
+ struct git_smart_subtransport_definition definition;
+ definition.callback = custom_transport_callback;
+ definition.rpc = false;
+ definition.param = param;
+ return git_transport_smart(out, owner, &definition);
+}
+
+void test_transport_register__custom_transport_callbacks(void)
+{
+ git_transport *transport;
+ int called = 0;
+ const char *url = "custom://somepath";
+ git_remote_connect_options opts = GIT_REMOTE_CONNECT_OPTIONS_INIT;
+ git_repository *repo;
+ git_remote *remote;
+
+ cl_git_pass(git_repository_init(&repo, "./transport", 0));
+ cl_git_pass(git_remote_create(&remote, repo, "test",
+ cl_fixture("testrepo.git")));
+
+ cl_git_pass(git_transport_register("custom", custom_transport, &called));
+
+ cl_git_pass(git_transport_new(&transport, remote, url));
+
+ opts.follow_redirects = GIT_REMOTE_REDIRECT_NONE;
+ opts.proxy_opts.url = proxy_url;
+
+ /* This is expected to fail, since the subtransport_stream is not implemented */
+ transport->connect(transport, url, GIT_SERVICE_UPLOADPACK_LS, &opts);
+ /* the counter is increased twice if everything goes as planned */
+ cl_assert_equal_i(2, called);
+ cl_git_pass(transport->close(transport));
+ transport->free(transport);
+
+ cl_git_pass(git_transport_unregister("custom"));
+ git_remote_free(remote);
+ git_repository_free(repo);
+ cl_fixture_cleanup("testrepo.git");
+}