9 #include "thread-utils.h"
13 struct object_directory
{
14 struct object_directory
*next
;
17 * Used to store the results of readdir(3) calls when we are OK
18 * sacrificing accuracy due to races for speed. That includes
19 * object existence with OBJECT_INFO_QUICK, as well as
20 * our search for unique abbreviated hashes. Don't use it for tasks
21 * requiring greater accuracy!
23 * Be sure to call odb_load_loose_cache() before using.
25 uint32_t loose_objects_subdir_seen
[8]; /* 256 bits */
26 struct oid_array loose_objects_cache
[256];
29 * Path to the alternative object store. If this is a relative path,
30 * it is relative to the current working directory.
35 KHASH_INIT(odb_path_map
, const char * /* key: odb_path */,
36 struct object_directory
*, 1, fspathhash
, fspatheq
);
38 void prepare_alt_odb(struct repository
*r
);
39 char *compute_alternate_path(const char *path
, struct strbuf
*err
);
40 typedef int alt_odb_fn(struct object_directory
*, void *);
41 int foreach_alt_odb(alt_odb_fn
, void*);
42 typedef void alternate_ref_fn(const struct object_id
*oid
, void *);
43 void for_each_alternate_ref(alternate_ref_fn
, void *);
46 * Add the directory to the on-disk alternates file; the new entry will also
47 * take effect in the current process.
49 void add_to_alternates_file(const char *dir
);
52 * Add the directory to the in-memory list of alternates (along with any
53 * recursive alternates it points to), but do not modify the on-disk alternates
56 void add_to_alternates_memory(const char *dir
);
59 * Populate and return the loose object cache array corresponding to the
62 struct oid_array
*odb_loose_cache(struct object_directory
*odb
,
63 const struct object_id
*oid
);
65 /* Empty the loose object cache for the specified object directory. */
66 void odb_clear_loose_cache(struct object_directory
*odb
);
69 struct hashmap_entry packmap_ent
;
70 struct packed_git
*next
;
72 struct pack_window
*windows
;
74 const void *index_data
;
77 uint32_t num_bad_objects
;
79 unsigned char *bad_object_sha1
;
83 int index
; /* for builtin/pack-objects.c */
84 unsigned pack_local
:1,
91 unsigned char hash
[GIT_MAX_RAWSZ
];
92 struct revindex_entry
*revindex
;
93 const uint32_t *revindex_data
;
94 const uint32_t *revindex_map
;
96 /* something like ".git/objects/pack/xxxxx.pack" */
97 char pack_name
[FLEX_ARRAY
]; /* more */
100 struct multi_pack_index
;
102 static inline int pack_map_entry_cmp(const void *unused_cmp_data
,
103 const struct hashmap_entry
*entry
,
104 const struct hashmap_entry
*entry2
,
107 const char *key
= keydata
;
108 const struct packed_git
*pg1
, *pg2
;
110 pg1
= container_of(entry
, const struct packed_git
, packmap_ent
);
111 pg2
= container_of(entry2
, const struct packed_git
, packmap_ent
);
113 return strcmp(pg1
->pack_name
, key
? key
: pg2
->pack_name
);
116 struct raw_object_store
{
118 * Set of all object directories; the main directory is first (and
119 * cannot be NULL after initialization). Subsequent directories are
122 struct object_directory
*odb
;
123 struct object_directory
**odb_tail
;
124 kh_odb_path_map_t
*odb_by_path
;
126 int loaded_alternates
;
129 * A list of alternate object directories loaded from the environment;
130 * this should not generally need to be accessed directly, but will
131 * populate the "odb" list when prepare_alt_odb() is run.
136 * Objects that should be substituted by other objects
137 * (see git-replace(1)).
139 struct oidmap
*replace_map
;
140 unsigned replace_map_initialized
: 1;
141 pthread_mutex_t replace_mutex
; /* protect object replace functions */
143 struct commit_graph
*commit_graph
;
144 unsigned commit_graph_attempted
: 1; /* if loading has been attempted */
149 * should only be accessed directly by packfile.c and midx.c
151 struct multi_pack_index
*multi_pack_index
;
156 * should only be accessed directly by packfile.c
159 struct packed_git
*packed_git
;
160 /* A most-recently-used ordered version of the packed_git list. */
161 struct list_head packed_git_mru
;
164 struct packed_git
**packs
;
169 * A map of packfiles to packed_git structs for tracking which
170 * packs have been loaded already.
172 struct hashmap pack_map
;
175 * A fast, rough count of the number of objects in the repository.
176 * These two fields are not meant for direct access. Use
177 * approximate_object_count() instead.
179 unsigned long approximate_object_count
;
180 unsigned approximate_object_count_valid
: 1;
183 * Whether packed_git has already been populated with this repository's
186 unsigned packed_git_initialized
: 1;
189 struct raw_object_store
*raw_object_store_new(void);
190 void raw_object_store_clear(struct raw_object_store
*o
);
193 * Put in `buf` the name of the file in the local object database that
194 * would be used to store a loose object with the specified oid.
196 const char *loose_object_path(struct repository
*r
, struct strbuf
*buf
,
197 const struct object_id
*oid
);
199 void *map_loose_object(struct repository
*r
, const struct object_id
*oid
,
200 unsigned long *size
);
202 void *read_object_file_extended(struct repository
*r
,
203 const struct object_id
*oid
,
204 enum object_type
*type
,
205 unsigned long *size
, int lookup_replace
);
206 static inline void *repo_read_object_file(struct repository
*r
,
207 const struct object_id
*oid
,
208 enum object_type
*type
,
211 return read_object_file_extended(r
, oid
, type
, size
, 1);
213 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
214 #define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size)
217 /* Read and unpack an object file into memory, write memory to an object file */
218 int oid_object_info(struct repository
*r
, const struct object_id
*, unsigned long *);
220 int hash_object_file(const struct git_hash_algo
*algo
, const void *buf
,
221 unsigned long len
, const char *type
,
222 struct object_id
*oid
);
224 int write_object_file(const void *buf
, unsigned long len
,
225 const char *type
, struct object_id
*oid
);
227 int hash_object_file_literally(const void *buf
, unsigned long len
,
228 const char *type
, struct object_id
*oid
,
232 * Add an object file to the in-memory object store, without writing it
235 * Callers are responsible for calling write_object_file to record the
236 * object in persistent storage before writing any other new objects
239 int pretend_object_file(void *, unsigned long, enum object_type
,
240 struct object_id
*oid
);
242 int force_object_loose(const struct object_id
*oid
, time_t mtime
);
245 * Open the loose object at path, check its hash, and return the contents,
246 * type, and size. If the object is a blob, then "contents" may return NULL,
247 * to allow streaming of large blobs.
249 * Returns 0 on success, negative on error (details may be written to stderr).
251 int read_loose_object(const char *path
,
252 const struct object_id
*expected_oid
,
253 enum object_type
*type
,
257 /* Retry packed storage after checking packed and loose storage */
258 #define HAS_OBJECT_RECHECK_PACKED 1
261 * Returns 1 if the object exists. This function will not lazily fetch objects
262 * in a partial clone.
264 int has_object(struct repository
*r
, const struct object_id
*oid
,
268 * These macros and functions are deprecated. If checking existence for an
269 * object that is likely to be missing and/or whose absence is relatively
270 * inconsequential (or is consequential but the caller is prepared to handle
271 * it), use has_object(), which has better defaults (no lazy fetch in a partial
272 * clone and no rechecking of packed storage). In the unlikely event that a
273 * caller needs to assert existence of an object that it fully expects to
274 * exist, and wants to trigger a lazy fetch in a partial clone, use
275 * oid_object_info_extended() with a NULL struct object_info.
277 * These functions can be removed once all callers have migrated to
278 * has_object() and/or oid_object_info_extended().
280 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
281 #define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
282 #define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
284 int repo_has_object_file(struct repository
*r
, const struct object_id
*oid
);
285 int repo_has_object_file_with_flags(struct repository
*r
,
286 const struct object_id
*oid
, int flags
);
287 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
288 #define has_object_file(oid) repo_has_object_file(the_repository, oid)
289 #define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags)
293 * Return true iff an alternate object database has a loose object
294 * with the specified name. This function does not respect replace
297 int has_loose_object_nonlocal(const struct object_id
*);
299 void assert_oid_type(const struct object_id
*oid
, enum object_type expect
);
302 * Enabling the object read lock allows multiple threads to safely call the
303 * following functions in parallel: repo_read_object_file(), read_object_file(),
304 * read_object_file_extended(), read_object_with_reference(), read_object(),
305 * oid_object_info() and oid_object_info_extended().
307 * obj_read_lock() and obj_read_unlock() may also be used to protect other
308 * section which cannot execute in parallel with object reading. Since the used
309 * lock is a recursive mutex, these sections can even contain calls to object
310 * reading functions. However, beware that in these cases zlib inflation won't
311 * be performed in parallel, losing performance.
313 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
314 * any of its callees end up calling it, this recursive call won't benefit from
315 * parallel inflation.
317 void enable_obj_read_lock(void);
318 void disable_obj_read_lock(void);
320 extern int obj_read_use_lock
;
321 extern pthread_mutex_t obj_read_mutex
;
323 static inline void obj_read_lock(void)
325 if(obj_read_use_lock
)
326 pthread_mutex_lock(&obj_read_mutex
);
329 static inline void obj_read_unlock(void)
331 if(obj_read_use_lock
)
332 pthread_mutex_unlock(&obj_read_mutex
);
337 enum object_type
*typep
;
338 unsigned long *sizep
;
340 struct object_id
*delta_base_oid
;
341 struct strbuf
*type_name
;
354 * ... Nothing to expose in this case
357 * ... Nothing to expose in this case
361 struct packed_git
*pack
;
363 unsigned int is_delta
;
369 * Initializer for a "struct object_info" that wants no items. You may
370 * also memset() the memory to all-zeroes.
372 #define OBJECT_INFO_INIT {NULL}
374 /* Invoke lookup_replace_object() on the given hash */
375 #define OBJECT_INFO_LOOKUP_REPLACE 1
376 /* Allow reading from a loose object file of unknown/bogus type */
377 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
378 /* Do not retry packed storage after checking packed and loose storage */
379 #define OBJECT_INFO_QUICK 8
380 /* Do not check loose object */
381 #define OBJECT_INFO_IGNORE_LOOSE 16
383 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
386 #define OBJECT_INFO_SKIP_FETCH_OBJECT 32
388 * This is meant for bulk prefetching of missing blobs in a partial
389 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
391 #define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
393 int oid_object_info_extended(struct repository
*r
,
394 const struct object_id
*,
395 struct object_info
*, unsigned flags
);
398 * Iterate over the files in the loose-object parts of the object
399 * directory "path", triggering the following callbacks:
401 * - loose_object is called for each loose object we find.
403 * - loose_cruft is called for any files that do not appear to be
404 * loose objects. Note that we only look in the loose object
405 * directories "objects/[0-9a-f]{2}/", so we will not report
406 * "objects/foobar" as cruft.
408 * - loose_subdir is called for each top-level hashed subdirectory
409 * of the object directory (e.g., "$OBJDIR/f0"). It is called
410 * after the objects in the directory are processed.
412 * Any callback that is NULL will be ignored. Callbacks returning non-zero
413 * will end the iteration.
415 * In the "buf" variant, "path" is a strbuf which will also be used as a
416 * scratch buffer, but restored to its original contents before
417 * the function returns.
419 typedef int each_loose_object_fn(const struct object_id
*oid
,
422 typedef int each_loose_cruft_fn(const char *basename
,
425 typedef int each_loose_subdir_fn(unsigned int nr
,
428 int for_each_file_in_obj_subdir(unsigned int subdir_nr
,
430 each_loose_object_fn obj_cb
,
431 each_loose_cruft_fn cruft_cb
,
432 each_loose_subdir_fn subdir_cb
,
434 int for_each_loose_file_in_objdir(const char *path
,
435 each_loose_object_fn obj_cb
,
436 each_loose_cruft_fn cruft_cb
,
437 each_loose_subdir_fn subdir_cb
,
439 int for_each_loose_file_in_objdir_buf(struct strbuf
*path
,
440 each_loose_object_fn obj_cb
,
441 each_loose_cruft_fn cruft_cb
,
442 each_loose_subdir_fn subdir_cb
,
445 /* Flags for for_each_*_object() below. */
446 enum for_each_object_flags
{
447 /* Iterate only over local objects, not alternates. */
448 FOR_EACH_OBJECT_LOCAL_ONLY
= (1<<0),
450 /* Only iterate over packs obtained from the promisor remote. */
451 FOR_EACH_OBJECT_PROMISOR_ONLY
= (1<<1),
454 * Visit objects within a pack in packfile order rather than .idx order
456 FOR_EACH_OBJECT_PACK_ORDER
= (1<<2),
460 * Iterate over all accessible loose objects without respect to
461 * reachability. By default, this includes both local and alternate objects.
462 * The order in which objects are visited is unspecified.
464 * Any flags specific to packs are ignored.
466 int for_each_loose_object(each_loose_object_fn
, void *,
467 enum for_each_object_flags flags
);
470 * Iterate over all accessible packed objects without respect to reachability.
471 * By default, this includes both local and alternate packs.
473 * Note that some objects may appear twice if they are found in multiple packs.
474 * Each pack is visited in an unspecified order. By default, objects within a
475 * pack are visited in pack-idx order (i.e., sorted by oid).
477 typedef int each_packed_object_fn(const struct object_id
*oid
,
478 struct packed_git
*pack
,
481 int for_each_object_in_pack(struct packed_git
*p
,
482 each_packed_object_fn
, void *data
,
483 enum for_each_object_flags flags
);
484 int for_each_packed_object(each_packed_object_fn
, void *,
485 enum for_each_object_flags flags
);
487 #endif /* OBJECT_STORE_H */