Skip to content

Commit 0820a4b

Browse files
pks-tgitster
authored andcommitted
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>
1 parent f82e430 commit 0820a4b

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

odb.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ static void read_info_alternates(struct object_database *odb,
141141
const char *relative_base,
142142
int depth);
143143

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+
144158
static struct odb_source *link_alt_odb_entry(struct object_database *odb,
145159
const char *dir,
146160
const char *relative_base,
@@ -178,10 +192,7 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
178192
if (!alt_odb_usable(odb, pathbuf.buf, tmp.buf))
179193
goto error;
180194

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);
185196

186197
/* add the alternate entry */
187198
*odb->sources_tail = alternate;
@@ -341,9 +352,7 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
341352
* Make a new primary odb and link the old primary ODB in as an
342353
* alternate
343354
*/
344-
source = xcalloc(1, sizeof(*source));
345-
source->odb = odb;
346-
source->path = xstrdup(dir);
355+
source = odb_source_new(odb, dir, false);
347356

348357
/*
349358
* Disable ref updates while a temporary odb is active, since

odb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ struct odb_source {
8989
char *path;
9090
};
9191

92+
struct odb_source *odb_source_new(struct object_database *odb,
93+
const char *path,
94+
bool local);
95+
9296
struct packed_git;
9397
struct packfile_store;
9498
struct cached_object_entry;

repository.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,24 @@ void repo_set_gitdir(struct repository *repo,
160160
* until after xstrdup(root). Then we can free it.
161161
*/
162162
char *old_gitdir = repo->gitdir;
163+
char *objects_path = NULL;
163164

164165
repo->gitdir = xstrdup(gitfile ? gitfile : root);
165166
free(old_gitdir);
166167

167168
repo_set_commondir(repo, o->commondir);
169+
expand_base_dir(&objects_path, o->object_dir,
170+
repo->commondir, "objects");
168171

169172
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);
173175
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;
174180
}
175-
expand_base_dir(&repo->objects->sources->path, o->object_dir,
176-
repo->commondir, "objects");
177181

178182
repo->objects->sources->disable_ref_updates = o->disable_ref_updates;
179183

0 commit comments

Comments
 (0)