diff mbox series

[2/2] fixup! tmp-objdir: new API for creating temporary writable databases

Message ID ef5a087813b7dfd232a9366eee09774d197e2307.1635287730.git.gitgitgadget@gmail.com (mailing list archive)
State Accepted
Commit a082404cf104d1c685d204aa3fcd26bf5a69673d
Headers show
Series tmp-objdir: fix regressions in core.fsyncobjectfiles=batch | expand

Commit Message

Neeraj Singh (WINDOWS-SFS) Oct. 26, 2021, 10:35 p.m. UTC
From: Neeraj Singh <neerajsi@microsoft.com>

When setup_work_tree executes, it redoes setup of the object database
path and various other aspects of the_repository.  This destroys the
temporary object database state.

This commit removes the temporary object database and reapplies it
around the operations in the chdir_notify callback.

Signed-off-by: Neeraj Singh <neerajsi@microsoft.com>
---
 environment.c |  5 +++++
 tmp-objdir.c  | 25 +++++++++++++++++++++++++
 tmp-objdir.h  | 15 +++++++++++++++
 3 files changed, 45 insertions(+)
diff mbox series

Patch

diff --git a/environment.c b/environment.c
index 46ec5072c05..7ba5ae06c71 100644
--- a/environment.c
+++ b/environment.c
@@ -17,6 +17,7 @@ 
 #include "commit.h"
 #include "strvec.h"
 #include "object-store.h"
+#include "tmp-objdir.h"
 #include "chdir-notify.h"
 #include "shallow.h"
 
@@ -344,10 +345,14 @@  static void update_relative_gitdir(const char *name,
 				   void *data)
 {
 	char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
+	struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
 	trace_printf_key(&trace_setup_key,
 			 "setup: move $GIT_DIR to '%s'",
 			 path);
+
 	set_git_dir_1(path);
+	if (tmp_objdir)
+		tmp_objdir_reapply_primary_odb(tmp_objdir, old_cwd, new_cwd);
 	free(path);
 }
 
diff --git a/tmp-objdir.c b/tmp-objdir.c
index 45d42a7bcf0..3d38eeab66b 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c
@@ -1,5 +1,6 @@ 
 #include "cache.h"
 #include "tmp-objdir.h"
+#include "chdir-notify.h"
 #include "dir.h"
 #include "sigchain.h"
 #include "string-list.h"
@@ -12,6 +13,7 @@  struct tmp_objdir {
 	struct strbuf path;
 	struct strvec env;
 	struct object_directory *prev_odb;
+	int will_destroy;
 };
 
 /*
@@ -315,4 +317,27 @@  void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
 	if (t->prev_odb)
 		BUG("the primary object database is already replaced");
 	t->prev_odb = set_temporary_primary_odb(t->path.buf, will_destroy);
+	t->will_destroy = will_destroy;
+}
+
+struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
+{
+	if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
+		return NULL;
+
+	restore_primary_odb(the_tmp_objdir->prev_odb, the_tmp_objdir->path.buf);
+	the_tmp_objdir->prev_odb = NULL;
+	return the_tmp_objdir;
+}
+
+void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
+		const char *new_cwd)
+{
+	char *path;
+
+	path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
+	strbuf_reset(&t->path);
+	strbuf_addstr(&t->path, path);
+	free(path);
+	tmp_objdir_replace_primary_odb(t, t->will_destroy);
 }
diff --git a/tmp-objdir.h b/tmp-objdir.h
index 75754cbfba6..a3145051f25 100644
--- a/tmp-objdir.h
+++ b/tmp-objdir.h
@@ -59,4 +59,19 @@  void tmp_objdir_add_as_alternate(const struct tmp_objdir *);
  */
 void tmp_objdir_replace_primary_odb(struct tmp_objdir *, int will_destroy);
 
+/*
+ * If the primary object database was replaced by a temporary object directory,
+ * restore it to its original value while keeping the directory contents around.
+ * Returns NULL if the primary object database was not replaced.
+ */
+struct tmp_objdir *tmp_objdir_unapply_primary_odb(void);
+
+/*
+ * Reapplies the former primary temporary object database, after protentially
+ * changing its relative path.
+ */
+void tmp_objdir_reapply_primary_odb(struct tmp_objdir *, const char *old_cwd,
+		const char *new_cwd);
+
+
 #endif /* TMP_OBJDIR_H */