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

[PATCH v3 3/6] setup: handle ODB-related environment variables in `odb_new()`

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 5, 2026, 09:28 UTC
Message-ID
<20260805-pks-odb-create-on-disk-v3-3-c0ee3ac5141f@pks.im>
In-Reply-To
<20260805-pks-odb-create-on-disk-v3-0-c0ee3ac5141f@pks.im>

When initializing a repository's object database we have to respect the GIT_OBJECT_DIRECTORY and GIT_ALTERNATE_OBJECT_DIRECTORIES environment variables, which can be set by the user to override the default location of where we write objects to and read objects from.

This is handled in `apply_repository_format()`, which is fine. But in a subsequent commit we'll have to defer constructing the object database to a later point in some cases, and that will require a second site where we call `odb_new()`. And of course, that second site would have to handle those environment variables, as well.

It would be somewhat awkward to duplicate the logic though. But there's a better alternative: instead of handling this logic in "setup.c", we can easily handle environment variables in `odb_new()` itself. This ensures that object database creation is neatly self-contained, and we don't have to duplicate any of the logic.

Another benefit is that in a future patch series we plan to move handling of alternates into the backends themselves [1], and that will require us to also handle those environment variables in the "files" backend itself. So moving the logic into the ODB level already gets us one step closer to that goal.

Refactor the logic accordingly.
[1]: https://lore.kernel.org/git/amLgMqkqxR8mKIbT@pks.im/
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 odb.c                         | 20 ++++++++++++--------
 odb.h                         | 17 +++++++++++++++--
 setup.c                       | 11 ++++-------
 t/unit-tests/u-odb-inmemory.c |  2 +-
 4 files changed, 32 insertions(+), 18 deletions(-)
diff --git a/odb.c b/odb.c
index cf6e7938c0..b463afa072 100644
--- a/odb.c
+++ b/odb.c
@@ -1004,26 +1004,30 @@ int odb_write_object_stream(struct object_database *odb,
 }
 
 struct object_database *odb_new(struct repository *repo,
-				const char *primary_source,
-				const char *secondary_sources)
+				enum odb_new_flags flags)
 {
-	struct object_database *o = xmalloc(sizeof(*o));
-	char *to_free = NULL;
+	char *primary_source = NULL, *secondary_sources = NULL;
+	struct object_database *o;
 
-	memset(o, 0, sizeof(*o));
+	CALLOC_ARRAY(o, 1);
 	o->repo = repo;
 	pthread_mutex_init(&o->replace_mutex, NULL);
 	string_list_init_dup(&o->submodule_source_paths);
 
+	if (flags & ODB_NEW_HONOR_ENV) {
+		primary_source = xstrdup_or_null(getenv(DB_ENVIRONMENT));
+		secondary_sources = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
+	}
 	if (!primary_source)
-		primary_source = to_free = xstrfmt("%s/objects", repo->commondir);
+		primary_source = xstrfmt("%s/objects", repo->commondir);
+
 	o->sources = odb_source_new(o, primary_source, true);
 	o->sources_tail = &o->sources->next;
 	o->alternate_db = xstrdup_or_null(secondary_sources);
 	o->inmemory_objects = &odb_source_inmemory_new(o)->base;
 
-	free(to_free);
-
+	free(secondary_sources);
+	free(primary_source);
 	return o;
 }
 
diff --git a/odb.h b/odb.h
index 7995bed97b..8ec335c7f7 100644
--- a/odb.h
+++ b/odb.h
@@ -100,6 +100,20 @@ struct object_database {
 	struct string_list submodule_source_paths;
 };
 
