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
28 * Flags to pass to transport
33 GIT_TRANSPORTFLAGS_NONE
= 0,
34 } git_transport_flags_t
;
36 struct git_transport
{
37 unsigned int version
; /**< The struct version */
39 /** Set progress and error callbacks */
40 int GIT_CALLBACK(set_callbacks
)(
41 git_transport
*transport
,
42 git_transport_message_cb progress_cb
,
43 git_transport_message_cb error_cb
,
44 git_transport_certificate_check_cb certificate_check_cb
,
47 /** Set custom headers for HTTP requests */
48 int GIT_CALLBACK(set_custom_headers
)(
49 git_transport
*transport
,
50 const git_strarray
*custom_headers
);
53 * Connect the transport to the remote repository, using the given
56 int GIT_CALLBACK(connect
)(
57 git_transport
*transport
,
59 git_credential_acquire_cb cred_acquire_cb
,
60 void *cred_acquire_payload
,
61 const git_proxy_options
*proxy_opts
,
66 * Get the list of available references in the remote repository.
68 * This function may be called after a successful call to
69 * `connect()`. The array returned is owned by the transport and
70 * must be kept valid until the next call to one of its functions.
73 const git_remote_head
***out
,
75 git_transport
*transport
);
77 /** Executes the push whose context is in the git_push object. */
78 int GIT_CALLBACK(push
)(git_transport
*transport
, git_push
*push
, const git_remote_callbacks
*callbacks
);
81 * Negotiate a fetch with the remote repository.
83 * This function may be called after a successful call to `connect()`,
84 * when the direction is GIT_DIRECTION_FETCH. The function performs a
85 * negotiation to calculate the `wants` list for the fetch.
87 int GIT_CALLBACK(negotiate_fetch
)(
88 git_transport
*transport
,
90 const git_remote_head
* const *refs
,
94 * Start downloading the packfile from the remote repository.
96 * This function may be called after a successful call to
97 * negotiate_fetch(), when the direction is GIT_DIRECTION_FETCH.
99 int GIT_CALLBACK(download_pack
)(
100 git_transport
*transport
,
101 git_repository
*repo
,
102 git_indexer_progress
*stats
,
103 git_indexer_progress_cb progress_cb
,
104 void *progress_payload
);
106 /** Checks to see if the transport is connected */
107 int GIT_CALLBACK(is_connected
)(git_transport
*transport
);
109 /** Reads the flags value previously passed into connect() */
110 int GIT_CALLBACK(read_flags
)(git_transport
*transport
, int *flags
);
112 /** Cancels any outstanding transport operation */
113 void GIT_CALLBACK(cancel
)(git_transport
*transport
);
116 * Close the connection to the remote repository.
118 * This function is the reverse of connect() -- it terminates the
119 * connection to the remote end.
121 int GIT_CALLBACK(close
)(git_transport
*transport
);
123 /** Frees/destructs the git_transport object. */
124 void GIT_CALLBACK(free
)(git_transport
*transport
);
127 #define GIT_TRANSPORT_VERSION 1
128 #define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION}
131 * Initializes a `git_transport` with default values. Equivalent to
132 * creating an instance with GIT_TRANSPORT_INIT.
134 * @param opts the `git_transport` struct to initialize
135 * @param version Version of struct; pass `GIT_TRANSPORT_VERSION`
136 * @return Zero on success; -1 on failure.
138 GIT_EXTERN(int) git_transport_init(
140 unsigned int version
);
143 * Function to use to create a transport from a URL. The transport database
144 * is scanned to find a transport that implements the scheme of the URI (i.e.
145 * git:// or http://) and a transport object is returned to the caller.
147 * @param out The newly created transport (out)
148 * @param owner The git_remote which will own this transport
149 * @param url The URL to connect to
150 * @return 0 or an error code
152 GIT_EXTERN(int) git_transport_new(git_transport
**out
, git_remote
*owner
, const char *url
);
155 * Create an ssh transport with custom git command paths
157 * This is a factory function suitable for setting as the transport
158 * callback in a remote (or for a clone in the options).
160 * The payload argument must be a strarray pointer with the paths for
161 * the `git-upload-pack` and `git-receive-pack` at index 0 and 1.
163 * @param out the resulting transport
164 * @param owner the owning remote
165 * @param payload a strarray with the paths
166 * @return 0 or an error code
168 GIT_EXTERN(int) git_transport_ssh_with_paths(git_transport
**out
, git_remote
*owner
, void *payload
);
171 * Add a custom transport definition, to be used in addition to the built-in
172 * set of transports that come with libgit2.
174 * The caller is responsible for synchronizing calls to git_transport_register
175 * and git_transport_unregister with other calls to the library that
176 * instantiate transports.
178 * @param prefix The scheme (ending in "://") to match, i.e. "git://"
179 * @param cb The callback used to create an instance of the transport
180 * @param param A fixed parameter to pass to cb at creation time
181 * @return 0 or an error code
183 GIT_EXTERN(int) git_transport_register(
189 * Unregister a custom transport definition which was previously registered
190 * with git_transport_register.
192 * The caller is responsible for synchronizing calls to git_transport_register
193 * and git_transport_unregister with other calls to the library that
194 * instantiate transports.
196 * @param prefix From the previous call to git_transport_register
197 * @return 0 or an error code
199 GIT_EXTERN(int) git_transport_unregister(
202 /* Transports which come with libgit2 (match git_transport_cb). The expected
203 * value for "param" is listed in-line below. */
206 * Create an instance of the dummy transport.
208 * @param out The newly created transport (out)
209 * @param owner The git_remote which will own this transport
210 * @param payload You must pass NULL for this parameter.
211 * @return 0 or an error code
213 GIT_EXTERN(int) git_transport_dummy(
216 /* NULL */ void *payload
);
219 * Create an instance of the local transport.
221 * @param out The newly created transport (out)
222 * @param owner The git_remote which will own this transport
223 * @param payload You must pass NULL for this parameter.
224 * @return 0 or an error code
226 GIT_EXTERN(int) git_transport_local(
229 /* NULL */ void *payload
);
232 * Create an instance of the smart transport.
234 * @param out The newly created transport (out)
235 * @param owner The git_remote which will own this transport
236 * @param payload A pointer to a git_smart_subtransport_definition
237 * @return 0 or an error code
239 GIT_EXTERN(int) git_transport_smart(
242 /* (git_smart_subtransport_definition *) */ void *payload
);
245 * Call the certificate check for this transport.
247 * @param transport a smart transport
248 * @param cert the certificate to pass to the caller
249 * @param valid whether we believe the certificate is valid
250 * @param hostname the hostname we connected to
251 * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH
252 * to indicate that there is no callback registered (or the callback
253 * refused to validate the certificate and callers should behave as
254 * if no callback was set), or < 0 for an error
256 GIT_EXTERN(int) git_transport_smart_certificate_check(git_transport
*transport
, git_cert
*cert
, int valid
, const char *hostname
);
259 * Call the credentials callback for this transport
261 * @param out the pointer where the creds are to be stored
262 * @param transport a smart transport
263 * @param user the user we saw on the url (if any)
264 * @param methods available methods for authentication
265 * @return the return value of the callback: 0 for no error, GIT_PASSTHROUGH
266 * to indicate that there is no callback registered (or the callback
267 * refused to provide credentials and callers should behave as if no
268 * callback was set), or < 0 for an error
270 GIT_EXTERN(int) git_transport_smart_credentials(git_credential
**out
, git_transport
*transport
, const char *user
, int methods
);
273 * Get a copy of the proxy options
275 * The url is copied and must be freed by the caller.
277 * @param out options struct to fill
278 * @param transport the transport to extract the data from.
280 GIT_EXTERN(int) git_transport_smart_proxy_options(git_proxy_options
*out
, git_transport
*transport
);
283 *** End of base transport interface ***
284 *** Begin interface for subtransports for the smart transport ***
287 /** Actions that the smart transport can ask a subtransport to perform */
289 GIT_SERVICE_UPLOADPACK_LS
= 1,
290 GIT_SERVICE_UPLOADPACK
= 2,
291 GIT_SERVICE_RECEIVEPACK_LS
= 3,
292 GIT_SERVICE_RECEIVEPACK
= 4,
293 } git_smart_service_t
;
295 typedef struct git_smart_subtransport git_smart_subtransport
;
296 typedef struct git_smart_subtransport_stream git_smart_subtransport_stream
;
299 * A stream used by the smart transport to read and write data
300 * from a subtransport.
302 * This provides a customization point in case you need to
303 * support some other communication method.
305 struct git_smart_subtransport_stream
{
306 git_smart_subtransport
*subtransport
; /**< The owning subtransport */
309 * Read available data from the stream.
311 * The implementation may read less than requested.
313 int GIT_CALLBACK(read
)(
314 git_smart_subtransport_stream
*stream
,
320 * Write data to the stream
322 * The implementation must write all data or return an error.
324 int GIT_CALLBACK(write
)(
325 git_smart_subtransport_stream
*stream
,
329 /** Free the stream */
330 void GIT_CALLBACK(free
)(
331 git_smart_subtransport_stream
*stream
);
335 * An implementation of a subtransport which carries data for the
338 struct git_smart_subtransport
{
340 * Setup a subtransport stream for the requested action.
342 int GIT_CALLBACK(action
)(
343 git_smart_subtransport_stream
**out
,
344 git_smart_subtransport
*transport
,
346 git_smart_service_t action
);
349 * Close the subtransport.
351 * Subtransports are guaranteed a call to close() between
352 * calls to action(), except for the following two "natural" progressions
353 * of actions against a constant URL:
355 * - UPLOADPACK_LS -> UPLOADPACK
356 * - RECEIVEPACK_LS -> RECEIVEPACK
358 int GIT_CALLBACK(close
)(git_smart_subtransport
*transport
);
360 /** Free the subtransport */
361 void GIT_CALLBACK(free
)(git_smart_subtransport
*transport
);
364 /** A function which creates a new subtransport for the smart transport */
365 typedef int GIT_CALLBACK(git_smart_subtransport_cb
)(
366 git_smart_subtransport
**out
,
367 git_transport
*owner
,
371 * Definition for a "subtransport"
373 * The smart transport knows how to speak the git protocol, but it has no
374 * knowledge of how to establish a connection between it and another endpoint,
375 * or how to move data back and forth. For this, a subtransport interface is
376 * declared, and the smart transport delegates this work to the subtransports.
378 * Three subtransports are provided by libgit2: ssh, git, http(s).
380 * Subtransports can either be RPC = 0 (persistent connection) or RPC = 1
381 * (request/response). The smart transport handles the differences in its own
382 * logic. The git subtransport is RPC = 0, while http is RPC = 1.
384 typedef struct git_smart_subtransport_definition
{
385 /** The function to use to create the git_smart_subtransport */
386 git_smart_subtransport_cb callback
;
389 * True if the protocol is stateless; false otherwise. For example,
390 * http:// is stateless, but git:// is not.
394 /** User-specified parameter passed to the callback */
396 } git_smart_subtransport_definition
;
398 /* Smart transport subtransports that come with libgit2 */
401 * Create an instance of the http subtransport.
403 * This subtransport also supports https.
405 * @param out The newly created subtransport
406 * @param owner The smart transport to own this subtransport
407 * @return 0 or an error code
409 GIT_EXTERN(int) git_smart_subtransport_http(
410 git_smart_subtransport
**out
,
411 git_transport
*owner
,
415 * Create an instance of the git subtransport.
417 * @param out The newly created subtransport
418 * @param owner The smart transport to own this subtransport
419 * @return 0 or an error code
421 GIT_EXTERN(int) git_smart_subtransport_git(
422 git_smart_subtransport
**out
,
423 git_transport
*owner
,
427 * Create an instance of the ssh subtransport.
429 * @param out The newly created subtransport
430 * @param owner The smart transport to own this subtransport
431 * @return 0 or an error code
433 GIT_EXTERN(int) git_smart_subtransport_ssh(
434 git_smart_subtransport
**out
,
435 git_transport
*owner
,