blob: 76efc7edee5be4a9197a242f526c74b05f49802a [file] [log] [blame]
Jeff King2564d992016-10-03 16:49:11 -04001#ifndef TMP_OBJDIR_H
2#define TMP_OBJDIR_H
3
4/*
5 * This API allows you to create a temporary object directory, advertise it to
6 * sub-processes via GIT_OBJECT_DIRECTORY and GIT_ALTERNATE_OBJECT_DIRECTORIES,
7 * and then either migrate its object into the main object directory, or remove
8 * it. The library handles unexpected signal/exit death by cleaning up the
9 * temporary directory.
10 *
11 * Example:
12 *
Neeraj Singhb3cecf42021-12-06 22:05:04 +000013 * struct tmp_objdir *t = tmp_objdir_create("incoming");
Jeff King2564d992016-10-03 16:49:11 -040014 * if (!run_command_v_opt_cd_env(cmd, 0, NULL, tmp_objdir_env(t)) &&
15 * !tmp_objdir_migrate(t))
16 * printf("success!\n");
17 * else
18 * die("failed...tmp_objdir will clean up for us");
19 *
20 */
21
22struct tmp_objdir;
23
24/*
Neeraj Singhb3cecf42021-12-06 22:05:04 +000025 * Create a new temporary object directory with the specified prefix;
26 * returns NULL on failure.
Jeff King2564d992016-10-03 16:49:11 -040027 */
Neeraj Singhb3cecf42021-12-06 22:05:04 +000028struct tmp_objdir *tmp_objdir_create(const char *prefix);
Jeff King2564d992016-10-03 16:49:11 -040029
30/*
31 * Return a list of environment strings, suitable for use with
32 * child_process.env, that can be passed to child programs to make use of the
33 * temporary object directory.
34 */
35const char **tmp_objdir_env(const struct tmp_objdir *);
36
37/*
38 * Finalize a temporary object directory by migrating its objects into the main
39 * object database, removing the temporary directory, and freeing any
40 * associated resources.
41 */
42int tmp_objdir_migrate(struct tmp_objdir *);
43
44/*
45 * Destroy a temporary object directory, discarding any objects it contains.
46 */
47int tmp_objdir_destroy(struct tmp_objdir *);
48
49/*
Elijah Newren7b90ab42022-02-02 02:37:29 +000050 * Remove all objects from the temporary object directory, while leaving it
51 * around so more objects can be added.
52 */
53void tmp_objdir_discard_objects(struct tmp_objdir *);
54
55/*
Jeff King2564d992016-10-03 16:49:11 -040056 * Add the temporary object directory as an alternate object store in the
57 * current process.
58 */
59void tmp_objdir_add_as_alternate(const struct tmp_objdir *);
60
Neeraj Singhb3cecf42021-12-06 22:05:04 +000061/*
62 * Replaces the writable object store in the current process with the temporary
63 * object directory and makes the former main object store an alternate.
64 * If will_destroy is nonzero, the object directory may not be migrated.
65 */
66void tmp_objdir_replace_primary_odb(struct tmp_objdir *, int will_destroy);
67
68/*
69 * If the primary object database was replaced by a temporary object directory,
70 * restore it to its original value while keeping the directory contents around.
71 * Returns NULL if the primary object database was not replaced.
72 */
73struct tmp_objdir *tmp_objdir_unapply_primary_odb(void);
74
75/*
76 * Reapplies the former primary temporary object database, after potentially
77 * changing its relative path.
78 */
79void tmp_objdir_reapply_primary_odb(struct tmp_objdir *, const char *old_cwd,
80 const char *new_cwd);
81
82
Jeff King2564d992016-10-03 16:49:11 -040083#endif /* TMP_OBJDIR_H */