Skip to content

Commit 66bce9d

Browse files
pks-tgitster
authored andcommitted
repository: initialize index in repo_init()
When Git starts, one of the first things it will do is to call `initialize_the_repository()`. This function sets up both the global `the_repository` and `the_index` variables as required. Part of that setup is also to set `the_repository.index = &the_index` so that the index can be accessed via the repository. When calling `repo_init()` on a repository though we set the complete struct to all-zeroes, which will also cause us to unset the `index` pointer. And as we don't re-initialize the index in that function, we will end up with a `NULL` pointer here. This has been fine until now becaues this function is only used to create a new repository. git-init(1) does not access the index at all after initializing the repository, whereas git-checkout(1) only uses `the_index` directly. We are about to remove `the_index` though, which will uncover this partially-initialized repository structure. Refactor the code and create a common `initialize_repository()` function that gets called from `repo_init()` and `initialize_the_repository()`. This function sets up both the repository and the index as required. Like this, we can easily special-case when `repo_init()` gets called with `the_repository`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f59aa5e commit 66bce9d

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

repository.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ static struct repository the_repo;
2525
struct repository *the_repository;
2626
struct index_state the_index;
2727

28+
static void initialize_repository(struct repository *repo,
29+
struct index_state *index)
30+
{
31+
repo->index = index;
32+
repo->objects = raw_object_store_new();
33+
repo->remote_state = remote_state_new();
34+
repo->parsed_objects = parsed_object_pool_new();
35+
index_state_init(index, repo);
36+
}
37+
2838
void initialize_the_repository(void)
2939
{
3040
the_repository = &the_repo;
31-
32-
the_repo.index = &the_index;
33-
the_repo.objects = raw_object_store_new();
34-
the_repo.remote_state = remote_state_new();
35-
the_repo.parsed_objects = parsed_object_pool_new();
36-
37-
index_state_init(&the_index, the_repository);
38-
41+
initialize_repository(the_repository, &the_index);
3942
repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
4043
}
4144

@@ -188,9 +191,12 @@ int repo_init(struct repository *repo,
188191
struct repository_format format = REPOSITORY_FORMAT_INIT;
189192
memset(repo, 0, sizeof(*repo));
190193

191-
repo->objects = raw_object_store_new();
192-
repo->parsed_objects = parsed_object_pool_new();
193-
repo->remote_state = remote_state_new();
194+
if (repo == the_repository) {
195+
initialize_repository(the_repository, &the_index);
196+
} else {
197+
ALLOC_ARRAY(repo->index, 1);
198+
initialize_repository(repo, repo->index);
199+
}
194200

195201
if (repo_init_gitdir(repo, gitdir))
196202
goto error;

0 commit comments

Comments
 (0)