]> git.proxmox.com Git - libgit2.git/blob - include/git2/odb.h
Imported Upstream version 0.26.0
[libgit2.git] / include / git2 / odb.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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 "oidarray.h"
14
15 /**
16 * @file git2/odb.h
17 * @brief Git object database routines
18 * @defgroup git_odb Git object database routines
19 * @ingroup Git
20 * @{
21 */
22 GIT_BEGIN_DECL
23
24 /**
25 * Function type for callbacks from git_odb_foreach.
26 */
27 typedef int (*git_odb_foreach_cb)(const git_oid *id, void *payload);
28
29 /**
30 * Create a new object database with no backends.
31 *
32 * Before the ODB can be used for read/writing, a custom database
33 * backend must be manually added using `git_odb_add_backend()`
34 *
35 * @param out location to store the database pointer, if opened.
36 * Set to NULL if the open failed.
37 * @return 0 or an error code
38 */
39 GIT_EXTERN(int) git_odb_new(git_odb **out);
40
41 /**
42 * Create a new object database and automatically add
43 * the two default backends:
44 *
45 * - git_odb_backend_loose: read and write loose object files
46 * from disk, assuming `objects_dir` as the Objects folder
47 *
48 * - git_odb_backend_pack: read objects from packfiles,
49 * assuming `objects_dir` as the Objects folder which
50 * contains a 'pack/' folder with the corresponding data
51 *
52 * @param out location to store the database pointer, if opened.
53 * Set to NULL if the open failed.
54 * @param objects_dir path of the backends' "objects" directory.
55 * @return 0 or an error code
56 */
57 GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
58
59 /**
60 * Add an on-disk alternate to an existing Object DB.
61 *
62 * Note that the added path must point to an `objects`, not
63 * to a full repository, to use it as an alternate store.
64 *
65 * Alternate backends are always checked for objects *after*
66 * all the main backends have been exhausted.
67 *
68 * Writing is disabled on alternate backends.
69 *
70 * @param odb database to add the backend to
71 * @param path path to the objects folder for the alternate
72 * @return 0 on success; error code otherwise
73 */
74 GIT_EXTERN(int) git_odb_add_disk_alternate(git_odb *odb, const char *path);
75
76 /**
77 * Close an open object database.
78 *
79 * @param db database pointer to close. If NULL no action is taken.
80 */
81 GIT_EXTERN(void) git_odb_free(git_odb *db);
82
83 /**
84 * Read an object from the database.
85 *
86 * This method queries all available ODB backends
87 * trying to read the given OID.
88 *
89 * The returned object is reference counted and
90 * internally cached, so it should be closed
91 * by the user once it's no longer in use.
92 *
93 * @param out pointer where to store the read object
94 * @param db database to search for the object in.
95 * @param id identity of the object to read.
96 * @return
97 * - 0 if the object was read;
98 * - GIT_ENOTFOUND if the object is not in the database.
99 */
100 GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id);
101
102 /**
103 * Read an object from the database, given a prefix
104 * of its identifier.
105 *
106 * This method queries all available ODB backends
107 * trying to match the 'len' first hexadecimal
108 * characters of the 'short_id'.
109 * The remaining (GIT_OID_HEXSZ-len)*4 bits of
110 * 'short_id' must be 0s.
111 * 'len' must be at least GIT_OID_MINPREFIXLEN,
112 * and the prefix must be long enough to identify
113 * a unique object in all the backends; the
114 * method will fail otherwise.
115 *
116 * The returned object is reference counted and
117 * internally cached, so it should be closed
118 * by the user once it's no longer in use.
119 *
120 * @param out pointer where to store the read object
121 * @param db database to search for the object in.
122 * @param short_id a prefix of the id of the object to read.
123 * @param len the length of the prefix
124 * @return
125 * - 0 if the object was read;
126 * - GIT_ENOTFOUND if the object is not in the database.
127 * - GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix)
128 */
129 GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len);
130
131 /**
132 * Read the header of an object from the database, without
133 * reading its full contents.
134 *
135 * The header includes the length and the type of an object.
136 *
137 * Note that most backends do not support reading only the header
138 * of an object, so the whole object will be read and then the
139 * header will be returned.
140 *
141 * @param len_out pointer where to store the length
142 * @param type_out pointer where to store the type
143 * @param db database to search for the object in.
144 * @param id identity of the object to read.
145 * @return
146 * - 0 if the object was read;
147 * - GIT_ENOTFOUND if the object is not in the database.
148 */
149 GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_otype *type_out, git_odb *db, const git_oid *id);
150
151 /**
152 * Determine if the given object can be found in the object database.
153 *
154 * @param db database to be searched for the given object.
155 * @param id the object to search for.
156 * @return
157 * - 1, if the object was found
158 * - 0, otherwise
159 */
160 GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
161
162 /**
163 * Determine if an object can be found in the object database by an
164 * abbreviated object ID.
165 *
166 * @param out The full OID of the found object if just one is found.
167 * @param db The database to be searched for the given object.
168 * @param short_id A prefix of the id of the object to read.
169 * @param len The length of the prefix.
170 * @return 0 if found, GIT_ENOTFOUND if not found, GIT_EAMBIGUOUS if multiple
171 * matches were found, other value < 0 if there was a read error.
172 */
173 GIT_EXTERN(int) git_odb_exists_prefix(
174 git_oid *out, git_odb *db, const git_oid *short_id, size_t len);
175
176 /**
177 * The information about object IDs to query in `git_odb_expand_ids`,
178 * which will be populated upon return.
179 */
180 typedef struct git_odb_expand_id {
181 /** The object ID to expand */
182 git_oid id;
183
184 /**
185 * The length of the object ID (in nibbles, or packets of 4 bits; the
186 * number of hex characters)
187 * */
188 unsigned short length;
189
190 /**
191 * The (optional) type of the object to search for; leave as `0` or set
192 * to `GIT_OBJ_ANY` to query for any object matching the ID.
193 */
194 git_otype type;
195 } git_odb_expand_id;
196
197 /**
198 * Determine if one or more objects can be found in the object database
199 * by their abbreviated object ID and type. The given array will be
200 * updated in place: for each abbreviated ID that is unique in the
201 * database, and of the given type (if specified), the full object ID,
202 * object ID length (`GIT_OID_HEXSZ`) and type will be written back to
203 * the array. For IDs that are not found (or are ambiguous), the
204 * array entry will be zeroed.
205 *
206 * Note that since this function operates on multiple objects, the
207 * underlying database will not be asked to be reloaded if an object is
208 * not found (which is unlike other object database operations.)
209 *
210 * @param db The database to be searched for the given objects.
211 * @param ids An array of short object IDs to search for
212 * @param count The length of the `ids` array
213 * @return 0 on success or an error code on failure
214 */
215 GIT_EXTERN(int) git_odb_expand_ids(
216 git_odb *db,
217 git_odb_expand_id *ids,
218 size_t count);
219
220 /**
221 * Refresh the object database to load newly added files.
222 *
223 * If the object databases have changed on disk while the library
224 * is running, this function will force a reload of the underlying
225 * indexes.
226 *
227 * Use this function when you're confident that an external
228 * application has tampered with the ODB.
229 *
230 * NOTE that it is not necessary to call this function at all. The
231 * library will automatically attempt to refresh the ODB
232 * when a lookup fails, to see if the looked up object exists
233 * on disk but hasn't been loaded yet.
234 *
235 * @param db database to refresh
236 * @return 0 on success, error code otherwise
237 */
238 GIT_EXTERN(int) git_odb_refresh(struct git_odb *db);
239
240 /**
241 * List all objects available in the database
242 *
243 * The callback will be called for each object available in the
244 * database. Note that the objects are likely to be returned in the index
245 * order, which would make accessing the objects in that order inefficient.
246 * Return a non-zero value from the callback to stop looping.
247 *
248 * @param db database to use
249 * @param cb the callback to call for each object
250 * @param payload data to pass to the callback
251 * @return 0 on success, non-zero callback return value, or error code
252 */
253 GIT_EXTERN(int) git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload);
254
255 /**
256 * Write an object directly into the ODB
257 *
258 * This method writes a full object straight into the ODB.
259 * For most cases, it is preferred to write objects through a write
260 * stream, which is both faster and less memory intensive, specially
261 * for big objects.
262 *
263 * This method is provided for compatibility with custom backends
264 * which are not able to support streaming writes
265 *
266 * @param out pointer to store the OID result of the write
267 * @param odb object database where to store the object
268 * @param data buffer with the data to store
269 * @param len size of the buffer
270 * @param type type of the data to store
271 * @return 0 or an error code
272 */
273 GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size_t len, git_otype type);
274
275 /**
276 * Open a stream to write an object into the ODB
277 *
278 * The type and final length of the object must be specified
279 * when opening the stream.
280 *
281 * The returned stream will be of type `GIT_STREAM_WRONLY`, and it
282 * won't be effective until `git_odb_stream_finalize_write` is called
283 * and returns without an error
284 *
285 * The stream must always be freed when done with `git_odb_stream_free` or
286 * will leak memory.
287 *
288 * @see git_odb_stream
289 *
290 * @param out pointer where to store the stream
291 * @param db object database where the stream will write
292 * @param size final size of the object that will be written
293 * @param type type of the object that will be written
294 * @return 0 if the stream was created; error code otherwise
295 */
296 GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_off_t size, git_otype type);
297
298 /**
299 * Write to an odb stream
300 *
301 * This method will fail if the total number of received bytes exceeds the
302 * size declared with `git_odb_open_wstream()`
303 *
304 * @param stream the stream
305 * @param buffer the data to write
306 * @param len the buffer's length
307 * @return 0 if the write succeeded; error code otherwise
308 */
309 GIT_EXTERN(int) git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len);
310
311 /**
312 * Finish writing to an odb stream
313 *
314 * The object will take its final name and will be available to the
315 * odb.
316 *
317 * This method will fail if the total number of received bytes
318 * differs from the size declared with `git_odb_open_wstream()`
319 *
320 * @param out pointer to store the resulting object's id
321 * @param stream the stream
322 * @return 0 on success; an error code otherwise
323 */
324 GIT_EXTERN(int) git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream);
325
326 /**
327 * Read from an odb stream
328 *
329 * Most backends don't implement streaming reads
330 */
331 GIT_EXTERN(int) git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len);
332
333 /**
334 * Free an odb stream
335 *
336 * @param stream the stream to free
337 */
338 GIT_EXTERN(void) git_odb_stream_free(git_odb_stream *stream);
339
340 /**
341 * Open a stream to read an object from the ODB
342 *
343 * Note that most backends do *not* support streaming reads
344 * because they store their objects as compressed/delta'ed blobs.
345 *
346 * It's recommended to use `git_odb_read` instead, which is
347 * assured to work on all backends.
348 *
349 * The returned stream will be of type `GIT_STREAM_RDONLY` and
350 * will have the following methods:
351 *
352 * - stream->read: read `n` bytes from the stream
353 * - stream->free: free the stream
354 *
355 * The stream must always be free'd or will leak memory.
356 *
357 * @see git_odb_stream
358 *
359 * @param out pointer where to store the stream
360 * @param db object database where the stream will read from
361 * @param oid oid of the object the stream will read from
362 * @return 0 if the stream was created; error code otherwise
363 */
364 GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **out, git_odb *db, const git_oid *oid);
365
366 /**
367 * Open a stream for writing a pack file to the ODB.
368 *
369 * If the ODB layer understands pack files, then the given
370 * packfile will likely be streamed directly to disk (and a
371 * corresponding index created). If the ODB layer does not
372 * understand pack files, the objects will be stored in whatever
373 * format the ODB layer uses.
374 *
375 * @see git_odb_writepack
376 *
377 * @param out pointer to the writepack functions
378 * @param db object database where the stream will read from
379 * @param progress_cb function to call with progress information.
380 * Be aware that this is called inline with network and indexing operations,
381 * so performance may be affected.
382 * @param progress_payload payload for the progress callback
383 */
384 GIT_EXTERN(int) git_odb_write_pack(
385 git_odb_writepack **out,
386 git_odb *db,
387 git_transfer_progress_cb progress_cb,
388 void *progress_payload);
389
390 /**
391 * Determine the object-ID (sha1 hash) of a data buffer
392 *
393 * The resulting SHA-1 OID will be the identifier for the data
394 * buffer as if the data buffer it were to written to the ODB.
395 *
396 * @param out the resulting object-ID.
397 * @param data data to hash
398 * @param len size of the data
399 * @param type of the data to hash
400 * @return 0 or an error code
401 */
402 GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_otype type);
403
404 /**
405 * Read a file from disk and fill a git_oid with the object id
406 * that the file would have if it were written to the Object
407 * Database as an object of the given type (w/o applying filters).
408 * Similar functionality to git.git's `git hash-object` without
409 * the `-w` flag, however, with the --no-filters flag.
410 * If you need filters, see git_repository_hashfile.
411 *
412 * @param out oid structure the result is written into.
413 * @param path file to read and determine object id for
414 * @param type the type of the object that will be hashed
415 * @return 0 or an error code
416 */
417 GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_otype type);
418
419 /**
420 * Create a copy of an odb_object
421 *
422 * The returned copy must be manually freed with `git_odb_object_free`.
423 * Note that because of an implementation detail, the returned copy will be
424 * the same pointer as `source`: the object is internally refcounted, so the
425 * copy still needs to be freed twice.
426 *
427 * @param dest pointer where to store the copy
428 * @param source object to copy
429 * @return 0 or an error code
430 */
431 GIT_EXTERN(int) git_odb_object_dup(git_odb_object **dest, git_odb_object *source);
432
433 /**
434 * Close an ODB object
435 *
436 * This method must always be called once a `git_odb_object` is no
437 * longer needed, otherwise memory will leak.
438 *
439 * @param object object to close
440 */
441 GIT_EXTERN(void) git_odb_object_free(git_odb_object *object);
442
443 /**
444 * Return the OID of an ODB object
445 *
446 * This is the OID from which the object was read from
447 *
448 * @param object the object
449 * @return a pointer to the OID
450 */
451 GIT_EXTERN(const git_oid *) git_odb_object_id(git_odb_object *object);
452
453 /**
454 * Return the data of an ODB object
455 *
456 * This is the uncompressed, raw data as read from the ODB,
457 * without the leading header.
458 *
459 * This pointer is owned by the object and shall not be free'd.
460 *
461 * @param object the object
462 * @return a pointer to the data
463 */
464 GIT_EXTERN(const void *) git_odb_object_data(git_odb_object *object);
465
466 /**
467 * Return the size of an ODB object
468 *
469 * This is the real size of the `data` buffer, not the
470 * actual size of the object.
471 *
472 * @param object the object
473 * @return the size
474 */
475 GIT_EXTERN(size_t) git_odb_object_size(git_odb_object *object);
476
477 /**
478 * Return the type of an ODB object
479 *
480 * @param object the object
481 * @return the type
482 */
483 GIT_EXTERN(git_otype) git_odb_object_type(git_odb_object *object);
484
485 /**
486 * Add a custom backend to an existing Object DB
487 *
488 * The backends are checked in relative ordering, based on the
489 * value of the `priority` parameter.
490 *
491 * Read <sys/odb_backend.h> for more information.
492 *
493 * @param odb database to add the backend to
494 * @param backend pointer to a git_odb_backend instance
495 * @param priority Value for ordering the backends queue
496 * @return 0 on success; error code otherwise
497 */
498 GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority);
499
500 /**
501 * Add a custom backend to an existing Object DB; this
502 * backend will work as an alternate.
503 *
504 * Alternate backends are always checked for objects *after*
505 * all the main backends have been exhausted.
506 *
507 * The backends are checked in relative ordering, based on the
508 * value of the `priority` parameter.
509 *
510 * Writing is disabled on alternate backends.
511 *
512 * Read <sys/odb_backend.h> for more information.
513 *
514 * @param odb database to add the backend to
515 * @param backend pointer to a git_odb_backend instance
516 * @param priority Value for ordering the backends queue
517 * @return 0 on success; error code otherwise
518 */
519 GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority);
520
521 /**
522 * Get the number of ODB backend objects
523 *
524 * @param odb object database
525 * @return number of backends in the ODB
526 */
527 GIT_EXTERN(size_t) git_odb_num_backends(git_odb *odb);
528
529 /**
530 * Lookup an ODB backend object by index
531 *
532 * @param out output pointer to ODB backend at pos
533 * @param odb object database
534 * @param pos index into object database backend list
535 * @return 0 on success; GIT_ENOTFOUND if pos is invalid; other errors < 0
536 */
537 GIT_EXTERN(int) git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos);
538
539 /** @} */
540 GIT_END_DECL
541 #endif