Coverage Report

Created: 2026-10-17 07:15

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