Files
git/tmp-objdir.h
Patrick Steinhardt b79d085ef8 odb: move reparenting logic into respective subsystems
The primary object database source may be initialized with a relative
path. When reparenting the process to a different working directory we
thus have to update this path and have it point to the same path, but
relative to the new working directory.

This logic is handled in the object database layer. It consists of three
steps:

  1. We undo any potential temporary object directory, which are used
     for transactions. This is done so that we don't end up modifying
     the temporary object database source that got applied for the
     transaction.

  2. We then iterate through the non-transactional sources and reparent
     their respective paths.

  3. We reapply the temporary object directory, but update its path.

All of this logic is heavily tied to how the object database source
handles paths in the first place. It's an internal implementation
detail, and as sources may not even use an on-disk path at all it is not
a mechanism that applies to all potential sources.

Refactor the code so that the logic to reparent the sources is hosted by
the "files" source and the temporary object directory subsystems,
respectively. This logic is easier to reason about, but it also ensures
that this logic is handled at the correct level.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-02-23 13:50:00 -08:00

72 lines
2.2 KiB
C

#ifndef TMP_OBJDIR_H
#define TMP_OBJDIR_H
/*
* This API allows you to create a temporary object directory, advertise it to
* sub-processes via GIT_OBJECT_DIRECTORY and GIT_ALTERNATE_OBJECT_DIRECTORIES,
* and then either migrate its object into the main object directory, or remove
* it. The library handles unexpected signal/exit death by cleaning up the
* temporary directory.
*
* Example:
*
* struct child_process child = CHILD_PROCESS_INIT;
* struct tmp_objdir *t = tmp_objdir_create(repo, "incoming");
* strvec_push(&child.args, cmd);
* strvec_pushv(&child.env, tmp_objdir_env(t));
* if (!run_command(&child)) && !tmp_objdir_migrate(t))
* printf("success!\n");
* else
* die("failed...tmp_objdir will clean up for us");
*
*/
struct repository;
struct tmp_objdir;
/*
* Create a new temporary object directory with the specified prefix;
* returns NULL on failure.
*/
struct tmp_objdir *tmp_objdir_create(struct repository *r, const char *prefix);
/*
* Return a list of environment strings, suitable for use with
* child_process.env, that can be passed to child programs to make use of the
* temporary object directory.
*/
const char **tmp_objdir_env(const struct tmp_objdir *);
/*
* Finalize a temporary object directory by migrating its objects into the main
* object database, removing the temporary directory, and freeing any
* associated resources.
*/
int tmp_objdir_migrate(struct tmp_objdir *);
/*
* Destroy a temporary object directory, discarding any objects it contains.
*/
int tmp_objdir_destroy(struct tmp_objdir *);
/*
* Remove all objects from the temporary object directory, while leaving it
* around so more objects can be added.
*/
void tmp_objdir_discard_objects(struct tmp_objdir *);
/*
* Add the temporary object directory as an alternate object store in the
* current process.
*/
void tmp_objdir_add_as_alternate(const 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.
*/
void tmp_objdir_replace_primary_odb(struct tmp_objdir *, int will_destroy);
#endif /* TMP_OBJDIR_H */