Skip to content

Commit 3a756a6

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a511f39 commit 3a756a6

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

odb.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ static void read_info_alternates(struct object_database *odb,
142142
static struct odb_source *link_alt_odb_entry(struct object_database *odb,
143143
const char *entry,
144144
const char *relative_base,
145-
int depth,
146-
const char *normalized_objdir)
145+
int depth)
147146
{
148147
struct odb_source *alternate = NULL;
149148
struct strbuf pathbuf = STRBUF_INIT;
@@ -170,7 +169,10 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
170169
while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
171170
strbuf_setlen(&pathbuf, pathbuf.len - 1);
172171

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))
174176
goto error;
175177

176178
CALLOC_ARRAY(alternate, 1);
@@ -227,7 +229,6 @@ static const char *parse_alt_odb_entry(const char *string,
227229
static void link_alt_odb_entries(struct object_database *odb, const char *alt,
228230
int sep, const char *relative_base, int depth)
229231
{
230-
struct strbuf objdirbuf = STRBUF_INIT;
231232
struct strbuf entry = STRBUF_INIT;
232233

233234
if (!alt || !*alt)
@@ -239,17 +240,13 @@ static void link_alt_odb_entries(struct object_database *odb, const char *alt,
239240
return;
240241
}
241242

242-
strbuf_realpath(&objdirbuf, odb->sources->path, 1);
243-
244243
while (*alt) {
245244
alt = parse_alt_odb_entry(alt, sep, &entry);
246245
if (!entry.len)
247246
continue;
248-
link_alt_odb_entry(odb, entry.buf,
249-
relative_base, depth, objdirbuf.buf);
247+
link_alt_odb_entry(odb, entry.buf, relative_base, depth);
250248
}
251249
strbuf_release(&entry);
252-
strbuf_release(&objdirbuf);
253250
}
254251

255252
static void read_info_alternates(struct object_database *odb,
@@ -318,20 +315,12 @@ void odb_add_to_alternates_file(struct object_database *odb,
318315
struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
319316
const char *reference)
320317
{
321-
struct odb_source *alternate;
322-
char *objdir;
323-
324318
/*
325319
* Make sure alternates are initialized, or else our entry may be
326320
* overwritten when they are.
327321
*/
328322
odb_prepare_alternates(odb);
329-
330-
objdir = real_pathdup(odb->sources->path, 1);
331-
alternate = link_alt_odb_entry(odb, reference, NULL, 0, objdir);
332-
333-
free(objdir);
334-
return alternate;
323+
return link_alt_odb_entry(odb, reference, NULL, 0);
335324
}
336325

337326
struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,

0 commit comments

Comments
 (0)