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/proxy.h"
13 #include "git2/remote.h"
14 #include "git2/strarray.h"
15 #include "git2/transport.h"
16 #include "git2/types.h"
19 * @file git2/sys/transport.h
20 * @brief Git custom transport registration interfaces and functions
21 * @defgroup git_transport Git custom transport registration
28 struct git_transport
{
29 unsigned int version
; /**< The struct version */
32 * Connect the transport to the remote repository, using the given
35 int GIT_CALLBACK(connect
)(
36 git_transport
*transport
,
39 const git_remote_connect_options
*connect_opts
);
42 * Resets the connect options for the given transport. This
43 * is useful for updating settings or callbacks for an already
44 * connected transport.
46 int GIT_CALLBACK(set_connect_opts
)(
47 git_transport
*transport
,
48 const git_remote_connect_options
*connect_opts
);
51 * Gets the capabilities for this remote repository.
53 * This function may be called after a successful call to
56 int GIT_CALLBACK(capabilities
)(
57 unsigned int *capabilities
,
58 git_transport
*transport
);
61 * Get the list of available references in the remote repository.
63 * This function may be called after a successful call to
64 * `connect()`. The array returned is owned by the transport and
65 * must be kept valid until the next call to one of its functions.
68 const git_remote_head
***out
,
70 git_transport
*transport
);
72 /** Executes the push whose context is in the git_push object. */
73 int GIT_CALLBACK(push
)(
74 git_transport
*transport
,
78 * Negotiate a fetch with the remote repository.
80 * This function may be called after a successful call to `connect()`,
81 * when the direction is GIT_DIRECTION_FETCH. The function performs a
82 * negotiation to calculate the `wants` list for the fetch.
84 int GIT_CALLBACK(negotiate_fetch
)(
85 git_transport
*transport
,
87 const git_remote_head
* const *refs
,
91 * Start downloading the packfile from the remote repository.
93 * This function may be called after a successful call to
94 * negotiate_fetch(), when the direction is GIT_DIRECTION_FETCH.
96 int GIT_CALLBACK(download_pack
)(
97 git_transport
*transport
,
99 git_indexer_progress
*stats
);
101 /** Checks to see if the transport is connected */
102 int GIT_CALLBACK(is_connected
)(git_transport
*transport
);
104 /** Cancels any outstanding transport operation */
105 void GIT_CALLBACK(cancel
)(git_transport
*transport
);
108 * Close the connection to the remote repository.
110 * This function is the reverse of connect() -- it terminates the
111 * connection to the remote end.
113 int GIT_CALLBACK(close
)(git_transport
*transport
);
115 /** Frees/destructs the git_transport object. */
116 void GIT_CALLBACK(free
)(git_transport
*transport
);
119 #define GIT_TRANSPORT_VERSION 1
120 #define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION}
123 * Initializes a `git_transport` with default values. Equivalent to
124 * creating an instance with GIT_TRANSPORT_INIT.
126 * @param opts the `git_transport` struct to initialize
127 * @param version Version of struct; pass `GIT_TRANSPORT_VERSION`
128 * @return Zero on success; -1 on failure.
130 GIT_EXTERN(int) git_transport_init(
132 unsigned int version
);
135 * Function to use to create a transport from a URL. The transport database
136 * is scanned to find a transport that implements the scheme of the URI (i.e.
137 * git:// or http://) and a transport object is returned to the caller.
139 * @param out The newly created transport (out)
140 * @param owner The git_remote which will own this transport
141 * @param url The URL to connect to
142 * @return 0 or an error code
144 GIT_EXTERN(int) git_transport_new(git_transport
**out
, git_remote
*owner
, const char *url
);
147 * Create an ssh transport with custom git command paths
149 * This is a factory function suitable for setting as the transport
150 * callback in a remote (or for a clone in the options).
152 * The payload argument must be a strarray pointer with the paths for
153 * the `git-upload-pack` and `git-receive-pack` at index 0 and 1.
155 * @param out the resulting transport
156 * @param owner the owning remote
157 * @param payload a strarray with the paths
158 * @return 0 or an error code
160 GIT_EXTERN(int) git_transport_ssh_with_paths(git_transport
**out
, git_remote
*owner
, void *payload
);
163 * Add a custom transport definition, to be used in addition to the built-in
164 * set of transports that come with libgit2.
166 * The caller is responsible for synchronizing calls to git_transport_register
167 * and git_transport_unregister with other calls to the library that
168 * instantiate transports.
170 * @param prefix The scheme (ending in "://") to match, i.e. "git://"
171 * @param cb The callback used to create an instance of the transport
172 * @param param A fixed parameter to pass to cb at creation time
173 * @return 0 or an error code
175 GIT_EXTERN(int) git_transport_register(
181 * Unregister a custom transport definition which was previously registered
182 * with git_transport_register.
184 * The caller is responsible for synchronizing calls to git_transport_register
185 * and git_transport_unregister with other calls to the library that
186 * instantiate transports.
188 * @param prefix From the previous call to git_transport_register
189 * @return 0 or an error code
191 GIT_EXTERN(int) git_transport_unregister(
194 /* Transports which come with libgit2 (match git_transport_cb). The expected
195 * value for "param" is listed in-line below. */
198 * Create an instance of the dummy transport.
200 * @param out The newly created transport (out)
201 * @param owner The git_remote which will own this transport
202 * @param payload You must pass NULL for this parameter.
203 * @return 0 or an error code
205 GIT_EXTERN(int) git_transport_dummy(
208 /* NULL */ void *payload
);
211 * Create an instance of the local transport.
213 * @param out The newly created transport (out)
214 * @param owner The git_remote which will own this transport
215 * @param payload You must pass NULL for this parameter.
216 * @return 0 or an error code
218 GIT_EXTERN(int) git_transport_local(
221 /* NULL */ void *payload
);
224 * Create an instance of the smart transport.
226 * @param out The newly created transport (out)
227 * @param owner The git_remote which will own this transport
228 * @param payload A pointer to a git_smart_subtransport_definition
229 * @return 0 or an error code
231 GIT_EXTERN(int) git_transport_smart(
234 /* (git_smart_subtransport_definition *) */ void *payload
);
237 * Call the certificate check for this transport.
239 * @param transport a smart transport
240 * @param cert the certificate to pass to the caller
241 * @param valid whether we believe the certificate is valid
242 * @param hostname the hostname we connected to
243 * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH
244 * to indicate that there is no callback registered (or the callback
245 * refused to validate the certificate and callers should behave as
246 * if no callback was set), or < 0 for an error
248 GIT_EXTERN(int) git_transport_smart_certificate_check(git_transport
*transport
, git_cert
*cert
, int valid
, const char *hostname
);
251 * Call the credentials callback for this transport
253 * @param out the pointer where the creds are to be stored
254 * @param transport a smart transport
255 * @param user the user we saw on the url (if any)
256 * @param methods available methods for authentication
257 * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH
258 * to indicate that there is no callback registered (or the callback
259 * refused to provide credentials and callers should behave as if no
260 * callback was set), or < 0 for an error
262 GIT_EXTERN(int) git_transport_smart_credentials(git_credential
**out
, git_transport
*transport
, const char *user
, int methods
);
265 * Get a copy of the remote connect options
267 * All data is copied and must be freed by the caller by calling
268 * `git_remote_connect_options_dispose`.
270 * @param out options struct to fill
271 * @param transport the transport to extract the data from.
273 GIT_EXTERN(int) git_transport_remote_connect_options(
274 git_remote_connect_options
*out
,
275 git_transport
*transport
);
278 *** End of base transport interface ***
279 *** Begin interface for subtransports for the smart transport ***
282 /** Actions that the smart transport can ask a subtransport to perform */
284 GIT_SERVICE_UPLOADPACK_LS
= 1,
285 GIT_SERVICE_UPLOADPACK
= 2,
286 GIT_SERVICE_RECEIVEPACK_LS
= 3,
287 GIT_SERVICE_RECEIVEPACK
= 4
288 } git_smart_service_t
;
290 typedef struct git_smart_subtransport git_smart_subtransport
;
291 typedef struct git_smart_subtransport_stream git_smart_subtransport_stream
;
294 * A stream used by the smart transport to read and write data
295 * from a subtransport.
297 * This provides a customization point in case you need to
298 * support some other communication method.
300 struct git_smart_subtransport_stream
{
301 git_smart_subtransport
*subtransport
; /**< The owning subtransport */
304 * Read available data from the stream.
306 * The implementation may read less than requested.
308 int GIT_CALLBACK(read
)(
309 git_smart_subtransport_stream
*stream
,
315 * Write data to the stream
317 * The implementation must write all data or return an error.
319 int GIT_CALLBACK(write
)(
320 git_smart_subtransport_stream
*stream
,
324 /** Free the stream */
325 void GIT_CALLBACK(free
)(
326 git_smart_subtransport_stream
*stream
);
330 * An implementation of a subtransport which carries data for the
333 struct git_smart_subtransport
{
335 * Setup a subtransport stream for the requested action.
337 int GIT_CALLBACK(action
)(
338 git_smart_subtransport_stream
**out
,
339 git_smart_subtransport
*transport
,
341 git_smart_service_t action
);
344 * Close the subtransport.
346 * Subtransports are guaranteed a call to close() between
347 * calls to action(), except for the following two "natural" progressions
348 * of actions against a constant URL:
350 * - UPLOADPACK_LS -> UPLOADPACK
351 * - RECEIVEPACK_LS -> RECEIVEPACK
353 int GIT_CALLBACK(close
)(git_smart_subtransport
*transport
);
355 /** Free the subtransport */
356 void GIT_CALLBACK(free
)(git_smart_subtransport
*transport
);
359 /** A function which creates a new subtransport for the smart transport */
360 typedef int GIT_CALLBACK(git_smart_subtransport_cb
)(
361 git_smart_subtransport
**out
,
362 git_transport
*owner
,
366 * Definition for a "subtransport"
368 * The smart transport knows how to speak the git protocol, but it has no
369 * knowledge of how to establish a connection between it and another endpoint,
370 * or how to move data back and forth. For this, a subtransport interface is
371 * declared, and the smart transport delegates this work to the subtransports.
373 * Three subtransports are provided by libgit2: ssh, git, http(s).
375 * Subtransports can either be RPC = 0 (persistent connection) or RPC = 1
376 * (request/response). The smart transport handles the differences in its own
377 * logic. The git subtransport is RPC = 0, while http is RPC = 1.
379 typedef struct git_smart_subtransport_definition
{
380 /** The function to use to create the git_smart_subtransport */
381 git_smart_subtransport_cb callback
;
384 * True if the protocol is stateless; false otherwise. For example,
385 * http:// is stateless, but git:// is not.
389 /** User-specified parameter passed to the callback */
391 } git_smart_subtransport_definition
;
393 /* Smart transport subtransports that come with libgit2 */
396 * Create an instance of the http subtransport.
398 * This subtransport also supports https.
400 * @param out The newly created subtransport
401 * @param owner The smart transport to own this subtransport
402 * @return 0 or an error code
404 GIT_EXTERN(int) git_smart_subtransport_http(
405 git_smart_subtransport
**out
,
406 git_transport
*owner
,
410 * Create an instance of the git subtransport.
412 * @param out The newly created subtransport
413 * @param owner The smart transport to own this subtransport
414 * @return 0 or an error code
416 GIT_EXTERN(int) git_smart_subtransport_git(
417 git_smart_subtransport
**out
,
418 git_transport
*owner
,
422 * Create an instance of the ssh subtransport.
424 * @param out The newly created subtransport
425 * @param owner The smart transport to own this subtransport
426 * @return 0 or an error code
428 GIT_EXTERN(int) git_smart_subtransport_ssh(
429 git_smart_subtransport
**out
,
430 git_transport
*owner
,