Merge pull request #2490 from csware/ssh-wintunnel Allow to override default ssh transport_cb - in order to allow third party ssh transports
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
diff --git a/src/transport.c b/src/transport.c
index c0311fa..d390e14 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -46,34 +46,37 @@ static git_vector custom_transports = GIT_VECTOR_INIT;
#define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0])) - 1
-static int transport_find_fn(
- git_transport_cb *out,
- const char *url,
- void **param)
+static transport_definition * transport_find_by_url(const char *url)
{
size_t i = 0;
- transport_definition *d, *definition = NULL;
+ transport_definition *d;
/* Find a user transport who wants to deal with this URI */
git_vector_foreach(&custom_transports, i, d) {
if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) {
- definition = d;
- break;
+ return d;
}
}
/* Find a system transport for this URI */
- if (!definition) {
- for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) {
- d = &transports[i];
-
- if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) {
- definition = d;
- break;
- }
+ for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) {
+ d = &transports[i];
+
+ if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) {
+ return d;
}
}
+ return NULL;
+}
+
+static int transport_find_fn(
+ git_transport_cb *out,
+ const char *url,
+ void **param)
+{
+ transport_definition *definition = transport_find_by_url(url);
+
#ifdef GIT_WIN32
/* On Windows, it might not be possible to discern between absolute local
* and ssh paths - first check if this is a valid local path that points
@@ -89,12 +92,15 @@ static int transport_find_fn(
/* It could be a SSH remote path. Check to see if there's a :
* SSH is an unsupported transport mechanism in this version of libgit2 */
- if (!definition && strrchr(url, ':'))
-#ifdef GIT_SSH
- definition = &ssh_transport_definition;
-#else
- definition = &dummy_transport_definition;
+ if (!definition && strrchr(url, ':')) {
+ // re-search transports again with ssh:// as url so that we can find a third party ssh transport
+ definition = transport_find_by_url("ssh://");
+#ifndef GIT_SSH
+ if (!definition) {
+ definition = &dummy_transport_definition;
+ }
#endif
+ }
#ifndef GIT_WIN32
/* Check to see if the path points to a file on the local file system */
diff --git a/tests/transport/register.c b/tests/transport/register.c
new file mode 100644
index 0000000..937194c
--- /dev/null
+++ b/tests/transport/register.c
@@ -0,0 +1,66 @@
+#include "clar_libgit2.h"
+#include "git2/sys/transport.h"
+
+static git_transport _transport = GIT_TRANSPORT_INIT;
+
+static int dummy_transport(git_transport **transport, git_remote *owner, void *param)
+{
+ *transport = &_transport;
+ GIT_UNUSED(owner);
+ GIT_UNUSED(param);
+ return 0;
+}
+
+void test_transport_register__custom_transport(void)
+{
+ git_transport *transport;
+
+ cl_git_pass(git_transport_register("something", dummy_transport, NULL));
+
+ cl_git_pass(git_transport_new(&transport, NULL, "something://somepath"));
+
+ cl_assert(transport == &_transport);
+
+ cl_git_pass(git_transport_unregister("something"));
+}
+
+void test_transport_register__custom_transport_error_doubleregister(void)
+{
+ cl_git_pass(git_transport_register("something", dummy_transport, NULL));
+
+ cl_git_fail_with(git_transport_register("something", dummy_transport, NULL), GIT_EEXISTS);
+
+ cl_git_pass(git_transport_unregister("something"));
+}
+
+void test_transport_register__custom_transport_error_remove_non_existing(void)
+{
+ cl_git_fail_with(git_transport_unregister("something"), GIT_ENOTFOUND);
+}
+
+void test_transport_register__custom_transport_ssh(void)
+{
+ git_transport *transport;
+
+#ifndef GIT_SSH
+ cl_git_fail_with(git_transport_new(&transport, NULL, "ssh://somehost:somepath"), -1);
+ cl_git_fail_with(git_transport_new(&transport, NULL, "git@somehost:somepath"), -1);
+#else
+ cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath"));
+#endif
+
+ cl_git_pass(git_transport_register("ssh", dummy_transport, NULL));
+
+ cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath"));
+
+ cl_assert(transport == &_transport);
+
+ cl_git_pass(git_transport_unregister("ssh"));
+
+#ifndef GIT_SSH
+ cl_git_fail_with(git_transport_new(&transport, NULL, "ssh://somehost:somepath"), -1);
+ cl_git_fail_with(git_transport_new(&transport, NULL, "git@somehost:somepath"), -1);
+#else
+ cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath"));
+#endif
+}