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/transport.h"
13 #include "git2/types.h"
14 #include "git2/strarray.h"
15 #include "git2/proxy.h"
18 * @file git2/sys/transport.h
19 * @brief Git custom transport registration interfaces and functions
20 * @defgroup git_transport Git custom transport registration
27 struct git_transport
{
28 unsigned int version
; /**< The struct version */
31 * Connect the transport to the remote repository, using the given
34 int GIT_CALLBACK(connect
)(
35 git_transport
*transport
,
38 const git_remote_connect_options
*connect_opts
);
41 * Resets the connect options for the given transport. This
42 * is useful for updating settings or callbacks for an already
43 * connected transport.
45 int GIT_CALLBACK(set_connect_opts
)(
46 git_transport
*transport
,
47 const git_remote_connect_options
*connect_opts
);
50 * Gets the capabilities for this remote repository.
52 * This function may be called after a successful call to
55 int GIT_CALLBACK(capabilities
)(
56 unsigned int *capabilities
,
57 git_transport
*transport
);
60 * Get the list of available references in the remote repository.
62 * This function may be called after a successful call to
63 * `connect()`. The array returned is owned by the transport and
64 * must be kept valid until the next call to one of its functions.
67 const git_remote_head
***out
,
69 git_transport
*transport
);
71 /** Executes the push whose context is in the git_push object. */
72 int GIT_CALLBACK(push
)(
73 git_transport
*transport
,
77 * Negotiate a fetch with the remote repository.
79 * This function may be called after a successful call to `connect()`,
80 * when the direction is GIT_DIRECTION_FETCH. The function performs a
81 * negotiation to calculate the `wants` list for the fetch.
83 int GIT_CALLBACK(negotiate_fetch
)(
84 git_transport
*transport
,
86 const git_remote_head
* const *refs
,
90 * Start downloading the packfile from the remote repository.
92 * This function may be called after a successful call to
93 * negotiate_fetch(), when the direction is GIT_DIRECTION_FETCH.
95 int GIT_CALLBACK(download_pack
)(
96 git_transport
*transport
,
98 git_indexer_progress
*stats
);
100 /** Checks to see if the transport is connected */
101 int GIT_CALLBACK(is_connected
)(git_transport
*transport
);
103 /** Cancels any outstanding transport operation */
104 void GIT_CALLBACK(cancel
)(git_transport
*transport
);
107 * Close the connection to the remote repository.
109 * This function is the reverse of connect() -- it terminates the
110 * connection to the remote end.
112 int GIT_CALLBACK(close
)(git_transport
*transport
);
114 /** Frees/destructs the git_transport object. */
115 void GIT_CALLBACK(free
)(git_transport
*transport
);
118 #define GIT_TRANSPORT_VERSION 1
119 #define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION}
122 * Initializes a `git_transport` with default values. Equivalent to
123 * creating an instance with GIT_TRANSPORT_INIT.
125 * @param opts the `git_transport` struct to initialize
126 * @param version Version of struct; pass `GIT_TRANSPORT_VERSION`
127 * @return Zero on success; -1 on failure.
129 GIT_EXTERN(int) git_transport_init(
131 unsigned int version
);
134 * Function to use to create a transport from a URL. The transport database
135 * is scanned to find a transport that implements the scheme of the URI (i.e.
136 * git:// or http://) and a transport object is returned to the caller.
138 * @param out The newly created transport (out)
139 * @param owner The git_remote which will own this transport
140 * @param url The URL to connect to
141 * @return 0 or an error code
143 GIT_EXTERN(int) git_transport_new(git_transport
**out
, git_remote
*owner
, const char *url
);
146 * Create an ssh transport with custom git command paths
148 * This is a factory function suitable for setting as the transport
149 * callback in a remote (or for a clone in the options).
151 * The payload argument must be a strarray pointer with the paths for
152 * the `git-upload-pack` and `git-receive-pack` at index 0 and 1.
154 * @param out the resulting transport
155 * @param owner the owning remote
156 * @param payload a strarray with the paths
157 * @return 0 or an error code
159 GIT_EXTERN(int) git_transport_ssh_with_paths(git_transport
**out
, git_remote
*owner
, void *payload
);
162 * Add a custom transport definition, to be used in addition to the built-in
163 * set of transports that come with libgit2.
165 * The caller is responsible for synchronizing calls to git_transport_register
166 * and git_transport_unregister with other calls to the library that
167 * instantiate transports.
169 * @param prefix The scheme (ending in "://") to match, i.e. "git://"
170 * @param cb The callback used to create an instance of the transport
171 * @param param A fixed parameter to pass to cb at creation time
172 * @return 0 or an error code
174 GIT_EXTERN(int) git_transport_register(
180 * Unregister a custom transport definition which was previously registered
181 * with git_transport_register.
183 * The caller is responsible for synchronizing calls to git_transport_register
184 * and git_transport_unregister with other calls to the library that
185 * instantiate transports.
187 * @param prefix From the previous call to git_transport_register
188 * @return 0 or an error code
190 GIT_EXTERN(int) git_transport_unregister(
193 /* Transports which come with libgit2 (match git_transport_cb). The expected
194 * value for "param" is listed in-line below. */
197 * Create an instance of the dummy 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_dummy(
207 /* NULL */ void *payload
);
210 * Create an instance of the local transport.
212 * @param out The newly created transport (out)
213 * @param owner The git_remote which will own this transport
214 * @param payload You must pass NULL for this parameter.
215 * @return 0 or an error code
217 GIT_EXTERN(int) git_transport_local(
220 /* NULL */ void *payload
);
223 * Create an instance of the smart transport.
225 * @param out The newly created transport (out)
226 * @param owner The git_remote which will own this transport
227 * @param payload A pointer to a git_smart_subtransport_definition
228 * @return 0 or an error code
230 GIT_EXTERN(int) git_transport_smart(
233 /* (git_smart_subtransport_definition *) */ void *payload
);
236 * Call the certificate check for this transport.
238 * @param transport a smart transport
239 * @param cert the certificate to pass to the caller
240 * @param valid whether we believe the certificate is valid
241 * @param hostname the hostname we connected to
242 * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH
243 * to indicate that there is no callback registered (or the callback
244 * refused to validate the certificate and callers should behave as
245 * if no callback was set), or < 0 for an error
247 GIT_EXTERN(int) git_transport_smart_certificate_check(git_transport
*transport
, git_cert
*cert
, int valid
, const char *hostname
);
250 * Call the credentials callback for this transport
252 * @param out the pointer where the creds are to be stored
253 * @param transport a smart transport
254 * @param user the user we saw on the url (if any)
255 * @param methods available methods for authentication
256 * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH
257 * to indicate that there is no callback registered (or the callback
258 * refused to provide credentials and callers should behave as if no
259 * callback was set), or < 0 for an error
261 GIT_EXTERN(int) git_transport_smart_credentials(git_credential
**out
, git_transport
*transport
, const char *user
, int methods
);
264 * Get a copy of the proxy options
266 * The url is copied and must be freed by the caller.
268 * @param out options struct to fill
269 * @param transport the transport to extract the data from.
271 GIT_EXTERN(int) git_transport_smart_proxy_options(git_proxy_options
*out
, git_transport
*transport
);
274 *** End of base transport interface ***
275 *** Begin interface for subtransports for the smart transport ***
278 /** Actions that the smart transport can ask a subtransport to perform */
280 GIT_SERVICE_UPLOADPACK_LS
= 1,
281 GIT_SERVICE_UPLOADPACK
= 2,
282 GIT_SERVICE_RECEIVEPACK_LS
= 3,
283 GIT_SERVICE_RECEIVEPACK
= 4
284 } git_smart_service_t
;
286 typedef struct git_smart_subtransport git_smart_subtransport
;
287 typedef struct git_smart_subtransport_stream git_smart_subtransport_stream
;
290 * A stream used by the smart transport to read and write data
291 * from a subtransport.
293 * This provides a customization point in case you need to
294 * support some other communication method.
296 struct git_smart_subtransport_stream
{
297 git_smart_subtransport
*subtransport
; /**< The owning subtransport */
300 * Read available data from the stream.
302 * The implementation may read less than requested.
304 int GIT_CALLBACK(read
)(
305 git_smart_subtransport_stream
*stream
,
311 * Write data to the stream
313 * The implementation must write all data or return an error.
315 int GIT_CALLBACK(write
)(
316 git_smart_subtransport_stream
*stream
,
320 /** Free the stream */
321 void GIT_CALLBACK(free
)(
322 git_smart_subtransport_stream
*stream
);
326 * An implementation of a subtransport which carries data for the
329 struct git_smart_subtransport
{
331 * Setup a subtransport stream for the requested action.
333 int GIT_CALLBACK(action
)(
334 git_smart_subtransport_stream
**out
,
335 git_smart_subtransport
*transport
,
337 git_smart_service_t action
);
340 * Close the subtransport.
342 * Subtransports are guaranteed a call to close() between
343 * calls to action(), except for the following two "natural" progressions
344 * of actions against a constant URL:
346 * - UPLOADPACK_LS -> UPLOADPACK
347 * - RECEIVEPACK_LS -> RECEIVEPACK
349 int GIT_CALLBACK(close
)(git_smart_subtransport
*transport
);
351 /** Free the subtransport */
352 void GIT_CALLBACK(free
)(git_smart_subtransport
*transport
);
355 /** A function which creates a new subtransport for the smart transport */
356 typedef int GIT_CALLBACK(git_smart_subtransport_cb
)(
357 git_smart_subtransport
**out
,
358 git_transport
*owner
,
362 * Definition for a "subtransport"
364 * The smart transport knows how to speak the git protocol, but it has no
365 * knowledge of how to establish a connection between it and another endpoint,
366 * or how to move data back and forth. For this, a subtransport interface is
367 * declared, and the smart transport delegates this work to the subtransports.
369 * Three subtransports are provided by libgit2: ssh, git, http(s).
371 * Subtransports can either be RPC = 0 (persistent connection) or RPC = 1
372 * (request/response). The smart transport handles the differences in its own
373 * logic. The git subtransport is RPC = 0, while http is RPC = 1.
375 typedef struct git_smart_subtransport_definition
{
376 /** The function to use to create the git_smart_subtransport */
377 git_smart_subtransport_cb callback
;
380 * True if the protocol is stateless; false otherwise. For example,
381 * http:// is stateless, but git:// is not.
385 /** User-specified parameter passed to the callback */
387 } git_smart_subtransport_definition
;
389 /* Smart transport subtransports that come with libgit2 */
392 * Create an instance of the http subtransport.
394 * This subtransport also supports https.
396 * @param out The newly created subtransport
397 * @param owner The smart transport to own this subtransport
398 * @return 0 or an error code
400 GIT_EXTERN(int) git_smart_subtransport_http(
401 git_smart_subtransport
**out
,
402 git_transport
*owner
,
406 * Create an instance of the git subtransport.
408 * @param out The newly created subtransport
409 * @param owner The smart transport to own this subtransport
410 * @return 0 or an error code
412 GIT_EXTERN(int) git_smart_subtransport_git(
413 git_smart_subtransport
**out
,
414 git_transport
*owner
,
418 * Create an instance of the ssh subtransport.
420 * @param out The newly created subtransport
421 * @param owner The smart transport to own this subtransport
422 * @return 0 or an error code
424 GIT_EXTERN(int) git_smart_subtransport_ssh(
425 git_smart_subtransport
**out
,
426 git_transport
*owner
,