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