odb: simplify calling `link_alt_odb_entry()`

Callers of `link_alt_odb_entry()` are expected to pass in three different paths: - The (potentially relative) path of the object directory that we're about to add. - The base that should be used to resolve a relative object directory path. - The resolved path to the object database's objects directory. Juggling those three paths makes the calling convention somewhat hard to grok at first. As it turns out, the third parameter is redundant: we always pass in the resolved path of the object database's primary source, and we already pass in the database itself. So instead, we can resolve that path in the function itself. One downside of this is that one caller of `link_alt_odb_entry()` calls this function in a loop, so we were able to resolve the directory a single time, only. But ultimately, we only ever end up with a rather limited number of alternates anyway, so the extra couple of cycles we save feels more like a micro optimization. Refactor the code accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 11, 2025 at 15:46 UTC 57363dfa0dce05aac735d5cfd626e6aac8cb706c
1 file changed +7 -18
odb.c
+7 -18
@@ -142,8 +142,7 @@ static void read_info_alternates(struct object_database *odb,
142 static struct odb_source *link_alt_odb_entry(struct object_database *odb,
143 const char *dir,
144 const char *relative_base,
145 - int depth,
146 - const char *normalized_objdir)
145 + int depth)
146 {
147 struct odb_source *alternate = NULL;
148 struct strbuf pathbuf = STRBUF_INIT;
@@ -170,7 +169,10 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
169 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
170 strbuf_setlen(&pathbuf, pathbuf.len - 1);
171
173 - if (!alt_odb_usable(odb, &pathbuf, normalized_objdir, &pos))
172 + strbuf_reset(&tmp);
173 + strbuf_realpath(&tmp, odb->sources->path, 1);
174 +
175 + if (!alt_odb_usable(odb, &pathbuf, tmp.buf, &pos))
176 goto error;
177
178 CALLOC_ARRAY(alternate, 1);
@@ -227,7 +229,6 @@ static const char *parse_alt_odb_entry(const char *string,
229 static void link_alt_odb_entries(struct object_database *odb, const char *alt,
230 int sep, const char *relative_base, int depth)
231 {
230 - struct strbuf objdirbuf = STRBUF_INIT;
232 struct strbuf dir = STRBUF_INIT;
233
234 if (!alt || !*alt)
@@ -239,17 +240,13 @@ static void link_alt_odb_entries(struct object_database *odb, const char *alt,
240 return;
241 }
242
242 - strbuf_realpath(&objdirbuf, odb->sources->path, 1);
243 -
243 while (*alt) {
244 alt = parse_alt_odb_entry(alt, sep, &dir);
245 if (!dir.len)
246 continue;
248 - link_alt_odb_entry(odb, dir.buf,
249 - relative_base, depth, objdirbuf.buf);
247 + link_alt_odb_entry(odb, dir.buf, relative_base, depth);
248 }
249 strbuf_release(&dir);
252 - strbuf_release(&objdirbuf);
250 }
251
252 static void read_info_alternates(struct object_database *odb,
@@ -317,20 +314,12 @@ void odb_add_to_alternates_file(struct object_database *odb,
314 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
315 const char *dir)
316 {
320 - struct odb_source *alternate;
321 - char *objdir;
322 -
317 /*
318 * Make sure alternates are initialized, or else our entry may be
319 * overwritten when they are.
320 */
321 odb_prepare_alternates(odb);
328 -
329 - objdir = real_pathdup(odb->sources->path, 1);
330 - alternate = link_alt_odb_entry(odb, dir, NULL, 0, objdir);
331 -
332 - free(objdir);
333 - return alternate;
322 + return link_alt_odb_entry(odb, dir, NULL, 0);
323 }
324
325 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,