]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb: introduce `odb_source_new()`
authorPatrick Steinhardt <ps@pks.im>
Mon, 3 Nov 2025 07:41:57 +0000 (08:41 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 3 Nov 2025 20:18:45 +0000 (12:18 -0800)
We have three different locations where we create a new ODB source.
Deduplicate the logic via a new `odb_source_new()` function.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
odb.c
odb.h
repository.c

diff --git a/odb.c b/odb.c
index 57d85ed9505e3fb2a22beeb663527fe543104f41..d2d4c514ae5c09af549b40cfee15a4f5c98ad519 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -141,6 +141,20 @@ static void read_info_alternates(struct object_database *odb,
                                 const char *relative_base,
                                 int depth);
 
+struct odb_source *odb_source_new(struct object_database *odb,
+                                 const char *path,
+                                 bool local)
+{
+       struct odb_source *source;
+
+       CALLOC_ARRAY(source, 1);
+       source->odb = odb;
+       source->local = local;
+       source->path = xstrdup(path);
+
+       return source;
+}
+
 static struct odb_source *link_alt_odb_entry(struct object_database *odb,
                                             const char *dir,
                                             const char *relative_base,
@@ -178,10 +192,7 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
        if (!alt_odb_usable(odb, pathbuf.buf, tmp.buf))
                goto error;
 
-       CALLOC_ARRAY(alternate, 1);
-       alternate->odb = odb;
-       alternate->local = false;
-       alternate->path = strbuf_detach(&pathbuf, NULL);
+       alternate = odb_source_new(odb, pathbuf.buf, false);
 
        /* add the alternate entry */
        *odb->sources_tail = alternate;
@@ -341,9 +352,7 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
         * Make a new primary odb and link the old primary ODB in as an
         * alternate
         */
-       source = xcalloc(1, sizeof(*source));
-       source->odb = odb;
-       source->path = xstrdup(dir);
+       source = odb_source_new(odb, dir, false);
 
        /*
         * Disable ref updates while a temporary odb is active, since
diff --git a/odb.h b/odb.h
index e6602dd90c833c9d7c86c5e8b374c98b8db49284..2bec895d1352f4416f51f2dab33fb3b4be318622 100644 (file)
--- a/odb.h
+++ b/odb.h
@@ -89,6 +89,10 @@ struct odb_source {
        char *path;
 };
 
+struct odb_source *odb_source_new(struct object_database *odb,
+                                 const char *path,
+                                 bool local);
+
 struct packed_git;
 struct packfile_store;
 struct cached_object_entry;
index 6faf5c73981ebf6c520919a5475f0243f08cea87..6aaa7ba00869bf6aa0450f3fbdf27590f7439228 100644 (file)
@@ -160,20 +160,24 @@ void repo_set_gitdir(struct repository *repo,
         * until after xstrdup(root). Then we can free it.
         */
        char *old_gitdir = repo->gitdir;
+       char *objects_path = NULL;
 
        repo->gitdir = xstrdup(gitfile ? gitfile : root);
        free(old_gitdir);
 
        repo_set_commondir(repo, o->commondir);
+       expand_base_dir(&objects_path, o->object_dir,
+                       repo->commondir, "objects");
 
        if (!repo->objects->sources) {
-               CALLOC_ARRAY(repo->objects->sources, 1);
-               repo->objects->sources->odb = repo->objects;
-               repo->objects->sources->local = true;
+               repo->objects->sources = odb_source_new(repo->objects,
+                                                       objects_path, true);
                repo->objects->sources_tail = &repo->objects->sources->next;
+               free(objects_path);
+       } else {
+               free(repo->objects->sources->path);
+               repo->objects->sources->path = objects_path;
        }
-       expand_base_dir(&repo->objects->sources->path, o->object_dir,
-                       repo->commondir, "objects");
 
        repo->objects->sources->disable_ref_updates = o->disable_ref_updates;