git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/6] repository: initialize index in `repo_init()`

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 18, 2024, 12:14 UTC
Message-ID
<416612e7e31a4b055b3fe17a5cdf78f8d07bd9e2.1713442061.git.ps@pks.im>
In-Reply-To
<cover.1713442061.git.ps@pks.im>

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 <ps@pks.im>
---
 repository.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
diff --git a/repository.c b/repository.c
index e15b416944..d64d15d952 100644
--- a/repository.c
+++ b/repository.c
@@ -25,17 +25,20 @@ static struct repository the_repo;
 struct repository *the_repository;
 struct index_state the_index;
 
+static void initialize_repository(struct repository *repo,
+				  struct index_state *index)
+{
+	repo->index = index;
+	repo->objects = raw_object_store_new();
+	repo->remote_state = remote_state_new();
+	repo->parsed_objects = parsed_object_pool_new();
+	index_state_init(index, repo);
+}
+
 void initialize_the_repository(void)
 {
 	the_repository = &the_repo;
-
-	the_repo.index = &the_index;
-	the_repo.objects = raw_object_store_new();
-	the_repo.remote_state = remote_state_new();
-	the_repo.parsed_objects = parsed_object_pool_new();
-
-	index_state_init(&the_index, the_repository);
-
+	initialize_repository(the_repository, &the_index);
 	repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
 }
 
@@ -188,9 +191,12 @@ int repo_init(struct repository *repo,
 	struct repository_format format = REPOSITORY_FORMAT_INIT;
 	memset(repo, 0, sizeof(*repo));
 
-	repo->objects = raw_object_store_new();
-	repo->parsed_objects = parsed_object_pool_new();
-	repo->remote_state = remote_state_new();
+	if (repo == the_repository) {
+		initialize_repository(the_repository, &the_index);
+	} else {
+		ALLOC_ARRAY(repo->index, 1);
+		initialize_repository(repo, repo->index);
+	}
 
 	if (repo_init_gitdir(repo, gitdir))
 		goto error;
-- 
2.44.GIT
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 21 of 26 in “global: drop external `the_index` variable”
  1. 0/5 global: drop external `the_index` variablePatrick Steinhardt, Apr 15, 2024
  2. 1/5 t/helper: stop using `the_index`Patrick Steinhardt, Apr 15, 2024
  3. Karthik NayakApr 17, 2024
  4. 2/5 builtin: stop using `the_index`Patrick Steinhardt, Apr 15, 2024
  5. Karthik NayakApr 17, 2024
  6. Patrick SteinhardtApr 18, 2024
  7. 3/5 repository: initialize index in `repo_init()`Patrick Steinhardt, Apr 15, 2024
  8. Karthik NayakApr 17, 2024
  9. Patrick SteinhardtApr 18, 2024
  10. 4/5 builtin/clone: stop using `the_index`Patrick Steinhardt, Apr 15, 2024
  11. 5/5 repository: drop global `the_index` variablePatrick Steinhardt, Apr 15, 2024
  12. Phillip WoodApr 15, 2024
  13. Patrick SteinhardtApr 15, 2024
  14. Junio C HamanoApr 15, 2024
  15. Patrick SteinhardtApr 16, 2024
  16. Karthik NayakApr 17, 2024
  17. Patrick SteinhardtApr 18, 2024
  18. 0/6 global: drop `the_index` variablePatrick Steinhardt, Apr 18, 2024
  19. 1/6 t/helper: stop using `the_index`Patrick Steinhardt, Apr 18, 2024
  20. 2/6 builtin: stop using `the_index`Patrick Steinhardt, Apr 18, 2024
  21. 3/6 repository: initialize index in `repo_init()`Patrick Steinhardt, Apr 18, 2024
  22. 4/6 builtin/clone: stop using `the_index`Patrick Steinhardt, Apr 18, 2024
  23. 5/6 repository: drop `the_index` variablePatrick Steinhardt, Apr 18, 2024
  24. 6/6 repository: drop `initialize_the_repository()`Patrick Steinhardt, Apr 18, 2024
  25. Junio C HamanoApr 18, 2024
  26. Patrick SteinhardtApr 19, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.