[PATCH 09/13] tmp-objdir: replace primary source at creation time
HOTtoday
From: Patrick Steinhardt <hidden>
Date: 2026-10-02 10:08:52
Subsystem:
the rest · Maintainer:
Linus Torvalds
When creating transactions via the "tmp-objdir" subsystem callers are expected to first call `tmp_objdir_create()` to create the quarantine directory and then `tmp_objdir_replace_primary_odb()` to activate that quarantine directory as the primary object database source so that all newly written objects are written into it. This dance is performed by all users of temporary object directories, so that makes the setup unnecessarily involved. Merge the logic to replace the primary object database source into `tmp_objdir_create()` to simplify the dance. Note that with the quarantine directory being installed by `tmp_objdir_create()`, users of this function no longer get a pointer to the underlying quarantine directory. So instead of repreparing only that directory, we now reprepare the whole source instead, and as we know that the source has wired up the quarantine directory already, this will cause us to reprepare the quarantine directory, too. Signed-off-by: Patrick Steinhardt <redacted> --- log-tree.c | 3 +-- object-file.c | 8 ++------ tmp-objdir.c | 47 +++++++++++++++++++---------------------------- tmp-objdir.h | 18 +++++------------- 4 files changed, 27 insertions(+), 49 deletions(-)
diff --git a/log-tree.c b/log-tree.c
index 83a3c4bf9b..361dfadbb3 100644
--- a/log-tree.c
+++ b/log-tree.c@@ -1043,10 +1043,9 @@ static int do_remerge_diff(struct rev_info *opt, * into the alternative object store list as the primary. */ if (opt->remerge_diff && !opt->remerge_objdir) { - opt->remerge_objdir = tmp_objdir_create(the_repository, "remerge-diff"); + opt->remerge_objdir = tmp_objdir_create(the_repository, "remerge-diff", 1); if (!opt->remerge_objdir) return error(_("unable to create temporary object directory")); - tmp_objdir_replace_primary_odb(opt->remerge_objdir, 1); } /* Setup merge options */
diff --git a/object-file.c b/object-file.c
index fe9139c96d..02521afcaf 100644
--- a/object-file.c
+++ b/object-file.c@@ -488,7 +488,6 @@ struct odb_transaction_files { enum odb_transaction_flags flags; struct tmp_objdir *objdir; - struct odb_files_dir *quarantine; struct transaction_packfile packfile; const char *prefix;
@@ -511,12 +510,10 @@ int odb_transaction_files_prepare(struct odb_transaction *base) if (!transaction || transaction->objdir) return 0; - transaction->objdir = tmp_objdir_create(base->source->odb->repo, transaction->prefix); + transaction->objdir = tmp_objdir_create(base->source->odb->repo, transaction->prefix, 0); if (!transaction->objdir) return error(_("unable to create temporary object directory")); - transaction->quarantine = tmp_objdir_replace_primary_odb(transaction->objdir, 0); - return 0; }
@@ -1443,8 +1440,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base, return -1; } - odb_source_prepare(&transaction->quarantine->packed->base, - ODB_PREPARE_FLUSH_CACHES); + odb_source_prepare(base->source, ODB_PREPARE_FLUSH_CACHES); } return 0;
diff --git a/tmp-objdir.c b/tmp-objdir.c
index a3903b6e6d..719b55e580 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c@@ -57,7 +57,7 @@ static void tmp_objdir_reparent(const char *old_cwd, /* * Restore the primary source that was previously replaced by - * `tmp_objdir_replace_primary_odb()`. + * `tmp_objdir_create()`. */ static void tmp_objdir_restore_source(struct tmp_objdir *t) {
@@ -157,8 +157,10 @@ static int setup_tmp_objdir(const char *root) } struct tmp_objdir *tmp_objdir_create(struct repository *r, - const char *prefix) + const char *prefix, + int will_destroy) { + struct odb_source_files *files = odb_source_files_downcast(r->objects->sources); static int installed_handlers; struct tmp_objdir *t;
@@ -167,6 +169,7 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r, t = xcalloc(1, sizeof(*t)); t->repo = r; + t->will_destroy = will_destroy; strbuf_init(&t->path, 0); strvec_init(&t->env);
@@ -204,6 +207,20 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r, env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT, absolute_path(t->path.buf)); + /* + * Make a new primary source and link the old primary source in as an + * alternate. Disable ref updates while a temporary source is active, + * since the objects in the database may roll back. + */ + t->temp_dir = odb_files_dir_new(t->repo->objects, t->path.buf, false); + t->temp_dir->loose->base.will_destroy = will_destroy; + t->temp_dir->packed->base.will_destroy = will_destroy; + t->temp_dir->next = files->dirs; + + t->orig_dir = files->dirs; + files->dirs = t->temp_dir; + t->repo->disable_ref_updates = true; + return t; }
@@ -345,29 +362,3 @@ const char **tmp_objdir_env(const struct tmp_objdir *t) return NULL; return t->env.v; } - -struct odb_files_dir *tmp_objdir_replace_primary_odb(struct tmp_objdir *t, - int will_destroy) -{ - struct odb_source_files *files = odb_source_files_downcast(t->repo->objects->sources); - - if (t->temp_dir) - BUG("the primary object database is already replaced"); - t->will_destroy = will_destroy; - - /* - * Make a new primary source and link the old primary source in as an - * alternate. Disable ref updates while a temporary source is active, - * since the objects in the database may roll back. - */ - t->temp_dir = odb_files_dir_new(t->repo->objects, t->path.buf, false); - t->temp_dir->loose->base.will_destroy = will_destroy; - t->temp_dir->packed->base.will_destroy = will_destroy; - t->temp_dir->next = files->dirs; - - t->orig_dir = files->dirs; - files->dirs = t->temp_dir; - t->repo->disable_ref_updates = true; - - return t->temp_dir; -}
diff --git a/tmp-objdir.h b/tmp-objdir.h
index 5ed0db1ee0..a301b01d84 100644
--- a/tmp-objdir.h
+++ b/tmp-objdir.h@@ -11,7 +11,7 @@ * Example: * * struct child_process child = CHILD_PROCESS_INIT; - * struct tmp_objdir *t = tmp_objdir_create(repo, "incoming"); + * struct tmp_objdir *t = tmp_objdir_create(repo, "incoming", 0); * strvec_push(&child.args, cmd); * strvec_pushv(&child.env, tmp_objdir_env(t)); * if (!run_command(&child)) && !tmp_objdir_migrate(t))
@@ -25,10 +25,11 @@ struct repository; struct tmp_objdir; /* - * Create a new temporary object directory with the specified prefix; - * returns NULL on failure. + * Create a new temporary object directory with the specified prefix and + * install the directory as the primary write target; returns NULL on failure. */ -struct tmp_objdir *tmp_objdir_create(struct repository *r, const char *prefix); +struct tmp_objdir *tmp_objdir_create(struct repository *r, const char *prefix, + int will_destroy); /* * Return a list of environment strings, suitable for use with
@@ -55,13 +56,4 @@ int tmp_objdir_destroy(struct tmp_objdir *); */ void tmp_objdir_discard_objects(struct tmp_objdir *); -/* - * Replaces the writable object store in the current process with the temporary - * object directory and makes the former main object store an alternate. - * If will_destroy is nonzero, the object directory may not be migrated. Returns - * the newly installed primary source. - */ -struct odb_files_dir *tmp_objdir_replace_primary_odb(struct tmp_objdir *, - int will_destroy); - #endif /* TMP_OBJDIR_H */
--
2.56.0.379.gc618271300.dirty