at commit 0d5ebb32

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