Skip to content

Commit 35d9fc6

Browse files
pks-tgitster
authored andcommitted
odb: handle initialization of sources in odb_new()
The logic to set up a new object database is currently distributed across two functions in "repository.c": - In `initialize_repository()` we initialize an empty object database. This object database is not fully initialized and doesn't have any sources attached to it. - The primary object database source is then created in `repo_set_gitdir()`. Ideally though, the logic should be entirely self-contained so that we can iterate more readily on how exactly the sources themselves get set up. Refactor `odb_new()` to handle both allocation and setup of the object database. This ensures that the object database is always initialized and ready for use, and it allows us to change how the sources get set up eventually. Note that `repo_set_gitdir()` still reaches into the sources when the function gets called with an already-initialized object database. This will be fixed in the next commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c257bd5 commit 35d9fc6

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

odb.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,15 +1034,27 @@ int odb_write_object_stream(struct object_database *odb,
10341034
return odb_source_loose_write_stream(odb->sources, stream, len, oid);
10351035
}
10361036

1037-
struct object_database *odb_new(struct repository *repo)
1037+
struct object_database *odb_new(struct repository *repo,
1038+
const char *primary_source,
1039+
const char *secondary_sources)
10381040
{
10391041
struct object_database *o = xmalloc(sizeof(*o));
1042+
char *to_free = NULL;
10401043

10411044
memset(o, 0, sizeof(*o));
10421045
o->repo = repo;
10431046
o->packfiles = packfile_store_new(o);
10441047
pthread_mutex_init(&o->replace_mutex, NULL);
10451048
string_list_init_dup(&o->submodule_source_paths);
1049+
1050+
if (!primary_source)
1051+
primary_source = to_free = xstrfmt("%s/objects", repo->commondir);
1052+
o->sources = odb_source_new(o, primary_source, true);
1053+
o->sources_tail = &o->sources->next;
1054+
o->alternate_db = xstrdup_or_null(secondary_sources);
1055+
1056+
free(to_free);
1057+
10461058
return o;
10471059
}
10481060

odb.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,20 @@ struct object_database {
159159
struct string_list submodule_source_paths;
160160
};
161161

162-
struct object_database *odb_new(struct repository *repo);
162+
/*
163+
* Create a new object database for the given repository.
164+
*
165+
* If the primary source parameter is set it will override the usual primary
166+
* object directory derived from the repository's common directory. The
167+
* alternate sources are expected to be a PATH_SEP-separated list of secondary
168+
* sources. Note that these alternate sources will be added in addition to, not
169+
* instead of, the alternates identified by the primary source.
170+
*
171+
* Returns the newly created object database.
172+
*/
173+
struct object_database *odb_new(struct repository *repo,
174+
const char *primary_source,
175+
const char *alternate_sources);
163176

164177
/* Free the object database and release all resources. */
165178
void odb_free(struct object_database *o);

repository.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static void set_default_hash_algo(struct repository *repo)
5252

5353
void initialize_repository(struct repository *repo)
5454
{
55-
repo->objects = odb_new(repo);
5655
repo->remote_state = remote_state_new();
5756
repo->parsed_objects = parsed_object_pool_new(repo);
5857
ALLOC_ARRAY(repo->index, 1);
@@ -160,29 +159,26 @@ void repo_set_gitdir(struct repository *repo,
160159
* until after xstrdup(root). Then we can free it.
161160
*/
162161
char *old_gitdir = repo->gitdir;
163-
char *objects_path = NULL;
164162

165163
repo->gitdir = xstrdup(gitfile ? gitfile : root);
166164
free(old_gitdir);
167165

168166
repo_set_commondir(repo, o->commondir);
169-
expand_base_dir(&objects_path, o->object_dir,
170-
repo->commondir, "objects");
171-
172-
if (!repo->objects->sources) {
173-
repo->objects->sources = odb_source_new(repo->objects,
174-
objects_path, true);
175-
repo->objects->sources_tail = &repo->objects->sources->next;
176-
free(objects_path);
167+
168+
if (!repo->objects) {
169+
repo->objects = odb_new(repo, o->object_dir, o->alternate_db);
177170
} else {
171+
char *objects_path = NULL;
172+
expand_base_dir(&objects_path, o->object_dir,
173+
repo->commondir, "objects");
178174
free(repo->objects->sources->path);
179175
repo->objects->sources->path = objects_path;
176+
free(repo->objects->alternate_db);
177+
repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
180178
}
181179

182180
repo->disable_ref_updates = o->disable_ref_updates;
183181

184-
free(repo->objects->alternate_db);
185-
repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
186182
expand_base_dir(&repo->graft_file, o->graft_file,
187183
repo->commondir, "info/grafts");
188184
expand_base_dir(&repo->index_file, o->index_file,

0 commit comments

Comments
 (0)