]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
5e0de328 | 2 | * Copyright (C) 2009-2012 the libgit2 contributors |
bb742ede VM |
3 | * |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
8f866dae CMN |
7 | #include "common.h" |
8 | #include "git2/types.h" | |
d88d4311 | 9 | #include "git2/remote.h" |
8f866dae CMN |
10 | #include "git2/net.h" |
11 | #include "transport.h" | |
253d6df5 | 12 | #include "path.h" |
8f866dae | 13 | |
2869f404 | 14 | static struct { |
8f866dae CMN |
15 | char *prefix; |
16 | git_transport_cb fn; | |
17 | } transports[] = { | |
ecb6ca0e | 18 | {"git://", git_transport_git}, |
3d975abc | 19 | {"http://", git_transport_http}, |
8f866dae CMN |
20 | {"https://", git_transport_dummy}, |
21 | {"file://", git_transport_local}, | |
22 | {"git+ssh://", git_transport_dummy}, | |
23 | {"ssh+git://", git_transport_dummy}, | |
24 | {NULL, 0} | |
25 | }; | |
26 | ||
86360ffd | 27 | #define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0])) - 1 |
2869f404 VM |
28 | |
29 | static git_transport_cb transport_find_fn(const char *url) | |
8f866dae | 30 | { |
2869f404 | 31 | size_t i = 0; |
8f866dae | 32 | |
58448910 | 33 | // First, check to see if it's an obvious URL, which a URL scheme |
2869f404 | 34 | for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) { |
8f866dae CMN |
35 | if (!strncasecmp(url, transports[i].prefix, strlen(transports[i].prefix))) |
36 | return transports[i].fn; | |
8f866dae CMN |
37 | } |
38 | ||
253d6df5 RW |
39 | /* still here? Check to see if the path points to a file on the local file system */ |
40 | if ((git_path_exists(url) == GIT_SUCCESS) && git_path_isdir(url)) | |
41 | return &git_transport_local; | |
58448910 | 42 | |
253d6df5 RW |
43 | /* It could be a SSH remote path. Check to see if there's a : */ |
44 | if (strrchr(url, ':')) | |
45 | return &git_transport_dummy; /* SSH is an unsupported transport mechanism in this version of libgit2 */ | |
58448910 | 46 | |
2869f404 | 47 | return NULL; |
8f866dae CMN |
48 | } |
49 | ||
50 | /************** | |
51 | * Public API * | |
52 | **************/ | |
53 | ||
854eccbb | 54 | int git_transport_dummy(git_transport **transport) |
8f866dae | 55 | { |
854eccbb | 56 | GIT_UNUSED(transport); |
4376f7f6 CMN |
57 | giterr_set(GITERR_NET, "This transport isn't implemented. Sorry"); |
58 | return -1; | |
8f866dae CMN |
59 | } |
60 | ||
ce90a407 | 61 | int git_transport_new(git_transport **out, const char *url) |
8f866dae CMN |
62 | { |
63 | git_transport_cb fn; | |
64 | git_transport *transport; | |
65 | int error; | |
66 | ||
2869f404 VM |
67 | fn = transport_find_fn(url); |
68 | ||
2869f404 | 69 | if (fn == NULL) |
4f8efc97 | 70 | return git__throw(GIT_EINVALIDARGS, "Unsupported URL or non-existent path"); |
8f866dae | 71 | |
4e913309 CMN |
72 | error = fn(&transport); |
73 | if (error < GIT_SUCCESS) | |
4376f7f6 | 74 | return error; |
d6258deb | 75 | |
8f866dae | 76 | transport->url = git__strdup(url); |
4376f7f6 | 77 | GITERR_CHECK_ALLOC(transport->url); |
8f866dae | 78 | |
8f866dae CMN |
79 | *out = transport; |
80 | ||
81 | return GIT_SUCCESS; | |
82 | } | |
d88d4311 VM |
83 | |
84 | /* from remote.h */ | |
85 | int git_remote_valid_url(const char *url) | |
86 | { | |
87 | return transport_find_fn(url) != NULL; | |
88 | } | |
89 | ||
7a544966 RW |
90 | int git_remote_supported_url(const char* url) |
91 | { | |
92 | git_transport_cb transport_fn = transport_find_fn(url); | |
93 | ||
94 | return ((transport_fn != NULL) && (transport_fn != &git_transport_dummy)); | |
95 | } |