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

[PATCH v3 2/2] tmp-objdir: disable ref updates when replacing the primary odb

From
NGNeeraj Singh via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 6, 2021, 22:05 UTC
Message-ID
<4e44121c2d7bced65e25eb7ec5156290132bec94.1638828305.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1091.v3.git.1638828305.gitgitgadget@gmail.com>
From: Neeraj Singh <neerajsi@microsoft.com>

When creating a subprocess with a temporary ODB, we set the GIT_QUARANTINE_ENVIRONMENT env var to tell child Git processes not to update refs, since the tmp-objdir may go away.

Introduce a similar mechanism for in-process temporary ODBs when we call tmp_objdir_replace_primary_odb. Now both mechanisms set the disable_ref_updates flag on the odb, which is queried by the ref_transaction_prepare function.

Peff's test case [1] was invoking ref updates via the cachetextconv setting. That particular code silently does nothing when a ref update is forbidden. See the call to notes_cache_put in fill_textconv where errors are ignored.

[1] https://lore.kernel.org/git/YVOn3hDsb5pnxR53@coredump.intra.peff.net/
Reported-by: Jeff King <peff@peff.net>
Signed-off-by: Neeraj Singh <neerajsi@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 environment.c  | 4 ++++
 object-file.c  | 6 ++++++
 object-store.h | 7 +++++++
 refs.c         | 2 +-
 repository.c   | 2 ++
 repository.h   | 1 +
 6 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/environment.c b/environment.c
index fe51dfe24d4..a8b64f5194f 100644
--- a/environment.c
+++ b/environment.c
@@ -169,6 +169,10 @@ void setup_git_env(const char *git_dir)
 	args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
 	args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
 	args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
+	if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
+		args.disable_ref_updates = 1;
+	}
+
 	repo_set_gitdir(the_repository, git_dir, &args);
 	strvec_clear(&to_free);
 
diff --git a/object-file.c b/object-file.c
index 0b6a61aeaff..659ef7623ff 100644
--- a/object-file.c
+++ b/object-file.c
@@ -699,6 +699,12 @@ struct object_directory *set_temporary_primary_odb(const char *dir, int will_des
 	 */
 	new_odb = xcalloc(1, sizeof(*new_odb));
 	new_odb->path = xstrdup(dir);
+
+	/*
+	 * Disable ref updates while a temporary odb is active, since
+	 * the objects in the database may roll back.
+	 */
+	new_odb->disable_ref_updates = 1;
 	new_odb->will_destroy = will_destroy;
 	new_odb->next = the_repository->objects->odb;
 	the_repository->objects->odb = new_odb;
diff --git a/object-store.h b/object-store.h
index cb173e69392..6f89482df03 100644
--- a/object-store.h
+++ b/object-store.h
@@ -27,6 +27,13 @@ struct object_directory {
 	uint32_t loose_objects_subdir_seen[8]; /* 256 bits */
 	struct oidtree *loose_objects_cache;
 
+	/*
+	 * This is a temporary object store created by the tmp_objdir
+	 * facility. Disable ref updates since the objects in the store
+	 * might be discarded on rollback.
+	 */
+	int disable_ref_updates;
+
 	/*
 	 * This object store is ephemeral, so there is no need to fsync.
 	 */
diff --git a/refs.c b/refs.c
index d7cc0a23a3b..ac744e85f5f 100644
--- a/refs.c
+++ b/refs.c
@@ -2137,7 +2137,7 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 		break;
 	}
 
-	if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
+	if (refs->repo->objects->odb->disable_ref_updates) {
 		strbuf_addstr(err,
 			      _("ref updates forbidden inside quarantine environment"));
 		return -1;
diff --git a/repository.c b/repository.c
index c5b90ba93ea..dce8e35ac20 100644
--- a/repository.c
+++ b/repository.c
@@ -80,6 +80,8 @@ void repo_set_gitdir(struct repository *repo,
 	expand_base_dir(&repo->objects->odb->path, o->object_dir,
 			repo->commondir, "objects");
 
+	repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
+
 	free(repo->objects->alternate_db);
 	repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
 	expand_base_dir(&repo->graft_file, o->graft_file,
diff --git a/repository.h b/repository.h
index a057653981c..7c04e99ac5c 100644
--- a/repository.h
+++ b/repository.h
@@ -158,6 +158,7 @@ struct set_gitdir_args {
 	const char *graft_file;
 	const char *index_file;
 	const char *alternate_db;
+	int disable_ref_updates;
 };
 
 void repo_set_gitdir(struct repository *repo, const char *root,
-- 
gitgitgadget
Previous: Neeraj Singh via GitGitGadgetNext: Elijah Newren
Message 17 of 18 in “ns/tmp-objdir: add support for temporary writable databases”
  1. 0/2 ns/tmp-objdir: add support for temporary writable databasesNeeraj K. Singh via GitGitGadget, Dec 4, 2021
  2. 1/2 tmp-objdir: new API for creating temporary writable databasesNeeraj Singh via GitGitGadget, Dec 4, 2021
  3. 2/2 tmp-objdir: disable ref updates when replacing the primary odbNeeraj Singh via GitGitGadget, Dec 4, 2021
  4. Junio C HamanoDec 5, 2021
  5. Neeraj SinghDec 5, 2021
  6. Junio C HamanoDec 5, 2021
  7. Neeraj SinghDec 6, 2021
  8. 0/2 ns/tmp-objdir: add support for temporary writable databasesNeeraj K. Singh via GitGitGadget, Dec 6, 2021
  9. 1/2 tmp-objdir: new API for creating temporary writable databasesNeeraj Singh via GitGitGadget, Dec 6, 2021
  10. Junio C HamanoDec 6, 2021
  11. Neeraj SinghDec 6, 2021
  12. Junio C HamanoDec 6, 2021
  13. 2/2 tmp-objdir: disable ref updates when replacing the primary odbNeeraj Singh via GitGitGadget, Dec 6, 2021
  14. Neeraj SinghDec 6, 2021
  15. 0/2 ns/tmp-objdir: add support for temporary writable databasesNeeraj K. Singh via GitGitGadget, Dec 6, 2021
  16. 1/2 tmp-objdir: new API for creating temporary writable databasesNeeraj Singh via GitGitGadget, Dec 6, 2021
  17. 2/2 tmp-objdir: disable ref updates when replacing the primary odbNeeraj Singh via GitGitGadget, Dec 6, 2021
  18. Elijah NewrenDec 8, 2021

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.