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.
7 #ifndef INCLUDE_git_odb_h__
8 #define INCLUDE_git_odb_h__
18 * @brief Git object database routines
19 * @defgroup git_odb Git object database routines
25 /** Flags controlling the behavior of ODB lookup operations */
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.
33 GIT_ODB_LOOKUP_NO_REFRESH
= (1 << 0)
34 } git_odb_lookup_flags_t
;
37 * Function type for callbacks from git_odb_foreach.
39 typedef int GIT_CALLBACK(git_odb_foreach_cb
)(const git_oid
*id
, void *payload
);
42 * Create a new object database with no backends.
44 * Before the ODB can be used for read/writing, a custom database
45 * backend must be manually added using `git_odb_add_backend()`
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
51 GIT_EXTERN(int) git_odb_new(git_odb
**out
);
54 * Create a new object database and automatically add
55 * the two default backends:
57 * - git_odb_backend_loose: read and write loose object files
58 * from disk, assuming `objects_dir` as the Objects folder
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
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
69 GIT_EXTERN(int) git_odb_open(git_odb
**out
, const char *objects_dir
);
72 * Add an on-disk alternate to an existing Object DB.
74 * Note that the added path must point to an `objects`, not
75 * to a full repository, to use it as an alternate store.
77 * Alternate backends are always checked for objects *after*
78 * all the main backends have been exhausted.
80 * Writing is disabled on alternate backends.
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
86 GIT_EXTERN(int) git_odb_add_disk_alternate(git_odb
*odb
, const char *path
);
89 * Close an open object database.
91 * @param db database pointer to close. If NULL no action is taken.
93 GIT_EXTERN(void) git_odb_free(git_odb
*db
);
96 * Read an object from the database.
98 * This method queries all available ODB backends
99 * trying to read the given OID.
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.
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.
111 GIT_EXTERN(int) git_odb_read(git_odb_object
**out
, git_odb
*db
, const git_oid
*id
);
114 * Read an object from the database, given a prefix
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.
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.
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)
139 GIT_EXTERN(int) git_odb_read_prefix(git_odb_object
**out
, git_odb
*db
, const git_oid
*short_id
, size_t len
);
142 * Read the header of an object from the database, without
143 * reading its full contents.
145 * The header includes the length and the type of an object.
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.
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
158 GIT_EXTERN(int) git_odb_read_header(size_t *len_out
, git_object_t
*type_out
, git_odb
*db
, const git_oid
*id
);
161 * Determine if the given object can be found in the object database.
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
167 GIT_EXTERN(int) git_odb_exists(git_odb
*db
, const git_oid
*id
);
170 * Determine if the given object can be found in the object database, with
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
178 GIT_EXTERN(int) git_odb_exists_ext(git_odb
*db
, const git_oid
*id
, unsigned int flags
);
181 * Determine if an object can be found in the object database by an
182 * abbreviated object ID.
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.
191 GIT_EXTERN(int) git_odb_exists_prefix(
192 git_oid
*out
, git_odb
*db
, const git_oid
*short_id
, size_t len
);
195 * The information about object IDs to query in `git_odb_expand_ids`,
196 * which will be populated upon return.
198 typedef struct git_odb_expand_id
{
199 /** The object ID to expand */
203 * The length of the object ID (in nibbles, or packets of 4 bits; the
204 * number of hex characters)
206 unsigned short length
;
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.
216 * Determine if one or more objects can be found in the object database
217 * by their abbreviated object ID and type.
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.
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.)
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
234 GIT_EXTERN(int) git_odb_expand_ids(
236 git_odb_expand_id
*ids
,
240 * Refresh the object database to load newly added files.
242 * If the object databases have changed on disk while the library
243 * is running, this function will force a reload of the underlying
246 * Use this function when you're confident that an external
247 * application has tampered with the ODB.
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.
254 * @param db database to refresh
255 * @return 0 on success, error code otherwise
257 GIT_EXTERN(int) git_odb_refresh(struct git_odb
*db
);
260 * List all objects available in the database
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.
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
272 GIT_EXTERN(int) git_odb_foreach(git_odb
*db
, git_odb_foreach_cb cb
, void *payload
);
275 * Write an object directly into the ODB
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
282 * This method is provided for compatibility with custom backends
283 * which are not able to support streaming writes
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
292 GIT_EXTERN(int) git_odb_write(git_oid
*out
, git_odb
*odb
, const void *data
, size_t len
, git_object_t type
);
295 * Open a stream to write an object into the ODB
297 * The type and final length of the object must be specified
298 * when opening the stream.
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
304 * The stream must always be freed when done with `git_odb_stream_free` or
307 * @see git_odb_stream
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
315 GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream
**out
, git_odb
*db
, git_object_size_t size
, git_object_t type
);
318 * Write to an odb stream
320 * This method will fail if the total number of received bytes exceeds the
321 * size declared with `git_odb_open_wstream()`
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
328 GIT_EXTERN(int) git_odb_stream_write(git_odb_stream
*stream
, const char *buffer
, size_t len
);
331 * Finish writing to an odb stream
333 * The object will take its final name and will be available to the
336 * This method will fail if the total number of received bytes
337 * differs from the size declared with `git_odb_open_wstream()`
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
343 GIT_EXTERN(int) git_odb_stream_finalize_write(git_oid
*out
, git_odb_stream
*stream
);
346 * Read from an odb stream
348 * Most backends don't implement streaming reads
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
355 GIT_EXTERN(int) git_odb_stream_read(git_odb_stream
*stream
, char *buffer
, size_t len
);
360 * @param stream the stream to free
362 GIT_EXTERN(void) git_odb_stream_free(git_odb_stream
*stream
);
365 * Open a stream to read an object from the ODB
367 * Note that most backends do *not* support streaming reads
368 * because they store their objects as compressed/delta'ed blobs.
370 * It's recommended to use `git_odb_read` instead, which is
371 * assured to work on all backends.
373 * The returned stream will be of type `GIT_STREAM_RDONLY` and
374 * will have the following methods:
376 * - stream->read: read `n` bytes from the stream
377 * - stream->free: free the stream
379 * The stream must always be free'd or will leak memory.
381 * @see git_odb_stream
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
390 GIT_EXTERN(int) git_odb_open_rstream(
391 git_odb_stream
**out
,
398 * Open a stream for writing a pack file to the ODB.
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.
406 * @see git_odb_writepack
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.
416 GIT_EXTERN(int) git_odb_write_pack(
417 git_odb_writepack
**out
,
419 git_indexer_progress_cb progress_cb
,
420 void *progress_payload
);
423 * Write a `multi-pack-index` file from all the `.pack` files in the ODB.
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
431 * @param db object database where the `multi-pack-index` file will be written.
432 * @return 0 or an error code.
434 GIT_EXTERN(int) git_odb_write_multi_pack_index(
438 * Determine the object-ID (sha1 hash) of a data buffer
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.
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
449 GIT_EXTERN(int) git_odb_hash(git_oid
*out
, const void *data
, size_t len
, git_object_t type
);
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.
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
464 GIT_EXTERN(int) git_odb_hashfile(git_oid
*out
, const char *path
, git_object_t type
);
467 * Create a copy of an odb_object
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.
474 * @param dest pointer where to store the copy
475 * @param source object to copy
476 * @return 0 or an error code
478 GIT_EXTERN(int) git_odb_object_dup(git_odb_object
**dest
, git_odb_object
*source
);
481 * Close an ODB object
483 * This method must always be called once a `git_odb_object` is no
484 * longer needed, otherwise memory will leak.
486 * @param object object to close
488 GIT_EXTERN(void) git_odb_object_free(git_odb_object
*object
);
491 * Return the OID of an ODB object
493 * This is the OID from which the object was read from
495 * @param object the object
496 * @return a pointer to the OID
498 GIT_EXTERN(const git_oid
*) git_odb_object_id(git_odb_object
*object
);
501 * Return the data of an ODB object
503 * This is the uncompressed, raw data as read from the ODB,
504 * without the leading header.
506 * This pointer is owned by the object and shall not be free'd.
508 * @param object the object
509 * @return a pointer to the data
511 GIT_EXTERN(const void *) git_odb_object_data(git_odb_object
*object
);
514 * Return the size of an ODB object
516 * This is the real size of the `data` buffer, not the
517 * actual size of the object.
519 * @param object the object
522 GIT_EXTERN(size_t) git_odb_object_size(git_odb_object
*object
);
525 * Return the type of an ODB object
527 * @param object the object
530 GIT_EXTERN(git_object_t
) git_odb_object_type(git_odb_object
*object
);
533 * Add a custom backend to an existing Object DB
535 * The backends are checked in relative ordering, based on the
536 * value of the `priority` parameter.
538 * Read <sys/odb_backend.h> for more information.
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
545 GIT_EXTERN(int) git_odb_add_backend(git_odb
*odb
, git_odb_backend
*backend
, int priority
);
548 * Add a custom backend to an existing Object DB; this
549 * backend will work as an alternate.
551 * Alternate backends are always checked for objects *after*
552 * all the main backends have been exhausted.
554 * The backends are checked in relative ordering, based on the
555 * value of the `priority` parameter.
557 * Writing is disabled on alternate backends.
559 * Read <sys/odb_backend.h> for more information.
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
566 GIT_EXTERN(int) git_odb_add_alternate(git_odb
*odb
, git_odb_backend
*backend
, int priority
);
569 * Get the number of ODB backend objects
571 * @param odb object database
572 * @return number of backends in the ODB
574 GIT_EXTERN(size_t) git_odb_num_backends(git_odb
*odb
);
577 * Lookup an ODB backend object by index
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
584 GIT_EXTERN(int) git_odb_get_backend(git_odb_backend
**out
, git_odb
*odb
, size_t pos
);
587 * Set the git commit-graph for the ODB.
589 * After a successful call, the ownership of the cgraph parameter will be
590 * transferred to libgit2, and the caller should not free it.
592 * The commit-graph can also be unset by explicitly passing NULL as the cgraph
595 * @param odb object database
596 * @param cgraph the git commit-graph
597 * @return 0 on success; error code otherwise
599 GIT_EXTERN(int) git_odb_set_commit_graph(git_odb
*odb
, git_commit_graph
*cgraph
);