odb: introduce `odb_source_new()`

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>

Patrick Steinhardt committed Nov 3, 2025 at 08:41 UTC 0820a4b120f310d87ac8817ade63896a901c9267
3 files changed +29 -12
odb.c
+16 -7
@@ -141,6 +141,20 @@ static void read_info_alternates(struct object_database *odb,
141 const char *relative_base,
142 int depth);
143
144 +struct odb_source *odb_source_new(struct object_database *odb,
145 + const char *path,
146 + bool local)
147 +{
148 + struct odb_source *source;
149 +
150 + CALLOC_ARRAY(source, 1);
151 + source->odb = odb;
152 + source->local = local;
153 + source->path = xstrdup(path);
154 +
155 + return source;
156 +}
157 +
158 static struct odb_source *link_alt_odb_entry(struct object_database *odb,
159 const char *dir,
160 const char *relative_base,
@@ -178,10 +192,7 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
192 if (!alt_odb_usable(odb, pathbuf.buf, tmp.buf))
193 goto error;
194
181 - CALLOC_ARRAY(alternate, 1);
182 - alternate->odb = odb;
183 - alternate->local = false;
184 - alternate->path = strbuf_detach(&pathbuf, NULL);
195 + alternate = odb_source_new(odb, pathbuf.buf, false);
196
197 /* add the alternate entry */
198 *odb->sources_tail = alternate;
@@ -341,9 +352,7 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
352 * Make a new primary odb and link the old primary ODB in as an
353 * alternate
354 */
344 - source = xcalloc(1, sizeof(*source));
345 - source->odb = odb;
346 - source->path = xstrdup(dir);
355 + source = odb_source_new(odb, dir, false);
356
357 /*
358 * Disable ref updates while a temporary odb is active, since
odb.h
+4
@@ -89,6 +89,10 @@ struct odb_source {
89 char *path;
90 };
91
92 +struct odb_source *odb_source_new(struct object_database *odb,
93 + const char *path,
94 + bool local);
95 +
96 struct packed_git;
97 struct packfile_store;
98 struct cached_object_entry;
repository.c
+9 -5
@@ -160,20 +160,24 @@ void repo_set_gitdir(struct repository *repo,
160 * until after xstrdup(root). Then we can free it.
161 */
162 char *old_gitdir = repo->gitdir;
163 + char *objects_path = NULL;
164
165 repo->gitdir = xstrdup(gitfile ? gitfile : root);
166 free(old_gitdir);
167
168 repo_set_commondir(repo, o->commondir);
169 + expand_base_dir(&objects_path, o->object_dir,
170 + repo->commondir, "objects");
171
172 if (!repo->objects->sources) {
170 - CALLOC_ARRAY(repo->objects->sources, 1);
171 - repo->objects->sources->odb = repo->objects;
172 - repo->objects->sources->local = true;
173 + repo->objects->sources = odb_source_new(repo->objects,
174 + objects_path, true);
175 repo->objects->sources_tail = &repo->objects->sources->next;
176 + free(objects_path);
177 + } else {
178 + free(repo->objects->sources->path);
179 + repo->objects->sources->path = objects_path;
180 }
175 - expand_base_dir(&repo->objects->sources->path, o->object_dir,
176 - repo->commondir, "objects");
181
182 repo->objects->sources->disable_ref_updates = o->disable_ref_updates;
183