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__
16 * @brief Git object database routines
17 * @defgroup git_odb Git object database routines
24 * Function type for callbacks from git_odb_foreach.
26 typedef int (*git_odb_foreach_cb
)(const git_oid
*id
, void *payload
);
29 * Create a new object database with no backends.
31 * Before the ODB can be used for read/writing, a custom database
32 * backend must be manually added using `git_odb_add_backend()`
34 * @param out location to store the database pointer, if opened.
35 * Set to NULL if the open failed.
36 * @return 0 or an error code
38 GIT_EXTERN(int) git_odb_new(git_odb
**out
);
41 * Create a new object database and automatically add
42 * the two default backends:
44 * - git_odb_backend_loose: read and write loose object files
45 * from disk, assuming `objects_dir` as the Objects folder
47 * - git_odb_backend_pack: read objects from packfiles,
48 * assuming `objects_dir` as the Objects folder which
49 * contains a 'pack/' folder with the corresponding data
51 * @param out location to store the database pointer, if opened.
52 * Set to NULL if the open failed.
53 * @param objects_dir path of the backends' "objects" directory.
54 * @return 0 or an error code
56 GIT_EXTERN(int) git_odb_open(git_odb
**out
, const char *objects_dir
);
59 * Add an on-disk alternate to an existing Object DB.
61 * Note that the added path must point to an `objects`, not
62 * to a full repository, to use it as an alternate store.
64 * Alternate backends are always checked for objects *after*
65 * all the main backends have been exhausted.
67 * Writing is disabled on alternate backends.
69 * @param odb database to add the backend to
70 * @param path path to the objects folder for the alternate
71 * @return 0 on success; error code otherwise
73 GIT_EXTERN(int) git_odb_add_disk_alternate(git_odb
*odb
, const char *path
);
76 * Close an open object database.
78 * @param db database pointer to close. If NULL no action is taken.
80 GIT_EXTERN(void) git_odb_free(git_odb
*db
);
83 * Read an object from the database.
85 * This method queries all available ODB backends
86 * trying to read the given OID.
88 * The returned object is reference counted and
89 * internally cached, so it should be closed
90 * by the user once it's no longer in use.
92 * @param out pointer where to store the read object
93 * @param db database to search for the object in.
94 * @param id identity of the object to read.
96 * - 0 if the object was read;
97 * - GIT_ENOTFOUND if the object is not in the database.
99 GIT_EXTERN(int) git_odb_read(git_odb_object
**out
, git_odb
*db
, const git_oid
*id
);
102 * Read an object from the database, given a prefix
105 * This method queries all available ODB backends
106 * trying to match the 'len' first hexadecimal
107 * characters of the 'short_id'.
108 * The remaining (GIT_OID_HEXSZ-len)*4 bits of
109 * 'short_id' must be 0s.
110 * 'len' must be at least GIT_OID_MINPREFIXLEN,
111 * and the prefix must be long enough to identify
112 * a unique object in all the backends; the
113 * method will fail otherwise.
115 * The returned object is reference counted and
116 * internally cached, so it should be closed
117 * by the user once it's no longer in use.
119 * @param out pointer where to store the read object
120 * @param db database to search for the object in.
121 * @param short_id a prefix of the id of the object to read.
122 * @param len the length of the prefix
124 * - 0 if the object was read;
125 * - GIT_ENOTFOUND if the object is not in the database.
126 * - GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix)
128 GIT_EXTERN(int) git_odb_read_prefix(git_odb_object
**out
, git_odb
*db
, const git_oid
*short_id
, size_t len
);
131 * Read the header of an object from the database, without
132 * reading its full contents.
134 * The header includes the length and the type of an object.
136 * Note that most backends do not support reading only the header
137 * of an object, so the whole object will be read and then the
138 * header will be returned.
140 * @param len_out pointer where to store the length
141 * @param type_out pointer where to store the type
142 * @param db database to search for the object in.
143 * @param id identity of the object to read.
145 * - 0 if the object was read;
146 * - GIT_ENOTFOUND if the object is not in the database.
148 GIT_EXTERN(int) git_odb_read_header(size_t *len_out
, git_otype
*type_out
, git_odb
*db
, const git_oid
*id
);
151 * Determine if the given object can be found in the object database.
153 * @param db database to be searched for the given object.
154 * @param id the object to search for.
156 * - 1, if the object was found
159 GIT_EXTERN(int) git_odb_exists(git_odb
*db
, const git_oid
*id
);
162 * Determine if objects can be found in the object database from a short OID.
164 * @param out The full OID of the found object if just one is found.
165 * @param db The database to be searched for the given object.
166 * @param short_id A prefix of the id of the object to read.
167 * @param len The length of the prefix.
168 * @return 0 if found, GIT_ENOTFOUND if not found, GIT_EAMBIGUOUS if multiple
169 * matches were found, other value < 0 if there was a read error.
171 GIT_EXTERN(int) git_odb_exists_prefix(
172 git_oid
*out
, git_odb
*db
, const git_oid
*short_id
, size_t len
);
175 * Refresh the object database to load newly added files.
177 * If the object databases have changed on disk while the library
178 * is running, this function will force a reload of the underlying
181 * Use this function when you're confident that an external
182 * application has tampered with the ODB.
184 * NOTE that it is not necessary to call this function at all. The
185 * library will automatically attempt to refresh the ODB
186 * when a lookup fails, to see if the looked up object exists
187 * on disk but hasn't been loaded yet.
189 * @param db database to refresh
190 * @return 0 on success, error code otherwise
192 GIT_EXTERN(int) git_odb_refresh(struct git_odb
*db
);
195 * List all objects available in the database
197 * The callback will be called for each object available in the
198 * database. Note that the objects are likely to be returned in the index
199 * order, which would make accessing the objects in that order inefficient.
200 * Return a non-zero value from the callback to stop looping.
202 * @param db database to use
203 * @param cb the callback to call for each object
204 * @param payload data to pass to the callback
205 * @return 0 on success, non-zero callback return value, or error code
207 GIT_EXTERN(int) git_odb_foreach(git_odb
*db
, git_odb_foreach_cb cb
, void *payload
);
210 * Write an object directly into the ODB
212 * This method writes a full object straight into the ODB.
213 * For most cases, it is preferred to write objects through a write
214 * stream, which is both faster and less memory intensive, specially
217 * This method is provided for compatibility with custom backends
218 * which are not able to support streaming writes
220 * @param out pointer to store the OID result of the write
221 * @param odb object database where to store the object
222 * @param data buffer with the data to store
223 * @param len size of the buffer
224 * @param type type of the data to store
225 * @return 0 or an error code
227 GIT_EXTERN(int) git_odb_write(git_oid
*out
, git_odb
*odb
, const void *data
, size_t len
, git_otype type
);
230 * Open a stream to write an object into the ODB
232 * The type and final length of the object must be specified
233 * when opening the stream.
235 * The returned stream will be of type `GIT_STREAM_WRONLY`, and it
236 * won't be effective until `git_odb_stream_finalize_write` is called
237 * and returns without an error
239 * The stream must always be freed when done with `git_odb_stream_free` or
242 * @see git_odb_stream
244 * @param out pointer where to store the stream
245 * @param db object database where the stream will write
246 * @param size final size of the object that will be written
247 * @param type type of the object that will be written
248 * @return 0 if the stream was created; error code otherwise
250 GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream
**out
, git_odb
*db
, size_t size
, git_otype type
);
253 * Write to an odb stream
255 * This method will fail if the total number of received bytes exceeds the
256 * size declared with `git_odb_open_wstream()`
258 * @param stream the stream
259 * @param buffer the data to write
260 * @param len the buffer's length
261 * @return 0 if the write succeeded; error code otherwise
263 GIT_EXTERN(int) git_odb_stream_write(git_odb_stream
*stream
, const char *buffer
, size_t len
);
266 * Finish writing to an odb stream
268 * The object will take its final name and will be available to the
271 * This method will fail if the total number of received bytes
272 * differs from the size declared with `git_odb_open_wstream()`
274 * @param out pointer to store the resulting object's id
275 * @param stream the stream
276 * @return 0 on success; an error code otherwise
278 GIT_EXTERN(int) git_odb_stream_finalize_write(git_oid
*out
, git_odb_stream
*stream
);
281 * Read from an odb stream
283 * Most backends don't implement streaming reads
285 GIT_EXTERN(int) git_odb_stream_read(git_odb_stream
*stream
, char *buffer
, size_t len
);
290 * @param stream the stream to free
292 GIT_EXTERN(void) git_odb_stream_free(git_odb_stream
*stream
);
295 * Open a stream to read an object from the ODB
297 * Note that most backends do *not* support streaming reads
298 * because they store their objects as compressed/delta'ed blobs.
300 * It's recommended to use `git_odb_read` instead, which is
301 * assured to work on all backends.
303 * The returned stream will be of type `GIT_STREAM_RDONLY` and
304 * will have the following methods:
306 * - stream->read: read `n` bytes from the stream
307 * - stream->free: free the stream
309 * The stream must always be free'd or will leak memory.
311 * @see git_odb_stream
313 * @param out pointer where to store the stream
314 * @param db object database where the stream will read from
315 * @param oid oid of the object the stream will read from
316 * @return 0 if the stream was created; error code otherwise
318 GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream
**out
, git_odb
*db
, const git_oid
*oid
);
321 * Open a stream for writing a pack file to the ODB.
323 * If the ODB layer understands pack files, then the given
324 * packfile will likely be streamed directly to disk (and a
325 * corresponding index created). If the ODB layer does not
326 * understand pack files, the objects will be stored in whatever
327 * format the ODB layer uses.
329 * @see git_odb_writepack
331 * @param out pointer to the writepack functions
332 * @param db object database where the stream will read from
333 * @param progress_cb function to call with progress information.
334 * Be aware that this is called inline with network and indexing operations,
335 * so performance may be affected.
336 * @param progress_payload payload for the progress callback
338 GIT_EXTERN(int) git_odb_write_pack(
339 git_odb_writepack
**out
,
341 git_transfer_progress_callback progress_cb
,
342 void *progress_payload
);
345 * Determine the object-ID (sha1 hash) of a data buffer
347 * The resulting SHA-1 OID will be the identifier for the data
348 * buffer as if the data buffer it were to written to the ODB.
350 * @param out the resulting object-ID.
351 * @param data data to hash
352 * @param len size of the data
353 * @param type of the data to hash
354 * @return 0 or an error code
356 GIT_EXTERN(int) git_odb_hash(git_oid
*out
, const void *data
, size_t len
, git_otype type
);
359 * Read a file from disk and fill a git_oid with the object id
360 * that the file would have if it were written to the Object
361 * Database as an object of the given type (w/o applying filters).
362 * Similar functionality to git.git's `git hash-object` without
363 * the `-w` flag, however, with the --no-filters flag.
364 * If you need filters, see git_repository_hashfile.
366 * @param out oid structure the result is written into.
367 * @param path file to read and determine object id for
368 * @param type the type of the object that will be hashed
369 * @return 0 or an error code
371 GIT_EXTERN(int) git_odb_hashfile(git_oid
*out
, const char *path
, git_otype type
);
374 * Create a copy of an odb_object
376 * The returned copy must be manually freed with `git_odb_object_free`.
377 * Note that because of an implementation detail, the returned copy will be
378 * the same pointer as `source`: the object is internally refcounted, so the
379 * copy still needs to be freed twice.
381 * @param dest pointer where to store the copy
382 * @param source object to copy
383 * @return 0 or an error code
385 GIT_EXTERN(int) git_odb_object_dup(git_odb_object
**dest
, git_odb_object
*source
);
388 * Close an ODB object
390 * This method must always be called once a `git_odb_object` is no
391 * longer needed, otherwise memory will leak.
393 * @param object object to close
395 GIT_EXTERN(void) git_odb_object_free(git_odb_object
*object
);
398 * Return the OID of an ODB object
400 * This is the OID from which the object was read from
402 * @param object the object
403 * @return a pointer to the OID
405 GIT_EXTERN(const git_oid
*) git_odb_object_id(git_odb_object
*object
);
408 * Return the data of an ODB object
410 * This is the uncompressed, raw data as read from the ODB,
411 * without the leading header.
413 * This pointer is owned by the object and shall not be free'd.
415 * @param object the object
416 * @return a pointer to the data
418 GIT_EXTERN(const void *) git_odb_object_data(git_odb_object
*object
);
421 * Return the size of an ODB object
423 * This is the real size of the `data` buffer, not the
424 * actual size of the object.
426 * @param object the object
429 GIT_EXTERN(size_t) git_odb_object_size(git_odb_object
*object
);
432 * Return the type of an ODB object
434 * @param object the object
437 GIT_EXTERN(git_otype
) git_odb_object_type(git_odb_object
*object
);
440 * Add a custom backend to an existing Object DB
442 * The backends are checked in relative ordering, based on the
443 * value of the `priority` parameter.
445 * Read <odb_backends.h> for more information.
447 * @param odb database to add the backend to
448 * @param backend pointer to a git_odb_backend instance
449 * @param priority Value for ordering the backends queue
450 * @return 0 on success; error code otherwise
452 GIT_EXTERN(int) git_odb_add_backend(git_odb
*odb
, git_odb_backend
*backend
, int priority
);
455 * Add a custom backend to an existing Object DB; this
456 * backend will work as an alternate.
458 * Alternate backends are always checked for objects *after*
459 * all the main backends have been exhausted.
461 * The backends are checked in relative ordering, based on the
462 * value of the `priority` parameter.
464 * Writing is disabled on alternate backends.
466 * Read <odb_backends.h> for more information.
468 * @param odb database to add the backend to
469 * @param backend pointer to a git_odb_backend instance
470 * @param priority Value for ordering the backends queue
471 * @return 0 on success; error code otherwise
473 GIT_EXTERN(int) git_odb_add_alternate(git_odb
*odb
, git_odb_backend
*backend
, int priority
);
476 * Get the number of ODB backend objects
478 * @param odb object database
479 * @return number of backends in the ODB
481 GIT_EXTERN(size_t) git_odb_num_backends(git_odb
*odb
);
484 * Lookup an ODB backend object by index
486 * @param out output pointer to ODB backend at pos
487 * @param odb object database
488 * @param pos index into object database backend list
489 * @return 0 on success; GIT_ENOTFOUND if pos is invalid; other errors < 0
491 GIT_EXTERN(int) git_odb_get_backend(git_odb_backend
**out
, git_odb
*odb
, size_t pos
);