odb: move reparenting logic into respective subsystems

The primary object database source may be initialized with a relative path. When the process changes its current 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>

Patrick Steinhardt committed Mar 5, 2026 at 15:19 UTC 7e0aa0ab803405076b82ca66d328d314d17870ac
4 files changed +42 -75
odb.c
-37
@@ -1,6 +1,5 @@
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 -#include "chdir-notify.h"
3 #include "commit-graph.h"
4 #include "config.h"
5 #include "dir.h"
@@ -1037,38 +1036,6 @@ int odb_write_object_stream(struct object_database *odb,
1036 return odb_source_loose_write_stream(odb->sources, stream, len, oid);
1037 }
1038
1040 -static void odb_update_commondir(const char *name UNUSED,
1041 - const char *old_cwd,
1042 - const char *new_cwd,
1043 - void *cb_data)
1044 -{
1045 - struct object_database *odb = cb_data;
1046 - struct tmp_objdir *tmp_objdir;
1047 - struct odb_source *source;
1048 -
1049 - tmp_objdir = tmp_objdir_unapply_primary_odb();
1050 -
1051 - /*
1052 - * In theory, we only have to do this for the primary object source, as
1053 - * alternates' paths are always resolved to an absolute path.
1054 - */
1055 - for (source = odb->sources; source; source = source->next) {
1056 - char *path;
1057 -
1058 - if (is_absolute_path(source->path))
1059 - continue;
1060 -
1061 - path = reparent_relative_path(old_cwd, new_cwd,
1062 - source->path);
1063 -
1064 - free(source->path);
1065 - source->path = path;
1066 - }
1067 -
1068 - if (tmp_objdir)
1069 - tmp_objdir_reapply_primary_odb(tmp_objdir, old_cwd, new_cwd);
1070 -}
1071 -
1039 struct object_database *odb_new(struct repository *repo,
1040 const char *primary_source,
1041 const char *secondary_sources)
@@ -1089,8 +1056,6 @@ struct object_database *odb_new(struct repository *repo,
1056
1057 free(to_free);
1058
1092 - chdir_notify_register(NULL, odb_update_commondir, o);
1093 -
1059 return o;
1060 }
1061
@@ -1136,8 +1101,6 @@ void odb_free(struct object_database *o)
1101
1102 string_list_clear(&o->submodule_source_paths, 0);
1103
1139 - chdir_notify_unregister(NULL, odb_update_commondir, o);
1140 -
1104 free(o);
1105 }
1106
odb/source-files.c
+23
@@ -1,13 +1,28 @@
1 #include "git-compat-util.h"
2 +#include "abspath.h"
3 +#include "chdir-notify.h"
4 #include "object-file.h"
5 #include "odb/source.h"
6 #include "odb/source-files.h"
7 #include "packfile.h"
8
9 +static void odb_source_files_reparent(const char *name UNUSED,
10 + const char *old_cwd,
11 + const char *new_cwd,
12 + void *cb_data)
13 +{
14 + struct odb_source_files *files = cb_data;
15 + char *path = reparent_relative_path(old_cwd, new_cwd,
16 + files->base.path);
17 + free(files->base.path);
18 + files->base.path = path;
19 +}
20 +
21 void odb_source_files_free(struct odb_source_files *files)
22 {
23 if (!files)
24 return;
25 + chdir_notify_unregister(NULL, odb_source_files_reparent, files);
26 odb_source_loose_free(files->loose);
27 packfile_store_free(files->packed);
28 odb_source_release(&files->base);
@@ -25,5 +40,13 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
40 files->loose = odb_source_loose_new(&files->base);
41 files->packed = packfile_store_new(&files->base);
42
43 + /*
44 + * Ideally, we would only ever store absolute paths in the source. This
45 + * is not (yet) possible though because we access and assume relative
46 + * paths in the primary ODB source in some user-facing functionality.
47 + */
48 + if (!is_absolute_path(path))
49 + chdir_notify_register(NULL, odb_source_files_reparent, files);
50 +
51 return files;
52 }
tmp-objdir.c
+19 -23
@@ -36,6 +36,21 @@ static void tmp_objdir_free(struct tmp_objdir *t)
36 free(t);
37 }
38
39 +static void tmp_objdir_reparent(const char *name UNUSED,
40 + const char *old_cwd,
41 + const char *new_cwd,
42 + void *cb_data)
43 +{
44 + struct tmp_objdir *t = cb_data;
45 + char *path;
46 +
47 + path = reparent_relative_path(old_cwd, new_cwd,
48 + t->path.buf);
49 + strbuf_reset(&t->path);
50 + strbuf_addstr(&t->path, path);
51 + free(path);
52 +}
53 +
54 int tmp_objdir_destroy(struct tmp_objdir *t)
55 {
56 int err;
@@ -51,6 +66,7 @@ int tmp_objdir_destroy(struct tmp_objdir *t)
66
67 err = remove_dir_recursively(&t->path, 0);
68
69 + chdir_notify_unregister(NULL, tmp_objdir_reparent, t);
70 tmp_objdir_free(t);
71
72 return err;
@@ -137,6 +153,9 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r,
153 strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX",
154 repo_get_object_directory(r), prefix);
155
156 + if (!is_absolute_path(t->path.buf))
157 + chdir_notify_register(NULL, tmp_objdir_reparent, t);
158 +
159 if (!mkdtemp(t->path.buf)) {
160 /* free, not destroy, as we never touched the filesystem */
161 tmp_objdir_free(t);
@@ -315,26 +334,3 @@ void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
334 t->path.buf, will_destroy);
335 t->will_destroy = will_destroy;
336 }
318 -
319 -struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
320 -{
321 - if (!the_tmp_objdir || !the_tmp_objdir->prev_source)
322 - return NULL;
323 -
324 - odb_restore_primary_source(the_tmp_objdir->repo->objects,
325 - the_tmp_objdir->prev_source, the_tmp_objdir->path.buf);
326 - the_tmp_objdir->prev_source = NULL;
327 - return the_tmp_objdir;
328 -}
329 -
330 -void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
331 - const char *new_cwd)
332 -{
333 - char *path;
334 -
335 - path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
336 - strbuf_reset(&t->path);
337 - strbuf_addstr(&t->path, path);
338 - free(path);
339 - tmp_objdir_replace_primary_odb(t, t->will_destroy);
340 -}
tmp-objdir.h
-15
@@ -68,19 +68,4 @@ void tmp_objdir_add_as_alternate(const struct tmp_objdir *);
68 */
69 void tmp_objdir_replace_primary_odb(struct tmp_objdir *, int will_destroy);
70
71 -/*
72 - * If the primary object database was replaced by a temporary object directory,
73 - * restore it to its original value while keeping the directory contents around.
74 - * Returns NULL if the primary object database was not replaced.
75 - */
76 -struct tmp_objdir *tmp_objdir_unapply_primary_odb(void);
77 -
78 -/*
79 - * Reapplies the former primary temporary object database, after potentially
80 - * changing its relative path.
81 - */
82 -void tmp_objdir_reapply_primary_odb(struct tmp_objdir *, const char *old_cwd,
83 - const char *new_cwd);
84 -
85 -
71 #endif /* TMP_OBJDIR_H */