1 #include "git-compat-util.h"
3 #include "commit-graph.h"
6 #include "environment.h"
12 #include "object-file-convert.h"
13 #include "object-file.h"
17 #include "promisor-remote.h"
19 #include "replace-object.h"
20 #include "run-command.h"
24 #include "submodule.h"
26 #include "write-or-die.h"
28 KHASH_INIT(odb_path_map
, const char * /* key: odb_path */,
29 struct odb_source
*, 1, fspathhash
, fspatheq
)
32 * This is meant to hold a *small* number of objects that you would
33 * want odb_read_object() to be able to return, but yet you do not want
34 * to write them into the object store (e.g. a browse-only
37 struct cached_object_entry
{
39 struct cached_object
{
40 enum object_type type
;
46 static const struct cached_object
*find_cached_object(struct object_database
*object_store
,
47 const struct object_id
*oid
)
49 static const struct cached_object empty_tree
= {
53 const struct cached_object_entry
*co
= object_store
->cached_objects
;
55 for (size_t i
= 0; i
< object_store
->cached_object_nr
; i
++, co
++)
56 if (oideq(&co
->oid
, oid
))
59 if (oid
->algo
&& oideq(oid
, hash_algos
[oid
->algo
].empty_tree
))
65 int odb_mkstemp(struct object_database
*odb
,
66 struct strbuf
*temp_filename
, const char *pattern
)
70 * we let the umask do its job, don't try to be more
71 * restrictive except to remove write permission.
74 repo_git_path_replace(odb
->repo
, temp_filename
, "objects/%s", pattern
);
75 fd
= git_mkstemp_mode(temp_filename
->buf
, mode
);
80 /* some mkstemp implementations erase temp_filename on failure */
81 repo_git_path_replace(odb
->repo
, temp_filename
, "objects/%s", pattern
);
82 safe_create_leading_directories(odb
->repo
, temp_filename
->buf
);
83 return xmkstemp_mode(temp_filename
->buf
, mode
);
87 * Return non-zero iff the path is usable as an alternate object database.
89 static int alt_odb_usable(struct object_database
*o
,
91 const char *normalized_objdir
, khiter_t
*pos
)
95 /* Detect cases where alternate disappeared */
96 if (!is_directory(path
->buf
)) {
97 error(_("object directory %s does not exist; "
98 "check .git/objects/info/alternates"),
104 * Prevent the common mistake of listing the same
105 * thing twice, or object directory itself.
107 if (!o
->source_by_path
) {
110 o
->source_by_path
= kh_init_odb_path_map();
111 assert(!o
->sources
->next
);
112 p
= kh_put_odb_path_map(o
->source_by_path
, o
->sources
->path
, &r
);
113 assert(r
== 1); /* never used */
114 kh_value(o
->source_by_path
, p
) = o
->sources
;
116 if (fspatheq(path
->buf
, normalized_objdir
))
118 *pos
= kh_put_odb_path_map(o
->source_by_path
, path
->buf
, &r
);
119 /* r: 0 = exists, 1 = never used, 2 = deleted */
120 return r
== 0 ? 0 : 1;
124 * Prepare alternate object database registry.
126 * The variable alt_odb_list points at the list of struct
127 * odb_source. The elements on this list come from
128 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
129 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
130 * whose contents is similar to that environment variable but can be
131 * LF separated. Its base points at a statically allocated buffer that
132 * contains "/the/directory/corresponding/to/.git/objects/...", while
133 * its name points just after the slash at the end of ".git/objects/"
134 * in the example above, and has enough space to hold all hex characters
135 * of the object ID, an extra slash for the first level indirection, and
136 * the terminating NUL.
138 static void read_info_alternates(struct object_database
*odb
,
139 const char *relative_base
,
142 static int link_alt_odb_entry(struct object_database
*odb
,
143 const struct strbuf
*entry
,
144 const char *relative_base
,
146 const char *normalized_objdir
)
148 struct odb_source
*alternate
;
149 struct strbuf pathbuf
= STRBUF_INIT
;
150 struct strbuf tmp
= STRBUF_INIT
;
154 if (!is_absolute_path(entry
->buf
) && relative_base
) {
155 strbuf_realpath(&pathbuf
, relative_base
, 1);
156 strbuf_addch(&pathbuf
, '/');
158 strbuf_addbuf(&pathbuf
, entry
);
160 if (!strbuf_realpath(&tmp
, pathbuf
.buf
, 0)) {
161 error(_("unable to normalize alternate object path: %s"),
165 strbuf_swap(&pathbuf
, &tmp
);
168 * The trailing slash after the directory name is given by
169 * this function at the end. Remove duplicates.
171 while (pathbuf
.len
&& pathbuf
.buf
[pathbuf
.len
- 1] == '/')
172 strbuf_setlen(&pathbuf
, pathbuf
.len
- 1);
174 if (!alt_odb_usable(odb
, &pathbuf
, normalized_objdir
, &pos
))
177 CALLOC_ARRAY(alternate
, 1);
178 alternate
->odb
= odb
;
179 /* pathbuf.buf is already in r->objects->source_by_path */
180 alternate
->path
= strbuf_detach(&pathbuf
, NULL
);
182 /* add the alternate entry */
183 *odb
->sources_tail
= alternate
;
184 odb
->sources_tail
= &(alternate
->next
);
185 alternate
->next
= NULL
;
186 assert(odb
->source_by_path
);
187 kh_value(odb
->source_by_path
, pos
) = alternate
;
189 /* recursively add alternates */
190 read_info_alternates(odb
, alternate
->path
, depth
+ 1);
193 strbuf_release(&tmp
);
194 strbuf_release(&pathbuf
);
198 static const char *parse_alt_odb_entry(const char *string
,
206 if (*string
== '#') {
207 /* comment; consume up to next separator */
208 end
= strchrnul(string
, sep
);
209 } else if (*string
== '"' && !unquote_c_style(out
, string
, &end
)) {
211 * quoted path; unquote_c_style has copied the
212 * data for us and set "end". Broken quoting (e.g.,
213 * an entry that doesn't end with a quote) falls
214 * back to the unquoted case below.
217 /* normal, unquoted path */
218 end
= strchrnul(string
, sep
);
219 strbuf_add(out
, string
, end
- string
);
227 static void link_alt_odb_entries(struct object_database
*odb
, const char *alt
,
228 int sep
, const char *relative_base
, int depth
)
230 struct strbuf objdirbuf
= STRBUF_INIT
;
231 struct strbuf entry
= STRBUF_INIT
;
237 error(_("%s: ignoring alternate object stores, nesting too deep"),
242 strbuf_realpath(&objdirbuf
, odb
->sources
->path
, 1);
245 alt
= parse_alt_odb_entry(alt
, sep
, &entry
);
248 link_alt_odb_entry(odb
, &entry
,
249 relative_base
, depth
, objdirbuf
.buf
);
251 strbuf_release(&entry
);
252 strbuf_release(&objdirbuf
);
255 static void read_info_alternates(struct object_database
*odb
,
256 const char *relative_base
,
260 struct strbuf buf
= STRBUF_INIT
;
262 path
= xstrfmt("%s/info/alternates", relative_base
);
263 if (strbuf_read_file(&buf
, path
, 1024) < 0) {
264 warn_on_fopen_errors(path
);
269 link_alt_odb_entries(odb
, buf
.buf
, '\n', relative_base
, depth
);
270 strbuf_release(&buf
);
274 void odb_add_to_alternates_file(struct object_database
*odb
,
275 const char *reference
)
277 struct lock_file lock
= LOCK_INIT
;
278 char *alts
= repo_git_path(odb
->repo
, "objects/info/alternates");
282 hold_lock_file_for_update(&lock
, alts
, LOCK_DIE_ON_ERROR
);
283 out
= fdopen_lock_file(&lock
, "w");
285 die_errno(_("unable to fdopen alternates lockfile"));
287 in
= fopen(alts
, "r");
289 struct strbuf line
= STRBUF_INIT
;
291 while (strbuf_getline(&line
, in
) != EOF
) {
292 if (!strcmp(reference
, line
.buf
)) {
296 fprintf_or_die(out
, "%s\n", line
.buf
);
299 strbuf_release(&line
);
302 else if (errno
!= ENOENT
)
303 die_errno(_("unable to read alternates file"));
306 rollback_lock_file(&lock
);
308 fprintf_or_die(out
, "%s\n", reference
);
309 if (commit_lock_file(&lock
))
310 die_errno(_("unable to move new alternates file into place"));
311 if (odb
->loaded_alternates
)
312 link_alt_odb_entries(odb
, reference
,
318 void odb_add_to_alternates_memory(struct object_database
*odb
,
319 const char *reference
)
322 * Make sure alternates are initialized, or else our entry may be
323 * overwritten when they are.
325 odb_prepare_alternates(odb
);
327 link_alt_odb_entries(odb
, reference
,
331 struct odb_source
*odb_set_temporary_primary_source(struct object_database
*odb
,
332 const char *dir
, int will_destroy
)
334 struct odb_source
*source
;
337 * Make sure alternates are initialized, or else our entry may be
338 * overwritten when they are.
340 odb_prepare_alternates(odb
);
343 * Make a new primary odb and link the old primary ODB in as an
346 source
= xcalloc(1, sizeof(*source
));
348 source
->path
= xstrdup(dir
);
351 * Disable ref updates while a temporary odb is active, since
352 * the objects in the database may roll back.
354 source
->disable_ref_updates
= 1;
355 source
->will_destroy
= will_destroy
;
356 source
->next
= odb
->sources
;
357 odb
->sources
= source
;
361 static void free_object_directory(struct odb_source
*source
)
364 odb_clear_loose_cache(source
);
365 loose_object_map_clear(&source
->loose_map
);
369 void odb_restore_primary_source(struct object_database
*odb
,
370 struct odb_source
*restore_source
,
371 const char *old_path
)
373 struct odb_source
*cur_source
= odb
->sources
;
375 if (strcmp(old_path
, cur_source
->path
))
376 BUG("expected %s as primary object store; found %s",
377 old_path
, cur_source
->path
);
379 if (cur_source
->next
!= restore_source
)
380 BUG("we expect the old primary object store to be the first alternate");
382 odb
->sources
= restore_source
;
383 free_object_directory(cur_source
);
386 char *compute_alternate_path(const char *path
, struct strbuf
*err
)
388 char *ref_git
= NULL
;
392 ref_git
= real_pathdup(path
, 0);
395 strbuf_addf(err
, _("path '%s' does not exist"), path
);
399 repo
= read_gitfile(ref_git
);
401 repo
= read_gitfile(mkpath("%s/.git", ref_git
));
404 ref_git
= xstrdup(repo
);
407 if (!repo
&& is_directory(mkpath("%s/.git/objects", ref_git
))) {
408 char *ref_git_git
= mkpathdup("%s/.git", ref_git
);
410 ref_git
= ref_git_git
;
411 } else if (!is_directory(mkpath("%s/objects", ref_git
))) {
412 struct strbuf sb
= STRBUF_INIT
;
414 if (get_common_dir(&sb
, ref_git
)) {
416 _("reference repository '%s' as a linked "
417 "checkout is not supported yet."),
422 strbuf_addf(err
, _("reference repository '%s' is not a "
423 "local repository."), path
);
427 if (!access(mkpath("%s/shallow", ref_git
), F_OK
)) {
428 strbuf_addf(err
, _("reference repository '%s' is shallow"),
434 if (!access(mkpath("%s/info/grafts", ref_git
), F_OK
)) {
436 _("reference repository '%s' is grafted"),
444 FREE_AND_NULL(ref_git
);
450 struct odb_source
*odb_find_source(struct object_database
*odb
, const char *obj_dir
)
452 struct odb_source
*source
;
453 char *obj_dir_real
= real_pathdup(obj_dir
, 1);
454 struct strbuf odb_path_real
= STRBUF_INIT
;
456 odb_prepare_alternates(odb
);
457 for (source
= odb
->sources
; source
; source
= source
->next
) {
458 strbuf_realpath(&odb_path_real
, source
->path
, 1);
459 if (!strcmp(obj_dir_real
, odb_path_real
.buf
))
464 strbuf_release(&odb_path_real
);
467 die(_("could not find object directory matching %s"), obj_dir
);
471 void odb_add_submodule_source_by_path(struct object_database
*odb
,
474 string_list_insert(&odb
->submodule_source_paths
, path
);
477 static void fill_alternate_refs_command(struct repository
*repo
,
478 struct child_process
*cmd
,
479 const char *repo_path
)
483 if (!repo_config_get_value(repo
, "core.alternateRefsCommand", &value
)) {
486 strvec_push(&cmd
->args
, value
);
487 strvec_push(&cmd
->args
, repo_path
);
491 strvec_pushf(&cmd
->args
, "--git-dir=%s", repo_path
);
492 strvec_push(&cmd
->args
, "for-each-ref");
493 strvec_push(&cmd
->args
, "--format=%(objectname)");
495 if (!repo_config_get_value(repo
, "core.alternateRefsPrefixes", &value
)) {
496 strvec_push(&cmd
->args
, "--");
497 strvec_split(&cmd
->args
, value
);
501 strvec_pushv(&cmd
->env
, (const char **)local_repo_env
);
505 static void read_alternate_refs(struct repository
*repo
,
507 odb_for_each_alternate_ref_fn
*cb
,
510 struct child_process cmd
= CHILD_PROCESS_INIT
;
511 struct strbuf line
= STRBUF_INIT
;
514 fill_alternate_refs_command(repo
, &cmd
, path
);
516 if (start_command(&cmd
))
519 fh
= xfdopen(cmd
.out
, "r");
520 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
521 struct object_id oid
;
524 if (parse_oid_hex_algop(line
.buf
, &oid
, &p
, repo
->hash_algo
) || *p
) {
525 warning(_("invalid line while parsing alternate refs: %s"),
534 finish_command(&cmd
);
535 strbuf_release(&line
);
538 struct alternate_refs_data
{
539 odb_for_each_alternate_ref_fn
*fn
;
543 static int refs_from_alternate_cb(struct odb_source
*alternate
,
546 struct strbuf path
= STRBUF_INIT
;
548 struct alternate_refs_data
*cb
= payload
;
550 if (!strbuf_realpath(&path
, alternate
->path
, 0))
552 if (!strbuf_strip_suffix(&path
, "/objects"))
556 /* Is this a git repository with refs? */
557 strbuf_addstr(&path
, "/refs");
558 if (!is_directory(path
.buf
))
560 strbuf_setlen(&path
, base_len
);
562 read_alternate_refs(alternate
->odb
->repo
, path
.buf
, cb
->fn
, cb
->payload
);
565 strbuf_release(&path
);
569 void odb_for_each_alternate_ref(struct object_database
*odb
,
570 odb_for_each_alternate_ref_fn cb
, void *payload
)
572 struct alternate_refs_data data
;
574 data
.payload
= payload
;
575 odb_for_each_alternate(odb
, refs_from_alternate_cb
, &data
);
578 int odb_for_each_alternate(struct object_database
*odb
,
579 odb_for_each_alternate_fn cb
, void *payload
)
581 struct odb_source
*alternate
;
584 odb_prepare_alternates(odb
);
585 for (alternate
= odb
->sources
->next
; alternate
; alternate
= alternate
->next
) {
586 r
= cb(alternate
, payload
);
593 void odb_prepare_alternates(struct object_database
*odb
)
595 if (odb
->loaded_alternates
)
598 link_alt_odb_entries(odb
, odb
->alternate_db
, PATH_SEP
, NULL
, 0);
600 read_info_alternates(odb
, odb
->sources
->path
, 0);
601 odb
->loaded_alternates
= 1;
604 int odb_has_alternates(struct object_database
*odb
)
606 odb_prepare_alternates(odb
);
607 return !!odb
->sources
->next
;
610 int obj_read_use_lock
= 0;
611 pthread_mutex_t obj_read_mutex
;
613 void enable_obj_read_lock(void)
615 if (obj_read_use_lock
)
618 obj_read_use_lock
= 1;
619 init_recursive_mutex(&obj_read_mutex
);
622 void disable_obj_read_lock(void)
624 if (!obj_read_use_lock
)
627 obj_read_use_lock
= 0;
628 pthread_mutex_destroy(&obj_read_mutex
);
631 int fetch_if_missing
= 1;
633 static int register_all_submodule_sources(struct object_database
*odb
)
635 int ret
= odb
->submodule_source_paths
.nr
;
637 for (size_t i
= 0; i
< odb
->submodule_source_paths
.nr
; i
++)
638 odb_add_to_alternates_memory(odb
,
639 odb
->submodule_source_paths
.items
[i
].string
);
641 string_list_clear(&odb
->submodule_source_paths
, 0);
642 trace2_data_intmax("submodule", odb
->repo
,
643 "register_all_submodule_sources/registered", ret
);
644 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
645 BUG("register_all_submodule_sources() called");
650 static int do_oid_object_info_extended(struct object_database
*odb
,
651 const struct object_id
*oid
,
652 struct object_info
*oi
, unsigned flags
)
654 static struct object_info blank_oi
= OBJECT_INFO_INIT
;
655 const struct cached_object
*co
;
658 const struct object_id
*real
= oid
;
659 int already_retried
= 0;
662 if (flags
& OBJECT_INFO_LOOKUP_REPLACE
)
663 real
= lookup_replace_object(odb
->repo
, oid
);
665 if (is_null_oid(real
))
671 co
= find_cached_object(odb
, real
);
674 *(oi
->typep
) = co
->type
;
676 *(oi
->sizep
) = co
->size
;
678 *(oi
->disk_sizep
) = 0;
679 if (oi
->delta_base_oid
)
680 oidclr(oi
->delta_base_oid
, odb
->repo
->hash_algo
);
682 *oi
->contentp
= xmemdupz(co
->buf
, co
->size
);
683 oi
->whence
= OI_CACHED
;
688 if (find_pack_entry(odb
->repo
, real
, &e
))
691 /* Most likely it's a loose object. */
692 if (!loose_object_info(odb
->repo
, real
, oi
, flags
))
695 /* Not a loose object; someone else may have just packed it. */
696 if (!(flags
& OBJECT_INFO_QUICK
)) {
697 reprepare_packed_git(odb
->repo
);
698 if (find_pack_entry(odb
->repo
, real
, &e
))
703 * This might be an attempt at accessing a submodule object as
704 * if it were in main object store (having called
705 * `odb_add_submodule_source_by_path()` on that submodule's
706 * ODB). If any such ODBs exist, register them and try again.
708 if (register_all_submodule_sources(odb
))
709 /* We added some alternates; retry */
712 /* Check if it is a missing object */
713 if (fetch_if_missing
&& repo_has_promisor_remote(odb
->repo
) &&
715 !(flags
& OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
716 promisor_remote_get_direct(odb
->repo
, real
, 1);
721 if (flags
& OBJECT_INFO_DIE_IF_CORRUPT
) {
722 const struct packed_git
*p
;
723 if ((flags
& OBJECT_INFO_LOOKUP_REPLACE
) && !oideq(real
, oid
))
724 die(_("replacement %s not found for %s"),
725 oid_to_hex(real
), oid_to_hex(oid
));
726 if ((p
= has_packed_and_bad(odb
->repo
, real
)))
727 die(_("packed object %s (stored in %s) is corrupt"),
728 oid_to_hex(real
), p
->pack_name
);
735 * We know that the caller doesn't actually need the
736 * information below, so return early.
739 rtype
= packed_object_info(odb
->repo
, e
.p
, e
.offset
, oi
);
741 mark_bad_packed_object(e
.p
, real
);
742 return do_oid_object_info_extended(odb
, real
, oi
, 0);
743 } else if (oi
->whence
== OI_PACKED
) {
744 oi
->u
.packed
.offset
= e
.offset
;
745 oi
->u
.packed
.pack
= e
.p
;
746 oi
->u
.packed
.is_delta
= (rtype
== OBJ_REF_DELTA
||
747 rtype
== OBJ_OFS_DELTA
);
753 static int oid_object_info_convert(struct repository
*r
,
754 const struct object_id
*input_oid
,
755 struct object_info
*input_oi
, unsigned flags
)
757 const struct git_hash_algo
*input_algo
= &hash_algos
[input_oid
->algo
];
758 int do_die
= flags
& OBJECT_INFO_DIE_IF_CORRUPT
;
759 enum object_type type
;
760 struct object_id oid
, delta_base_oid
;
761 struct object_info new_oi
, *oi
;
766 if (repo_oid_to_algop(r
, input_oid
, r
->hash_algo
, &oid
)) {
768 die(_("missing mapping of %s to %s"),
769 oid_to_hex(input_oid
), r
->hash_algo
->name
);
773 /* Is new_oi needed? */
775 if (input_oi
&& (input_oi
->delta_base_oid
|| input_oi
->sizep
||
776 input_oi
->contentp
)) {
778 /* Does delta_base_oid need to be converted? */
779 if (input_oi
->delta_base_oid
)
780 new_oi
.delta_base_oid
= &delta_base_oid
;
781 /* Will the attributes differ when converted? */
782 if (input_oi
->sizep
|| input_oi
->contentp
) {
783 new_oi
.contentp
= &content
;
784 new_oi
.sizep
= &size
;
785 new_oi
.typep
= &type
;
790 ret
= odb_read_object_info_extended(r
->objects
, &oid
, oi
, flags
);
796 if (new_oi
.contentp
) {
797 struct strbuf outbuf
= STRBUF_INIT
;
799 if (type
!= OBJ_BLOB
) {
800 ret
= convert_object_file(r
, &outbuf
,
801 r
->hash_algo
, input_algo
,
802 content
, size
, type
, !do_die
);
807 content
= strbuf_detach(&outbuf
, NULL
);
810 *input_oi
->sizep
= size
;
811 if (input_oi
->contentp
)
812 *input_oi
->contentp
= content
;
816 *input_oi
->typep
= type
;
818 if (new_oi
.delta_base_oid
== &delta_base_oid
) {
819 if (repo_oid_to_algop(r
, &delta_base_oid
, input_algo
,
820 input_oi
->delta_base_oid
)) {
822 die(_("missing mapping of %s to %s"),
823 oid_to_hex(&delta_base_oid
),
828 input_oi
->whence
= new_oi
.whence
;
829 input_oi
->u
= new_oi
.u
;
833 int odb_read_object_info_extended(struct object_database
*odb
,
834 const struct object_id
*oid
,
835 struct object_info
*oi
,
840 if (oid
->algo
&& (hash_algo_by_ptr(odb
->repo
->hash_algo
) != oid
->algo
))
841 return oid_object_info_convert(odb
->repo
, oid
, oi
, flags
);
844 ret
= do_oid_object_info_extended(odb
, oid
, oi
, flags
);
850 /* returns enum object_type or negative */
851 int odb_read_object_info(struct object_database
*odb
,
852 const struct object_id
*oid
,
853 unsigned long *sizep
)
855 enum object_type type
;
856 struct object_info oi
= OBJECT_INFO_INIT
;
860 if (odb_read_object_info_extended(odb
, oid
, &oi
,
861 OBJECT_INFO_LOOKUP_REPLACE
) < 0)
866 int odb_pretend_object(struct object_database
*odb
,
867 void *buf
, unsigned long len
, enum object_type type
,
868 struct object_id
*oid
)
870 struct cached_object_entry
*co
;
873 hash_object_file(odb
->repo
->hash_algo
, buf
, len
, type
, oid
);
874 if (odb_has_object(odb
, oid
, 0) ||
875 find_cached_object(odb
, oid
))
878 ALLOC_GROW(odb
->cached_objects
,
879 odb
->cached_object_nr
+ 1, odb
->cached_object_alloc
);
880 co
= &odb
->cached_objects
[odb
->cached_object_nr
++];
881 co
->value
.size
= len
;
882 co
->value
.type
= type
;
883 co_buf
= xmalloc(len
);
884 memcpy(co_buf
, buf
, len
);
885 co
->value
.buf
= co_buf
;
886 oidcpy(&co
->oid
, oid
);
890 void *odb_read_object(struct object_database
*odb
,
891 const struct object_id
*oid
,
892 enum object_type
*type
,
895 struct object_info oi
= OBJECT_INFO_INIT
;
896 unsigned flags
= OBJECT_INFO_DIE_IF_CORRUPT
| OBJECT_INFO_LOOKUP_REPLACE
;
902 if (odb_read_object_info_extended(odb
, oid
, &oi
, flags
))
908 void *odb_read_object_peeled(struct object_database
*odb
,
909 const struct object_id
*oid
,
910 enum object_type required_type
,
912 struct object_id
*actual_oid_return
)
914 enum object_type type
;
917 struct object_id actual_oid
;
919 oidcpy(&actual_oid
, oid
);
922 const char *ref_type
= NULL
;
924 buffer
= odb_read_object(odb
, &actual_oid
, &type
, &isize
);
927 if (type
== required_type
) {
929 if (actual_oid_return
)
930 oidcpy(actual_oid_return
, &actual_oid
);
933 /* Handle references */
934 else if (type
== OBJ_COMMIT
)
936 else if (type
== OBJ_TAG
)
937 ref_type
= "object ";
942 ref_length
= strlen(ref_type
);
944 if (ref_length
+ odb
->repo
->hash_algo
->hexsz
> isize
||
945 memcmp(buffer
, ref_type
, ref_length
) ||
946 get_oid_hex_algop((char *) buffer
+ ref_length
, &actual_oid
,
947 odb
->repo
->hash_algo
)) {
952 /* Now we have the ID of the referred-to object in
953 * actual_oid. Check again. */
957 int odb_has_object(struct object_database
*odb
, const struct object_id
*oid
,
960 unsigned object_info_flags
= 0;
962 if (!startup_info
->have_repository
)
964 if (!(flags
& HAS_OBJECT_RECHECK_PACKED
))
965 object_info_flags
|= OBJECT_INFO_QUICK
;
966 if (!(flags
& HAS_OBJECT_FETCH_PROMISOR
))
967 object_info_flags
|= OBJECT_INFO_SKIP_FETCH_OBJECT
;
969 return odb_read_object_info_extended(odb
, oid
, NULL
, object_info_flags
) >= 0;
972 void odb_assert_oid_type(struct object_database
*odb
,
973 const struct object_id
*oid
, enum object_type expect
)
975 enum object_type type
= odb_read_object_info(odb
, oid
, NULL
);
977 die(_("%s is not a valid object"), oid_to_hex(oid
));
979 die(_("%s is not a valid '%s' object"), oid_to_hex(oid
),
983 struct object_database
*odb_new(struct repository
*repo
)
985 struct object_database
*o
= xmalloc(sizeof(*o
));
987 memset(o
, 0, sizeof(*o
));
989 INIT_LIST_HEAD(&o
->packed_git_mru
);
990 hashmap_init(&o
->pack_map
, pack_map_entry_cmp
, NULL
, 0);
991 pthread_mutex_init(&o
->replace_mutex
, NULL
);
992 string_list_init_dup(&o
->submodule_source_paths
);
996 static void free_object_directories(struct object_database
*o
)
999 struct odb_source
*next
;
1001 next
= o
->sources
->next
;
1002 free_object_directory(o
->sources
);
1005 kh_destroy_odb_path_map(o
->source_by_path
);
1006 o
->source_by_path
= NULL
;
1009 void odb_clear(struct object_database
*o
)
1011 FREE_AND_NULL(o
->alternate_db
);
1013 oidmap_clear(&o
->replace_map
, 1);
1014 pthread_mutex_destroy(&o
->replace_mutex
);
1016 free_commit_graph(o
->commit_graph
);
1017 o
->commit_graph
= NULL
;
1018 o
->commit_graph_attempted
= 0;
1020 free_object_directories(o
);
1021 o
->sources_tail
= NULL
;
1022 o
->loaded_alternates
= 0;
1024 for (size_t i
= 0; i
< o
->cached_object_nr
; i
++)
1025 free((char *) o
->cached_objects
[i
].value
.buf
);
1026 FREE_AND_NULL(o
->cached_objects
);
1028 INIT_LIST_HEAD(&o
->packed_git_mru
);
1029 close_object_store(o
);
1032 * `close_object_store()` only closes the packfiles, but doesn't free
1033 * them. We thus have to do this manually.
1035 for (struct packed_git
*p
= o
->packed_git
, *next
; p
; p
= next
) {
1039 o
->packed_git
= NULL
;
1041 hashmap_clear(&o
->pack_map
);
1042 string_list_clear(&o
->submodule_source_paths
, 0);