Skip to content

Commit 7e0aa0a

Browse files
pks-tgitster
authored andcommitted
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>
1 parent d9ecf26 commit 7e0aa0a

4 files changed

Lines changed: 42 additions & 75 deletions

File tree

odb.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "git-compat-util.h"
22
#include "abspath.h"
3-
#include "chdir-notify.h"
43
#include "commit-graph.h"
54
#include "config.h"
65
#include "dir.h"
@@ -1037,38 +1036,6 @@ int odb_write_object_stream(struct object_database *odb,
10371036
return odb_source_loose_write_stream(odb->sources, stream, len, oid);
10381037
}
10391038

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-
10721039
struct object_database *odb_new(struct repository *repo,
10731040
const char *primary_source,
10741041
const char *secondary_sources)
@@ -1089,8 +1056,6 @@ struct object_database *odb_new(struct repository *repo,
10891056

10901057
free(to_free);
10911058

1092-
chdir_notify_register(NULL, odb_update_commondir, o);
1093-
10941059
return o;
10951060
}
10961061

@@ -1136,8 +1101,6 @@ void odb_free(struct object_database *o)
11361101

11371102
string_list_clear(&o->submodule_source_paths, 0);
11381103

1139-
chdir_notify_unregister(NULL, odb_update_commondir, o);
1140-
11411104
free(o);
11421105
}
11431106

odb/source-files.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
#include "git-compat-util.h"
2+
#include "abspath.h"
3+
#include "chdir-notify.h"
24
#include "object-file.h"
35
#include "odb/source.h"
46
#include "odb/source-files.h"
57
#include "packfile.h"
68

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+
721
void odb_source_files_free(struct odb_source_files *files)
822
{
923
if (!files)
1024
return;
25+
chdir_notify_unregister(NULL, odb_source_files_reparent, files);
1126
odb_source_loose_free(files->loose);
1227
packfile_store_free(files->packed);
1328
odb_source_release(&files->base);
@@ -25,5 +40,13 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
2540
files->loose = odb_source_loose_new(&files->base);
2641
files->packed = packfile_store_new(&files->base);
2742

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+
2851
return files;
2952
}

tmp-objdir.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ static void tmp_objdir_free(struct tmp_objdir *t)
3636
free(t);
3737
}
3838

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+
3954
int tmp_objdir_destroy(struct tmp_objdir *t)
4055
{
4156
int err;
@@ -51,6 +66,7 @@ int tmp_objdir_destroy(struct tmp_objdir *t)
5166

5267
err = remove_dir_recursively(&t->path, 0);
5368

69+
chdir_notify_unregister(NULL, tmp_objdir_reparent, t);
5470
tmp_objdir_free(t);
5571

5672
return err;
@@ -137,6 +153,9 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r,
137153
strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX",
138154
repo_get_object_directory(r), prefix);
139155

156+
if (!is_absolute_path(t->path.buf))
157+
chdir_notify_register(NULL, tmp_objdir_reparent, t);
158+
140159
if (!mkdtemp(t->path.buf)) {
141160
/* free, not destroy, as we never touched the filesystem */
142161
tmp_objdir_free(t);
@@ -315,26 +334,3 @@ void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
315334
t->path.buf, will_destroy);
316335
t->will_destroy = will_destroy;
317336
}
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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,4 @@ void tmp_objdir_add_as_alternate(const struct tmp_objdir *);
6868
*/
6969
void tmp_objdir_replace_primary_odb(struct tmp_objdir *, int will_destroy);
7070

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-
8671
#endif /* TMP_OBJDIR_H */

0 commit comments

Comments
 (0)