]> git.proxmox.com Git - libgit2.git/blob - include/git2/odb.h
Merge pull request #1048 from pwkelley/basic_auth
[libgit2.git] / include / git2 / odb.h
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
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.
6 */
7 #ifndef INCLUDE_git_odb_h__
8 #define INCLUDE_git_odb_h__
9
10 #include "common.h"
11 #include "types.h"
12 #include "oid.h"
13 #include "odb_backend.h"
14 #include "indexer.h"
15
16 /**
17 * @file git2/odb.h
18 * @brief Git object database routines
19 * @defgroup git_odb Git object database routines
20 * @ingroup Git
21 * @{
22 */
23 GIT_BEGIN_DECL
24
25 /**
26 * Create a new object database with no backends.
27 *
28 * Before the ODB can be used for read/writing, a custom database
29 * backend must be manually added using `git_odb_add_backend()`
30 *
31 * @param out location to store the database pointer, if opened.
32 * Set to NULL if the open failed.
33 * @return 0 or an error code
34 */
35 GIT_EXTERN(int) git_odb_new(git_odb **out);
36
37 /**
38 * Create a new object database and automatically add
39 * the two default backends:
40 *
41 * - git_odb_backend_loose: read and write loose object files
42 * from disk, assuming `objects_dir` as the Objects folder
43 *
44 * - git_odb_backend_pack: read objects from packfiles,
45 * assuming `objects_dir` as the Objects folder which
46 * contains a 'pack/' folder with the corresponding data
47 *
48 * @param out location to store the database pointer, if opened.
49 * Set to NULL if the open failed.
50 * @param objects_dir path of the backends' "objects" directory.
51 * @return 0 or an error code
52 */
53 GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
54
55 /**
56 * Add a custom backend to an existing Object DB
57 *
58 * The backends are checked in relative ordering, based on the
59 * value of the `priority` parameter.
60 *
61 * Read <odb_backends.h> for more information.
62 *
63 * @param odb database to add the backend to
64 * @param backend pointer to a git_odb_backend instance
65 * @param priority Value for ordering the backends queue
66 * @return 0 on success; error code otherwise
67 */
68 GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority);
69
70 /**
71 * Add a custom backend to an existing Object DB; this
72 * backend will work as an alternate.
73 *
74 * Alternate backends are always checked for objects *after*
75 * all the main backends have been exhausted.
76 *
77 * The backends are checked in relative ordering, based on the
78 * value of the `priority` parameter.
79 *
80 * Writing is disabled on alternate backends.
81 *
82 * Read <odb_backends.h> for more information.
83 *
84 * @param odb database to add the backend to
85 * @param backend pointer to a git_odb_backend instance
86 * @param priority Value for ordering the backends queue
87 * @return 0 on success; error code otherwise
88 */
89 GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority);
90
91 /**
92 * Close an open object database.
93 *
94 * @param db database pointer to close. If NULL no action is taken.
95 */
96 GIT_EXTERN(void) git_odb_free(git_odb *db);
97
98 /**
99 * Read an object from the database.
100 *
101 * This method queries all available ODB backends
102 * trying to read the given OID.
103 *
104 * The returned object is reference counted and
105 * internally cached, so it should be closed
106 * by the user once it's no longer in use.
107 *
108 * @param out pointer where to store the read object
109 * @param db database to search for the object in.
110 * @param id identity of the object to read.
111 * @return
112 * - 0 if the object was read;
113 * - GIT_ENOTFOUND if the object is not in the database.
114 */
115 GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id);
116
117 /**
118 * Read an object from the database, given a prefix
119 * of its identifier.
120 *
121 * This method queries all available ODB backends
122 * trying to match the 'len' first hexadecimal
123 * characters of the 'short_id'.
124 * The remaining (GIT_OID_HEXSZ-len)*4 bits of
125 * 'short_id' must be 0s.
126 * 'len' must be at least GIT_OID_MINPREFIXLEN,
127 * and the prefix must be long enough to identify
128 * a unique object in all the backends; the
129 * method will fail otherwise.
130 *
131 * The returned object is reference counted and
132 * internally cached, so it should be closed
133 * by the user once it's no longer in use.
134 *
135 * @param out pointer where to store the read object
136 * @param db database to search for the object in.
137 * @param short_id a prefix of the id of the object to read.
138 * @param len the length of the prefix
139 * @return 0 if the object was read;
140 * GIT_ENOTFOUND if the object is not in the database.
141 * GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix)
142 */
143 GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len);
144
145 /**
146 * Read the header of an object from the database, without
147 * reading its full contents.
148 *
149 * The header includes the length and the type of an object.
150 *
151 * Note that most backends do not support reading only the header
152 * of an object, so the whole object will be read and then the
153 * header will be returned.
154 *
155 * @param len_p pointer where to store the length
156 * @param type_p pointer where to store the type
157 * @param db database to search for the object in.
158 * @param id identity of the object to read.
159 * @return
160 * - 0 if the object was read;
161 * - GIT_ENOTFOUND if the object is not in the database.
162 */
163 GIT_EXTERN(int) git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id);
164
165 /**
166 * Determine if the given object can be found in the object database.
167 *
168 * @param db database to be searched for the given object.
169 * @param id the object to search for.
170 * @return
171 * - 1, if the object was found
172 * - 0, otherwise
173 */
174 GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
175
176 /**
177 * List all objects available in the database
178 *
179 * The callback will be called for each object available in the
180 * database. Note that the objects are likely to be returned in the index
181 * order, which would make accessing the objects in that order inefficient.
182 * Return a non-zero value from the callback to stop looping.
183 *
184 * @param db database to use
185 * @param cb the callback to call for each object
186 * @param data data to pass to the callback
187 * @return 0 on success, GIT_EUSER on non-zero callback, or error code
188 */
189 GIT_EXTERN(int) git_odb_foreach(git_odb *db, int (*cb)(git_oid *oid, void *data), void *data);
190
191 /**
192 * Write an object directly into the ODB
193 *
194 * This method writes a full object straight into the ODB.
195 * For most cases, it is preferred to write objects through a write
196 * stream, which is both faster and less memory intensive, specially
197 * for big objects.
198 *
199 * This method is provided for compatibility with custom backends
200 * which are not able to support streaming writes
201 *
202 * @param oid pointer to store the OID result of the write
203 * @param odb object database where to store the object
204 * @param data buffer with the data to store
205 * @param len size of the buffer
206 * @param type type of the data to store
207 * @return 0 or an error code
208 */
209 GIT_EXTERN(int) git_odb_write(git_oid *oid, git_odb *odb, const void *data, size_t len, git_otype type);
210
211 /**
212 * Open a stream to write an object into the ODB
213 *
214 * The type and final length of the object must be specified
215 * when opening the stream.
216 *
217 * The returned stream will be of type `GIT_STREAM_WRONLY` and
218 * will have the following methods:
219 *
220 * - stream->write: write `n` bytes into the stream
221 * - stream->finalize_write: close the stream and store the object in
222 * the odb
223 * - stream->free: free the stream
224 *
225 * The streaming write won't be effective until `stream->finalize_write`
226 * is called and returns without an error
227 *
228 * The stream must always be free'd or will leak memory.
229 *
230 * @see git_odb_stream
231 *
232 * @param stream pointer where to store the stream
233 * @param db object database where the stream will write
234 * @param size final size of the object that will be written
235 * @param type type of the object that will be written
236 * @return 0 if the stream was created; error code otherwise
237 */
238 GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_t size, git_otype type);
239
240 /**
241 * Open a stream to read an object from the ODB
242 *
243 * Note that most backends do *not* support streaming reads
244 * because they store their objects as compressed/delta'ed blobs.
245 *
246 * It's recommended to use `git_odb_read` instead, which is
247 * assured to work on all backends.
248 *
249 * The returned stream will be of type `GIT_STREAM_RDONLY` and
250 * will have the following methods:
251 *
252 * - stream->read: read `n` bytes from the stream
253 * - stream->free: free the stream
254 *
255 * The stream must always be free'd or will leak memory.
256 *
257 * @see git_odb_stream
258 *
259 * @param stream pointer where to store the stream
260 * @param db object database where the stream will read from
261 * @param oid oid of the object the stream will read from
262 * @return 0 if the stream was created; error code otherwise
263 */
264 GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid);
265
266 /**
267 * Open a stream for writing a pack file to the ODB.
268 *
269 * If the ODB layer understands pack files, then the given
270 * packfile will likely be streamed directly to disk (and a
271 * corresponding index created). If the ODB layer does not
272 * understand pack files, the objects will be stored in whatever
273 * format the ODB layer uses.
274 *
275 * @see git_odb_writepack
276 *
277 * @param writepack pointer to the writepack functions
278 * @param db object database where the stream will read from
279 * @param progress_cb function to call with progress information.
280 * Be aware that this is called inline with network and indexing operations,
281 * so performance may be affected.
282 * @param progress_payload payload for the progress callback
283 */
284 GIT_EXTERN(int) git_odb_write_pack(git_odb_writepack **writepack, git_odb *db, git_transfer_progress_callback progress_cb, void *progress_payload);
285
286 /**
287 * Determine the object-ID (sha1 hash) of a data buffer
288 *
289 * The resulting SHA-1 OID will be the identifier for the data
290 * buffer as if the data buffer it were to written to the ODB.
291 *
292 * @param id the resulting object-ID.
293 * @param data data to hash
294 * @param len size of the data
295 * @param type of the data to hash
296 * @return 0 or an error code
297 */
298 GIT_EXTERN(int) git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type);
299
300 /**
301 * Read a file from disk and fill a git_oid with the object id
302 * that the file would have if it were written to the Object
303 * Database as an object of the given type (w/o applying filters).
304 * Similar functionality to git.git's `git hash-object` without
305 * the `-w` flag, however, with the --no-filters flag.
306 * If you need filters, see git_repository_hashfile.
307 *
308 * @param out oid structure the result is written into.
309 * @param path file to read and determine object id for
310 * @param type the type of the object that will be hashed
311 * @return 0 or an error code
312 */
313 GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_otype type);
314
315 /**
316 * Close an ODB object
317 *
318 * This method must always be called once a `git_odb_object` is no
319 * longer needed, otherwise memory will leak.
320 *
321 * @param object object to close
322 */
323 GIT_EXTERN(void) git_odb_object_free(git_odb_object *object);
324
325 /**
326 * Return the OID of an ODB object
327 *
328 * This is the OID from which the object was read from
329 *
330 * @param object the object
331 * @return a pointer to the OID
332 */
333 GIT_EXTERN(const git_oid *) git_odb_object_id(git_odb_object *object);
334
335 /**
336 * Return the data of an ODB object
337 *
338 * This is the uncompressed, raw data as read from the ODB,
339 * without the leading header.
340 *
341 * This pointer is owned by the object and shall not be free'd.
342 *
343 * @param object the object
344 * @return a pointer to the data
345 */
346 GIT_EXTERN(const void *) git_odb_object_data(git_odb_object *object);
347
348 /**
349 * Return the size of an ODB object
350 *
351 * This is the real size of the `data` buffer, not the
352 * actual size of the object.
353 *
354 * @param object the object
355 * @return the size
356 */
357 GIT_EXTERN(size_t) git_odb_object_size(git_odb_object *object);
358
359 /**
360 * Return the type of an ODB object
361 *
362 * @param object the object
363 * @return the type
364 */
365 GIT_EXTERN(git_otype) git_odb_object_type(git_odb_object *object);
366
367 /** @} */
368 GIT_END_DECL
369 #endif