]> git.proxmox.com Git - libgit2.git/blob - src/transport.c
Merge pull request #444 from carlosmn/fetch-fixes
[libgit2.git] / src / transport.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
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 */
7 #include "common.h"
8 #include "git2/types.h"
9 #include "git2/transport.h"
10 #include "git2/net.h"
11 #include "transport.h"
12
13 struct {
14 char *prefix;
15 git_transport_cb fn;
16 } transports[] = {
17 {"git://", git_transport_git},
18 {"http://", git_transport_http},
19 {"https://", git_transport_dummy},
20 {"file://", git_transport_local},
21 {"git+ssh://", git_transport_dummy},
22 {"ssh+git://", git_transport_dummy},
23 {NULL, 0}
24 };
25
26 static git_transport_cb transport_new_fn(const char *url)
27 {
28 int i = 0;
29
30 while (1) {
31 if (transports[i].prefix == NULL)
32 break;
33
34 if (!strncasecmp(url, transports[i].prefix, strlen(transports[i].prefix)))
35 return transports[i].fn;
36
37 ++i;
38 }
39
40 /*
41 * If we still haven't found the transport, we assume we mean a
42 * local file.
43 * TODO: Parse "example.com:project.git" as an SSH URL
44 */
45 return git_transport_local;
46 }
47
48 /**************
49 * Public API *
50 **************/
51
52 int git_transport_dummy(git_transport **GIT_UNUSED(transport))
53 {
54 GIT_UNUSED_ARG(transport);
55 return git__throw(GIT_ENOTIMPLEMENTED, "This protocol isn't implemented. Sorry");
56 }
57
58 int git_transport_new(git_transport **out, const char *url)
59 {
60 git_transport_cb fn;
61 git_transport *transport;
62 int error;
63
64 fn = transport_new_fn(url);
65
66 error = fn(&transport);
67 if (error < GIT_SUCCESS)
68 return git__rethrow(error, "Failed to create new transport");
69
70 transport->url = git__strdup(url);
71 if (transport->url == NULL)
72 return GIT_ENOMEM;
73
74 *out = transport;
75
76 return GIT_SUCCESS;
77 }