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 *** End of base transport interface ***
246 *** Begin interface for subtransports for the smart transport ***
249 /* The smart transport knows how to speak the git protocol, but it has no
250 * knowledge of how to establish a connection between it and another endpoint,
251 * or how to move data back and forth. For this, a subtransport interface is
252 * declared, and the smart transport delegates this work to the subtransports.
253 * Three subtransports are implemented: git, http, and winhttp. (The http and
254 * winhttp transports each implement both http and https.) */
256 /* Subtransports can either be RPC = 0 (persistent connection) or RPC = 1
257 * (request/response). The smart transport handles the differences in its own
258 * logic. The git subtransport is RPC = 0, while http and winhttp are both
261 /* Actions that the smart transport can ask
262 * a subtransport to perform */
264 GIT_SERVICE_UPLOADPACK_LS
= 1,
265 GIT_SERVICE_UPLOADPACK
= 2,
266 GIT_SERVICE_RECEIVEPACK_LS
= 3,
267 GIT_SERVICE_RECEIVEPACK
= 4,
268 } git_smart_service_t
;
270 typedef struct git_smart_subtransport git_smart_subtransport
;
271 typedef struct git_smart_subtransport_stream git_smart_subtransport_stream
;
273 /* A stream used by the smart transport to read and write data
274 * from a subtransport */
275 struct git_smart_subtransport_stream
{
276 /* The owning subtransport */
277 git_smart_subtransport
*subtransport
;
280 git_smart_subtransport_stream
*stream
,
286 git_smart_subtransport_stream
*stream
,
291 git_smart_subtransport_stream
*stream
);
294 /* An implementation of a subtransport which carries data for the
296 struct git_smart_subtransport
{
298 git_smart_subtransport_stream
**out
,
299 git_smart_subtransport
*transport
,
301 git_smart_service_t action
);
303 /* Subtransports are guaranteed a call to close() between
304 * calls to action(), except for the following two "natural" progressions
305 * of actions against a constant URL.
307 * 1. UPLOADPACK_LS -> UPLOADPACK
308 * 2. RECEIVEPACK_LS -> RECEIVEPACK */
309 int (*close
)(git_smart_subtransport
*transport
);
311 void (*free
)(git_smart_subtransport
*transport
);
314 /* A function which creates a new subtransport for the smart transport */
315 typedef int (*git_smart_subtransport_cb
)(
316 git_smart_subtransport
**out
,
317 git_transport
* owner
,
321 * Definition for a "subtransport"
323 * This is used to let the smart protocol code know about the protocol
324 * which you are implementing.
326 typedef struct git_smart_subtransport_definition
{
327 /** The function to use to create the git_smart_subtransport */
328 git_smart_subtransport_cb callback
;
331 * True if the protocol is stateless; false otherwise. For example,
332 * http:// is stateless, but git:// is not.
336 /** Param of the callback
339 } git_smart_subtransport_definition
;
341 /* Smart transport subtransports that come with libgit2 */
344 * Create an instance of the http subtransport. This subtransport
345 * also supports https. On Win32, this subtransport may be implemented
346 * using the WinHTTP library.
348 * @param out The newly created subtransport
349 * @param owner The smart transport to own this subtransport
350 * @return 0 or an error code
352 GIT_EXTERN(int) git_smart_subtransport_http(
353 git_smart_subtransport
**out
,
354 git_transport
* owner
,
358 * Create an instance of the git subtransport.
360 * @param out The newly created subtransport
361 * @param owner The smart transport to own this subtransport
362 * @return 0 or an error code
364 GIT_EXTERN(int) git_smart_subtransport_git(
365 git_smart_subtransport
**out
,
366 git_transport
* owner
,
370 * Create an instance of the ssh subtransport.
372 * @param out The newly created subtransport
373 * @param owner The smart transport to own this subtransport
374 * @return 0 or an error code
376 GIT_EXTERN(int) git_smart_subtransport_ssh(
377 git_smart_subtransport
**out
,
378 git_transport
* owner
,