]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb: simplify calling `link_alt_odb_entry()`
authorPatrick Steinhardt <ps@pks.im>
Mon, 11 Aug 2025 13:46:45 +0000 (15:46 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 11 Aug 2025 16:22:22 +0000 (09:22 -0700)
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>
odb.c

diff --git a/odb.c b/odb.c
index 0c808bb288f1688d97b60f42918fd625d8db5645..4f884e3b509a596cee3d624fab66bded4748b368 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -142,8 +142,7 @@ static void read_info_alternates(struct object_database *odb,
 static struct odb_source *link_alt_odb_entry(struct object_database *odb,
                                             const char *dir,
                                             const char *relative_base,
-                                            int depth,
-                                            const char *normalized_objdir)
+                                            int depth)
 {
        struct odb_source *alternate = NULL;
        struct strbuf pathbuf = STRBUF_INIT;
@@ -170,7 +169,10 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
        while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
                strbuf_setlen(&pathbuf, pathbuf.len - 1);
 
-       if (!alt_odb_usable(odb, &pathbuf, normalized_objdir, &pos))
+       strbuf_reset(&tmp);
+       strbuf_realpath(&tmp, odb->sources->path, 1);
+
+       if (!alt_odb_usable(odb, &pathbuf, tmp.buf, &pos))
                goto error;
 
        CALLOC_ARRAY(alternate, 1);
@@ -227,7 +229,6 @@ static const char *parse_alt_odb_entry(const char *string,
 static void link_alt_odb_entries(struct object_database *odb, const char *alt,
                                 int sep, const char *relative_base, int depth)
 {
-       struct strbuf objdirbuf = STRBUF_INIT;
        struct strbuf dir = STRBUF_INIT;
 
        if (!alt || !*alt)
@@ -239,17 +240,13 @@ static void link_alt_odb_entries(struct object_database *odb, const char *alt,
                return;
        }
 
-       strbuf_realpath(&objdirbuf, odb->sources->path, 1);
-
        while (*alt) {
                alt = parse_alt_odb_entry(alt, sep, &dir);
                if (!dir.len)
                        continue;
-               link_alt_odb_entry(odb, dir.buf,
-                                  relative_base, depth, objdirbuf.buf);
+               link_alt_odb_entry(odb, dir.buf, relative_base, depth);
        }
        strbuf_release(&dir);
-       strbuf_release(&objdirbuf);
 }
 
 static void read_info_alternates(struct object_database *odb,
@@ -317,20 +314,12 @@ void odb_add_to_alternates_file(struct object_database *odb,
 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
                                                const char *dir)
 {
-       struct odb_source *alternate;
-       char *objdir;
-
        /*
         * Make sure alternates are initialized, or else our entry may be
         * overwritten when they are.
         */
        odb_prepare_alternates(odb);
-
-       objdir = real_pathdup(odb->sources->path, 1);
-       alternate = link_alt_odb_entry(odb, dir, NULL, 0, objdir);
-
-       free(objdir);
-       return alternate;
+       return link_alt_odb_entry(odb, dir, NULL, 0);
 }
 
 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,