blob: 82201ec3e7b09ca5fa53b5be9d5ce8d144c1d42e [file] [log] [blame]
Stefan Beller90c62152018-03-23 18:20:55 +01001#ifndef OBJECT_STORE_H
2#define OBJECT_STORE_H
3
Elijah Newren1c028402023-02-24 00:09:32 +00004#include "object.h"
Stefan Bellerd88f9fd2018-04-11 17:21:05 -07005#include "oidmap.h"
Jonathan Tan14727b72018-07-11 15:42:38 -07006#include "list.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -04007#include "oid-array.h"
Jonathan Tan14727b72018-07-11 15:42:38 -07008#include "strbuf.h"
Matheus Tavares31877c92020-01-15 23:39:53 -03009#include "thread-utils.h"
Eric Wongcf2dc1c2021-07-07 23:10:15 +000010#include "khash.h"
11#include "dir.h"
Eric Wong92d8ed82021-07-07 23:10:19 +000012#include "oidtree.h"
René Scharfe09ef6612021-09-11 22:43:26 +020013#include "oidset.h"
Stefan Bellerd88f9fd2018-04-11 17:21:05 -070014
Jeff King263db402018-11-12 09:48:47 -050015struct object_directory {
16 struct object_directory *next;
Stefan Beller0d4a1322018-03-23 18:20:56 +010017
Stefan Beller0d4a1322018-03-23 18:20:56 +010018 /*
Jeff King3a2e0822018-11-12 09:50:56 -050019 * Used to store the results of readdir(3) calls when we are OK
20 * sacrificing accuracy due to races for speed. That includes
Jeff King61c77112018-11-12 09:54:42 -050021 * object existence with OBJECT_INFO_QUICK, as well as
Jeff King3a2e0822018-11-12 09:50:56 -050022 * our search for unique abbreviated hashes. Don't use it for tasks
23 * requiring greater accuracy!
24 *
25 * Be sure to call odb_load_loose_cache() before using.
Stefan Beller0d4a1322018-03-23 18:20:56 +010026 */
Eric Wong33f379e2021-07-07 23:10:17 +000027 uint32_t loose_objects_subdir_seen[8]; /* 256 bits */
Eric Wong92d8ed82021-07-07 23:10:19 +000028 struct oidtree *loose_objects_cache;
Stefan Beller0d4a1322018-03-23 18:20:56 +010029
Stefan Beller77f012e2018-03-23 18:21:08 +010030 /*
Neeraj Singhecd81df2021-12-06 22:05:05 +000031 * This is a temporary object store created by the tmp_objdir
32 * facility. Disable ref updates since the objects in the store
33 * might be discarded on rollback.
34 */
35 int disable_ref_updates;
36
37 /*
Neeraj Singhb3cecf42021-12-06 22:05:04 +000038 * This object store is ephemeral, so there is no need to fsync.
39 */
40 int will_destroy;
41
42 /*
Stefan Beller77f012e2018-03-23 18:21:08 +010043 * Path to the alternative object store. If this is a relative path,
44 * it is relative to the current working directory.
45 */
Jeff Kingf0eaf632018-11-12 09:50:39 -050046 char *path;
Stefan Beller031dc922018-03-23 18:20:57 +010047};
Jeff Kingf0eaf632018-11-12 09:50:39 -050048
Han Xin2b6070a2022-06-11 10:44:19 +080049struct input_stream {
50 const void *(*read)(struct input_stream *, unsigned long *len);
51 void *data;
52 int is_finished;
53};
54
Eric Wongcf2dc1c2021-07-07 23:10:15 +000055KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
Carlo Marcelo Arenas Belóndd3c8a72021-08-08 18:38:32 -070056 struct object_directory *, 1, fspathhash, fspatheq)
Eric Wongcf2dc1c2021-07-07 23:10:15 +000057
Stefan Beller13068bf2018-03-23 18:21:09 +010058void prepare_alt_odb(struct repository *r);
Stefan Beller0d4a1322018-03-23 18:20:56 +010059char *compute_alternate_path(const char *path, struct strbuf *err);
Taylor Blauf57a7392021-09-01 16:34:01 -040060struct object_directory *find_odb(struct repository *r, const char *obj_dir);
Jeff King263db402018-11-12 09:48:47 -050061typedef int alt_odb_fn(struct object_directory *, void *);
Stefan Beller0d4a1322018-03-23 18:20:56 +010062int foreach_alt_odb(alt_odb_fn, void*);
Jeff King709dfa62019-07-01 09:17:40 -040063typedef void alternate_ref_fn(const struct object_id *oid, void *);
64void for_each_alternate_ref(alternate_ref_fn, void *);
Stefan Beller0d4a1322018-03-23 18:20:56 +010065
66/*
Stefan Beller0d4a1322018-03-23 18:20:56 +010067 * Add the directory to the on-disk alternates file; the new entry will also
68 * take effect in the current process.
69 */
70void add_to_alternates_file(const char *dir);
71
72/*
73 * Add the directory to the in-memory list of alternates (along with any
74 * recursive alternates it points to), but do not modify the on-disk alternates
75 * file.
76 */
77void add_to_alternates_memory(const char *dir);
78
Jeff King3a2e0822018-11-12 09:50:56 -050079/*
Neeraj Singhb3cecf42021-12-06 22:05:04 +000080 * Replace the current writable object directory with the specified temporary
81 * object directory; returns the former primary object directory.
82 */
83struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy);
84
85/*
86 * Restore a previous ODB replaced by set_temporary_main_odb.
87 */
88void restore_primary_odb(struct object_directory *restore_odb, const char *old_path);
89
90/*
René Scharfe0000d652019-01-06 17:45:30 +010091 * Populate and return the loose object cache array corresponding to the
92 * given object ID.
93 */
Eric Wong92d8ed82021-07-07 23:10:19 +000094struct oidtree *odb_loose_cache(struct object_directory *odb,
René Scharfe0000d652019-01-06 17:45:30 +010095 const struct object_id *oid);
96
René Scharfed4e19e52019-01-06 17:45:39 +010097/* Empty the loose object cache for the specified object directory. */
98void odb_clear_loose_cache(struct object_directory *odb);
99
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000100/* Clear and free the specified object directory */
101void free_object_directory(struct object_directory *odb);
102
Stefan Bellera80d72d2018-03-23 18:20:59 +0100103struct packed_git {
Colin Stolleyec485402019-11-27 16:24:53 -0600104 struct hashmap_entry packmap_ent;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100105 struct packed_git *next;
106 struct list_head mru;
107 struct pack_window *windows;
108 off_t pack_size;
109 const void *index_data;
110 size_t index_size;
111 uint32_t num_objects;
brian m. carlson629dffc2020-05-25 19:59:10 +0000112 uint32_t crc_offset;
René Scharfe09ef6612021-09-11 22:43:26 +0200113 struct oidset bad_objects;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100114 int index_version;
115 time_t mtime;
116 int pack_fd;
Nguyễn Thái Ngọc Duy43fa44f2018-04-14 17:35:05 +0200117 int index; /* for builtin/pack-objects.c */
Stefan Bellera80d72d2018-03-23 18:20:59 +0100118 unsigned pack_local:1,
119 pack_keep:1,
Nguyễn Thái Ngọc Duyed7e5fc2018-04-15 17:36:13 +0200120 pack_keep_in_core:1,
Stefan Bellera80d72d2018-03-23 18:20:59 +0100121 freshened:1,
122 do_not_close:1,
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700123 pack_promisor:1,
Taylor Blau94cd7752022-05-20 19:17:35 -0400124 multi_pack_index:1,
125 is_cruft:1;
brian m. carlson538b1522019-02-19 00:05:03 +0000126 unsigned char hash[GIT_MAX_RAWSZ];
Stefan Bellera80d72d2018-03-23 18:20:59 +0100127 struct revindex_entry *revindex;
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500128 const uint32_t *revindex_data;
129 const uint32_t *revindex_map;
130 size_t revindex_size;
Taylor Blau94cd7752022-05-20 19:17:35 -0400131 /*
132 * mtimes_map points at the beginning of the memory mapped region of
133 * this pack's corresponding .mtimes file, and mtimes_size is the size
134 * of that .mtimes file
135 */
136 const uint32_t *mtimes_map;
137 size_t mtimes_size;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100138 /* something like ".git/objects/pack/xxxxx.pack" */
139 char pack_name[FLEX_ARRAY]; /* more */
140};
141
Derrick Stolee4d805602018-07-12 15:39:23 -0400142struct multi_pack_index;
143
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200144static inline int pack_map_entry_cmp(const void *cmp_data UNUSED,
Colin Stolleyec485402019-11-27 16:24:53 -0600145 const struct hashmap_entry *entry,
146 const struct hashmap_entry *entry2,
147 const void *keydata)
148{
149 const char *key = keydata;
150 const struct packed_git *pg1, *pg2;
151
152 pg1 = container_of(entry, const struct packed_git, packmap_ent);
153 pg2 = container_of(entry2, const struct packed_git, packmap_ent);
154
155 return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
156}
157
Stefan Beller90c62152018-03-23 18:20:55 +0100158struct raw_object_store {
159 /*
Jeff Kingf0eaf632018-11-12 09:50:39 -0500160 * Set of all object directories; the main directory is first (and
161 * cannot be NULL after initialization). Subsequent directories are
162 * alternates.
Stefan Beller90c62152018-03-23 18:20:55 +0100163 */
Jeff Kingf0eaf632018-11-12 09:50:39 -0500164 struct object_directory *odb;
165 struct object_directory **odb_tail;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000166 kh_odb_path_map_t *odb_by_path;
167
Jeff Kingf0eaf632018-11-12 09:50:39 -0500168 int loaded_alternates;
Stefan Beller90c62152018-03-23 18:20:55 +0100169
Jeff Kingf0eaf632018-11-12 09:50:39 -0500170 /*
171 * A list of alternate object directories loaded from the environment;
172 * this should not generally need to be accessed directly, but will
173 * populate the "odb" list when prepare_alt_odb() is run.
174 */
Stefan Beller90c62152018-03-23 18:20:55 +0100175 char *alternate_db;
Stefan Beller031dc922018-03-23 18:20:57 +0100176
Stefan Bellera80d72d2018-03-23 18:20:59 +0100177 /*
Stefan Bellerd88f9fd2018-04-11 17:21:05 -0700178 * Objects that should be substituted by other objects
179 * (see git-replace(1)).
180 */
Stefan Bellerc1274492018-04-11 17:21:07 -0700181 struct oidmap *replace_map;
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300182 unsigned replace_map_initialized : 1;
183 pthread_mutex_t replace_mutex; /* protect object replace functions */
Stefan Bellerd88f9fd2018-04-11 17:21:05 -0700184
Jonathan Tan85277502018-07-11 15:42:41 -0700185 struct commit_graph *commit_graph;
186 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
187
Stefan Bellerd88f9fd2018-04-11 17:21:05 -0700188 /*
Stefan Bellera80d72d2018-03-23 18:20:59 +0100189 * private data
190 *
Derrick Stoleec4d25222018-07-12 15:39:33 -0400191 * should only be accessed directly by packfile.c and midx.c
192 */
193 struct multi_pack_index *multi_pack_index;
194
195 /*
196 * private data
197 *
Stefan Bellera80d72d2018-03-23 18:20:59 +0100198 * should only be accessed directly by packfile.c
199 */
200
201 struct packed_git *packed_git;
202 /* A most-recently-used ordered version of the packed_git list. */
203 struct list_head packed_git_mru;
Stefan Beller5508f692018-03-23 18:21:01 +0100204
Jeff King20b031f2021-02-22 21:25:23 -0500205 struct {
206 struct packed_git **packs;
207 unsigned flags;
208 } kept_pack_cache;
209
Stefan Beller5508f692018-03-23 18:21:01 +0100210 /*
Colin Stolleyec485402019-11-27 16:24:53 -0600211 * A map of packfiles to packed_git structs for tracking which
212 * packs have been loaded already.
213 */
214 struct hashmap pack_map;
215
216 /*
Stefan Beller9a005802018-03-23 18:21:02 +0100217 * A fast, rough count of the number of objects in the repository.
218 * These two fields are not meant for direct access. Use
219 * approximate_object_count() instead.
220 */
221 unsigned long approximate_object_count;
222 unsigned approximate_object_count_valid : 1;
223
224 /*
Stefan Beller5508f692018-03-23 18:21:01 +0100225 * Whether packed_git has already been populated with this repository's
226 * packs.
227 */
228 unsigned packed_git_initialized : 1;
Stefan Beller90c62152018-03-23 18:20:55 +0100229};
230
231struct raw_object_store *raw_object_store_new(void);
232void raw_object_store_clear(struct raw_object_store *o);
233
Stefan Bellercf78ae42018-03-23 18:21:10 +0100234/*
235 * Put in `buf` the name of the file in the local object database that
Jeff King514c5fd2019-01-07 03:35:42 -0500236 * would be used to store a loose object with the specified oid.
Stefan Bellercf78ae42018-03-23 18:21:10 +0100237 */
Jeff King514c5fd2019-01-07 03:35:42 -0500238const char *loose_object_path(struct repository *r, struct strbuf *buf,
239 const struct object_id *oid);
Stefan Bellercf78ae42018-03-23 18:21:10 +0100240
Jeff King514c5fd2019-01-07 03:35:42 -0500241void *map_loose_object(struct repository *r, const struct object_id *oid,
242 unsigned long *size);
Stefan Bellere35454f2018-03-23 18:21:14 +0100243
Jeff King0ba05cf2023-01-07 08:50:33 -0500244void *repo_read_object_file(struct repository *r,
245 const struct object_id *oid,
246 enum object_type *type,
247 unsigned long *size);
Stefan Bellerafd69dc2018-11-13 16:12:47 -0800248#ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
249#define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size)
250#endif
Stefan Bellercbd53a22018-05-15 16:42:15 -0700251
252/* Read and unpack an object file into memory, write memory to an object file */
253int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
254
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +0100255void hash_object_file(const struct git_hash_algo *algo, const void *buf,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100256 unsigned long len, enum object_type type,
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +0100257 struct object_id *oid);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700258
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +0200259int write_object_file_flags(const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100260 enum object_type type, struct object_id *oid,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +0200261 unsigned flags);
262static inline int write_object_file(const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100263 enum object_type type, struct object_id *oid)
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +0200264{
265 return write_object_file_flags(buf, len, type, oid, 0);
266}
Stefan Bellercbd53a22018-05-15 16:42:15 -0700267
Ævar Arnfjörð Bjarmason0ff7b4f2022-02-05 00:48:31 +0100268int write_object_file_literally(const void *buf, unsigned long len,
269 const char *type, struct object_id *oid,
270 unsigned flags);
Han Xin2b6070a2022-06-11 10:44:19 +0800271int stream_loose_object(struct input_stream *in_stream, size_t len,
272 struct object_id *oid);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700273
Jonathan Nieder60440d72020-01-03 16:13:31 -0800274/*
275 * Add an object file to the in-memory object store, without writing it
276 * to disk.
277 *
278 * Callers are responsible for calling write_object_file to record the
279 * object in persistent storage before writing any other new objects
280 * that reference it.
281 */
Denton Liu55454422019-04-29 04:28:14 -0400282int pretend_object_file(void *, unsigned long, enum object_type,
Denton Liuad6dad02019-04-29 04:28:23 -0400283 struct object_id *oid);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700284
Denton Liu55454422019-04-29 04:28:14 -0400285int force_object_loose(const struct object_id *oid, time_t mtime);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700286
Elijah Newren1c028402023-02-24 00:09:32 +0000287struct object_info {
288 /* Request */
289 enum object_type *typep;
290 unsigned long *sizep;
291 off_t *disk_sizep;
292 struct object_id *delta_base_oid;
293 struct strbuf *type_name;
294 void **contentp;
295
296 /* Response */
297 enum {
298 OI_CACHED,
299 OI_LOOSE,
300 OI_PACKED,
301 OI_DBCACHED
302 } whence;
303 union {
304 /*
305 * struct {
306 * ... Nothing to expose in this case
307 * } cached;
308 * struct {
309 * ... Nothing to expose in this case
310 * } loose;
311 */
312 struct {
313 struct packed_git *pack;
314 off_t offset;
315 unsigned int is_delta;
316 } packed;
317 } u;
318};
319
320/*
321 * Initializer for a "struct object_info" that wants no items. You may
322 * also memset() the memory to all-zeroes.
323 */
324#define OBJECT_INFO_INIT { 0 }
325
326/* Invoke lookup_replace_object() on the given hash */
327#define OBJECT_INFO_LOOKUP_REPLACE 1
328/* Allow reading from a loose object file of unknown/bogus type */
329#define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
330/* Do not retry packed storage after checking packed and loose storage */
331#define OBJECT_INFO_QUICK 8
332/*
333 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
334 * nonzero).
335 */
336#define OBJECT_INFO_SKIP_FETCH_OBJECT 16
337/*
338 * This is meant for bulk prefetching of missing blobs in a partial
339 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
340 */
341#define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
342
343/* Die if object corruption (not just an object being missing) was detected. */
344#define OBJECT_INFO_DIE_IF_CORRUPT 32
345
346int oid_object_info_extended(struct repository *r,
347 const struct object_id *,
348 struct object_info *, unsigned flags);
349
Stefan Bellercbd53a22018-05-15 16:42:15 -0700350/*
351 * Open the loose object at path, check its hash, and return the contents,
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +0200352 * use the "oi" argument to assert things about the object, or e.g. populate its
Stefan Bellercbd53a22018-05-15 16:42:15 -0700353 * type, and size. If the object is a blob, then "contents" may return NULL,
354 * to allow streaming of large blobs.
355 *
356 * Returns 0 on success, negative on error (details may be written to stderr).
357 */
358int read_loose_object(const char *path,
359 const struct object_id *expected_oid,
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +0200360 struct object_id *real_oid,
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +0200361 void **contents,
362 struct object_info *oi);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700363
Jonathan Tan1d8d9cb2020-08-05 16:06:49 -0700364/* Retry packed storage after checking packed and loose storage */
365#define HAS_OBJECT_RECHECK_PACKED 1
366
367/*
368 * Returns 1 if the object exists. This function will not lazily fetch objects
369 * in a partial clone.
370 */
371int has_object(struct repository *r, const struct object_id *oid,
372 unsigned flags);
373
374/*
375 * These macros and functions are deprecated. If checking existence for an
376 * object that is likely to be missing and/or whose absence is relatively
377 * inconsequential (or is consequential but the caller is prepared to handle
378 * it), use has_object(), which has better defaults (no lazy fetch in a partial
379 * clone and no rechecking of packed storage). In the unlikely event that a
380 * caller needs to assert existence of an object that it fully expects to
381 * exist, and wants to trigger a lazy fetch in a partial clone, use
382 * oid_object_info_extended() with a NULL struct object_info.
383 *
384 * These functions can be removed once all callers have migrated to
385 * has_object() and/or oid_object_info_extended().
386 */
Stefan Beller9b45f492018-11-13 16:12:48 -0800387int repo_has_object_file(struct repository *r, const struct object_id *oid);
388int repo_has_object_file_with_flags(struct repository *r,
389 const struct object_id *oid, int flags);
390#ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
391#define has_object_file(oid) repo_has_object_file(the_repository, oid)
392#define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags)
393#endif
Stefan Bellercbd53a22018-05-15 16:42:15 -0700394
395/*
396 * Return true iff an alternate object database has a loose object
397 * with the specified name. This function does not respect replace
398 * references.
399 */
Denton Liu55454422019-04-29 04:28:14 -0400400int has_loose_object_nonlocal(const struct object_id *);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700401
Taylor Blaub7573532022-05-20 19:17:52 -0400402int has_loose_object(const struct object_id *);
403
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +0100404/**
405 * format_object_header() is a thin wrapper around s xsnprintf() that
406 * writes the initial "<type> <obj-len>" part of the loose object
407 * header. It returns the size that snprintf() returns + 1.
408 */
409int format_object_header(char *str, size_t size, enum object_type type,
410 size_t objsize);
411
Denton Liu55454422019-04-29 04:28:14 -0400412void assert_oid_type(const struct object_id *oid, enum object_type expect);
Stefan Bellercbd53a22018-05-15 16:42:15 -0700413
Matheus Tavares31877c92020-01-15 23:39:53 -0300414/*
415 * Enabling the object read lock allows multiple threads to safely call the
416 * following functions in parallel: repo_read_object_file(), read_object_file(),
Jeff King0ba05cf2023-01-07 08:50:33 -0500417 * read_object_with_reference(), oid_object_info() and oid_object_info_extended().
Matheus Tavares31877c92020-01-15 23:39:53 -0300418 *
419 * obj_read_lock() and obj_read_unlock() may also be used to protect other
420 * section which cannot execute in parallel with object reading. Since the used
421 * lock is a recursive mutex, these sections can even contain calls to object
422 * reading functions. However, beware that in these cases zlib inflation won't
423 * be performed in parallel, losing performance.
424 *
425 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
426 * any of its callees end up calling it, this recursive call won't benefit from
427 * parallel inflation.
428 */
429void enable_obj_read_lock(void);
430void disable_obj_read_lock(void);
431
432extern int obj_read_use_lock;
433extern pthread_mutex_t obj_read_mutex;
434
435static inline void obj_read_lock(void)
436{
437 if(obj_read_use_lock)
438 pthread_mutex_lock(&obj_read_mutex);
439}
440
441static inline void obj_read_unlock(void)
442{
443 if(obj_read_use_lock)
444 pthread_mutex_unlock(&obj_read_mutex);
445}
446
Jeff King0889aae2018-08-14 14:21:18 -0400447/*
448 * Iterate over the files in the loose-object parts of the object
449 * directory "path", triggering the following callbacks:
450 *
451 * - loose_object is called for each loose object we find.
452 *
453 * - loose_cruft is called for any files that do not appear to be
454 * loose objects. Note that we only look in the loose object
455 * directories "objects/[0-9a-f]{2}/", so we will not report
456 * "objects/foobar" as cruft.
457 *
458 * - loose_subdir is called for each top-level hashed subdirectory
459 * of the object directory (e.g., "$OBJDIR/f0"). It is called
460 * after the objects in the directory are processed.
461 *
462 * Any callback that is NULL will be ignored. Callbacks returning non-zero
463 * will end the iteration.
464 *
465 * In the "buf" variant, "path" is a strbuf which will also be used as a
466 * scratch buffer, but restored to its original contents before
467 * the function returns.
468 */
469typedef int each_loose_object_fn(const struct object_id *oid,
470 const char *path,
471 void *data);
472typedef int each_loose_cruft_fn(const char *basename,
473 const char *path,
474 void *data);
475typedef int each_loose_subdir_fn(unsigned int nr,
476 const char *path,
477 void *data);
478int for_each_file_in_obj_subdir(unsigned int subdir_nr,
479 struct strbuf *path,
480 each_loose_object_fn obj_cb,
481 each_loose_cruft_fn cruft_cb,
482 each_loose_subdir_fn subdir_cb,
483 void *data);
484int for_each_loose_file_in_objdir(const char *path,
485 each_loose_object_fn obj_cb,
486 each_loose_cruft_fn cruft_cb,
487 each_loose_subdir_fn subdir_cb,
488 void *data);
489int for_each_loose_file_in_objdir_buf(struct strbuf *path,
490 each_loose_object_fn obj_cb,
491 each_loose_cruft_fn cruft_cb,
492 each_loose_subdir_fn subdir_cb,
493 void *data);
494
495/* Flags for for_each_*_object() below. */
496enum for_each_object_flags {
497 /* Iterate only over local objects, not alternates. */
498 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
499
500 /* Only iterate over packs obtained from the promisor remote. */
501 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
502
503 /*
504 * Visit objects within a pack in packfile order rather than .idx order
505 */
506 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
Taylor Blaua2418782021-08-29 22:48:52 -0400507
508 /* Only iterate over packs that are not marked as kept in-core. */
509 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS = (1<<3),
510
511 /* Only iterate over packs that do not have .keep files. */
512 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
Jeff King0889aae2018-08-14 14:21:18 -0400513};
514
515/*
516 * Iterate over all accessible loose objects without respect to
517 * reachability. By default, this includes both local and alternate objects.
518 * The order in which objects are visited is unspecified.
519 *
520 * Any flags specific to packs are ignored.
521 */
522int for_each_loose_object(each_loose_object_fn, void *,
523 enum for_each_object_flags flags);
524
525/*
526 * Iterate over all accessible packed objects without respect to reachability.
527 * By default, this includes both local and alternate packs.
528 *
529 * Note that some objects may appear twice if they are found in multiple packs.
530 * Each pack is visited in an unspecified order. By default, objects within a
531 * pack are visited in pack-idx order (i.e., sorted by oid).
532 */
533typedef int each_packed_object_fn(const struct object_id *oid,
534 struct packed_git *pack,
535 uint32_t pos,
536 void *data);
537int for_each_object_in_pack(struct packed_git *p,
538 each_packed_object_fn, void *data,
539 enum for_each_object_flags flags);
540int for_each_packed_object(each_packed_object_fn, void *,
541 enum for_each_object_flags flags);
542
Stefan Beller90c62152018-03-23 18:20:55 +0100543#endif /* OBJECT_STORE_H */