2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
8 #ifndef INCLUDE_sys_git_transport_h
9 #define INCLUDE_sys_git_transport_h
12 #include "git2/types.h"
13 #include "git2/strarray.h"
14 #include "git2/proxy.h"
17 * @file git2/sys/transport.h
18 * @brief Git custom transport registration interfaces and functions
19 * @defgroup git_transport Git custom transport registration
27 * Flags to pass to transport
32 GIT_TRANSPORTFLAGS_NONE
= 0,
33 } git_transport_flags_t
;
35 struct git_transport
{
37 /* Set progress and error callbacks */
39 git_transport
*transport
,
40 git_transport_message_cb progress_cb
,
41 git_transport_message_cb error_cb
,
42 git_transport_certificate_check_cb certificate_check_cb
,
45 /* Set custom headers for HTTP requests */
46 int (*set_custom_headers
)(
47 git_transport
*transport
,
48 const git_strarray
*custom_headers
);
50 /* Connect the transport to the remote repository, using the given
53 git_transport
*transport
,
55 git_cred_acquire_cb cred_acquire_cb
,
56 void *cred_acquire_payload
,
57 const git_proxy_options
*proxy_opts
,
61 /* This function may be called after a successful call to
62 * connect(). The array returned is owned by the transport and
63 * is guaranteed until the next call of a transport function. */
65 const git_remote_head
***out
,
67 git_transport
*transport
);
69 /* Executes the push whose context is in the git_push object. */
70 int(*push
)(git_transport
*transport
, git_push
*push
, const git_remote_callbacks
*callbacks
);
72 /* This function may be called after a successful call to connect(), when
73 * the direction is FETCH. The function performs a negotiation to calculate
74 * the wants list for the fetch. */
75 int (*negotiate_fetch
)(
76 git_transport
*transport
,
78 const git_remote_head
* const *refs
,
81 /* This function may be called after a successful call to negotiate_fetch(),
82 * when the direction is FETCH. This function retrieves the pack file for
83 * the fetch from the remote end. */
85 git_transport
*transport
,
87 git_transfer_progress
*stats
,
88 git_transfer_progress_cb progress_cb
,
89 void *progress_payload
);
91 /* Checks to see if the transport is connected */
92 int (*is_connected
)(git_transport
*transport
);
94 /* Reads the flags value previously passed into connect() */
95 int (*read_flags
)(git_transport
*transport
, int *flags
);
97 /* Cancels any outstanding transport operation */
98 void (*cancel
)(git_transport
*transport
);
100 /* This function is the reverse of connect() -- it terminates the
101 * connection to the remote end. */
102 int (*close
)(git_transport
*transport
);
104 /* Frees/destructs the git_transport object. */
105 void (*free
)(git_transport
*transport
);
108 #define GIT_TRANSPORT_VERSION 1
109 #define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION}
112 * Initializes a `git_transport` with default values. Equivalent to
113 * creating an instance with GIT_TRANSPORT_INIT.
115 * @param opts the `git_transport` struct to initialize
116 * @param version Version of struct; pass `GIT_TRANSPORT_VERSION`
117 * @return Zero on success; -1 on failure.
119 GIT_EXTERN(int) git_transport_init(
121 unsigned int version
);
124 * Function to use to create a transport from a URL. The transport database
125 * is scanned to find a transport that implements the scheme of the URI (i.e.
126 * git:// or http://) and a transport object is returned to the caller.
128 * @param out The newly created transport (out)
129 * @param owner The git_remote which will own this transport
130 * @param url The URL to connect to
131 * @return 0 or an error code
133 GIT_EXTERN(int) git_transport_new(git_transport
**out
, git_remote
*owner
, const char *url
);
136 * Create an ssh transport with custom git command paths
138 * This is a factory function suitable for setting as the transport
139 * callback in a remote (or for a clone in the options).
141 * The payload argument must be a strarray pointer with the paths for
142 * the `git-upload-pack` and `git-receive-pack` at index 0 and 1.
144 * @param out the resulting transport
145 * @param owner the owning remote
146 * @param payload a strarray with the paths
147 * @return 0 or an error code
149 GIT_EXTERN(int) git_transport_ssh_with_paths(git_transport
**out
, git_remote
*owner
, void *payload
);
152 * Add a custom transport definition, to be used in addition to the built-in
153 * set of transports that come with libgit2.
155 * The caller is responsible for synchronizing calls to git_transport_register
156 * and git_transport_unregister with other calls to the library that
157 * instantiate transports.
159 * @param prefix The scheme (ending in "://") to match, i.e. "git://"
160 * @param cb The callback used to create an instance of the transport
161 * @param param A fixed parameter to pass to cb at creation time
162 * @return 0 or an error code
164 GIT_EXTERN(int) git_transport_register(
171 * Unregister a custom transport definition which was previously registered
172 * with git_transport_register.
174 * @param prefix From the previous call to git_transport_register
175 * @return 0 or an error code
177 GIT_EXTERN(int) git_transport_unregister(
180 /* Transports which come with libgit2 (match git_transport_cb). The expected
181 * value for "param" is listed in-line below. */
184 * Create an instance of the dummy transport.
186 * @param out The newly created transport (out)
187 * @param owner The git_remote which will own this transport
188 * @param payload You must pass NULL for this parameter.
189 * @return 0 or an error code
191 GIT_EXTERN(int) git_transport_dummy(
194 /* NULL */ void *payload
);
197 * Create an instance of the local transport.
199 * @param out The newly created transport (out)
200 * @param owner The git_remote which will own this transport
201 * @param payload You must pass NULL for this parameter.
202 * @return 0 or an error code
204 GIT_EXTERN(int) git_transport_local(
207 /* NULL */ void *payload
);
210 * Create an instance of the smart transport.
212 * @param out The newly created transport (out)
213 * @param owner The git_remote which will own this transport
214 * @param payload A pointer to a git_smart_subtransport_definition
215 * @return 0 or an error code
217 GIT_EXTERN(int) git_transport_smart(
220 /* (git_smart_subtransport_definition *) */ void *payload
);
223 * Call the certificate check for this transport.
225 * @param transport a smart transport
226 * @param cert the certificate to pass to the caller
227 * @param valid whether we believe the certificate is valid
228 * @param hostname the hostname we connected to
229 * @return the return value of the callback
231 GIT_EXTERN(int) git_transport_smart_certificate_check(git_transport
*transport
, git_cert
*cert
, int valid
, const char *hostname
);
234 * Call the credentials callback for this transport
236 * @param out the pointer where the creds are to be stored
237 * @param transport a smart transport
238 * @param user the user we saw on the url (if any)
239 * @param methods available methods for authentication
240 * @return the return value of the callback
242 GIT_EXTERN(int) git_transport_smart_credentials(git_cred
**out
, git_transport
*transport
, const char *user
, int methods
);
245 * Get a copy of the proxy options
247 * The url is copied and must be freed by the caller.
249 * @param out options struct to fill
250 * @param transport the transport to extract the data from.
252 GIT_EXTERN(int) git_transport_smart_proxy_options(git_proxy_options
*out
, git_transport
*transport
);
255 *** End of base transport interface ***
256 *** Begin interface for subtransports for the smart transport ***
259 /* The smart transport knows how to speak the git protocol, but it has no
260 * knowledge of how to establish a connection between it and another endpoint,
261 * or how to move data back and forth. For this, a subtransport interface is
262 * declared, and the smart transport delegates this work to the subtransports.
263 * Three subtransports are implemented: git, http, and winhttp. (The http and
264 * winhttp transports each implement both http and https.) */
266 /* Subtransports can either be RPC = 0 (persistent connection) or RPC = 1
267 * (request/response). The smart transport handles the differences in its own
268 * logic. The git subtransport is RPC = 0, while http and winhttp are both
271 /* Actions that the smart transport can ask
272 * a subtransport to perform */
274 GIT_SERVICE_UPLOADPACK_LS
= 1,
275 GIT_SERVICE_UPLOADPACK
= 2,
276 GIT_SERVICE_RECEIVEPACK_LS
= 3,
277 GIT_SERVICE_RECEIVEPACK
= 4,
278 } git_smart_service_t
;
280 typedef struct git_smart_subtransport git_smart_subtransport
;
281 typedef struct git_smart_subtransport_stream git_smart_subtransport_stream
;
283 /* A stream used by the smart transport to read and write data
284 * from a subtransport */
285 struct git_smart_subtransport_stream
{
286 /* The owning subtransport */
287 git_smart_subtransport
*subtransport
;
290 git_smart_subtransport_stream
*stream
,
296 git_smart_subtransport_stream
*stream
,
301 git_smart_subtransport_stream
*stream
);
304 /* An implementation of a subtransport which carries data for the
306 struct git_smart_subtransport
{
308 git_smart_subtransport_stream
**out
,
309 git_smart_subtransport
*transport
,
311 git_smart_service_t action
);
313 /* Subtransports are guaranteed a call to close() between
314 * calls to action(), except for the following two "natural" progressions
315 * of actions against a constant URL.
317 * 1. UPLOADPACK_LS -> UPLOADPACK
318 * 2. RECEIVEPACK_LS -> RECEIVEPACK */
319 int (*close
)(git_smart_subtransport
*transport
);
321 void (*free
)(git_smart_subtransport
*transport
);
324 /* A function which creates a new subtransport for the smart transport */
325 typedef int (*git_smart_subtransport_cb
)(
326 git_smart_subtransport
**out
,
327 git_transport
* owner
,
331 * Definition for a "subtransport"
333 * This is used to let the smart protocol code know about the protocol
334 * which you are implementing.
336 typedef struct git_smart_subtransport_definition
{
337 /** The function to use to create the git_smart_subtransport */
338 git_smart_subtransport_cb callback
;
341 * True if the protocol is stateless; false otherwise. For example,
342 * http:// is stateless, but git:// is not.
346 /** Param of the callback
349 } git_smart_subtransport_definition
;
351 /* Smart transport subtransports that come with libgit2 */
354 * Create an instance of the http subtransport. This subtransport
355 * also supports https. On Win32, this subtransport may be implemented
356 * using the WinHTTP library.
358 * @param out The newly created subtransport
359 * @param owner The smart transport to own this subtransport
360 * @return 0 or an error code
362 GIT_EXTERN(int) git_smart_subtransport_http(
363 git_smart_subtransport
**out
,
364 git_transport
* owner
,
368 * Create an instance of the git subtransport.
370 * @param out The newly created subtransport
371 * @param owner The smart transport to own this subtransport
372 * @return 0 or an error code
374 GIT_EXTERN(int) git_smart_subtransport_git(
375 git_smart_subtransport
**out
,
376 git_transport
* owner
,
380 * Create an instance of the ssh subtransport.
382 * @param out The newly created subtransport
383 * @param owner The smart transport to own this subtransport
384 * @return 0 or an error code
386 GIT_EXTERN(int) git_smart_subtransport_ssh(
387 git_smart_subtransport
**out
,
388 git_transport
* owner
,