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"
15 * @file git2/sys/transport.h
16 * @brief Git custom transport registration interfaces and functions
17 * @defgroup git_transport Git custom transport registration
25 * Flags to pass to transport
30 GIT_TRANSPORTFLAGS_NONE
= 0,
31 } git_transport_flags_t
;
33 struct git_transport
{
35 /* Set progress and error callbacks */
37 git_transport
*transport
,
38 git_transport_message_cb progress_cb
,
39 git_transport_message_cb error_cb
,
40 git_transport_certificate_check_cb certificate_check_cb
,
43 /* Set custom headers for HTTP requests */
44 int (*set_custom_headers
)(
45 git_transport
*transport
,
46 const git_strarray
*custom_headers
);
48 /* Connect the transport to the remote repository, using the given
51 git_transport
*transport
,
53 git_cred_acquire_cb cred_acquire_cb
,
54 void *cred_acquire_payload
,
58 /* This function may be called after a successful call to
59 * connect(). The array returned is owned by the transport and
60 * is guaranteed until the next call of a transport function. */
62 const git_remote_head
***out
,
64 git_transport
*transport
);
66 /* Executes the push whose context is in the git_push object. */
67 int (*push
)(git_transport
*transport
, git_push
*push
, const git_remote_callbacks
*callbacks
);
69 /* This function may be called after a successful call to connect(), when
70 * the direction is FETCH. The function performs a negotiation to calculate
71 * the wants list for the fetch. */
72 int (*negotiate_fetch
)(
73 git_transport
*transport
,
75 const git_remote_head
* const *refs
,
78 /* This function may be called after a successful call to negotiate_fetch(),
79 * when the direction is FETCH. This function retrieves the pack file for
80 * the fetch from the remote end. */
82 git_transport
*transport
,
84 git_transfer_progress
*stats
,
85 git_transfer_progress_cb progress_cb
,
86 void *progress_payload
);
88 /* Checks to see if the transport is connected */
89 int (*is_connected
)(git_transport
*transport
);
91 /* Reads the flags value previously passed into connect() */
92 int (*read_flags
)(git_transport
*transport
, int *flags
);
94 /* Cancels any outstanding transport operation */
95 void (*cancel
)(git_transport
*transport
);
97 /* This function is the reverse of connect() -- it terminates the
98 * connection to the remote end. */
99 int (*close
)(git_transport
*transport
);
101 /* Frees/destructs the git_transport object. */
102 void (*free
)(git_transport
*transport
);
105 #define GIT_TRANSPORT_VERSION 1
106 #define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION}
109 * Initializes a `git_transport` with default values. Equivalent to
110 * creating an instance with GIT_TRANSPORT_INIT.
112 * @param opts the `git_transport` struct to initialize
113 * @param version Version of struct; pass `GIT_TRANSPORT_VERSION`
114 * @return Zero on success; -1 on failure.
116 GIT_EXTERN(int) git_transport_init(
118 unsigned int version
);
121 * Function to use to create a transport from a URL. The transport database
122 * is scanned to find a transport that implements the scheme of the URI (i.e.
123 * git:// or http://) and a transport object is returned to the caller.
125 * @param out The newly created transport (out)
126 * @param owner The git_remote which will own this transport
127 * @param url The URL to connect to
128 * @return 0 or an error code
130 GIT_EXTERN(int) git_transport_new(git_transport
**out
, git_remote
*owner
, const char *url
);
133 * Create an ssh transport with custom git command paths
135 * This is a factory function suitable for setting as the transport
136 * callback in a remote (or for a clone in the options).
138 * The payload argument must be a strarray pointer with the paths for
139 * the `git-upload-pack` and `git-receive-pack` at index 0 and 1.
141 * @param out the resulting transport
142 * @param owner the owning remote
143 * @param payload a strarray with the paths
144 * @return 0 or an error code
146 GIT_EXTERN(int) git_transport_ssh_with_paths(git_transport
**out
, git_remote
*owner
, void *payload
);
149 * Add a custom transport definition, to be used in addition to the built-in
150 * set of transports that come with libgit2.
152 * The caller is responsible for synchronizing calls to git_transport_register
153 * and git_transport_unregister with other calls to the library that
154 * instantiate transports.
156 * @param prefix The scheme (ending in "://") to match, i.e. "git://"
157 * @param cb The callback used to create an instance of the transport
158 * @param param A fixed parameter to pass to cb at creation time
159 * @return 0 or an error code
161 GIT_EXTERN(int) git_transport_register(
168 * Unregister a custom transport definition which was previously registered
169 * with git_transport_register.
171 * @param prefix From the previous call to git_transport_register
172 * @return 0 or an error code
174 GIT_EXTERN(int) git_transport_unregister(
177 /* Transports which come with libgit2 (match git_transport_cb). The expected
178 * value for "param" is listed in-line below. */
181 * Create an instance of the dummy transport.
183 * @param out The newly created transport (out)
184 * @param owner The git_remote which will own this transport
185 * @param payload You must pass NULL for this parameter.
186 * @return 0 or an error code
188 GIT_EXTERN(int) git_transport_dummy(
191 /* NULL */ void *payload
);
194 * Create an instance of the local transport.
196 * @param out The newly created transport (out)
197 * @param owner The git_remote which will own this transport
198 * @param payload You must pass NULL for this parameter.
199 * @return 0 or an error code
201 GIT_EXTERN(int) git_transport_local(
204 /* NULL */ void *payload
);
207 * Create an instance of the smart transport.
209 * @param out The newly created transport (out)
210 * @param owner The git_remote which will own this transport
211 * @param payload A pointer to a git_smart_subtransport_definition
212 * @return 0 or an error code
214 GIT_EXTERN(int) git_transport_smart(
217 /* (git_smart_subtransport_definition *) */ void *payload
);
220 * Call the certificate check for this transport.
222 * @param transport a smart transport
223 * @param cert the certificate to pass to the caller
224 * @param valid whether we believe the certificate is valid
225 * @param hostname the hostname we connected to
226 * @return the return value of the callback
228 GIT_EXTERN(int) git_transport_smart_certificate_check(git_transport
*transport
, git_cert
*cert
, int valid
, const char *hostname
);
231 * Call the credentials callback for this transport
233 * @param out the pointer where the creds are to be stored
234 * @param transport a smart transport
235 * @param user the user we saw on the url (if any)
236 * @param methods available methods for authentication
237 * @return the return value of the callback
239 GIT_EXTERN(int) git_transport_smart_credentials(git_cred
**out
, git_transport
*transport
, const char *user
, int methods
);
242 *** End of base transport interface ***
243 *** Begin interface for subtransports for the smart transport ***
246 /* The smart transport knows how to speak the git protocol, but it has no
247 * knowledge of how to establish a connection between it and another endpoint,
248 * or how to move data back and forth. For this, a subtransport interface is
249 * declared, and the smart transport delegates this work to the subtransports.
250 * Three subtransports are implemented: git, http, and winhttp. (The http and
251 * winhttp transports each implement both http and https.) */
253 /* Subtransports can either be RPC = 0 (persistent connection) or RPC = 1
254 * (request/response). The smart transport handles the differences in its own
255 * logic. The git subtransport is RPC = 0, while http and winhttp are both
258 /* Actions that the smart transport can ask
259 * a subtransport to perform */
261 GIT_SERVICE_UPLOADPACK_LS
= 1,
262 GIT_SERVICE_UPLOADPACK
= 2,
263 GIT_SERVICE_RECEIVEPACK_LS
= 3,
264 GIT_SERVICE_RECEIVEPACK
= 4,
265 } git_smart_service_t
;
267 typedef struct git_smart_subtransport git_smart_subtransport
;
268 typedef struct git_smart_subtransport_stream git_smart_subtransport_stream
;
270 /* A stream used by the smart transport to read and write data
271 * from a subtransport */
272 struct git_smart_subtransport_stream
{
273 /* The owning subtransport */
274 git_smart_subtransport
*subtransport
;
277 git_smart_subtransport_stream
*stream
,
283 git_smart_subtransport_stream
*stream
,
288 git_smart_subtransport_stream
*stream
);
291 /* An implementation of a subtransport which carries data for the
293 struct git_smart_subtransport
{
295 git_smart_subtransport_stream
**out
,
296 git_smart_subtransport
*transport
,
298 git_smart_service_t action
);
300 /* Subtransports are guaranteed a call to close() between
301 * calls to action(), except for the following two "natural" progressions
302 * of actions against a constant URL.
304 * 1. UPLOADPACK_LS -> UPLOADPACK
305 * 2. RECEIVEPACK_LS -> RECEIVEPACK */
306 int (*close
)(git_smart_subtransport
*transport
);
308 void (*free
)(git_smart_subtransport
*transport
);
311 /* A function which creates a new subtransport for the smart transport */
312 typedef int (*git_smart_subtransport_cb
)(
313 git_smart_subtransport
**out
,
314 git_transport
* owner
,
318 * Definition for a "subtransport"
320 * This is used to let the smart protocol code know about the protocol
321 * which you are implementing.
323 typedef struct git_smart_subtransport_definition
{
324 /** The function to use to create the git_smart_subtransport */
325 git_smart_subtransport_cb callback
;
328 * True if the protocol is stateless; false otherwise. For example,
329 * http:// is stateless, but git:// is not.
333 /** Param of the callback
336 } git_smart_subtransport_definition
;
338 /* Smart transport subtransports that come with libgit2 */
341 * Create an instance of the http subtransport. This subtransport
342 * also supports https. On Win32, this subtransport may be implemented
343 * using the WinHTTP library.
345 * @param out The newly created subtransport
346 * @param owner The smart transport to own this subtransport
347 * @return 0 or an error code
349 GIT_EXTERN(int) git_smart_subtransport_http(
350 git_smart_subtransport
**out
,
351 git_transport
* owner
,
355 * Create an instance of the git subtransport.
357 * @param out The newly created subtransport
358 * @param owner The smart transport to own this subtransport
359 * @return 0 or an error code
361 GIT_EXTERN(int) git_smart_subtransport_git(
362 git_smart_subtransport
**out
,
363 git_transport
* owner
,
367 * Create an instance of the ssh subtransport.
369 * @param out The newly created subtransport
370 * @param owner The smart transport to own this subtransport
371 * @return 0 or an error code
373 GIT_EXTERN(int) git_smart_subtransport_ssh(
374 git_smart_subtransport
**out
,
375 git_transport
* owner
,