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

[PATCH v2 5/6] repository: drop `the_index` variable

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

All users of `the_index` have been converted to use either a custom `struct index_state *` or the index provided by `the_repository`. We can thus drop the globally-accessible declaration of this variable. In fact, we can go further than that and drop `the_index` completely now and have it be allocated dynamically in `initialize_repository()` as all the other data structures in it are.

This concludes the quest to make Git `the_index` free, which has started with 4aab5b46f4 (Make read-cache.c "the_index" free., 2007-04-01).

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 repository.c | 25 ++++++-------------------
 repository.h |  3 ---
 2 files changed, 6 insertions(+), 22 deletions(-)
diff --git a/repository.c b/repository.c
index d64d15d952..089edbffa2 100644
--- a/repository.c
+++ b/repository.c
@@ -1,8 +1,3 @@
-/*
- * not really _using_ the compat macros, just make sure the_index
- * declaration matches the definition in this file.
- */
-#define USE_THE_INDEX_VARIABLE
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "repository.h"
@@ -23,22 +18,20 @@
 /* The main repository */
 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)
+static void initialize_repository(struct repository *repo)
 {
-	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);
+	ALLOC_ARRAY(repo->index, 1);
+	index_state_init(repo->index, repo);
 }
 
 void initialize_the_repository(void)
 {
 	the_repository = &the_repo;
-	initialize_repository(the_repository, &the_index);
+	initialize_repository(the_repository);
 	repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
 }
 
@@ -191,12 +184,7 @@ int repo_init(struct repository *repo,
 	struct repository_format format = REPOSITORY_FORMAT_INIT;
 	memset(repo, 0, sizeof(*repo));
 
-	if (repo == the_repository) {
-		initialize_repository(the_repository, &the_index);
-	} else {
-		ALLOC_ARRAY(repo->index, 1);
-		initialize_repository(repo, repo->index);
-	}
+	initialize_repository(repo);
 
 	if (repo_init_gitdir(repo, gitdir))
 		goto error;
@@ -313,8 +301,7 @@ void repo_clear(struct repository *repo)
 
 	if (repo->index) {
 		discard_index(repo->index);
-		if (repo->index != &the_index)
-			FREE_AND_NULL(repo->index);
+		FREE_AND_NULL(repo->index);
 	}
 
 	if (repo->promisor_remote_config) {
diff --git a/repository.h b/repository.h
index 268436779c..6f4af15417 100644
--- a/repository.h
+++ b/repository.h
@@ -187,9 +187,6 @@ struct repository {
 };
 
 extern struct repository *the_repository;
-#ifdef USE_THE_INDEX_VARIABLE
-extern struct index_state the_index;
-#endif
 
 /*
  * Define a custom repository layout. Any field can be NULL, which
-- 
2.44.GIT
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 23 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.