]> git.proxmox.com Git - libgit2.git/blob - include/git2/odb.h
Make git_odb_foreach_cb take const param
[libgit2.git] / include / git2 / odb.h
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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 "odb_backend.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 /**
26 * Create a new object database with no backends.
27 *
28 * Before the ODB can be used for read/writing, a custom database
29 * backend must be manually added using `git_odb_add_backend()`
30 *
31 * @param out location to store the database pointer, if opened.
32 * Set to NULL if the open failed.
33 * @return 0 or an error code
34 */
35 GIT_EXTERN(int) git_odb_new(git_odb **out);
36
37 /**
38 * Create a new object database and automatically add
39 * the two default backends:
40 *
41 * - git_odb_backend_loose: read and write loose object files
42 * from disk, assuming `objects_dir` as the Objects folder
43 *
44 * - git_odb_backend_pack: read objects from packfiles,
45 * assuming `objects_dir` as the Objects folder which
46 * contains a 'pack/' folder with the corresponding data
47 *
48 * @param out location to store the database pointer, if opened.
49 * Set to NULL if the open failed.
50 * @param objects_dir path of the backends' "objects" directory.
51 * @return 0 or an error code
52 */
53 GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
54
55 /**
56 * Add a custom backend to an existing Object DB
57 *
58 * The backends are checked in relative ordering, based on the
59 * value of the `priority` parameter.
60 *
61 * Read <odb_backends.h> for more information.
62 *
63 * @param odb database to add the backend to
64 * @param backend pointer to a git_odb_backend instance
65 * @param priority Value for ordering the backends queue
66 * @return 0 on success; error code otherwise
67 */
68 GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority);
69
70 /**
71 * Add a custom backend to an existing Object DB; this
72 * backend will work as an alternate.
73 *
74 * Alternate backends are always checked for objects *after*
75 * all the main backends have been exhausted.
76 *
77 * The backends are checked in relative ordering, based on the
78 * value of the `priority` parameter.
79 *
80 * Writing is disabled on alternate backends.
81 *
82 * Read <odb_backends.h> for more information.
83 *
84 * @param odb database to add the backend to
85 * @param backend pointer to a git_odb_backend instance
86 * @param priority Value for ordering the backends queue
87 * @return 0 on success; error code otherwise
88 */
89 GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority);
90
91 /**
92 * Close an open object database.
93 *
94 * @param db database pointer to close. If NULL no action is taken.
95 */
96 GIT_EXTERN(void) git_odb_free(git_odb *db);
97
98 /**
99 * Read an object from the database.
100 *
101 * This method queries all available ODB backends
102 * trying to read the given OID.
103 *
104 * The returned object is reference counted and
105 * internally cached, so it should be closed
106 * by the user once it's no longer in use.
107 *
108 * @param out pointer where to store the read object
109 * @param db database to search for the object in.
110 * @param id identity of the object to read.
111 * @return
112 * - 0 if the object was read;
113 * - GIT_ENOTFOUND if the object is not in the database.
114 */
115 GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id);
116
117 /**
118 * Read an object from the database, given a prefix
119 * of its identifier.
120 *
121 * This method queries all available ODB backends
122 * trying to match the 'len' first hexadecimal
123 * characters of the 'short_id'.
124 * The remaining (GIT_OID_HEXSZ-len)*4 bits of
125 * 'short_id' must be 0s.
126 * 'len' must be at least GIT_OID_MINPREFIXLEN,
127 * and the prefix must be long enough to identify
128 * a unique object in all the backends; the
129 * method will fail otherwise.
130 *
131 * The returned object is reference counted and
132 * internally cached, so it should be closed
133 * by the user once it's no longer in use.
134 *
135 * @param out pointer where to store the read object
136 * @param db database to search for the object in.
137 * @param short_id a prefix of the id of the object to read.
138 * @param len the length of the prefix
139 * @return
140 * - 0 if the object was read;
141 * - GIT_ENOTFOUND if the object is not in the database.
142 * - GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix)
143 */
144 GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len);
145
146 /**
147 * Read the header of an object from the database, without
148 * reading its full contents.
149 *
150 * The header includes the length and the type of an object.
151 *
152 * Note that most backends do not support reading only the header
153 * of an object, so the whole object will be read and then the
154 * header will be returned.
155 *
156 * @param len_out pointer where to store the length
157 * @param type_out pointer where to store the type
158 * @param db database to search for the object in.
159 * @param id identity of the object to read.
160 * @return
161 * - 0 if the object was read;
162 * - GIT_ENOTFOUND if the object is not in the database.
163 */
164 GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_otype *type_out, git_odb *db, const git_oid *id);
165
166 /**
167 * Determine if the given object can be found in the object database.
168 *
169 * @param db database to be searched for the given object.
170 * @param id the object to search for.
171 * @return
172 * - 1, if the object was found
173 * - 0, otherwise
174 */
175 GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
176
177 /**
178 * List all objects available in the database
179 *
180 * The callback will be called for each object available in the
181 * database. Note that the objects are likely to be returned in the index
182 * order, which would make accessing the objects in that order inefficient.
183 * Return a non-zero value from the callback to stop looping.
184 *
185 * @param db database to use
186 * @param cb the callback to call for each object
187 * @param payload data to pass to the callback
188 * @return 0 on success, GIT_EUSER on non-zero callback, or error code
189 */
190 GIT_EXTERN(int) git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload);
191
192 /**
193 * Write an object directly into the ODB
194 *
195 * This method writes a full object straight into the ODB.
196 * For most cases, it is preferred to write objects through a write
197 * stream, which is both faster and less memory intensive, specially
198 * for big objects.
199 *
200 * This method is provided for compatibility with custom backends
201 * which are not able to support streaming writes
202 *
203 * @param out pointer to store the OID result of the write
204 * @param odb object database where to store the object
205 * @param data buffer with the data to store
206 * @param len size of the buffer
207 * @param type type of the data to store
208 * @return 0 or an error code
209 */
210 GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size_t len, git_otype type);
211
212 /**
213 * Open a stream to write an object into the ODB
214 *
215 * The type and final length of the object must be specified
216 * when opening the stream.
217 *
218 * The returned stream will be of type `GIT_STREAM_WRONLY` and
219 * will have the following methods:
220 *
221 * - stream->write: write `n` bytes into the stream
222 * - stream->finalize_write: close the stream and store the object in
223 * the odb
224 * - stream->free: free the stream
225 *
226 * The streaming write won't be effective until `stream->finalize_write`
227 * is called and returns without an error
228 *
229 * The stream must always be free'd or will leak memory.
230 *
231 * @see git_odb_stream
232 *
233 * @param out pointer where to store the stream
234 * @param db object database where the stream will write
235 * @param size final size of the object that will be written
236 * @param type type of the object that will be written
237 * @return 0 if the stream was created; error code otherwise
238 */
239 GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, size_t size, git_otype type);
240
241 /**
242 * Open a stream to read an object from the ODB
243 *
244 * Note that most backends do *not* support streaming reads
245 * because they store their objects as compressed/delta'ed blobs.
246 *
247 * It's recommended to use `git_odb_read` instead, which is
248 * assured to work on all backends.
249 *
250 * The returned stream will be of type `GIT_STREAM_RDONLY` and
251 * will have the following methods:
252 *
253 * - stream->read: read `n` bytes from the stream
254 * - stream->free: free the stream
255 *
256 * The stream must always be free'd or will leak memory.
257 *
258 * @see git_odb_stream
259 *
260 * @param out pointer where to store the stream
261 * @param db object database where the stream will read from
262 * @param oid oid of the object the stream will read from
263 * @return 0 if the stream was created; error code otherwise
264 */
265 GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **out, git_odb *db, const git_oid *oid);
266
267 /**
268 * Open a stream for writing a pack file to the ODB.
269 *
270 * If the ODB layer understands pack files, then the given
271 * packfile will likely be streamed directly to disk (and a
272 * corresponding index created). If the ODB layer does not
273 * understand pack files, the objects will be stored in whatever
274 * format the ODB layer uses.
275 *
276 * @see git_odb_writepack
277 *
278 * @param out pointer to the writepack functions
279 * @param db object database where the stream will read from
280 * @param progress_cb function to call with progress information.
281 * Be aware that this is called inline with network and indexing operations,
282 * so performance may be affected.
283 * @param progress_payload payload for the progress callback
284 */
285 GIT_EXTERN(int) git_odb_write_pack(
286 git_odb_writepack **out,
287 git_odb *db,
288 git_transfer_progress_callback progress_cb,
289 void *progress_payload);
290
291 /**
292 * Determine the object-ID (sha1 hash) of a data buffer
293 *
294 * The resulting SHA-1 OID will be the identifier for the data
295 * buffer as if the data buffer it were to written to the ODB.
296 *
297 * @param out the resulting object-ID.
298 * @param data data to hash
299 * @param len size of the data
300 * @param type of the data to hash
301 * @return 0 or an error code
302 */
303 GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_otype type);
304
305 /**
306 * Read a file from disk and fill a git_oid with the object id
307 * that the file would have if it were written to the Object
308 * Database as an object of the given type (w/o applying filters).
309 * Similar functionality to git.git's `git hash-object` without
310 * the `-w` flag, however, with the --no-filters flag.
311 * If you need filters, see git_repository_hashfile.
312 *
313 * @param out oid structure the result is written into.
314 * @param path file to read and determine object id for
315 * @param type the type of the object that will be hashed
316 * @return 0 or an error code
317 */
318 GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_otype type);
319
320 /**
321 * Close an ODB object
322 *
323 * This method must always be called once a `git_odb_object` is no
324 * longer needed, otherwise memory will leak.
325 *
326 * @param object object to close
327 */
328 GIT_EXTERN(void) git_odb_object_free(git_odb_object *object);
329
330 /**
331 * Return the OID of an ODB object
332 *
333 * This is the OID from which the object was read from
334 *
335 * @param object the object
336 * @return a pointer to the OID
337 */
338 GIT_EXTERN(const git_oid *) git_odb_object_id(git_odb_object *object);
339
340 /**
341 * Return the data of an ODB object
342 *
343 * This is the uncompressed, raw data as read from the ODB,
344 * without the leading header.
345 *
346 * This pointer is owned by the object and shall not be free'd.
347 *
348 * @param object the object
349 * @return a pointer to the data
350 */
351 GIT_EXTERN(const void *) git_odb_object_data(git_odb_object *object);
352
353 /**
354 * Return the size of an ODB object
355 *
356 * This is the real size of the `data` buffer, not the
357 * actual size of the object.
358 *
359 * @param object the object
360 * @return the size
361 */
362 GIT_EXTERN(size_t) git_odb_object_size(git_odb_object *object);
363
364 /**
365 * Return the type of an ODB object
366 *
367 * @param object the object
368 * @return the type
369 */
370 GIT_EXTERN(git_otype) git_odb_object_type(git_odb_object *object);
371
372 /** @} */
373 GIT_END_DECL
374 #endif