Merge branch 'ps/odb-make-creation-pluggable' into seen

The creation of the on-disk data structures for the object database
has been made pluggable, allowing future backends to customize their
setup.  As part of this, the initialization of the object database
has been deferred, and the loading of the loose-object map has been
detangled from repository initialization.

* ps/odb-make-creation-pluggable:
  odb: make creation of on-disk structures pluggable
  odb/source: introduce function to map source type to name
  setup: defer object database creation
  setup: detangle loading of loose object maps
  loose: load loose object map for the correct source
seen
Junio C Hamano 2026-07-25 14:33:23 -07:00
commit 8d17fb86ed
13 changed files with 148 additions and 38 deletions

25
loose.c
View File

@ -61,12 +61,17 @@ static int insert_loose_map(struct odb_source_loose *loose,
return inserted;
}

static int load_one_loose_object_map(struct repository *repo, struct odb_source_loose *loose)
int loose_object_map_load(struct odb_source_loose *loose)
{
struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT;
struct repository *repo = loose->base.odb->repo;
struct strbuf buf = STRBUF_INIT;
char *path;
FILE *fp;
int ret = -1;

if (!should_use_loose_object_map(repo))
return 0;

if (!loose->map)
loose_object_map_init(&loose->map);
if (!loose->cache) {
@ -78,10 +83,10 @@ static int load_one_loose_object_map(struct repository *repo, struct odb_source_
insert_loose_map(loose, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
insert_loose_map(loose, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);

repo_common_path_replace(repo, &path, "objects/loose-object-idx");
fp = fopen(path.buf, "rb");
path = xstrfmt("%s/loose-object-idx", loose->base.path);
fp = fopen(path, "rb");
if (!fp) {
strbuf_release(&path);
free(path);
return 0;
}

@ -102,7 +107,7 @@ static int load_one_loose_object_map(struct repository *repo, struct odb_source_
err:
fclose(fp);
strbuf_release(&buf);
strbuf_release(&path);
free(path);
return ret;
}

@ -110,17 +115,13 @@ int repo_read_loose_object_map(struct repository *repo)
{
struct odb_source *source;

if (!should_use_loose_object_map(repo))
return 0;

odb_prepare_alternates(repo->objects);

for (source = repo->objects->sources; source; source = source->next) {
struct odb_source_files *files = odb_source_files_downcast(source);
if (load_one_loose_object_map(repo, files->loose) < 0) {
if (loose_object_map_load(files->loose) < 0)
return -1;
}
}

return 0;
}


View File

@ -13,6 +13,7 @@ struct loose_object_map {

void loose_object_map_init(struct loose_object_map **map);
void loose_object_map_clear(struct loose_object_map **map);
int loose_object_map_load(struct odb_source_loose *loose);
int repo_loose_object_map_oid(struct repository *repo,
const struct object_id *src,
const struct git_hash_algo *dest_algo,

View File

@ -51,6 +51,23 @@ static void odb_source_files_close(struct odb_source *source)
odb_source_close(&files->packed->base);
}

static int odb_source_files_create_on_disk(struct odb_source *source)
{
struct strbuf path = STRBUF_INIT;

safe_create_dir(source->odb->repo, source->path, 1);

strbuf_addf(&path, "%s/pack", source->path);
safe_create_dir(source->odb->repo, path.buf, 1);

strbuf_reset(&path);
strbuf_addf(&path, "%s/info", source->path);
safe_create_dir(source->odb->repo, path.buf, 1);

strbuf_release(&path);
return 0;
}

static void odb_source_files_prepare(struct odb_source *source,
enum odb_prepare_flags flags)
{
@ -742,6 +759,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,

files->base.free = odb_source_files_free;
files->base.close = odb_source_files_close;
files->base.create_on_disk = odb_source_files_create_on_disk;
files->base.prepare = odb_source_files_prepare;
files->base.read_object_info = odb_source_files_read_object_info;
files->base.read_object_stream = odb_source_files_read_object_stream;

View File

@ -43,7 +43,9 @@ bool odb_source_files_optimize_required(struct odb_source *source,
static inline struct odb_source_files *odb_source_files_downcast(struct odb_source *source)
{
if (source->type != ODB_SOURCE_FILES)
BUG("trying to downcast source of type '%d' to files", source->type);
BUG("trying to downcast source of type '%s' to '%s'",
odb_source_type_to_name(source->type),
odb_source_type_to_name(ODB_SOURCE_FILES));
return container_of(source, struct odb_source_files, base);
}


View File

@ -26,7 +26,9 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
static inline struct odb_source_inmemory *odb_source_inmemory_downcast(struct odb_source *source)
{
if (source->type != ODB_SOURCE_INMEMORY)
BUG("trying to downcast source of type '%d' to in-memory", source->type);
BUG("trying to downcast source of type '%s' to '%s'",
odb_source_type_to_name(source->type),
odb_source_type_to_name(ODB_SOURCE_INMEMORY));
return container_of(source, struct odb_source_inmemory, base);
}


View File

@ -1055,5 +1055,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
if (!is_absolute_path(loose->base.path))
chdir_notify_register(NULL, odb_source_loose_reparent, loose);

loose_object_map_load(loose);

return loose;
}

View File

@ -41,7 +41,9 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
static inline struct odb_source_loose *odb_source_loose_downcast(struct odb_source *source)
{
if (source->type != ODB_SOURCE_LOOSE)
BUG("trying to downcast source of type '%d' to loose", source->type);
BUG("trying to downcast source of type '%s' to '%s'",
odb_source_type_to_name(source->type),
odb_source_type_to_name(ODB_SOURCE_LOOSE));
return container_of(source, struct odb_source_loose, base);
}


View File

@ -78,7 +78,9 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
static inline struct odb_source_packed *odb_source_packed_downcast(struct odb_source *source)
{
if (source->type != ODB_SOURCE_PACKED)
BUG("trying to downcast source of type '%d' to packed", source->type);
BUG("trying to downcast source of type '%s' to '%s'",
odb_source_type_to_name(source->type),
odb_source_type_to_name(ODB_SOURCE_PACKED));
return container_of(source, struct odb_source_packed, base);
}


View File

@ -4,6 +4,25 @@
#include "odb/source.h"
#include "packfile.h"

static const char * const odb_source_names_by_type[] = {
[ODB_SOURCE_UNKNOWN] = "unknown",
[ODB_SOURCE_FILES] = "files",
[ODB_SOURCE_LOOSE] = "loose",
[ODB_SOURCE_PACKED] = "packed",
[ODB_SOURCE_INMEMORY] = "inmemory",
};

const char *odb_source_type_to_name(enum odb_source_type type)
{
const char *name;
if (type < 0 || type >= ARRAY_SIZE(odb_source_names_by_type))
type = ODB_SOURCE_UNKNOWN;
name = odb_source_names_by_type[type];
if (!name)
BUG("name missing in `odb_source_names_by_type` for '%d'", type);
return name;
}

struct odb_source *odb_source_new(struct object_database *odb,
const char *path,
bool local)

View File

@ -25,6 +25,12 @@ enum odb_source_type {
ODB_SOURCE_INMEMORY,
};

/*
* Convert between the enum and its name. Returns the equivalent of "unknown"
* for unknown types.
*/
const char *odb_source_type_to_name(enum odb_source_type type);

struct object_id;
struct odb_read_stream;
struct strvec;
@ -83,6 +89,18 @@ struct odb_source {
*/
void (*close)(struct odb_source *source);

/*
* This callback is expected to create on-disk data structures that are
* required for this source to operate.
*
* The callback is expected to return 0 on success, a negative error
* code otherwise.
*
* This callback may be NULL in case the source does not need any
* on-disk setup.
*/
int (*create_on_disk)(struct odb_source *source);

/*
* This callback is expected to prepare the source so that it becomes
* ready for use. It optionally clears underlying caches of the object
@ -327,6 +345,17 @@ static inline void odb_source_close(struct odb_source *source)
source->close(source);
}

/*
* Create on-disk data structures that are required for this source to operate
* correctly. Returns 0 on success, a negative error code otherwise.
*/
static inline int odb_source_create_on_disk(struct odb_source *source)
{
if (!source->create_on_disk)
return 0;
return source->create_on_disk(source);
}

/*
* Prepare the object database source and clear any caches. Depending on the
* backend used this may have the effect that concurrently-written objects

View File

@ -201,8 +201,6 @@ void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, uint32_t al
if (hash_algo_by_ptr(repo->hash_algo) == algo)
BUG("hash_algo and compat_hash_algo match");
repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
if (repo->compat_hash_algo)
repo_read_loose_object_map(repo);
#else
if (algo)
die(_("compatibility hash algorithm support requires Rust"));

65
setup.c
View File

@ -1760,6 +1760,13 @@ enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
return result;
}

static void get_object_directories(char **object_directory,
char **alternate_object_directories)
{
*object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
*alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
}

int apply_repository_format(struct repository *repo,
const struct repository_format *format,
enum apply_repository_format_flags flags,
@ -1779,8 +1786,9 @@ int apply_repository_format(struct repository *repo,
if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) {
const char *shallow_file;

object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
get_object_directories(&object_directory,
&alternate_object_directories);

shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
if (shallow_file)
set_alternate_shallow_file(repo, shallow_file);
@ -1788,8 +1796,6 @@ int apply_repository_format(struct repository *repo,

repo->bare_cfg = format->is_bare;
repo_set_hash_algo(repo, format->hash_algo);
repo->objects = odb_new(repo, object_directory,
alternate_object_directories);
repo_set_compat_hash_algo(repo, format->compat_hash_algo);
repo_set_ref_storage_format(repo,
format->ref_storage_format,
@ -1805,6 +1811,10 @@ int apply_repository_format(struct repository *repo,
repo->repository_format_precious_objects =
format->precious_objects;

if (!(flags & APPLY_REPOSITORY_FORMAT_SKIP_ODB_CREATION))
repo->objects = odb_new(repo, object_directory,
alternate_object_directories);

free(alternate_object_directories);
free(object_directory);
return 0;
@ -2653,25 +2663,37 @@ static int create_default_files(struct repository *repo,
return reinit;
}

static void create_object_directory(struct repository *repo)
static void create_object_database(struct repository *repo)
{
struct strbuf path = STRBUF_INIT;
size_t baselen;
char *object_directory, *alternate_object_directories;

strbuf_addstr(&path, repo_get_object_directory(repo));
baselen = path.len;
get_object_directories(&object_directory, &alternate_object_directories);

safe_create_dir(repo, path.buf, 1);
/*
* Create the "objects" directory in the common directory. This is done
* so that the repository can be discovered regardless of the backend
* used.
*
* Note that we only do this in case the object directory wasn't
* overwritten via an environment variable. If it _is_ being overridden
* then we skip this step, as the repository won't be discoverable
* anyway without the environment variable.
*/
if (!object_directory) {
struct strbuf objects_dir = STRBUF_INIT;
repo_common_path_append(repo, &objects_dir, "objects");
safe_create_dir(repo, objects_dir.buf, 1);
strbuf_release(&objects_dir);
}

strbuf_setlen(&path, baselen);
strbuf_addstr(&path, "/pack");
safe_create_dir(repo, path.buf, 1);
repo->objects = odb_new(repo, object_directory,
alternate_object_directories);

strbuf_setlen(&path, baselen);
strbuf_addstr(&path, "/info");
safe_create_dir(repo, path.buf, 1);
if (odb_source_create_on_disk(repo->objects->sources) < 0)
die("failed creating object database");

strbuf_release(&path);
free(alternate_object_directories);
free(object_directory);
}

static void separate_git_dir(struct repository *repo,
@ -2867,9 +2889,10 @@ int init_db(struct repository *repo,
*/
read_and_verify_repository_format(&repo_fmt, repo_get_git_dir(repo), NULL);
repository_format_configure(&repo_fmt, hash, ref_storage_format);
if (apply_repository_format(repo, &repo_fmt, APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0)
if (apply_repository_format(repo, &repo_fmt,
APPLY_REPOSITORY_FORMAT_HONOR_ENV |
APPLY_REPOSITORY_FORMAT_SKIP_ODB_CREATION, &err) < 0)
die("%s", err.buf);
startup_info->have_repository = 1;

/*
* Ensure `core.hidedotfiles` is processed. This must happen after we
@ -2885,7 +2908,9 @@ int init_db(struct repository *repo,

if (!(flags & INIT_DB_SKIP_REFDB))
create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
create_object_directory(repo);
create_object_database(repo);

startup_info->have_repository = 1;

if (repo_settings_get_shared_repository(repo)) {
char buf[10];

View File

@ -241,6 +241,15 @@ enum apply_repository_format_flags {
* relate to the object database.
*/
APPLY_REPOSITORY_FORMAT_HONOR_ENV = (1 << 0),

/*
* Usually, the object database is created after the repository format
* was applied. This step is skipped if this flag is set, which leaves
* us with a partially-working repository.
*
* This is useful when initializing a new repository.
*/
APPLY_REPOSITORY_FORMAT_SKIP_ODB_CREATION = (1 << 1),
};

/*