odb: move computation of normalized objdir into `alt_odb_usable()`
The function `alt_odb_usable()` receives as input the object database, the path it's supposed to determine usability for as well as the normalized path of the main object directory of the repository. The last part is derived by the function's caller from the object database. As we already pass the object database to `alt_odb_usable()` it is redundant information. Drop the extra parameter and compute the normalized object directory in the function itself. While at it, rename the function to `odb_is_source_usable()` to align it with modern terminology. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Dec 11, 2025 at 10:30 UTC
d17673ef4285d3d5f70909136f1ffe2745bcb71c
1 file changed
+15
-12
odb.c
+15
-12
@@ -89,17 +89,20 @@ int odb_mkstemp(struct object_database *odb,
89
/*
90
* Return non-zero iff the path is usable as an alternate object database.
91
*/
92
-static int alt_odb_usable(struct object_database *o, const char *path,
93
- const char *normalized_objdir)
92
+static bool odb_is_source_usable(struct object_database *o, const char *path)
93
{
94
int r;
95
+ struct strbuf normalized_objdir = STRBUF_INIT;
96
+ bool usable = false;
97
+
98
+ strbuf_realpath(&normalized_objdir, o->sources->path, 1);
99
100
/* Detect cases where alternate disappeared */
101
if (!is_directory(path)) {
102
error(_("object directory %s does not exist; "
103
"check .git/objects/info/alternates"),
104
path);
102
- return 0;
105
+ goto out;
106
}
107
108
/*
@@ -116,13 +119,17 @@ static int alt_odb_usable(struct object_database *o, const char *path,
119
kh_value(o->source_by_path, p) = o->sources;
120
}
121
119
- if (fspatheq(path, normalized_objdir))
120
- return 0;
122
+ if (fspatheq(path, normalized_objdir.buf))
123
+ goto out;
124
125
if (kh_get_odb_path_map(o->source_by_path, path) < kh_end(o->source_by_path))
123
- return 0;
126
+ goto out;
127
+
128
+ usable = true;
129
125
- return 1;
130
+out:
131
+ strbuf_release(&normalized_objdir);
132
+ return usable;
133
}
134
135
/*
@@ -164,13 +171,10 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
171
int depth)
172
{
173
struct odb_source *alternate = NULL;
167
- struct strbuf tmp = STRBUF_INIT;
174
khiter_t pos;
175
int ret;
176
171
- strbuf_realpath(&tmp, odb->sources->path, 1);
172
-
173
- if (!alt_odb_usable(odb, source, tmp.buf))
177
+ if (!odb_is_source_usable(odb, source))
178
goto error;
179
180
alternate = odb_source_new(odb, source, false);
@@ -188,7 +192,6 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
192
read_info_alternates(odb, alternate->path, depth + 1);
193
194
error:
191
- strbuf_release(&tmp);
195
return alternate;
196
}
197