+enum odb_new_flags {
+	/*
+	 * Honor environment variables when constructing the object database
+	 * sources. This makes us respect the following environment variables:
+	 *
+	 *   - GIT_OBJECT_DIRECTORY to override the primary object directory.
+	 *
+	 *   - GIT_ALTERNATE_OBJECT_DIRECTORIES to override alternates.
+	 *
+	 * Environment variables may be backend-specific.
+	 */
+	ODB_NEW_HONOR_ENV = (1 << 0),
+};
+
 /*
  * Create a new object database for the given repository.
  *
@@ -112,8 +126,7 @@ struct object_database {
  * Returns the newly created object database.
  */
 struct object_database *odb_new(struct repository *repo,
-				const char *primary_source,
-				const char *alternate_sources);
+				enum odb_new_flags flags);
 
 /* Free the object database and release all resources. */
 void odb_free(struct object_database *o);
diff --git a/setup.c b/setup.c
index 825572f5f1..5dfab3e79e 100644
--- a/setup.c
+++ b/setup.c
@@ -1765,7 +1765,7 @@ int apply_repository_format(struct repository *repo,
 			    enum apply_repository_format_flags flags,
 			    struct strbuf *err)
 {
-	char *object_directory = NULL, *alternate_object_directories = NULL;
+	enum odb_new_flags odb_new_flags = 0;
 
 	if (verify_repository_format(format, err) < 0)
 		return -1;
@@ -1779,8 +1779,6 @@ int apply_repository_format(struct repository *repo,
 	if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) {
 		const char *shallow_file;
 
-		object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
-		alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
 		shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
 		if (shallow_file)
 			set_alternate_shallow_file(repo, shallow_file);
@@ -1803,11 +1801,10 @@ int apply_repository_format(struct repository *repo,
 	repo->repository_format_precious_objects =
 		format->precious_objects;
 
-	repo->objects = odb_new(repo, object_directory,
-				alternate_object_directories);
+	if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV)
+		odb_new_flags |= ODB_NEW_HONOR_ENV;
+	repo->objects = odb_new(repo, odb_new_flags);
 
-	free(alternate_object_directories);
-	free(object_directory);
 	return 0;
 }
 
diff --git a/t/unit-tests/u-odb-inmemory.c b/t/unit-tests/u-odb-inmemory.c
index 6844bfc37c..db323e10fd 100644
--- a/t/unit-tests/u-odb-inmemory.c
+++ b/t/unit-tests/u-odb-inmemory.c
@@ -38,7 +38,7 @@ static void cl_assert_object_info(struct odb_source_inmemory *source,
 
 void test_odb_inmemory__initialize(void)
 {
-	odb = odb_new(&repo, "", "");
+	odb = odb_new(&repo, 0);
 }
 
 void test_odb_inmemory__cleanup(void)
-- 
2.55.0.679.g6767b8d81c.dirty
Previous: Patrick SteinhardtNext: Toon Claes
Message 38 of 68 in “odb: make creation of object database pluggable”
  1. 0/5 odb: make creation of object database pluggablePatrick Steinhardt, Jul 24, 2026
  2. 1/5 loose: load loose object map for the correct sourcePatrick Steinhardt, Jul 24, 2026
  3. Junio C HamanoJul 24, 2026
  4. Justin ToblerJul 28, 2026
  5. Toon ClaesJul 30, 2026
  6. Patrick SteinhardtAug 4, 2026
  7. 2/5 setup: detangle loading of loose object mapsPatrick Steinhardt, Jul 24, 2026
  8. Junio C HamanoJul 24, 2026
  9. Patrick SteinhardtAug 4, 2026
  10. Justin ToblerJul 28, 2026
  11. Toon ClaesJul 30, 2026
  12. Patrick SteinhardtAug 4, 2026
  13. 3/5 setup: defer object database creationPatrick Steinhardt, Jul 24, 2026
  14. Junio C HamanoJul 24, 2026
  15. Patrick SteinhardtAug 4, 2026
  16. Justin ToblerJul 28, 2026
  17. Patrick SteinhardtAug 4, 2026
  18. Patrick SteinhardtAug 4, 2026
  19. 4/5 odb/source: introduce function to map source type to namePatrick Steinhardt, Jul 24, 2026
  20. Junio C HamanoJul 26, 2026
  21. Patrick SteinhardtAug 4, 2026
  22. 5/5 odb: make creation of on-disk structures pluggablePatrick Steinhardt, Jul 24, 2026
  23. Junio C HamanoJul 26, 2026
  24. Patrick SteinhardtAug 4, 2026
  25. Justin ToblerJul 28, 2026
  26. 0/5 odb: make creation of object database pluggablePatrick Steinhardt, Aug 4, 2026
  27. 1/5 loose: load loose object map for the correct sourcePatrick Steinhardt, Aug 4, 2026
  28. 2/5 setup: detangle loading of loose object mapsPatrick Steinhardt, Aug 4, 2026
  29. 3/5 setup: defer object database creationPatrick Steinhardt, Aug 4, 2026
  30. Toon ClaesAug 4, 2026
  31. Patrick SteinhardtAug 5, 2026
  32. 4/5 odb/source: introduce function to map source type to namePatrick Steinhardt, Aug 4, 2026
  33. 5/5 odb: make creation of on-disk structures pluggablePatrick Steinhardt, Aug 4, 2026
  34. Justin ToblerAug 4, 2026
  35. 0/6 odb: make creation of object database pluggablePatrick Steinhardt, Aug 5, 2026
  36. 1/6 loose: load loose object map for the correct sourcePatrick Steinhardt, Aug 5, 2026
  37. 2/6 setup: detangle loading of loose object mapsPatrick Steinhardt, Aug 5, 2026
  38. 3/6 setup: handle ODB-related environment variables in `odb_new()`Patrick Steinhardt, Aug 5, 2026
  39. Toon ClaesAug 5, 2026
  40. Patrick SteinhardtAug 6, 2026
  41. 4/6 setup: defer object database creationPatrick Steinhardt, Aug 5, 2026
  42. Toon ClaesAug 5, 2026
  43. Patrick SteinhardtAug 6, 2026
  44. 5/6 odb/source: introduce function to map source type to namePatrick Steinhardt, Aug 5, 2026
  45. 6/6 odb: make creation of on-disk structures pluggablePatrick Steinhardt, Aug 5, 2026
  46. Toon ClaesAug 5, 2026
  47. 0/6 odb: make creation of object database pluggablePatrick Steinhardt, Aug 6, 2026
  48. 1/6 loose: load loose object map for the correct sourcePatrick Steinhardt, Aug 6, 2026
  49. 2/6 setup: detangle loading of loose object mapsPatrick Steinhardt, Aug 6, 2026
  50. 3/6 setup: handle ODB-related environment variables in `odb_new()`Patrick Steinhardt, Aug 6, 2026
  51. 4/6 setup: defer object database creationPatrick Steinhardt, Aug 6, 2026
  52. Toon ClaesAug 6, 2026
  53. Patrick SteinhardtAug 6, 2026
  54. Junio C HamanoAug 6, 2026
  55. 5/6 odb/source: introduce function to map source type to namePatrick Steinhardt, Aug 6, 2026
  56. 6/6 odb: make creation of on-disk structures pluggablePatrick Steinhardt, Aug 6, 2026
  57. Toon ClaesAug 6, 2026
  58. 0/6 odb: make creation of object database pluggablePatrick Steinhardt, Aug 7, 2026
  59. 1/6 loose: load loose object map for the correct sourcePatrick Steinhardt, Aug 7, 2026
  60. 2/6 setup: detangle loading of loose object mapsPatrick Steinhardt, Aug 7, 2026
  61. 3/6 setup: handle ODB-related environment variables in `odb_new()`Patrick Steinhardt, Aug 7, 2026
  62. 4/6 setup: defer object database creationPatrick Steinhardt, Aug 7, 2026
  63. Junio C HamanoAug 7, 2026
  64. Toon ClaesAug 7, 2026
  65. 5/6 odb/source: introduce function to map source type to namePatrick Steinhardt, Aug 7, 2026
  66. 6/6 odb: make creation of on-disk structures pluggablePatrick Steinhardt, Aug 7, 2026
  67. Toon ClaesAug 7, 2026
  68. Patrick SteinhardtAug 7, 2026

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.