transport: Add `git_transport_valid_url`
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
diff --git a/include/git2/transport.h b/include/git2/transport.h
index ddae32d..f56a2f4 100644
--- a/include/git2/transport.h
+++ b/include/git2/transport.h
@@ -27,6 +27,14 @@ GIT_BEGIN_DECL
*/
GIT_EXTERN(int) git_transport_new(git_transport **transport, const char *url);
+/**
+ * Return whether a string is a valid transport URL
+ *
+ * @param tranport the url to check
+ * @param 1 if the url is valid, 0 otherwise
+ */
+GIT_EXTERN(int) git_transport_valid_url(const char *url);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/transport.c b/src/transport.c
index 8d69db5..0d67e19 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -10,7 +10,7 @@
#include "git2/net.h"
#include "transport.h"
-struct {
+static struct {
char *prefix;
git_transport_cb fn;
} transports[] = {
@@ -23,26 +23,20 @@ struct {
{NULL, 0}
};
-static git_transport_cb transport_new_fn(const char *url)
+#define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0]))
+
+static git_transport_cb transport_find_fn(const char *url)
{
- int i = 0;
+ size_t i = 0;
- while (1) {
- if (transports[i].prefix == NULL)
- break;
+ /* TODO: Parse "example.com:project.git" as an SSH URL */
+ for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) {
if (!strncasecmp(url, transports[i].prefix, strlen(transports[i].prefix)))
return transports[i].fn;
-
- ++i;
}
- /*
- * If we still haven't found the transport, we assume we mean a
- * local file.
- * TODO: Parse "example.com:project.git" as an SSH URL
- */
- return git_transport_local;
+ return NULL;
}
/**************
@@ -55,13 +49,25 @@ int git_transport_dummy(git_transport **GIT_UNUSED(transport))
return git__throw(GIT_ENOTIMPLEMENTED, "This protocol isn't implemented. Sorry");
}
+int git_transport_valid_url(const char *url)
+{
+ return transport_find_fn(url) != NULL;
+}
+
int git_transport_new(git_transport **out, const char *url)
{
git_transport_cb fn;
git_transport *transport;
int error;
- fn = transport_new_fn(url);
+ fn = transport_find_fn(url);
+
+ /*
+ * If we haven't found the transport, we assume we mean a
+ * local file.
+ */
+ if (fn == NULL)
+ fn = &git_transport_local;
error = fn(&transport);
if (error < GIT_SUCCESS)