Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:38

0001 /*
0002  * Copyright (C) the libgit2 contributors. All rights reserved.
0003  *
0004  * This file is part of libgit2, distributed under the GNU GPL v2 with
0005  * a Linking Exception. For full terms see the included COPYING file.
0006  */
0007 #ifndef INCLUDE_git_odb_h__
0008 #define INCLUDE_git_odb_h__
0009 
0010 #include "common.h"
0011 #include "types.h"
0012 #include "oid.h"
0013 #include "oidarray.h"
0014 #include "indexer.h"
0015 
0016 /**
0017  * @file git2/odb.h
0018  * @brief Git object database routines
0019  * @defgroup git_odb Git object database routines
0020  * @ingroup Git
0021  * @{
0022  */
0023 GIT_BEGIN_DECL
0024 
0025 /** Flags controlling the behavior of ODB lookup operations */
0026 typedef enum {
0027     /**
0028      * Don't call `git_odb_refresh` if the lookup fails. Useful when doing
0029      * a batch of lookup operations for objects that may legitimately not
0030      * exist. When using this flag, you may wish to manually call
0031      * `git_odb_refresh` before processing a batch of objects.
0032      */
0033     GIT_ODB_LOOKUP_NO_REFRESH = (1 << 0)
0034 } git_odb_lookup_flags_t;
0035 
0036 /**
0037  * Function type for callbacks from git_odb_foreach.
0038  */
0039 typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload);
0040 
0041 /** Options for configuring a loose object backend. */
0042 typedef struct {
0043     unsigned int version; /**< version for the struct */
0044 
0045     /**
0046      * Type of object IDs to use for this object database, or
0047      * 0 for default (currently SHA1).
0048      */
0049     git_oid_t oid_type;
0050 } git_odb_options;
0051 
0052 /* The current version of the diff options structure */
0053 #define GIT_ODB_OPTIONS_VERSION 1
0054 
0055 /* Stack initializer for odb options.  Alternatively use
0056  * `git_odb_options_init` programmatic initialization.
0057  */
0058 #define GIT_ODB_OPTIONS_INIT { GIT_ODB_OPTIONS_VERSION }
0059 
0060 /**
0061  * Create a new object database with no backends.
0062  *
0063  * Before the ODB can be used for read/writing, a custom database
0064  * backend must be manually added using `git_odb_add_backend()`
0065  *
0066  * @param out location to store the database pointer, if opened.
0067  *          Set to NULL if the open failed.
0068  * @param opts the options for this object database or NULL for defaults
0069  * @return 0 or an error code
0070  */
0071 #ifdef GIT_EXPERIMENTAL_SHA256
0072 GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts);
0073 #else
0074 GIT_EXTERN(int) git_odb_new(git_odb **out);
0075 #endif
0076 
0077 /**
0078  * Create a new object database and automatically add
0079  * the two default backends:
0080  *
0081  *  - git_odb_backend_loose: read and write loose object files
0082  *      from disk, assuming `objects_dir` as the Objects folder
0083  *
0084  *  - git_odb_backend_pack: read objects from packfiles,
0085  *      assuming `objects_dir` as the Objects folder which
0086  *      contains a 'pack/' folder with the corresponding data
0087  *
0088  * @param out location to store the database pointer, if opened.
0089  *          Set to NULL if the open failed.
0090  * @param objects_dir path of the backends' "objects" directory.
0091  * @param opts the options for this object database or NULL for defaults
0092  * @return 0 or an error code
0093  */
0094 #ifdef GIT_EXPERIMENTAL_SHA256
0095 GIT_EXTERN(int) git_odb_open(
0096     git_odb **out,
0097     const char *objects_dir,
0098     const git_odb_options *opts);
0099 #else
0100 GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
0101 #endif
0102 
0103 /**
0104  * Add an on-disk alternate to an existing Object DB.
0105  *
0106  * Note that the added path must point to an `objects`, not
0107  * to a full repository, to use it as an alternate store.
0108  *
0109  * Alternate backends are always checked for objects *after*
0110  * all the main backends have been exhausted.
0111  *
0112  * Writing is disabled on alternate backends.
0113  *
0114  * @param odb database to add the backend to
0115  * @param path path to the objects folder for the alternate
0116  * @return 0 on success, error code otherwise
0117  */
0118 GIT_EXTERN(int) git_odb_add_disk_alternate(git_odb *odb, const char *path);
0119 
0120 /**
0121  * Close an open object database.
0122  *
0123  * @param db database pointer to close. If NULL no action is taken.
0124  */
0125 GIT_EXTERN(void) git_odb_free(git_odb *db);
0126 
0127 /**
0128  * Read an object from the database.
0129  *
0130  * This method queries all available ODB backends
0131  * trying to read the given OID.
0132  *
0133  * The returned object is reference counted and
0134  * internally cached, so it should be closed
0135  * by the user once it's no longer in use.
0136  *
0137  * @param out pointer where to store the read object
0138  * @param db database to search for the object in.
0139  * @param id identity of the object to read.
0140  * @return 0 if the object was read, GIT_ENOTFOUND if the object is
0141  *         not in the database.
0142  */
0143 GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id);
0144 
0145 /**
0146  * Read an object from the database, given a prefix
0147  * of its identifier.
0148  *
0149  * This method queries all available ODB backends
0150  * trying to match the 'len' first hexadecimal
0151  * characters of the 'short_id'.
0152  * The remaining (GIT_OID_SHA1_HEXSIZE-len)*4 bits of
0153  * 'short_id' must be 0s.
0154  * 'len' must be at least GIT_OID_MINPREFIXLEN,
0155  * and the prefix must be long enough to identify
0156  * a unique object in all the backends; the
0157  * method will fail otherwise.
0158  *
0159  * The returned object is reference counted and
0160  * internally cached, so it should be closed
0161  * by the user once it's no longer in use.
0162  *
0163  * @param out pointer where to store the read object
0164  * @param db database to search for the object in.
0165  * @param short_id a prefix of the id of the object to read.
0166  * @param len the length of the prefix
0167  * @return 0 if the object was read, GIT_ENOTFOUND if the object is not in the
0168  *         database. GIT_EAMBIGUOUS if the prefix is ambiguous
0169  *         (several objects match the prefix)
0170  */
0171 GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len);
0172 
0173 /**
0174  * Read the header of an object from the database, without
0175  * reading its full contents.
0176  *
0177  * The header includes the length and the type of an object.
0178  *
0179  * Note that most backends do not support reading only the header
0180  * of an object, so the whole object will be read and then the
0181  * header will be returned.
0182  *
0183  * @param len_out pointer where to store the length
0184  * @param type_out pointer where to store the type
0185  * @param db database to search for the object in.
0186  * @param id identity of the object to read.
0187  * @return 0 if the object was read, GIT_ENOTFOUND if the object is not
0188  *         in the database.
0189  */
0190 GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_object_t *type_out, git_odb *db, const git_oid *id);
0191 
0192 /**
0193  * Determine if the given object can be found in the object database.
0194  *
0195  * @param db database to be searched for the given object.
0196  * @param id the object to search for.
0197  * @return 1 if the object was found, 0 otherwise
0198  */
0199 GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
0200 
0201 /**
0202  * Determine if the given object can be found in the object database, with
0203  * extended options.
0204  *
0205  * @param db database to be searched for the given object.
0206  * @param id the object to search for.
0207  * @param flags flags affecting the lookup (see `git_odb_lookup_flags_t`)
0208  * @return 1 if the object was found, 0 otherwise
0209  */
0210 GIT_EXTERN(int) git_odb_exists_ext(git_odb *db, const git_oid *id, unsigned int flags);
0211 
0212 /**
0213  * Determine if an object can be found in the object database by an
0214  * abbreviated object ID.
0215  *
0216  * @param out The full OID of the found object if just one is found.
0217  * @param db The database to be searched for the given object.
0218  * @param short_id A prefix of the id of the object to read.
0219  * @param len The length of the prefix.
0220  * @return 0 if found, GIT_ENOTFOUND if not found, GIT_EAMBIGUOUS if multiple
0221  *         matches were found, other value < 0 if there was a read error.
0222  */
0223 GIT_EXTERN(int) git_odb_exists_prefix(
0224     git_oid *out, git_odb *db, const git_oid *short_id, size_t len);
0225 
0226 /**
0227  * The information about object IDs to query in `git_odb_expand_ids`,
0228  * which will be populated upon return.
0229  */
0230 typedef struct git_odb_expand_id {
0231     /** The object ID to expand */
0232     git_oid id;
0233 
0234     /**
0235      * The length of the object ID (in nibbles, or packets of 4 bits; the
0236      * number of hex characters)
0237      * */
0238     unsigned short length;
0239 
0240     /**
0241      * The (optional) type of the object to search for; leave as `0` or set
0242      * to `GIT_OBJECT_ANY` to query for any object matching the ID.
0243      */
0244     git_object_t type;
0245 } git_odb_expand_id;
0246 
0247 /**
0248  * Determine if one or more objects can be found in the object database
0249  * by their abbreviated object ID and type.
0250  *
0251  * The given array will be updated in place: for each abbreviated ID that is
0252  * unique in the database, and of the given type (if specified),
0253  * the full object ID, object ID length (`GIT_OID_SHA1_HEXSIZE`) and type will be
0254  * written back to the array. For IDs that are not found (or are ambiguous),
0255  * the array entry will be zeroed.
0256  *
0257  * Note that since this function operates on multiple objects, the
0258  * underlying database will not be asked to be reloaded if an object is
0259  * not found (which is unlike other object database operations.)
0260  *
0261  * @param db The database to be searched for the given objects.
0262  * @param ids An array of short object IDs to search for
0263  * @param count The length of the `ids` array
0264  * @return 0 on success or an error code on failure
0265  */
0266 GIT_EXTERN(int) git_odb_expand_ids(
0267     git_odb *db,
0268     git_odb_expand_id *ids,
0269     size_t count);
0270 
0271 /**
0272  * Refresh the object database to load newly added files.
0273  *
0274  * If the object databases have changed on disk while the library
0275  * is running, this function will force a reload of the underlying
0276  * indexes.
0277  *
0278  * Use this function when you're confident that an external
0279  * application has tampered with the ODB.
0280  *
0281  * NOTE that it is not necessary to call this function at all. The
0282  * library will automatically attempt to refresh the ODB
0283  * when a lookup fails, to see if the looked up object exists
0284  * on disk but hasn't been loaded yet.
0285  *
0286  * @param db database to refresh
0287  * @return 0 on success, error code otherwise
0288  */
0289 GIT_EXTERN(int) git_odb_refresh(struct git_odb *db);
0290 
0291 /**
0292  * List all objects available in the database
0293  *
0294  * The callback will be called for each object available in the
0295  * database. Note that the objects are likely to be returned in the index
0296  * order, which would make accessing the objects in that order inefficient.
0297  * Return a non-zero value from the callback to stop looping.
0298  *
0299  * @param db database to use
0300  * @param cb the callback to call for each object
0301  * @param payload data to pass to the callback
0302  * @return 0 on success, non-zero callback return value, or error code
0303  */
0304 GIT_EXTERN(int) git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload);
0305 
0306 /**
0307  * Write an object directly into the ODB
0308  *
0309  * This method writes a full object straight into the ODB.
0310  * For most cases, it is preferred to write objects through a write
0311  * stream, which is both faster and less memory intensive, specially
0312  * for big objects.
0313  *
0314  * This method is provided for compatibility with custom backends
0315  * which are not able to support streaming writes
0316  *
0317  * @param out pointer to store the OID result of the write
0318  * @param odb object database where to store the object
0319  * @param data buffer with the data to store
0320  * @param len size of the buffer
0321  * @param type type of the data to store
0322  * @return 0 or an error code
0323  */
0324 GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size_t len, git_object_t type);
0325 
0326 /**
0327  * Open a stream to write an object into the ODB
0328  *
0329  * The type and final length of the object must be specified
0330  * when opening the stream.
0331  *
0332  * The returned stream will be of type `GIT_STREAM_WRONLY`, and it
0333  * won't be effective until `git_odb_stream_finalize_write` is called
0334  * and returns without an error
0335  *
0336  * The stream must always be freed when done with `git_odb_stream_free` or
0337  * will leak memory.
0338  *
0339  * @see git_odb_stream
0340  *
0341  * @param out pointer where to store the stream
0342  * @param db object database where the stream will write
0343  * @param size final size of the object that will be written
0344  * @param type type of the object that will be written
0345  * @return 0 if the stream was created; error code otherwise
0346  */
0347 GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_object_size_t size, git_object_t type);
0348 
0349 /**
0350  * Write to an odb stream
0351  *
0352  * This method will fail if the total number of received bytes exceeds the
0353  * size declared with `git_odb_open_wstream()`
0354  *
0355  * @param stream the stream
0356  * @param buffer the data to write
0357  * @param len the buffer's length
0358  * @return 0 if the write succeeded, error code otherwise
0359  */
0360 GIT_EXTERN(int) git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len);
0361 
0362 /**
0363  * Finish writing to an odb stream
0364  *
0365  * The object will take its final name and will be available to the
0366  * odb.
0367  *
0368  * This method will fail if the total number of received bytes
0369  * differs from the size declared with `git_odb_open_wstream()`
0370  *
0371  * @param out pointer to store the resulting object's id
0372  * @param stream the stream
0373  * @return 0 on success, an error code otherwise
0374  */
0375 GIT_EXTERN(int) git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream);
0376 
0377 /**
0378  * Read from an odb stream
0379  *
0380  * Most backends don't implement streaming reads
0381  *
0382  * @param stream the stream
0383  * @param buffer a user-allocated buffer to store the data in.
0384  * @param len the buffer's length
0385  * @return 0 if the read succeeded, error code otherwise
0386  */
0387 GIT_EXTERN(int) git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len);
0388 
0389 /**
0390  * Free an odb stream
0391  *
0392  * @param stream the stream to free
0393  */
0394 GIT_EXTERN(void) git_odb_stream_free(git_odb_stream *stream);
0395 
0396 /**
0397  * Open a stream to read an object from the ODB
0398  *
0399  * Note that most backends do *not* support streaming reads
0400  * because they store their objects as compressed/delta'ed blobs.
0401  *
0402  * It's recommended to use `git_odb_read` instead, which is
0403  * assured to work on all backends.
0404  *
0405  * The returned stream will be of type `GIT_STREAM_RDONLY` and
0406  * will have the following methods:
0407  *
0408  *      - stream->read: read `n` bytes from the stream
0409  *      - stream->free: free the stream
0410  *
0411  * The stream must always be free'd or will leak memory.
0412  *
0413  * @see git_odb_stream
0414  *
0415  * @param out pointer where to store the stream
0416  * @param len pointer where to store the length of the object
0417  * @param type pointer where to store the type of the object
0418  * @param db object database where the stream will read from
0419  * @param oid oid of the object the stream will read from
0420  * @return 0 if the stream was created, error code otherwise
0421  */
0422 GIT_EXTERN(int) git_odb_open_rstream(
0423     git_odb_stream **out,
0424     size_t *len,
0425     git_object_t *type,
0426     git_odb *db,
0427     const git_oid *oid);
0428 
0429 /**
0430  * Open a stream for writing a pack file to the ODB.
0431  *
0432  * If the ODB layer understands pack files, then the given
0433  * packfile will likely be streamed directly to disk (and a
0434  * corresponding index created).  If the ODB layer does not
0435  * understand pack files, the objects will be stored in whatever
0436  * format the ODB layer uses.
0437  *
0438  * @see git_odb_writepack
0439  *
0440  * @param out pointer to the writepack functions
0441  * @param db object database where the stream will read from
0442  * @param progress_cb function to call with progress information.
0443  * Be aware that this is called inline with network and indexing operations,
0444  * so performance may be affected.
0445  * @param progress_payload payload for the progress callback
0446  * @return 0 or an error code.
0447  */
0448 GIT_EXTERN(int) git_odb_write_pack(
0449     git_odb_writepack **out,
0450     git_odb *db,
0451     git_indexer_progress_cb progress_cb,
0452     void *progress_payload);
0453 
0454 /**
0455  * Write a `multi-pack-index` file from all the `.pack` files in the ODB.
0456  *
0457  * If the ODB layer understands pack files, then this will create a file called
0458  * `multi-pack-index` next to the `.pack` and `.idx` files, which will contain
0459  * an index of all objects stored in `.pack` files. This will allow for
0460  * O(log n) lookup for n objects (regardless of how many packfiles there
0461  * exist).
0462  *
0463  * @param db object database where the `multi-pack-index` file will be written.
0464  * @return 0 or an error code.
0465  */
0466 GIT_EXTERN(int) git_odb_write_multi_pack_index(
0467     git_odb *db);
0468 
0469 /**
0470  * Determine the object-ID (sha1 or sha256 hash) of a data buffer
0471  *
0472  * The resulting OID will be the identifier for the data buffer as if
0473  * the data buffer it were to written to the ODB.
0474  *
0475  * @param out the resulting object-ID.
0476  * @param data data to hash
0477  * @param len size of the data
0478  * @param object_type of the data to hash
0479  * @param oid_type the oid type to hash to
0480  * @return 0 or an error code
0481  */
0482 #ifdef GIT_EXPERIMENTAL_SHA256
0483 GIT_EXTERN(int) git_odb_hash(
0484     git_oid *out,
0485     const void *data,
0486     size_t len,
0487     git_object_t object_type,
0488     git_oid_t oid_type);
0489 #else
0490 GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type);
0491 #endif
0492 
0493 /**
0494  * Read a file from disk and fill a git_oid with the object id
0495  * that the file would have if it were written to the Object
0496  * Database as an object of the given type (w/o applying filters).
0497  * Similar functionality to git.git's `git hash-object` without
0498  * the `-w` flag, however, with the --no-filters flag.
0499  * If you need filters, see git_repository_hashfile.
0500  *
0501  * @param out oid structure the result is written into.
0502  * @param path file to read and determine object id for
0503  * @param object_type of the data to hash
0504  * @param oid_type the oid type to hash to
0505  * @return 0 or an error code
0506  */
0507 #ifdef GIT_EXPERIMENTAL_SHA256
0508 GIT_EXTERN(int) git_odb_hashfile(
0509     git_oid *out,
0510     const char *path,
0511     git_object_t object_type,
0512     git_oid_t oid_type);
0513 #else
0514 GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_object_t type);
0515 #endif
0516 
0517 /**
0518  * Create a copy of an odb_object
0519  *
0520  * The returned copy must be manually freed with `git_odb_object_free`.
0521  * Note that because of an implementation detail, the returned copy will be
0522  * the same pointer as `source`: the object is internally refcounted, so the
0523  * copy still needs to be freed twice.
0524  *
0525  * @param dest pointer where to store the copy
0526  * @param source object to copy
0527  * @return 0 or an error code
0528  */
0529 GIT_EXTERN(int) git_odb_object_dup(git_odb_object **dest, git_odb_object *source);
0530 
0531 /**
0532  * Close an ODB object
0533  *
0534  * This method must always be called once a `git_odb_object` is no
0535  * longer needed, otherwise memory will leak.
0536  *
0537  * @param object object to close
0538  */
0539 GIT_EXTERN(void) git_odb_object_free(git_odb_object *object);
0540 
0541 /**
0542  * Return the OID of an ODB object
0543  *
0544  * This is the OID from which the object was read from
0545  *
0546  * @param object the object
0547  * @return a pointer to the OID
0548  */
0549 GIT_EXTERN(const git_oid *) git_odb_object_id(git_odb_object *object);
0550 
0551 /**
0552  * Return the data of an ODB object
0553  *
0554  * This is the uncompressed, raw data as read from the ODB,
0555  * without the leading header.
0556  *
0557  * This pointer is owned by the object and shall not be free'd.
0558  *
0559  * @param object the object
0560  * @return a pointer to the data
0561  */
0562 GIT_EXTERN(const void *) git_odb_object_data(git_odb_object *object);
0563 
0564 /**
0565  * Return the size of an ODB object
0566  *
0567  * This is the real size of the `data` buffer, not the
0568  * actual size of the object.
0569  *
0570  * @param object the object
0571  * @return the size
0572  */
0573 GIT_EXTERN(size_t) git_odb_object_size(git_odb_object *object);
0574 
0575 /**
0576  * Return the type of an ODB object
0577  *
0578  * @param object the object
0579  * @return the type
0580  */
0581 GIT_EXTERN(git_object_t) git_odb_object_type(git_odb_object *object);
0582 
0583 /**
0584  * Add a custom backend to an existing Object DB
0585  *
0586  * The backends are checked in relative ordering, based on the
0587  * value of the `priority` parameter.
0588  *
0589  * Read <sys/odb_backend.h> for more information.
0590  *
0591  * @param odb database to add the backend to
0592  * @param backend pointer to a git_odb_backend instance
0593  * @param priority Value for ordering the backends queue
0594  * @return 0 on success, error code otherwise
0595  */
0596 GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority);
0597 
0598 /**
0599  * Add a custom backend to an existing Object DB; this
0600  * backend will work as an alternate.
0601  *
0602  * Alternate backends are always checked for objects *after*
0603  * all the main backends have been exhausted.
0604  *
0605  * The backends are checked in relative ordering, based on the
0606  * value of the `priority` parameter.
0607  *
0608  * Writing is disabled on alternate backends.
0609  *
0610  * Read <sys/odb_backend.h> for more information.
0611  *
0612  * @param odb database to add the backend to
0613  * @param backend pointer to a git_odb_backend instance
0614  * @param priority Value for ordering the backends queue
0615  * @return 0 on success, error code otherwise
0616  */
0617 GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority);
0618 
0619 /**
0620  * Get the number of ODB backend objects
0621  *
0622  * @param odb object database
0623  * @return number of backends in the ODB
0624  */
0625 GIT_EXTERN(size_t) git_odb_num_backends(git_odb *odb);
0626 
0627 /**
0628  * Lookup an ODB backend object by index
0629  *
0630  * @param out output pointer to ODB backend at pos
0631  * @param odb object database
0632  * @param pos index into object database backend list
0633  * @return 0 on success, GIT_ENOTFOUND if pos is invalid, other errors < 0
0634  */
0635 GIT_EXTERN(int) git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos);
0636 
0637 /**
0638  * Set the git commit-graph for the ODB.
0639  *
0640  * After a successful call, the ownership of the cgraph parameter will be
0641  * transferred to libgit2, and the caller should not free it.
0642  *
0643  * The commit-graph can also be unset by explicitly passing NULL as the cgraph
0644  * parameter.
0645  *
0646  * @param odb object database
0647  * @param cgraph the git commit-graph
0648  * @return 0 on success; error code otherwise
0649  */
0650 GIT_EXTERN(int) git_odb_set_commit_graph(git_odb *odb, git_commit_graph *cgraph);
0651 
0652 /** @} */
0653 GIT_END_DECL
0654 #endif