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

[RFC PATCH 1/2] repository: have get_the_repository() to remove the_repository dependency

From
Stefan Beller <sbeller@google.com>
Date
Oct 18, 2018, 18:37 UTC
Message-ID
<20181018183758.81186-2-sbeller@google.com>
In-Reply-To
<20181018183758.81186-1-sbeller@google.com>

The struct 'the_repo' contains all data that of the main repository. As we move more and more globals into this struct, the usual way of accessing these is using 'the_repository', which can be used as a drop in replacement for accessing the migrated globals.

However during the migration of globals into the repository object, it is not clear if some code path rely on the_repository or can work on an arbitrary repository (as we'd eventually want for submodules) due to the excessive use of the_repository throughout the code base.

To address this, introduce a function 'get_the_repository(void)' which will return the main repository and set the_repository to NULL when the environment variable GIT_NO_THE_REPOSITORY is set.

This function is to be strictly used only at the beginning of builtin command to assign it to a local repository pointer that we'll use to pass through the code base.

By having the possibility to set the_repository to NULL, we'll get a segfault when we try to access the_repository instead of using the handle that we pass around.

This approach let's us have the_repository in the setup code, which in its current form is not yet able to transition into a world where the repository handle is passed around and only test the passing around of the repository handle for later stage code.

Eventually in the future the setup code will produce the repository handle and each 'cmd_foo(int argc, char **argv)' builtin would get the repository via an additional parameter.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 repository.c | 10 ++++++++++
 repository.h | 13 ++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/repository.c b/repository.c
index 5dd1486718..aaaababb88 100644
--- a/repository.c
+++ b/repository.c
@@ -20,6 +20,16 @@ void initialize_the_repository(void)
 	repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
 }
 
+struct repository *get_the_repository(void)
+{
+	struct repository *r = the_repository;
+
+	if (getenv("GIT_NO_THE_REPOSITORY"))
+		the_repository = NULL;
+
+	return r;
+}
+
 static void expand_base_dir(char **out, const char *in,
 			    const char *base_dir, const char *def_in)
 {
diff --git a/repository.h b/repository.h
index 9f16c42c1e..26f5d64f68 100644
--- a/repository.h
+++ b/repository.h
@@ -114,13 +114,24 @@ void repo_set_gitdir(struct repository *repo, const char *root,
 		     const struct set_gitdir_args *extra_args);
 void repo_set_worktree(struct repository *repo, const char *path);
 void repo_set_hash_algo(struct repository *repo, int algo);
-void initialize_the_repository(void);
 int repo_init(struct repository *r, const char *gitdir, const char *worktree);
 int repo_submodule_init(struct repository *submodule,
 			struct repository *superproject,
 			const char *path);
 void repo_clear(struct repository *repo);
 
+/*
+ * Initializes the repository 'the_repository', which is used in the transition
+ * phase of moving globals into the repository struct.
+ */
+void initialize_the_repository(void);
+
+/*
+ * To be called once; after the call use only returned repository, and do not
+ * use the_repository any more
+ */
+struct repository *get_the_repository(void);
+
 /*
  * Populates the repository's index from its index_file, an index struct will
  * be allocated if needed.
-- 
2.19.0
Previous: Stefan BellerNext: Stefan Beller
Message 29 of 46 in “Bring more repository handles into our code base”
  1. 00/19 Bring more repository handles into our code baseStefan Beller, Oct 16, 2018
  2. 01/19 sha1_file: allow read_object to read objects in arbitrary repositoriesStefan Beller, Oct 16, 2018
  3. 02/19 packfile: allow has_packed_and_bad to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  4. 03/19 object-store: allow read_object_file_extended to read from arbitrary repositoriesStefan Beller, Oct 16, 2018
  5. 04/19 object-store: prepare read_object_file to deal with arbitrary repositoriesStefan Beller, Oct 16, 2018
  6. 05/19 object-store: prepare has_{sha1, object}_file[_with_flags] to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  7. 06/19 object: parse_object to honor its repository argumentStefan Beller, Oct 16, 2018
  8. 07/19 commit: allow parse_commit* to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  9. 08/19 commit-reach.c: allow paint_down_to_common to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  10. 09/19 commit-reach.c: allow merge_bases_many to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  11. 10/19 commit-reach.c: allow remove_redundant to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  12. 11/19 commit-reach.c: allow get_merge_bases_many_0 to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  13. 12/19 commit-reach: prepare get_merge_bases to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  14. 13/19 commit-reach: prepare in_merge_bases[_many] to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  15. 14/19 commit: prepare get_commit_buffer to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  16. 15/19 commit: prepare repo_unuse_commit_buffer to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  17. 16/19 commit: prepare logmsg_reencode to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  18. 17/19 pretty: prepare format_commit_message to handle arbitrary repositoriesStefan Beller, Oct 16, 2018
  19. 18/19 submodule: use submodule repos for object lookupStefan Beller, Oct 16, 2018
  20. Jonathan TanOct 19, 2018
  21. SZEDER GáborOct 25, 2018
  22. Derrick StoleeOct 31, 2018
  23. Stefan BellerNov 1, 2018
  24. 19/19 submodule: don't add submodule as odb for pushStefan Beller, Oct 16, 2018
  25. Jonathan TanOct 19, 2018
  26. Derrick StoleeOct 17, 2018
  27. Stefan BellerOct 17, 2018
  28. 0/2 Bring the_repository into cmd_fooStefan Beller, Oct 18, 2018
  29. 1/2 repository: have get_the_repository() to remove the_repository dependencyStefan Beller, Oct 18, 2018
  30. 2/2 builtin/merge-base.c: do not rely on the_repository any moreStefan Beller, Oct 18, 2018
  31. Jonathan TanOct 18, 2018
  32. Stefan BellerOct 18, 2018
  33. Junio C HamanoOct 19, 2018
  34. New semantic patches vs. in-flight topics [was: Re: [PATCH 00/19] Bring more repository handles into our code base]SZEDER Gábor, Oct 22, 2018
  35. Stefan BellerOct 22, 2018
  36. SZEDER GáborOct 25, 2018
  37. Stefan BellerOct 25, 2018
  38. Junio C HamanoOct 22, 2018
  39. Stefan BellerOct 23, 2018
  40. Junio C HamanoOct 23, 2018
  41. Junio C HamanoOct 23, 2018
  42. Carlo ArenasOct 23, 2018
  43. Junio C HamanoOct 23, 2018
  44. Stefan BellerOct 23, 2018
  45. Junio C HamanoOct 24, 2018
  46. Jeff KingOct 25, 2018

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.