at commit 286f72ca

1 #ifndef ODB_H
2 #define ODB_H
3
4 #include "object.h"
5 #include "oid-array.h"
6 #include "oidset.h"
7 #include "oidmap.h"
8 #include "string-list.h"
9 #include "thread-utils.h"
10
11 struct cached_object_entry;
12 struct list_objects_filter_options;
13 struct odb_source_inmemory;
14 struct packed_git;
15 struct repository;
16 struct strbuf;
17 struct strvec;
18
19 /*
20 * Set this to 0 to prevent odb_read_object_info_extended() from fetching missing
21 * blobs. This has a difference only if extensions.partialClone is set.
22 *
23 * Its default value is 1.
24 */
25 extern int fetch_if_missing;
26
27 /*
28 * Compute the exact path an alternate is at and returns it. In case of
29 * error NULL is returned and the human readable error is added to `err`
30 * `path` may be relative and should point to $GIT_DIR.
31 * `err` must not be null.
32 */
33 char *compute_alternate_path(const char *path, struct strbuf *err);
34
35 /*
36 * The object database encapsulates access to objects in a repository. It
37 * manages one or more sources that store the actual objects which are
38 * configured via alternates.
39 */
40 struct object_database {
41 /* Repository that owns this database. */
42 struct repository *repo;
43
44 /*
45 * State of current object database transaction. Only one
46 * transaction may be pending at a time. Is NULL when no transaction is
47 * configured.
48 */
49 struct odb_transaction *transaction;
50
51 /*
52 * Set of all object directories; the main directory is first (and
53 * cannot be NULL after initialization). Subsequent directories are
54 * alternates.
55 */
56 struct odb_source *sources;
57 struct odb_source **sources_tail;
58 struct kh_odb_path_map *source_by_path;
59
60 int loaded_alternates;
61
62 /*
63 * A list of alternate object directories loaded from the environment;
64 * this should not generally need to be accessed directly, but will
65 * populate the "sources" list when odb_prepare_alternates() is run.
66 */
67 char *alternate_db;
68
69 /*
70 * Objects that should be substituted by other objects
71 * (see git-replace(1)).
72 */
73 struct oidmap replace_map;
74 unsigned replace_map_initialized : 1;
75 pthread_mutex_t replace_mutex; /* protect object replace functions */
76
77 struct commit_graph *commit_graph;
78 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
79
80 /*
81 * This is meant to hold a *small* number of objects that you would
82 * want odb_read_object() to be able to return, but yet you do not want
83 * to write them into the object store (e.g. a browse-only
84 * application).
85 */
86 struct odb_source *inmemory_objects;
87
88 /*
89 * A fast, rough count of the number of objects in the repository.
90 * These two fields are not meant for direct access. Use
91 * odb_count_objects() instead.
92 */
93 unsigned long object_count;
94 unsigned object_count_flags;
95 unsigned object_count_valid : 1;
96
97 /*
98 * Submodule source paths that will be added as additional sources to
99 * allow lookup of submodule objects via the main object database.
100 */
101 struct string_list submodule_source_paths;
102 };
103
104 enum odb_new_flags {
105 /*
106 * Honor environment variables when constructing the object database
107 * sources. This makes us respect the following environment variables:
108 *
109 * - GIT_OBJECT_DIRECTORY to override the primary object directory.
110 *
111 * - GIT_ALTERNATE_OBJECT_DIRECTORIES to override alternates.
112 *
113 * Environment variables may be backend-specific.
114 */
115 ODB_NEW_HONOR_ENV = (1 << 0),
116 };
117
118 /*
119 * Create a new object database for the given repository.
120 *
121 * If the primary source parameter is set it will override the usual primary
122 * object directory derived from the repository's common directory. The
123 * alternate sources are expected to be a PATH_SEP-separated list of secondary
124 * sources. Note that these alternate sources will be added in addition to, not
125 * instead of, the alternates identified by the primary source.
126 *
127 * Returns the newly created object database.
128 */
129 struct object_database *odb_new(struct repository *repo,
130 enum odb_new_flags flags);
131
132 /* Free the object database and release all resources. */
133 void odb_free(struct object_database *o);
134
135 enum odb_optimize_strategy {
136 ODB_OPTIMIZE_INCREMENTAL,
137 ODB_OPTIMIZE_GEOMETRIC,
138 };
139
140 enum odb_optimize_flags {
141 /* Enable verbose logging and progress reporting. */
142 ODB_OPTIMIZE_VERBOSE = (1 << 0),
143
144 /* Perform auto-maintenance, only optimizing objects as required. */
145 ODB_OPTIMIZE_AUTO = (1 << 1),
146
147 /* Recompute existing deltas. */
148 ODB_OPTIMIZE_NO_REUSE_DELTAS = (1 << 2),
149 };
150
151 struct odb_optimize_options {
152 enum odb_optimize_strategy strategy;
153 enum odb_optimize_flags flags;
154 const char *prune_expire;
155 const char *expire_to;
156 int depth;
157 int window;
158
159 /* Backend-specific options. */
160 int keep_largest_pack;
161 int cruft_packs;
162 unsigned long max_cruft_size;
163 };
164
165 /*
166 * Optimize the object database. Returns 0 on success, a negative error code
167 * otherwise.
168 */
169 int odb_optimize(struct object_database *odb,
170 const struct odb_optimize_options *opts);
171
172 /*
173 * Check whether optimization of the object database is required given the
174 * provided options. Returns true if optimization should be performed, false
175 * otherwise.
176 */
177 bool odb_optimize_required(struct object_database *odb,
178 const struct odb_optimize_options *opts);
179
180 /*
181 * Close the object database and all of its sources so that any held resources
182 * will be released. The database can still be used after closing it, in which
183 * case these resources may be reallocated.
184 */
185 void odb_close(struct object_database *o);
186
187 enum odb_prepare_flags {
188 /*
189 * Flush caches, reload alternates and then re-prepare each object
190 * source so that new objects may become accessible.
191 */
192 ODB_PREPARE_FLUSH_CACHES = (1 << 0),
193 };
194
195 /*
196 * Prepare the object database for use. Calling this function is generally not
197 * needed, but can be useful in case the caller wants to pre-open individual
198 * sources.
199 */
200 void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
201
202 /* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
203 void odb_reprepare(struct object_database *o);
204
205 /*
206 * Find source by its object directory path. Returns a `NULL` pointer in case
207 * the source could not be found.
208 */
209 struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
210
211 /* Same as `odb_find_source()`, but dies in case the source doesn't exist. */
212 struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir);
213
214 /*
215 * Replace the current writable object directory with the specified temporary
216 * object directory; returns the former primary source.
217 */
218 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
219 const char *dir, int will_destroy);
220
221 /*
222 * Restore the primary source that was previously replaced by
223 * `odb_set_temporary_primary_source()`.
224 */
225 void odb_restore_primary_source(struct object_database *odb,
226 struct odb_source *restore_source,
227 const char *old_path);
228
229 /*
230 * Call odb_add_submodule_source_by_path() to add the submodule at the given
231 * path to a list. The object stores of all submodules in that list will be
232 * added as additional sources in the object store when looking up objects.
233 */
234 void odb_add_submodule_source_by_path(struct object_database *odb,
235 const char *path);
236
237 /*
238 * Iterate through all alternates of the database and execute the provided
239 * callback function for each of them. Stop iterating once the callback
240 * function returns a non-zero value, in which case the value is bubbled up
241 * from the callback.
242 */
243 typedef int odb_for_each_alternate_fn(struct odb_source *, void *);
244 int odb_for_each_alternate(struct object_database *odb,
245 odb_for_each_alternate_fn cb, void *payload);
246
247 /*
248 * Iterate through all alternates of the database and yield their respective
249 * references.
250 */
251 typedef void odb_for_each_alternate_ref_fn(const struct object_id *oid, void *);
252 void odb_for_each_alternate_ref(struct object_database *odb,
253 odb_for_each_alternate_ref_fn cb, void *payload);
254
255 /*
256 * Create a temporary file rooted in the primary alternate's directory, or die
257 * on failure. The filename is taken from "pattern", which should have the
258 * usual "XXXXXX" trailer, and the resulting filename is written into the
259 * "template" buffer. Returns the open descriptor.
260 */
261 int odb_mkstemp(struct object_database *odb,
262 struct strbuf *temp_filename, const char *pattern);
263
264 /*
265 * Prepare alternate object sources for the given database by reading
266 * "objects/info/alternates" and opening the respective sources.
267 */
268 void odb_prepare_alternates(struct object_database *odb);
269
270 /*
271 * Check whether the object database has any alternates. The primary object
272 * source does not count as alternate.
273 */
274 int odb_has_alternates(struct object_database *odb);
275
276 /*
277 * Add the directory to the on-disk alternates file; the new entry will also
278 * take effect in the current process.
279 */
280 void odb_add_to_alternates_file(struct object_database *odb,
281 const char *dir);
282
283 /*
284 * Add the directory to the in-memory list of alternate sources (along with any
285 * recursive alternates it points to), but do not modify the on-disk alternates
286 * file.
287 */
288 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
289 const char *dir);
290
291 /*
292 * Read an object from the database. Returns the object data and assigns object
293 * type and size to the `type` and `size` pointers, if these pointers are
294 * non-NULL. Returns a `NULL` pointer in case the object does not exist.
295 *
296 * This function dies on corrupt objects; the callers who want to deal with
297 * them should arrange to call odb_read_object_info_extended() and give error
298 * messages themselves.
299 */
300 void *odb_read_object(struct object_database *odb,
301 const struct object_id *oid,
302 enum object_type *type,
303 size_t *size);
304
305 void *odb_read_object_peeled(struct object_database *odb,
306 const struct object_id *oid,
307 enum object_type required_type,
308 size_t *size,
309 struct object_id *oid_ret);
310
311 /*
312 * Add an object file to the in-memory object store, without writing it
313 * to disk.
314 *
315 * Callers are responsible for calling write_object_file to record the
316 * object in persistent storage before writing any other new objects
317 * that reference it.
318 */
319 int odb_pretend_object(struct object_database *odb,
320 void *buf, size_t len, enum object_type type,
321 struct object_id *oid);
322
323 /*
324 * Object database source information that can be used to uniquely identify an
325 * object and learn more about how exactly it is stored.
326 */
327 struct odb_source_info {
328 /* The source that this object has been looked up from. */
329 struct odb_source *source;
330
331 /*
332 * Backend-specific information about the specific object. This can be
333 * used for example to uniquely identify a given object in case it
334 * exists multiple times.
335 */
336 union {
337 /*
338 * struct {
339 * ... Nothing to expose in this case
340 * } cached;
341 * struct {
342 * ... Nothing to expose in this case
343 * } loose;
344 */
345 struct {
346 struct packed_git *pack;
347 off_t offset;
348 enum packed_object_type {
349 PACKED_OBJECT_TYPE_UNKNOWN,
350 PACKED_OBJECT_TYPE_FULL,
351 PACKED_OBJECT_TYPE_OFS_DELTA,
352 PACKED_OBJECT_TYPE_REF_DELTA,
353 } type;
354 } packed;
355 } u;
356 };
357
358 /*
359 * The object info contains the query and response that is to be used for
360 * functions that end up reading object information. Callers are expected to
361 * populate pointers whose information they want to request.
362 */
363 struct object_info {
364 /* The object type. */
365 enum object_type *typep;
366
367 /* The inflated object size in bytes. */
368 size_t *sizep;
369
370 /* The object size as stored on disk. */
371 off_t *disk_sizep;
372
373 /*
374 * The base the object is deltified against, in case it is stored as a
375 * delta.
376 */
377 struct object_id *delta_base_oid;
378
379 /* The object contents. Ownership of memory goes over to the caller. */
380 void **contentp;
381
382 /*
383 * The time the given looked-up object has been last modified.
384 *
385 * Note: the mtime may be ambiguous in case the object exists multiple
386 * times in the object database. It is thus _not_ recommended to use
387 * this field outside of contexts where you would read every instance
388 * of the object, like for example with `odb_for_each_object()`. As it
389 * is impossible to say at the ODB level what the intent of the caller
390 * is (e.g. whether to find the oldest or newest object), it is the
391 * responsibility of the caller to disambiguate the mtimes.
392 */
393 time_t *mtimep;
394
395 /*
396 * Backend-specific information that tells the caller where exactly an
397 * object was looked up from. This information should help disambiguate
398 * object lookups in case the same object exists in multiple sources,
399 * or multiple times in the same source.
400 */
401 struct odb_source_info *source_infop;
402
403 /*
404 * object-info protocol specific. Set by the protocol when the remote
405 * does not recognize the requested object.
406 */
407 unsigned int unrecognized:1;
408 };
409
410 /*
411 * Initializer for a "struct object_info" that wants no items. You may
412 * also memset() the memory to all-zeroes.
413 */
414 #define OBJECT_INFO_INIT { 0 }
415
416 /* Flags that can be passed to `odb_read_object_info_extended()`. */
417 enum object_info_flags {
418 /* Invoke lookup_replace_object() on the given hash. */
419 OBJECT_INFO_LOOKUP_REPLACE = (1 << 0),
420
421 /* Do not reprepare object sources when the first lookup has failed. */
422 OBJECT_INFO_QUICK = (1 << 1),
423
424 /*
425 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
426 * nonzero).
427 */
428 OBJECT_INFO_SKIP_FETCH_OBJECT = (1 << 2),
429
430 /* Die if object corruption (not just an object being missing) was detected. */
431 OBJECT_INFO_DIE_IF_CORRUPT = (1 << 3),
432
433 /*
434 * We have already tried reading the object, but it couldn't be found
435 * via any of the attached sources, and are now doing a second read.
436 * This second read asks the individual sources to also evaluate
437 * whether any on-disk state may have changed that may have caused the
438 * object to appear.
439 *
440 * This flag is for internal use, only. The second read only occurs
441 * when `OBJECT_INFO_QUICK` was not passed.
442 */
443 OBJECT_INFO_SECOND_READ = (1 << 4),
444
445 /*
446 * This is meant for bulk prefetching of missing blobs in a partial
447 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK.
448 */
449 OBJECT_INFO_FOR_PREFETCH = (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK),
450 };
451
452 /*
453 * Read object info from the object database and populate the `object_info`
454 * structure. Returns 0 on success, a negative error code otherwise.
455 */
456 int odb_read_object_info_extended(struct object_database *odb,
457 const struct object_id *oid,
458 struct object_info *oi,
459 enum object_info_flags flags);
460
461 /*
462 * Read a subset of object info for the given object ID. Returns an `enum
463 * object_type` on success, a negative error code otherwise. If successful and
464 * `sizep` is non-NULL, then the size of the object will be written to the
465 * pointer.
466 */
467 int odb_read_object_info(struct object_database *odb,
468 const struct object_id *oid,
469 size_t *sizep);
470
471 enum odb_has_object_flags {
472 /* Retry packed storage after checking packed and loose storage */
473 ODB_HAS_OBJECT_RECHECK_PACKED = (1 << 0),
474 /* Allow fetching the object in case the repository has a promisor remote. */
475 ODB_HAS_OBJECT_FETCH_PROMISOR = (1 << 1),
476 };
477
478 /*
479 * Returns 1 if the object exists. This function will not lazily fetch objects
480 * in a partial clone by default.
481 */
482 int odb_has_object(struct object_database *odb,
483 const struct object_id *oid,
484 enum odb_has_object_flags flags);
485
486 int odb_freshen_object(struct object_database *odb,
487 const struct object_id *oid);
488
489 void odb_assert_oid_type(struct object_database *odb,
490 const struct object_id *oid, enum object_type expect);
491
492 /*
493 * Enabling the object read lock allows multiple threads to safely call the
494 * following functions in parallel: odb_read_object(),
495 * odb_read_object_peeled(), odb_read_object_info() and odb().
496 *
497 * obj_read_lock() and obj_read_unlock() may also be used to protect other
498 * section which cannot execute in parallel with object reading. Since the used
499 * lock is a recursive mutex, these sections can even contain calls to object
500 * reading functions. However, beware that in these cases zlib inflation won't
501 * be performed in parallel, losing performance.
502 *
503 * TODO: odb_read_object_info_extended()'s call stack has a recursive behavior. If
504 * any of its callees end up calling it, this recursive call won't benefit from
505 * parallel inflation.
506 */
507 void enable_obj_read_lock(void);
508 void disable_obj_read_lock(void);
509
510 extern int obj_read_use_lock;
511 extern pthread_mutex_t obj_read_mutex;
512
513 static inline void obj_read_lock(void)
514 {
515 if(obj_read_use_lock)
516 pthread_mutex_lock(&obj_read_mutex);
517 }
518
519 static inline void obj_read_unlock(void)
520 {
521 if(obj_read_use_lock)
522 pthread_mutex_unlock(&obj_read_mutex);
523 }
524
525 /* Flags for for_each_*_object(). */
526 enum odb_for_each_object_flags {
527 /* Iterate only over local objects, not alternates. */
528 ODB_FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
529
530 /* Only iterate over packs obtained from the promisor remote. */
531 ODB_FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
532
533 /*
534 * Visit objects within a pack in packfile order rather than .idx order
535 */
536 ODB_FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
537
538 /* Only iterate over packs that are not marked as kept in-core. */
539 ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),
540
541 /* Only iterate over packs that do not have .keep files. */
542 ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
543 };
544
545 /*
546 * A callback function that can be used to iterate through objects. If given,
547 * the optional `oi` parameter will be populated the same as if you would call
548 * `odb_read_object_info()`.
549 *
550 * Returning a non-zero error code will cause iteration to abort. The error
551 * code will be propagated.
552 */
553 typedef int (*odb_for_each_object_cb)(const struct object_id *oid,
554 struct object_info *oi,
555 void *cb_data);
556
557 /*
558 * Options that can be passed to `odb_for_each_object()` and its
559 * backend-specific implementations.
560 */
561 struct odb_for_each_object_options {
562 /* A bitfield of `odb_for_each_object_flags`. */
563 enum odb_for_each_object_flags flags;
564
565 /*
566 * If set, only iterate through objects whose first `prefix_hex_len`
567 * hex characters matches the given prefix.
568 */
569 const struct object_id *prefix;
570 size_t prefix_hex_len;
571
572 /*
573 * Optional object filter that allows backends to skip yielding
574 * objects that are excluded by the filter as an optimization. The
575 * filter is a best-effort hint: backends may use it to skip
576 * excluded objects (e.g. by consulting a reachability bitmap), but
577 * are also free to ignore it entirely and yield every object. As a
578 * consequence, callers must re-apply the filter on yielded objects
579 * if they require strict filtering semantics.
580 */
581 const struct list_objects_filter_options *filter;
582 };
583
584 /*
585 * Iterate through all objects contained in the object database. Note that
586 * objects may be iterated over multiple times in case they are either stored
587 * in different backends or in case they are stored in multiple sources.
588 * If an object info request is given, then the object info will be read and
589 * passed to the callback as if `odb_read_object_info()` was called for the
590 * object.
591 *
592 * Returning a non-zero error code from the callback function will cause
593 * iteration to abort. The error code will be propagated.
594 *
595 * Returns 0 on success, a negative error code in case a failure occurred, or
596 * an arbitrary non-zero error code returned by the callback itself.
597 */
598 int odb_for_each_object_ext(struct object_database *odb,
599 const struct object_info *request,
600 odb_for_each_object_cb cb,
601 void *cb_data,
602 const struct odb_for_each_object_options *opts);
603
604 /* Same as `odb_for_each_object_ext()` with `opts.flags` set to the given flags. */
605 int odb_for_each_object(struct object_database *odb,
606 const struct object_info *request,
607 odb_for_each_object_cb cb,
608 void *cb_data,
609 enum odb_for_each_object_flags flags);
610
611 enum odb_count_objects_flags {
612 /*
613 * Instead of providing an accurate count, allow the number of objects
614 * to be approximated. Details of how this approximation works are
615 * subject to the specific source's implementation.
616 */
617 ODB_COUNT_OBJECTS_APPROXIMATE = (1 << 0),
618 };
619
620 /*
621 * Count the number of objects in the given object database. This object count
622 * may double-count objects that are stored in multiple backends, or which are
623 * stored multiple times in a single backend.
624 *
625 * Returns 0 on success, a negative error code otherwise. The number of objects
626 * will be assigned to the `out` pointer on success.
627 */
628 int odb_count_objects(struct object_database *odb,
629 enum odb_count_objects_flags flags,
630 unsigned long *out);
631
632 /*
633 * Given an object ID, find the minimum required length required to make the
634 * object ID unique across the whole object database.
635 *
636 * The `min_len` determines the minimum abbreviated length that'll be returned
637 * by this function. If `min_len < 0`, then the function will set a sensible
638 * default minimum abbreviation length.
639 *
640 * Returns 0 on success, a negative error code otherwise. The computed length
641 * will be assigned to `*out`.
642 */
643 int odb_find_abbrev_len(struct object_database *odb,
644 const struct object_id *oid,
645 int min_len,
646 unsigned *out);
647
648 enum odb_write_object_flags {
649 /*
650 * By default, `odb_write_object()` does not actually write anything
651 * into the object store, but only computes the object ID. This flag
652 * changes that so that the object will be written as a loose object
653 * and persisted.
654 */
655 ODB_WRITE_OBJECT_PERSIST = (1 << 0),
656
657 /*
658 * Do not print an error in case something goes wrong.
659 */
660 ODB_WRITE_OBJECT_SILENT = (1 << 1),
661 };
662
663 /*
664 * Write an object into the object database. The object is being written into
665 * the local alternate of the repository. If provided, the object ID of the
666 * final object is written into `oid`.
667 *
668 * If the caller provides a `compat_oid`, then this compatibility object hash
669 * will be stored instead of computing the compatibility hash ad-hoc.
670 *
671 * Returns 0 on success, a negative error code otherwise.
672 */
673 int odb_write_object_ext(struct object_database *odb,
674 const void *buf, unsigned long len,
675 enum object_type type,
676 struct object_id *oid,
677 const struct object_id *compat_oid,
678 enum odb_write_object_flags flags);
679
680 static inline int odb_write_object(struct object_database *odb,
681 const void *buf, unsigned long len,
682 enum object_type type,
683 struct object_id *oid)
684 {
685 return odb_write_object_ext(odb, buf, len, type, oid, NULL, 0);
686 }
687
688 struct odb_stream;
689
690 int odb_write_object_stream(struct object_database *odb,
691 struct odb_stream *stream,
692 struct object_id *oid);
693
694 /*
695 * Options for generating a packfile via `odb_generate_pack()`.
696 */
697 struct odb_generate_pack_options {
698 /* Tips of the object graph that shall be packed. */
699 struct oid_array wants;
700
701 /*
702 * Boundary of the object graph. Objects reachable from any of these
703 * tips are expected to already be available to whoever consumes the
704 * pack and shall thus not be packed.
705 */
706 struct oid_array haves;
707
708 /*
709 * The shallow boundary that shall be used when computing object
710 * reachability. When set, any shallow information of the repository
711 * itself shall be ignored in favor of these objects.
712 */
713 struct oid_array shallows;
714
715 /*
716 * Pre-expanded object filter specification that limits the set of
717 * objects that shall be packed. May be `NULL` in case no filter shall
718 * be applied.
719 */
720 const char *filter_spec;
721
722 /*
723 * Protocols that may be used to offload objects via packfile URIs.
724 * May be `NULL` in case packfile URIs shall not be used.
725 */
726 const struct string_list *uri_protocols;
727
728 /*
729 * Hook command that shall be executed instead of the internal
730 * machinery to generate the pack. It is up to the specific backend
731 * whether or not this hook is supported. May be `NULL` in case no
732 * hook shall be executed.
733 */
734 const char *pack_objects_hook;
735
736 /*
737 * File descriptor that the generated pack shall be written to. If set
738 * to `-1`, a pipe will be created and exposed via the pack generator's
739 * `out` field. If set to `0`, the pack will be written to the standard
740 * output stream. Otherwise, the provided descriptor will be written to
741 * and is consumed by the generator.
742 */
743 int pack_fd;
744
745 /*
746 * File descriptor that progress output shall be written to. The same
747 * semantics as for `pack_fd` apply, except that `0` will cause the
748 * generator to write to stderr instead of stdout.
749 */
750 int progress_fd;
751
752 /* Whether to print progress or not. */
753 enum {
754 /* Don't print progress output. */
755 ODB_GENERATE_PACK_PROGRESS_NONE,
756
757 /*
758 * Print progress while computing the packfile, but stop
759 * printing progress once starting to write it.
760 */
761 ODB_GENERATE_PACK_PROGRESS_STANDARD,
762
763 /*
764 * Similar to STANDARD, but also print progress when writing
765 * the packfile.
766 */
767 ODB_GENERATE_PACK_PROGRESS_VERBOSE,
768 } progress;
769
770 /* Allow the pack to contain deltas against unpacked objects. */
771 unsigned thin:1;
772
773 /* Use offset deltas instead of reference deltas. */
774 unsigned ofs_delta:1;
775
776 /* Include unasked-for annotated tags of packed objects. */
777 unsigned include_tag:1;
778
779 /* The generated pack is destined for a shallow consumer. */
780 unsigned shallow:1;
781
782 /* Allow objects that may be missing due to a promisor remote. */
783 unsigned missing_allow_promisor:1;
784
785 /* Do not use bitmap indices when computing reachability. */
786 unsigned disable_bitmaps:1;
787 };
788
789 #define ODB_GENERATE_PACK_OPTIONS_INIT { \
790 .wants = OID_ARRAY_INIT, \
791 .haves = OID_ARRAY_INIT, \
792 .shallows = OID_ARRAY_INIT, \
793 .pack_fd = -1, \
794 }
795
796 /* Release resources associated with the options. */
797 void odb_generate_pack_options_release(struct odb_generate_pack_options *opts);
798
799 /*
800 * A handle for an ongoing packfile generation as started via
801 * `odb_generate_pack()`.
802 */
803 struct odb_pack_generator {
804 /*
805 * File descriptor from which the generated pack can be read. Only set
806 * when the pack generation was started with `pack_fd == -1`. The
807 * caller is responsible for closing the descriptor.
808 */
809 int out;
810
811 /*
812 * File descriptor from which progress output can be read. Only set
813 * when the pack generation was started with `progress_fd == -1`. The
814 * caller is responsible for closing the descriptor.
815 */
816 int err;
817
818 /*
819 * Callback function to finish this generator. This callback is
820 * expected to wait for the packfile generation to complete and to then
821 * free the generator itself.
822 */
823 int (*finish)(struct odb_pack_generator *);
824 };
825
826 /*
827 * Start generating a packfile from the object database with the given
828 * options. The pack is generated asynchronously; the caller is expected to
829 * consume the file descriptors exposed via the pack generator and to then
830 * wait for completion via `odb_pack_generator_finish()`.
831 *
832 * Returns 0 on success and populates the `out` pointer with the pack
833 * generator. Returns a negative error code otherwise.
834 */
835 int odb_generate_pack(struct object_database *odb,
836 struct odb_pack_generator **out,
837 const struct odb_generate_pack_options *opts);
838
839 /*
840 * Wait for the packfile generation to complete and free the pack generator.
841 * Returns 0 on success, a negative error code otherwise.
842 */
843 int odb_pack_generator_finish(struct odb_pack_generator *generator);
844
845 void parse_alternates(const char *string,
846 int sep,
847 const char *relative_base,
848 struct strvec *out);
849
850 #endif /* ODB_H */